# 手写数字的识别

## 数据准备

```    import matplotlib.pyplot as plt
# Import datasets, classifiers and performance metrics
from sklearn import datasets, svm, metrics
# The digits dataset
```

```    for index, (image, label) in enumerate(zip(digits.images, digits.target)[:8]):
plt.subplot(2, 4, index + 1)
plt.axis('off')
plt.imshow(image, cmap=plt.cm.gray_r, interpolation='nearest')
plt.title('Training: %i' % label)
```

## 训练一个SVM模型

```    n_samples = len(digits.images)
data = digits.images.reshape((n_samples, -1))
# Create a classifier: a support vector classifier
classifier = svm.SVC(gamma=0.001)
# We learn the digits on the first half of the digits
classifier.fit(data[:n_samples / 2], digits.target[:n_samples / 2])
# Now predict the value of the digit on the second half:
expected = digits.target[n_samples / 2:]
predicted = classifier.predict(data[n_samples / 2:])
```

## 分析模型的表现

```    print (metrics.classification_report(expected, predicted))
```

```            precision    recall  f1-score   support
0       1.00      0.99      0.99        88
1       0.99      0.97      0.98        91
2       0.99      0.99      0.99        86
3       0.98      0.87      0.92        91
4       0.99      0.96      0.97        92
5       0.95      0.97      0.96        91
6       0.99      0.99      0.99        91
7       0.96      0.99      0.97        89
8       0.94      1.00      0.97        88
9       0.93      0.98      0.95        92
```

avg / total 0.97 0.97 0.97 899

```    cm= metrics.confusion_matrix(expected, predicted)
def plotCM(cm,title,colorbarOn,groupnames):
ncm=cm/cm.max()
plt.matshow(ncm, fignum=False, cmap='Blues', vmin=0, vmax=1.0)
ax=plt.axes()
ax.set_xticks(range(len(groupnames)))
ax.set_xticklabels(groupnames)
ax.xaxis.set_ticks_position("bottom")
ax.set_yticks(range(len(groupnames)))
ax.set_yticklabels(groupnames)
plt.title(title,size=12)
if colorbarOn=="on":
plt.colorbar()
plt.xlabel('Predicted class')
plt.ylabel('True class')
for i in range(cm.shape[0]):
for j in range(cm.shape[1]):
text(i,j,cm[i,j],size=15)
plotCM(cm,"confusion matrix: SVM classifier","off",range(10))
```

```    for index, (image, prediction) in enumerate(
zip(digits.images[n_samples / 2:], predicted)[:8]):
plt.subplot(2, 4, index + 1)
plt.axis('off')
plt.imshow(image, cmap=plt.cm.gray_r, interpolation='nearest')
plt.title('Prediction: %i' % prediction)
```