书scikit-learn机器学习-page51示例代码:
import matplotlib.pyplot as plt
from sklearn import datasets
digits=datasets.load_digits()
from sklearn.model_selection import train_test_split
Xtrain, Xtest, Ytrain, Ytest = train_test_split(digits.data, digits.target, test_size=0.2, random_state=2)
from sklearn import svm
clf=svm.SVC(gamma=0.001, C=100.)
clf.fit(Xtrain, Ytrain)
clf.score(Xtest, Ytest)
Ypred=clf.predict(Xtest)
#查看预测的情况
fig, axes=plt.subplots(4,4,figsize=(8,8))
for i, ax in enumerate(axes.flat):
ax.imshow(Xtest[i].reshape(8,8),cmap=plt.cm.gray_r,interpolation=‘nearest’)
ax.text(0.05,0.05,str(Ypred[i]),fontsize=32, transform=ax.transAxes, color=‘green’ if Ypred[i]== Ytest[i] else ‘red’)
ax.text(0,8, 0.05, str(Ytest[i]), fontsize=32, transform=ax.transAxes, color=‘black’)
ax.set_xticks([])
ax.set_yticks([])
#保存模型参数
from sklearn.externals import joblib
joblib.dump(clf, ‘digits_svm.pkl’)
clf=joblib.load(‘digits_svm.pkl’)
Ypred=clf.predict(Xtest)
clf.score(Ytest, Ypred)
#之前的support vector 模型就是clf, 使用sklearn包中的externals 中的joblib.dump功能将模型储存并且命名为digits_svm.pkl
#届时使用joblib.load调用原模型即可