python数据分析实战-第11章-识别手写数字

第11章 识别手写体数字  271
11.1 手写体识别  271
11.2 用scikit-learn识别手写体数字  271
11.3 Digits数据集  272
11.4 学习和预测  274
11.5 小结  276

1
2
from sklearn import svm
svc = svm.SVC(gamma=0.001, C=100.)
1
2
3
from sklearn import datasets

digits = datasets.load_digits()
1
digits.images[0]
array([[  0.,   0.,   5.,  13.,   9.,   1.,   0.,   0.],
       [  0.,   0.,  13.,  15.,  10.,  15.,   5.,   0.],
       [  0.,   3.,  15.,   2.,   0.,  11.,   8.,   0.],
       [  0.,   4.,  12.,   0.,   0.,   8.,   8.,   0.],
       [  0.,   5.,   8.,   0.,   0.,   9.,   8.,   0.],
       [  0.,   4.,  11.,   0.,   1.,  12.,   7.,   0.],
       [  0.,   2.,  14.,   5.,  10.,  12.,   0.,   0.],
       [  0.,   0.,   6.,  13.,  10.,   0.,   0.,   0.]])
1
digits.target.size
1797
1
digits.target
array([0, 1, 2, ..., 8, 9, 8])
1
2
3
4
import matplotlib.pyplot as plt
%matplotlib inline

plt.imshow(digits.images[0], cmap=plt.cm.gray_r, interpolation='nearest')
<matplotlib.image.AxesImage at 0x16657400>

png

1
svc.fit(digits.data[1:1790], digits.target[1:1790])
SVC(C=100.0, cache_size=200, class_weight=None, coef0=0.0, degree=3,
  gamma=0.001, kernel='rbf', max_iter=-1, probability=False,
  random_state=None, shrinking=True, tol=0.001, verbose=False)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import matplotlib.pyplot as plt
%matplotlib inline

plt.subplot(321)
plt.imshow(digits.images[1791], cmap=plt.cm.gray_r, interpolation='nearest')
plt.subplot(322)
plt.imshow(digits.images[1792], cmap=plt.cm.gray_r, interpolation='nearest')
plt.subplot(323)
plt.imshow(digits.images[1793], cmap=plt.cm.gray_r, interpolation='nearest')
plt.subplot(324)
plt.imshow(digits.images[1794], cmap=plt.cm.gray_r, interpolation='nearest')
plt.subplot(325)
plt.imshow(digits.images[1795], cmap=plt.cm.gray_r, interpolation='nearest')
plt.subplot(326)
plt.imshow(digits.images[1796], cmap=plt.cm.gray_r, interpolation='nearest')
<matplotlib.image.AxesImage at 0x175e6b00>

png

1
svc.predict(digits.data[1791:1976])
array([4, 9, 0, 8, 9, 8])
1
digits.target[1791:1976]
array([4, 9, 0, 8, 9, 8])

猜你喜欢

转载自www.cnblogs.com/LearnFromNow/p/9349939.html
今日推荐