menu

sk才是大成者,深度学习中 还能屹立不倒

sk才是大成者,深度学习中 还能屹立不倒

https://colab.research.google.com/drive/1_Z-tAmypFr9grcjjLpbCS4MV14J4qtwm?usp=sharing

#sklearn #保存模型

What you are looking for is called Model persistence in sklearn words and it is documented in introduction and in model persistence sections.

So you have initialized your classifier and trained it for a long time with

clf = some.classifier()
clf.fit(X, y)

After this you have two options:

1) Using Pickle

import pickle
# now you can save it to a file
with open('filename.pkl', 'wb') as f:
    pickle.dump(clf, f)

# and later you can load it
with open('filename.pkl', 'rb') as f:
    clf = pickle.load(f)

2) Using Joblib /usr/local/lib/python3.7/dist-packages/sklearn/externals/joblib/__init__.py:15: FutureWarning: sklearn.externals.joblib is deprecated in 0.21 and will be removed in 0.23. Please import this functionality directly from joblib, which can be installed with: pip install joblib. If this warning is raised when loading pickled models, you may need to re-serialize those models with scikit-learn 0.21+. warnings.warn(msg, category=FutureWarning)

还是pickle吧,那个更加通用,无恼用就可以。

from sklearn.externals import joblib
# now you can save it to a file
joblib.dump(clf, 'filename.pkl') 
# and later you can load it
clf = joblib.load('filename.pkl')

https://stackoverflow.com/questions/10592605/save-classifier-to-disk-in-scikit-learn

scikit-learn自带的参数优化

https://scikit-learn.org/stable/modules/grid_search.html

编辑