手写数字识别(分别用svm、决策树算法、朴素贝叶斯算法、KNN对MINIST数据进行分类)

本文主要是介绍手写数字识别(分别用svm、决策树算法、朴素贝叶斯算法、KNN对MINIST数据进行分类),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

实验四、手写数字识别

一、实验目的

  1. 学会用分类算法解决实际问题

二、实验工具

  1. Anaconda
  2. sklearn
  3. matplotlib
  4. pandas

三、实验简介

概念介绍

图像识别(Image Recognition)是指利用计算机对图像进行处理、分析和理解,以识别各种不同模式的目标和对像的技术。 

图像识别的发展经历了三个阶段:文字识别、数字图像处理与识别、物体识别。机器学习领域一般将此类识别问题转化为分类问题。

手写识别是常见的图像识别任务。计算机通过手写体图片来识别出图片中的字,与印刷字体不同的是,不同人的手写体风格迥异,大小不一, 造成了计算机对手写识别任务的一些困难。 

数字手写体识别由于其有限的类别(0~9共10个数字)成为了相对简单 的手写识别任务。DBRHD和MNIST是常用的两个数字手写识别数据集

数据介绍

MNIST是一个包含数字0~9的手写体图片数据集,图片已归一化为以手写数 字为中心的28*28规格的图片。

MNIST由训练集与测试集两个部分组成,各部分 规模如下: 
训练集:60,000个手写体图片及对应标签 
测试集:10,000个手写体图片及对应标签
image.png

四、实验内容

1. 采用SVM算法实现手写数字识别。

例如,采用SVM对MINIST数据集进行分类。

import numpy as np
from sklearn import svm
from sklearn.datasets  import load_digits
from sklearn.model_selection  import train_test_split
import _pickle as pickleif __name__ == '__main__':mnist = load_digits()x,test_x,y,test_y = train_test_split(mnist.data,mnist.target,test_size=0.25,random_state=40)model = svm.LinearSVC()model.fit(x, y)z = model.predict(test_x)print('准确率:',np.sum(z==test_y)/z.size)with open('./model.pkl','wb') as file:pickle.dump(model,file)

SVM算法手写数字识别准确率0.94

import numpy as np
from sklearn import svm
from sklearn.datasets import load_digits
from sklearn.model_selection import train_test_split
import _pickle as pickle
import matplotlib.pyplot as plt
mnist = load_digits()
x,test_x,y,test_y = train_test_split(mnist.data,mnist.target,test_size=0.25,random_state=40)
model = svm.LinearSVC()
model.fit(x,y)
z=model.predict(test_x)
print('准确率:',np.sum(z==test_y)/z.size)
with open('D:/shujukexue/model.pkl','wb') as file:pickle.dump(model,file)
#学习后识别520到525六张图片并给出预测
model.predict(mnist.data[520:526])
#实际的520到525代表的数
mnist.target[520:526]
#显示520到525数字图片
plt.subplot(321)
plt.imshow(mnist.images[520],cmap=plt.cm.gray_r,interpolation='nearest')
plt.subplot(322)
plt.imshow(mnist.images[521],cmap=plt.cm.gray_r,interpolation='nearest')
plt.subplot(323)
plt.imshow(mnist.images[522],cmap=plt.cm.gray_r,interpolation='nearest')
plt.subplot(324)
plt.imshow(mnist.images[523],cmap=plt.cm.gray_r,interpolation='nearest')
plt.subplot(325)
plt.imshow(mnist.images[524],cmap=plt.cm.gray_r,interpolation='nearest')
plt.subplot(326)
plt.imshow(mnist.images[525],cmap=plt.cm.gray_r,interpolation='nearest')

image.png
2. 参考以上程序,分别采用决策树算法、朴素贝叶斯算法、KNN对MINIST数据进行分类,并对比其性能好坏。

决策树算法手写数字识别,准确率:0.8466666666666667

import numpy as np
from sklearn.datasets import load_digits
from sklearn.model_selection import train_test_split
import _pickle as pickle
import matplotlib.pyplot as plt
mnist = load_digits()
x,test_x,y,test_y = train_test_split(mnist.data,mnist.target,test_size=0.25,random_state=40)
from sklearn.tree import DecisionTreeClassifier, export_graphviz
model = DecisionTreeClassifier(criterion="entropy")
model.fit(x,y)
z=model.predict(test_x)
print('准确率:',np.sum(z==test_y)/z.size)
#学习后识别99到105六张图片并给出预测
model.predict(mnist.data[99:105])
#实际的99到105代表的数
mnist.target[99:105]
#显示99到105数字图片
plt.subplot(321)
plt.imshow(mnist.images[99],cmap=plt.cm.gray_r,interpolation='nearest')
plt.subplot(322)
plt.imshow(mnist.images[100],cmap=plt.cm.gray_r,interpolation='nearest')
plt.subplot(323)
plt.imshow(mnist.images[101],cmap=plt.cm.gray_r,interpolation='nearest')
plt.subplot(324)
plt.imshow(mnist.images[102],cmap=plt.cm.gray_r,interpolation='nearest')
plt.subplot(325)
plt.imshow(mnist.images[103],cmap=plt.cm.gray_r,interpolation='nearest')
plt.subplot(326)
plt.imshow(mnist.images[104],cmap=plt.cm.gray_r,interpolation='nearest')
from six import StringIO
import pandas as pd
x = pd.DataFrame(x)
with open("D:/shujukexue/JueCetree.dot", 'w') as f:f = export_graphviz(model, feature_names = x.columns, out_file = f)

image.png
按照上次实验所学导出pdf、png
image.png
图片太大,选择其中一部分放大如图:
image.png

朴素贝叶斯算法:准确率:0.86

import numpy as np
from sklearn.datasets import load_digits
from sklearn.model_selection import train_test_split
import _pickle as pickle
import matplotlib.pyplot as plt
mnist = load_digits()
x,test_x,y,test_y = train_test_split(mnist.data,mnist.target,test_size=0.25,random_state=40)
from sklearn.naive_bayes import MultinomialNB
model = MultinomialNB()
model.fit(x,y)
z=model.predict(test_x)
print('准确率:',np.sum(z==test_y)/z.size)
#学习后识别1000到1006六张图片并给出预测
model.predict(mnist.data[1000:1006])
#实际的1000到1006代表的数
mnist.target[1000:1006]
#显示1000到1006数字图片
plt.subplot(321)
plt.imshow(mnist.images[1000],cmap=plt.cm.gray_r,interpolation='nearest')
plt.subplot(322)
plt.imshow(mnist.images[1001],cmap=plt.cm.gray_r,interpolation='nearest')
plt.subplot(323)
plt.imshow(mnist.images[1002],cmap=plt.cm.gray_r,interpolation='nearest')
plt.subplot(324)
plt.imshow(mnist.images[1003],cmap=plt.cm.gray_r,interpolation='nearest')
plt.subplot(325)
plt.imshow(mnist.images[1004],cmap=plt.cm.gray_r,interpolation='nearest')
plt.subplot(326)
plt.imshow(mnist.images[1005],cmap=plt.cm.gray_r,interpolation='nearest')

image.png

Knn算法手写数字识别,准确率:0.9822222222222222

import numpy as np
from sklearn.datasets import load_digits
from sklearn.model_selection import train_test_split
import _pickle as pickle
import matplotlib.pyplot as plt
mnist = load_digits()
x,test_x,y,test_y = train_test_split(mnist.data,mnist.target,test_size=0.25,random_state=40)
from sklearn.neighbors import KNeighborsClassifier
model = KNeighborsClassifier(n_neighbors=3)
model.fit(x,y)
z=model.predict(test_x)
print('准确率:',np.sum(z==test_y)/z.size)
#学习后识别1660到1666六张图片并给出预测
model.predict(mnist.data[1660:1666])
#实际的1660到1666代表的数
mnist.target[1660:1666]
#显示1660到1666数字图片
plt.subplot(321)
plt.imshow(mnist.images[1660],cmap=plt.cm.gray_r,interpolation='nearest')
plt.subplot(322)
plt.imshow(mnist.images[1661],cmap=plt.cm.gray_r,interpolation='nearest')
plt.subplot(323)
plt.imshow(mnist.images[1662],cmap=plt.cm.gray_r,interpolation='nearest')
plt.subplot(324)
plt.imshow(mnist.images[1663],cmap=plt.cm.gray_r,interpolation='nearest')
plt.subplot(325)
plt.imshow(mnist.images[1664],cmap=plt.cm.gray_r,interpolation='nearest')
plt.subplot(326)
plt.imshow(mnist.images[1665],cmap=plt.cm.gray_r,interpolation='nearest')

image.png

五、实验总结(写出本次实验的收获,遇到的问题等)

遇到的问题及收获:

        一开始直接把老师实验报告里给的SVM手写数字识别的代码打上,运行出了结果,得到了准确率,但是对代码完全不了解,导致用决策树算法、朴素贝叶斯算法、Knn算法对数据进行分类,实现手写数字识别完全没有思路,实验课上在网上查找手写数字识别的实现方法,一律追求结果,将一些博主给的代码复制到程序中运行,想先看看结果能不能出来,结果一直报错,本次实验课的两节课就这样被浪费了。
        实验课后,依旧完全没有思路,不知道如何下手,这次实验就这样被搁置了一个星期,在一个下午,想到可以从老师所给SVM算法实现手写数字识别入手,一步步的分析代码的作用,就开始对代码一行行的过,不懂的就百度,看了很多博客,通过博客SVC估计器对Digits数据集的学习与预测对Digits数据集有了深层次的理解(在本次实验的不同算法中都用到了此博客的预测数字显示、实际数字显示与图片显示)最后终于对算法有了深的认识。然后开始深一步了解决策树算法、朴素贝叶斯算法、Knn算法,一开始一个个的查找的资料,最后意外发现了一个博客AI学习—分类算法[K-近邻 + 朴素贝叶斯 + 决策树 + 随机森林] ,个人觉得这篇博客写的巨好,对我的本次实验帮助巨大!
        在用决策树算法完成手写数字识别求得准确率之后,又借用了上节课的知识生成了.dot文件然后据此生成了pdf和png,图片巨大,下面是对局部的放大
