python 识别手写体数字

本文介绍了一个简单的手写数字识别实验,使用Python的sklearn库进行。通过加载sklearn自带的手写数字数据集,并利用SVM的支持向量机算法进行训练和预测,最终实现了对手写数字的有效识别。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

python 有很多好的机器学习库, sklearn 算是之前比较经典常用的了, 现在用它来做一个小测试, 识别手写数字, 字体数据使用sklearn自带的, 代码如下:

from sklearn import datasets,svm
import matplotlib.pyplot as plt

""" 识别手写体数字 """

svc=svm.SVC(gamma=0.001,C=100.)
digits=datasets.load_digits() # 导入Digits数据集
# print(digits.DESCR) # 查看数据集的说明信息

def plts():
    ''' 显示要识别的数字图片 '''
    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')
    plt.show()


def svms():
    ''' 学习并返回识别结果 '''
    svc.fit(digits.data[:1791],digits.target[:1791]) # 训练
    res=svc.predict(digits.data[1791:1797]) # 识别
    return list(res)

if __name__=='__main__':
    result=svms()
    duibi=digits.target[1791:1797]
    print('识别的数字: {}\n实际的结果: {}'.format(result,list(duibi)))
    plts()

执行结果:

识别的数字: [4, 9, 0, 8, 9, 8]
实际的结果: [4, 9, 0, 8, 9, 8]

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值