3000 explained code solutions for 75 technologies

python-scikit-learnHow to save ML model to file

import pickle
from sklearn import datasets, linear_model, model_selection, metrics

X, y = datasets.load_diabetes(return_X_y=True)
X_train, X_test, y_train, y_test = model_selection.train_test_split(X, y)

model = linear_model.LinearRegression()
model.fit(X_train, y_train)

pickle.dump(model, open('/tmp/model.ml', 'wb'))ctrl + c

saves given model to the given file


path to the file to save model to


opens file descriptor (to write data to it)