sklearn模型保存

from sklearn import svm
from sklearn import datasets
import pickle  # 保存模块

clf = svm.SVC()
iris = datasets.load_iris()
x, y = iris.data, iris.target
clf.fit(x, y)
"""方法一:使用 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)
    print(clf2.predict(x[0:1]))

"""使用 joblib 保存"""
import joblib

# 保存Model(注:save文件夹要预先建立,否则会报错)
joblib.dump(clf, "save/clf.pkl")

# 读取Model
clf3 = joblib.load('save/clf.pkl')
# 测试读取后的Model
print(clf3.predict(x[0:1]))

你可能感兴趣的:(Scikit-Learn,sklearn,机器学习,python)