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)
Saving and loading Scikit-Learn models is part of the lifecycle of most models - typically, you'll train them in one runtime and serve them in another. With the model fit - let's go ahead and save it. Note: The data is scaled for the model to learn from.
#1 Pickle. Pickle is one of the most popular ways to serialize objects in Python; You can use Pickle to serialize your trained machine learning model and save it to a file. At a later time or in another script, you can deserialize the file to access the trained model and use it to make predictions.
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])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With