识别手写体数字

具体详见《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]))


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值