识别手写体数字

具体详见《Python数据分析实战》(人民邮电出版社)【意】Fabio Nelli著 杜春晓译

#使用sklearn.svm.SVC估计器,使用的是SVC技术
#需要导入scikit-learn的svm模块,创建SVC类型的估计器,并初始化设置
#无需为C和gamma设置特殊值,使用一般值即可,分析过程中可再调整
from sklearn import svm
svc = svm.SVC(gamma = 0.001, C = 100.)
#手写体识别问题可以使用scikit-learn库中的Digits图像数据集
#该数据集包含1797张8*8像素大小的灰度图,图像的内容为一个手写体数字

#导入Digits数据集
from sklearn import datasets
digits = datasets.load_digits()
#加载数据集后,访问DESCR属性,读取说明信息
#print('Digits参考文档:')
#print(digits.DESCR)

#图像所表示的数字也就是目标值,存储在digits.targets数组之中
#print('目标值:')
#print(digits.target)
#print('大小')
#print(digits.target.size)

#用前1791个元素作为训练集,剩余6个作为验证集
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')

#让先前定义的svc估计器进行学习
svc.fit(digits.data[1:1790],digits.target[1:1790])

#命令运行一段时间之后,输出训练得到的估计器
#svc(C=100.0,cache_size=200,class_weight=None,coefo=0.0,degree=3,gamma=0.001,kernel='rbf',max_iter=-1,probability=False,random_state=None,shrinking=True,tol=0.001,verbose=False)
print(svc.predict(digits.data[1791:1797]))


猜你喜欢

转载自blog.csdn.net/qq_38943651/article/details/79133929