机器学习实战python版第二章示例:手写识别系统
来源:互联网 发布:彩票中奖的人 知乎 编辑:程序博客网 时间:2024/06/05 10:17
手写识别系统和前面的例子差不多,我们所需要做的就是把图数据转换成一维数组数据:
数据准备:
def img2vector(filename): returnVect = zeros((1,1024))#创建一行1024列的数组 fr = open(filename) for i in range(32): lineStr = fr.readline()#按行读入数据 for j in range(32): returnVect[0,32*i+j] = int(lineStr[j])#将字符型数字转换为整型 return returnVect
内容也比较容易,将二维数组循环读入,保存为一维行:
结果如下。
from numpy import *
import matplotlib
import matplotlib.pyplot as plt
import kNN
testVector = kNN.img2vector(‘testDigits/0_13.txt’)
testVector[0,0:32]
array([ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 1., 1., 1., 1., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0.])
测试算法
转换为一维的以后就和前面的一样了,
def handwritingClassTest():
hwLabels = []
trainingFileList = listdir(‘trainingDigits’) #load the training set读取文件名
m = len(trainingFileList)#m等于文件的个数
trainingMat = zeros((m,1024))#建立m行1024列数组
for i in range(m):
fileNameStr = trainingFileList[i]
fileStr = fileNameStr.split(‘.’)[0] #take off .txt去掉.txt
classNumStr = int(fileStr.split(‘‘)[0])#前的数字,表示的就是该图对应的数字
hwLabels.append(classNumStr)
trainingMat[i,:] = img2vector(‘trainingDigits/%s’ % fileNameStr)
testFileList = listdir(‘testDigits’) #iterate through the test set
errorCount = 0.0
mTest = len(testFileList)
for i in range(mTest):
fileNameStr = testFileList[i]
fileStr = fileNameStr.split(‘.’)[0] #take off .txt
classNumStr = int(fileStr.split(‘_’)[0])
vectorUnderTest = img2vector(‘testDigits/%s’ % fileNameStr)
classifierResult = classify0(vectorUnderTest, trainingMat, hwLabels, 3)
print “the classifier came back with: %d, the real answer is: %d” % (classifierResult, classNumStr)
if (classifierResult != classNumStr): errorCount += 1.0
print “\nthe total number of errors is: %d” % errorCount
print “\nthe total error rate is: %f” % (errorCount/float(mTest))
the classifier came back with: 9, the real answer is: 9
the classifier came back with: 9, the real answer is: 9
the classifier came back with: 9, the real answer is: 9
the classifier came back with: 9, the real answer is: 9
the classifier came back with: 9, the real answer is: 9
the classifier came back with: 9, the real answer is: 9
the classifier came back with: 9, the real answer is: 9
the classifier came back with: 9, the real answer is: 9
the classifier came back with: 9, the real answer is: 9
the classifier came back with: 9, the real answer is: 9
the total number of errors is: 11
the total error rate is: 0.011628
- 机器学习实战python版第二章示例:手写识别系统
- Python机器学习实战之kNN手写识别系统
- 【机器学习实战02】手写识别系统
- 《机器学习实战》第二章:k-近邻算法(3)手写数字识别
- 【《机器学习实战》第2章读书笔记】手写数字识别系统剖析
- 机器学习 & python k-近邻算法处理手写识别系统
- 机器学习 & python k-近邻算法处理手写识别系统
- 机器学习实战kNN之手写识别
- 机器学习实战kNN之手写识别
- 机器学习实战kNN之手写识别
- 机器学习实战-knn_手写识别
- 机器学习--knn手写数字识别系统
- K近邻算法(一) python实现,手写数字识别(from机器学习实战)
- 机器学习实战笔记——基于KNN算法的手写识别系统
- 机器学习实战—k近邻算法(kNN)03-手写识别系统
- 机器学习实战之程序清单1-kNN(手写数字识别系统)
- 【10月23日】机器学习实战(一)KNN算法:手写识别系统
- 机器学习实战笔记-K近邻算法3(手写识别系统)
- ScrollView隐藏滚动条
- 第三方组件安装位置
- nyoj+区间覆盖贪心
- Windows系统下开源编解码库H.264与FFmpeg-2.8.2的编译与配置
- JAVA中的“抽象接口”
- 机器学习实战python版第二章示例:手写识别系统
- 欢迎使用CSDN-markdown编辑器
- 混淆矩阵
- django1.9.1+apache24+mod_wsgi详解(win)
- java学习前言
- 昌大软院大神养成计划之网页设计之路第四天
- 海量数据处理问题
- 判断java虚拟机内存里的对象已死两种方式
- 费马小定理