예제
import pickle
from sklearn.linear_model import LogisticRegression
# create a logistic regression object and train it on some data
clf = LogisticRegression()
X_train = [[1, 2], [3, 4]]
y_train = [0, 1]
clf.fit(X_train, y_train)
# save the model to a file using pickle
filename = 'logistic_regression_model.pkl'
with open(filename, 'wb') as file:
pickle.dump(clf, file)
# load the model from the file
with open(filename, 'rb') as file:
clf_loaded = pickle.load(file)
# use the loaded model for prediction
X_test = [[5, 6], [7, 8]]
y_pred = clf_loaded.predict(X_test)
print(y_pred) # output: [1 1]