import matplotlib.pyplot as plt
from sklearn import datasets, svm, metrics
#载入数据集
digits = datasets.load_digits()# The data that we are interested in is made of 8x8 images of digits, let's# have a look at the first 4 images, stored in the `images` attribute of the# dataset. If we were working from image files, we could load them using# matplotlib.pyplot.imread. Note that each image must have the same size. For these# images, we know which digit they represent: it is given in the 'target' of# the dataset.
images_and_labels =list(zip(digits.images, digits.target))#画图for index,(image, label)inenumerate(images_and_labels[:4]):
plt.subplot(2,4, index +1)
plt.axis('off')
plt.imshow(image, cmap=plt.cm.gray_r, interpolation='nearest')
plt.title('Training: %i'% label)#取图片样本数
n_samples =len(digits.images)#降维
data = digits.images.reshape((n_samples,-1))#调用svm算法,其中gamma=0.001
classifier = svm.SVC(gamma=0.001)#拟合
classifier.fit(data[:n_samples //2], digits.target[:n_samples //2])#测试集数据
expected = digits.target[n_samples //2:]#预测
predicted = classifier.predict(data[n_samples //2:])print("Classification report for classifier %s:\n%s\n"%(classifier, metrics.classification_report(expected, predicted)))print("Confusion matrix:\n%s"% metrics.confusion_matrix(expected, predicted))
images_and_predictions =list(zip(digits.images[n_samples //2:], predicted))#画图for index,(image, prediction)inenumerate(images_and_predictions[:4]):
plt.subplot(2,4, index +5)
plt.axis('off')
plt.imshow(image, cmap=plt.cm.gray_r, interpolation='nearest')
plt.title('Prediction: %i'% prediction)
plt.show()