Default / 默认 · September 1, 2021

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

Table of Content

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

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(), y)

After this you have two options:

1) Using Pickle

import pickle
<h1>now you can save it to a file</h1>
with open('filename.pkl', 'wb') as f:
    pickle.dump(clf, f)
<h1>and later you can load it</h1>
with open('filename.pkl', 'rb') as f:
    clf = pickle.load(f)

<p><strong>2) Using Joblib</strong>
/usr/local/lib/python3.7/dist-packages/sklearn/externals/joblib/ 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)</p>

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


%d bloggers like this: