[机器学习]手写数字识别
最编程
2024-05-19 13:12:37
...
前言
logistic回归,是一个分类算法,可以处理二元分类,多元分类。我们使用sklearn中的logistic对手写数字识别进行实践。
数据集
MNIST数据集来自美国国家标准与技术研究所,训练集由250个不同人手写数字构成,50%高中学生,50%来自人口普查局。
数据集展示
数据集下载
百度云盘:
链接:https://pan.baidu.com/s/1ZBU8XBsx7lp7gdN4ySSIWg
提取码:5mrf
关于使用pycharm图片不显示
pycharm默认会在右边进行绘图,由于某些原因导致图片不能显示,只能是白图的解决办法。
-
我们可以首先把图片显示调到独立画框显示。file->settings->Tools->Python Scientific 取消勾选 show plots in tool window.
-
进行上述操作之后会独立弹出画框进行画图,如果仍然不能显示可以进行下面操作。
-
找到Configure subplots
-
点击紧凑布局,就能显示,之前不能显示可能是因为图太大,导致我们没有看到。
- 当然你也可以自己调整布局,行距列距什么的。
- 当然你也可以自己调整布局,行距列距什么的。
-
逻辑回归手写数字识别
## logistis回归,是一个分类算法,可以处理二元分类,多元分类。
## 首先逻辑回归构造冠以的线性回归函数,然后使用sigmoid函数将回归值映射到散列类别
## sklearn 分类算法与手写数字识别
## 数据介绍
## MNIST数据集来自美国国家标准与技术研究所,训练集由250个不同人手写数字构成,50%高中学生,50%来自人口普查局
## 导包
import struct,os
import numpy as np
from array import array as pyarray
from numpy import append,array,int8,uint8,zeros
import matplotlib.pyplot as plt
from sklearn.metrics import accuracy_score, classification_report
from sklearn.linear_model import LogisticRegression
## 加载数据集
def load_mnist(image_file,label_file,path="mnist"):
digits=np.arange(10)
fname_image = os.path.join(path,image_file)
fname_label = os.path.join(path, label_file)
flbl = open(fname_label,'rb')
magic_nr, size = struct.unpack(">II", flbl.read(8))
lbl = pyarray("b",flbl.read())
flbl.close()
fimg = open(fname_image,'rb')
magic_nr, size, rows, cols = struct.unpack(">IIII", fimg.read(16))
img = pyarray("B",fimg.read())
fimg.close()
ind = [ k for k in range(size) if lbl[k] in digits ]
N = len(ind)
images = zeros((N, rows*cols),dtype = uint8)
labels = zeros((N,1), dtype = int8)
for i in range(len(ind)):
images[i] = array(img[ind[i]*rows*cols : (ind[i]+1)*rows*cols]).reshape((1,rows*cols))
labels[i] = lbl[ind[i]]
return images,labels
train_image, train_label = load_mnist('train-images.idx3-ubyte', 'train-labels.idx1-ubyte')
test_image, test_label = load_mnist('t10k-images.idx3-ubyte','t10k-labels.idx1-ubyte')
## 数据展示
## 28*28
def show_image(imgdata, imgtarget, show_column, show_row,titlename):
for index, (im, it) in enumerate(list(zip(imgdata, imgtarget))):
xx = im.reshape(28,28)
plt.subplots_adjust(left=1, bottom=None, right=3,top=2, wspace=None, hspace=None)
plt.subplot(show_row,show_column,index+1)
plt.axis('off')
plt.imshow(xx, cmap='gray', interpolation='nearest')
plt.title(titlename+':%i' % it)
# plt.savefig(titlename+'.png')
# 这个地方可能会有一个警告,可能因为图太大了,不过没关系,代码正常运行
plt.show()
show_image(train_image[:50], train_label[:50],10,5,'label')
## sklearn 分类模型
## 数据归一化
train_image = [im/255.0 for im in train_image]
test_image = [im/255.0 for im in test_image]
print(len(train_image))
print(len(test_image))
print(len(train_label))
print(len(test_label))
## 模型分类
## 模型实例化
lr = LogisticRegression(max_iter=1000)
## 模型训练
lr.fit(train_image,train_label.ravel())
## 模型验证
predict = lr.predict(test_image)
print("accuracy score: %.4lf"% accuracy_score(predict,test_label))
print("classfication report for %s:\n%s\n"%(lr, classification_report(test_label, predict)))
show_image(test_image[:100],predict,10,10,'predict')
结果展示
- 模型训练信息
- 可以看到
- 准确度:0.9256
- 训练次数最大1000次
- 精度、平均值、加权平均值等
- 识别效果展示
分析
我们展示了100张图片的识别效果,可以找到3张明显的识别错误,和模型的评估结果相似。
总结
我们可以多重复运行几次发现结果并没有变化,这可能也是logistic回归的缺点吧,我们也可以使用神经网络进行手写数字识别,但那是深度学习的内容,我们后续会对其进行实现。
推荐阅读
-
python 机器学习(手写数字识别)
-
PyTorch VGG16 手写数字识别教程
-
机器学习之识别图片中的数字
-
【摩尔线程+Colossal-AI强强联手】MusaBert登上CLUE榜单TOP10:技术细节揭秘 - 技术实力:摩尔线程凭借"软硬兼备"的技术底蕴,让MusaBert得以从底层优化到顶层。其内置多功能GPU配备AI加速和并行计算模块,提供了全面的AI与科学计算支持,为AI推理和低资源条件下的大模型训练等场景带来了高效、经济且环保的算力。 - 算法层面亮点:依托Colossal-AI AI大模型开发系统,MusaBert在训练过程中展现出了卓越的并行性能与易用性,特别在预处理阶段对DataLoader进行了优化,适应低资源环境高效处理海量数据。同时,通过精细的建模优化、领域内数据增强以及Adan优化器等手段,挖掘和展示了预训练语言模型出色的语义理解潜力。基于MusaBert,摩尔线程自主研发的MusaSim通过对比学习方法微调,结合百万对标注数据,MusaSim在多个任务如语义相似度、意图识别和情绪分析中均表现出色。 - 数据资源丰富:MusaBert除了自家高质量语义相似数据外,还融合了悟道开源200GB数据、CLUE社区80GB数据,以及浪潮公司提供的1TB高质量数据,保证模型即便在较小规模下仍具备良好性能。 当前,MusaBert已成功应用于摩尔线程的智能客服与数字人项目,并广泛服务于语义相似度、情绪识别、阅读理解与声韵识别等领域。为了降低大模型开发和应用难度,MusaBert及其相关高质量模型代码已在Colossal-AI仓库开源,可快速训练优质中文BERT模型。同时,通过摩尔线程与潞晨科技的深度合作,仅需一张多功能GPU单卡便能高效训练MusaBert或更大规模的GPT2模型,显著降低预训练成本,进一步推动双方在低资源大模型训练领域的共享目标。 MusaBert荣登CLUE榜单TOP10,象征着摩尔线程与潞晨科技联合研发团队在中文预训练研究领域的领先地位。展望未来,双方将携手探索更大规模的自然语言模型研究,充分运用上游数据资源,产出更为强大的模型并开源。持续强化在摩尔线程多功能GPU上的大模型训练能力,特别是在消费级显卡等低资源环境下,致力于降低使用大模型训练的门槛与成本,推动人工智能更加普惠。而潞晨科技作为重要合作伙伴,将继续发挥关键作用。
-
10个简单好玩的人工智能项目实操指南(含Python源码) - 例子5:手写数字识别
-
华为升级机器学习活体识别技术,打造更安全易用的互动体验
-
程序员机器学习入门 (XI) - 物体识别 YOLO - 识别人脸位置和是否佩戴人脸面具
-
在PyCharm中畅游机器学习深海,以支持向量机探索图像识别新航程
-
Python 机器学习设计 - 水果和蔬菜识别
-
使用深度学习(CNN)识别验证码(字母+数字)