Sklearn 保存模型,读取模型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)

import pickle #pickle模块

#保存Model(注:save文件夹要预先建立,否则会报错)
# with open('save/clf.pickle', 'wb') as f:
#     pickle.dump(clf, f)

#读取Model
# with open('save/clf.pickle', 'rb') as f:
#     clf2 = pickle.load(f)
#     #测试读取后的Model
#     print(clf2.predict(X[0:1]))


from sklearn.externals import joblib #jbolib模块
#
# #保存Model(注:save文件夹要预先建立,否则会报错)
# joblib.dump(clf, 'save/clf.pkl')


#读取Model
clf3 = joblib.load('save/clf.pkl')

#测试读取后的Model
print(clf3.predict(X[0:1]))

https://morvanzhou.github.io/tutorials/machine-learning/sklearn/3-5-save/

http://www.waitingfy.com/archives/4880

4880

Leave a Reply

Name and Email Address are required fields.
Your email will not be published or shared with third parties.