Bootstrap

机器学习笔记:python中使用sklearn对模型持久化

# sklearn 模型持久化
from sklearn import svm
from sklearn import datasets
import pickle
from sklearn.externals import joblib
import numpy as np


#svc算法 support vectors classification
clf = svm.SVC()
# 加载鸢尾花数据集
iris = datasets.load_iris()
print(clf)
X, y = iris.data, iris.target


clf.fit(X, y)


# s = pickle.dumps(clf)
# clf2 = pickle.loads(s)


# 使用joblib替换pickle
joblib.dump(clf, 'test.pkl')
clf2 = joblib.load('test.pkl')


for i in range(20):
num = np.random.randint(1,100)

print(clf2.predict(X[num-1:num]))



;