保存sklearn中模型的两种方法(pickle、joblib)

保存sklearn中模型的两种方法(pickle、joblib)

from sklearn import svm
from sklearn import datasets

clf = svm.SVC()
iris = datasets.load_iris()
X, y = iris.data, iris.target
clf.fit(X, y)

# pickle  python内置保存模型(方法1)

import pickle
s = pickle.dumps(clf)
clf2 = pickle.loads(s)
print(clf2.predict(X[0:1]))
print(y[0])
print('\n')

# joblib  scikit 保存模型(方法2)
from sklearn.externals import joblib

joblib.dump(clf, 'model/modelFile.pkl')
clf3 = joblib.load('model/modelFile.pkl')
print(clf3.predict(X[0:1]))
print(y[0])

输出:

[0]
0


[0]
0

参考链接:http://sklearn.apachecn.org/cn/0.19.0/tutorial/basic/tutorial.html

你可能感兴趣的:(sklearn,模型保存,pickle,joblib)