机器学习-实战-入门-MNIST手写数字识别
来源:互联网 发布:济南浪潮数据 编辑:程序博客网 时间:2024/06/06 02:00
作者:橘子派
声明:版权所有,转载请注明出处,谢谢。
源码地址:https://github.com/sileixinhua/Python_Machine_Learning_Sklearn_MNIST
实验环境:
Windows10
Sublime
Anaconda 1.6.0
Python3.6
代码功能包括:
一.ubyte数据集转换成csv形式
#将mnist数据集转换成CSV格式import structdef to_csv(name,maxdata):lbl_f = open("./data/"+name+"-labels.idx1-ubyte","rb")#打开标签数据集img_f = open("./data/"+name+"-images.idx3-ubyte","rb")#打开图像数据集csv_f = open("./data/"+name+",csv","w",encoding="utf-8")#写入CSV文件mag,lbl_count=struct.unpack(">II",lbl_f.read(8))#将字节流转换成python数据类型复制给标签mag,img_count=struct.unpack(">II",img_f.read(8))#将字节流转换成python数据类型复制给图像rows,cols=struct.unpack(">II",img_f.read(8))#将字节流转换成python数据类型复制给行列pixels=rows*cols#计算数据总量res=[]for idx in range(lbl_count):if idx > maxdata:break#设置计数器,大于数据个数总量时跳出循环label=struct.unpack("B",lbl_f.read(1))[0]bdata=img_f.read(pixels)sdata=list(map(lambda n:str(n),bdata))csv_f.write(str(label)+",")#写入标签csv_f.write(",".join(sdata)+"\r\n")#写入数据(数字)if idx < 10:s="P2 28 28 255\n"s+=" ".join(sdata)iname="./data/{0}-{1}-{2}.pgm".format(name,idx,label)with open(iname,"w",encoding="utf-8") as f:f.write(s)csv_f.close()#关闭CSV流lbl_f.close()#关闭标签流img_f.close()#关闭图像流to_csv("train",1000)#转换到train.csv 1000个数据to_csv("t10k",1000)#转换到t10k.csv 1000个数据
二.用sklearn的交叉验证处理数据,SVM训练数据预测结果,metrics生成分类报告和准确率
#用sklearn中的SVM来训练模型,预测数据集from sklearn import cross_validation,svm,metricsdef load_csv(fname):labels=[]images=[]with open(fname,"r") as f:for line in f:cols=line.split(",")if len(cols)<2:continuelabels.append(int(cols.pop(0)))vals=list(map(lambda n: int(n) / 256,cols))images.append(vals)return {"labels":labels,"images":images}data=load_csv("./data/train.csv")test=load_csv("./data/t10k.csv")clf=svm.SVC()clf.fit(data["images"],data["labels"])#训练数据集predict=clf.predict(test["images"])#预测测试集score=metrics.accuracy_score(test["labels"],predict)#生成测试精度report=metrics.classification_report(test["labels"],predict)#生成交叉验证的报告print(score)#显示数据精度print(report)#显示交叉验证数据集报告
运行结果
三组平均测试精度为0.772
参考文献:
《统计学习方法》
《Web scraping and machine learning by python》
1 0
- 机器学习-实战-入门-MNIST手写数字识别
- keras 手把手入门#1-MNIST手写数字识别 深度学习实战闪电入门
- TensorFlow实战—mnist手写数字识别
- Tensorflow入门 mnist手写数字识别
- 用MXnet入门实战深度学习之一:安装GPU版mxnet并跑一个MNIST手写数字识别
- MNIST手写数字识别
- TensorFlow学习---实现mnist手写数字识别
- 机器学习xgboost实战—手写数字识别
- 机器学习实战——手写数字识别
- 机器学习xgboost实战—手写数字识别
- 机器学习xgboost实战—手写数字识别 (DMatrix)
- KNN--用于手写数字识别(机器学习入门笔记)
- KNN--用于手写数字识别(机器学习入门笔记)
- OpenCV机器学习:SVM分类器实现MNIST手写数字识别
- TensorFlow实战-mnist手写数字识别(卷积神经网络)
- keras入门实战:手写数字识别
- cnn 手写数字识别 mnist
- tensorflow-mnist手写数字识别
- QT调用编辑caffe c++ API
- poj 2155 Matrix
- 抽象类细节及使用方法
- HDU2066
- Navicat常用快捷键
- 机器学习-实战-入门-MNIST手写数字识别
- WIN32窗口类风格和窗口风格(备查询)
- SuperTextView-从未如此惊艳!一个超级的TextView
- Ubuntu17.04安装Firefox的flash插件
- IntelliJ IDEA下的使用git
- Java 文档注释
- Redis与Memcached的区别
- ThinkPHP中的字段计算
- golang学习笔记