How to save a trained model by scikit-learn?

半世苍凉 提交于 2020-07-09 16:34:06

问题


I am trying to re-create the prediction of a trained model but I don't know how to save a model. For example, I want to save the trained Gaussian processing regressor model and recreate the prediction after I trained the model. The package I used to train model is scikit-learn.

kernel = DotProduct() + WhiteKernel()
gpr = GaussianProcessRegressor(kernel=kernel,random_state=0)
gpr.fit(X,y)

回答1:


You can use:

1. pickle

from sklearn import svm
from sklearn import datasets

iris = datasets.load_iris()
X, y = iris.data, iris.target

clf = svm.SVC()
clf.fit(X, y)  

##########################
# SAVE-LOAD using pickle #
##########################
import pickle

# save
with open('model.pkl','wb') as f:
    pickle.dump(clf,f)

# load
with open('model.pkl', 'rb') as f:
    clf2 = pickle.load(f)

clf2.predict(X[0:1])

2. joblib

From scikit-learn documentation:

In the specific case of scikit-learn, it may be better to use joblib’s replacement of pickle (dump & load), which is more efficient on objects that carry large numpy arrays internally as is often the case for fitted scikit-learn estimators, but can only pickle to the disk and not to a string:

from sklearn import svm
from sklearn import datasets

iris = datasets.load_iris()
X, y = iris.data, iris.target

clf = svm.SVC()
clf.fit(X, y)  

##########################
# SAVE-LOAD using joblib #
##########################
import joblib

# save
joblib.dump(clf, "model.pkl") 

# load
clf2 = joblib.load("model.pkl")

clf2.predict(X[0:1])



回答2:


You can save and load the model using the pickle operation to serialize your machine learning algorithms and save the serialized format to a file.

import pickle
# save the model to disk
filename = 'gpr_model.sav'
pickle.dump(gpr, open(filename, 'wb')) 

# load the model from disk
loaded_model = pickle.load(open(filename, 'rb'))

Hope it helps!

source



来源:https://stackoverflow.com/questions/56107259/how-to-save-a-trained-model-by-scikit-learn

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!