image.png
        通过本次实验学得要沉下心,一步步来,不能急于求成,学会学以致用!

这篇关于手写数字识别(分别用svm、决策树算法、朴素贝叶斯算法、KNN对MINIST数据进行分类)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!


原文地址:https://blog.csdn.net/Happy_change/article/details/110437240
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.chinasem.cn/article/219796

相关文章

Java使用Curator进行ZooKeeper操作的详细教程

《Java使用Curator进行ZooKeeper操作的详细教程》ApacheCurator是一个基于ZooKeeper的Java客户端库,它极大地简化了使用ZooKeeper的开发工作,在分布式系统... 目录1、简述2、核心功能2.1 CuratorFramework2.2 Recipes3、示例实践3

Java利用JSONPath操作JSON数据的技术指南

《Java利用JSONPath操作JSON数据的技术指南》JSONPath是一种强大的工具,用于查询和操作JSON数据,类似于SQL的语法,它为处理复杂的JSON数据结构提供了简单且高效... 目录1、简述2、什么是 jsONPath?3、Java 示例3.1 基本查询3.2 过滤查询3.3 递归搜索3.4

SpringBoot实现MD5加盐算法的示例代码

《SpringBoot实现MD5加盐算法的示例代码》加盐算法是一种用于增强密码安全性的技术,本文主要介绍了SpringBoot实现MD5加盐算法的示例代码,文中通过示例代码介绍的非常详细,对大家的学习... 目录一、什么是加盐算法二、如何实现加盐算法2.1 加盐算法代码实现2.2 注册页面中进行密码加盐2.

MySQL大表数据的分区与分库分表的实现

《MySQL大表数据的分区与分库分表的实现》数据库的分区和分库分表是两种常用的技术方案,本文主要介绍了MySQL大表数据的分区与分库分表的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有... 目录1. mysql大表数据的分区1.1 什么是分区?1.2 分区的类型1.3 分区的优点1.4 分

Mysql删除几亿条数据表中的部分数据的方法实现

《Mysql删除几亿条数据表中的部分数据的方法实现》在MySQL中删除一个大表中的数据时,需要特别注意操作的性能和对系统的影响,本文主要介绍了Mysql删除几亿条数据表中的部分数据的方法实现,具有一定... 目录1、需求2、方案1. 使用 DELETE 语句分批删除2. 使用 INPLACE ALTER T

Python Dash框架在数据可视化仪表板中的应用与实践记录

《PythonDash框架在数据可视化仪表板中的应用与实践记录》Python的PlotlyDash库提供了一种简便且强大的方式来构建和展示互动式数据仪表板,本篇文章将深入探讨如何使用Dash设计一... 目录python Dash框架在数据可视化仪表板中的应用与实践1. 什么是Plotly Dash?1.1

基于Flask框架添加多个AI模型的API并进行交互

《基于Flask框架添加多个AI模型的API并进行交互》:本文主要介绍如何基于Flask框架开发AI模型API管理系统,允许用户添加、删除不同AI模型的API密钥,感兴趣的可以了解下... 目录1. 概述2. 后端代码说明2.1 依赖库导入2.2 应用初始化2.3 API 存储字典2.4 路由函数2.5 应

Redis 中的热点键和数据倾斜示例详解

《Redis中的热点键和数据倾斜示例详解》热点键是指在Redis中被频繁访问的特定键,这些键由于其高访问频率,可能导致Redis服务器的性能问题,尤其是在高并发场景下,本文给大家介绍Redis中的热... 目录Redis 中的热点键和数据倾斜热点键(Hot Key)定义特点应对策略示例数据倾斜(Data S

Java时间轮调度算法的代码实现

《Java时间轮调度算法的代码实现》时间轮是一种高效的定时调度算法,主要用于管理延时任务或周期性任务,它通过一个环形数组(时间轮)和指针来实现,将大量定时任务分摊到固定的时间槽中,极大地降低了时间复杂... 目录1、简述2、时间轮的原理3. 时间轮的实现步骤3.1 定义时间槽3.2 定义时间轮3.3 使用时

Python使用date模块进行日期处理的终极指南

《Python使用date模块进行日期处理的终极指南》在处理与时间相关的数据时,Python的date模块是开发者最趁手的工具之一,本文将用通俗的语言,结合真实案例,带您掌握date模块的六大核心功能... 目录引言一、date模块的核心功能1.1 日期表示1.2 日期计算1.3 日期比较二、六大常用方法详