码迷,mamicode.com
首页 > 编程语言 > 详细

Python 学习笔记(Machine Learning In Action)K-近邻算法识别手写数字

时间:2016-08-12 21:22:23      阅读:587      评论:0      收藏:0      [点我收藏+]

标签:



1
from numpy import * 2 import matplotlib 3 from os import listdir 4 import kNN 5 def img2vector(filename): 6 returnVect = zeros((1,1024)) 7 fr = open(filename) 8 for i in range(32): 9 lineStr = fr.readline() 10 for j in range(32): 11 returnVect[0,32*i+j] = int(lineStr[j]) 12 return returnVect 13 def handwritingClassTest(): 14 hwLabels = [] 15 trainingFileList = listdir(trainingDigits) #load the training set 16 m = len(trainingFileList) 17 trainingMat = zeros((m,1024)) 18 for i in range(m): 19 fileNameStr = trainingFileList[i] 20 fileStr = fileNameStr.split(.)[0] #take off .txt 0_0.txt 将整个文件名以 . 分开 取第一部分也就是0_0 21 classNumStr = int(fileStr.split(_)[0]) #0_0 以 _ 分开 取第一部分也就是0 22 hwLabels.append(classNumStr) 23 trainingMat[i,:] = img2vector(trainingDigits/%s % fileNameStr) 24 testFileList = listdir(testDigits) #iterate through the test set 25 errorCount = 0.0 26 mTest = len(testFileList) 27 for i in range(mTest): 28 fileNameStr = testFileList[i] 29 fileStr = fileNameStr.split(.)[0] #take off .txt 30 classNumStr = int(fileStr.split(_)[0]) 31 vectorUnderTest = img2vector(testDigits/%s % fileNameStr) 32 classifierResult = kNN.classify0(vectorUnderTest, trainingMat, hwLabels, 3) 33 print "the classifier came back with: %d, the real answer is: %d" % (classifierResult, classNumStr) 34 if (classifierResult != classNumStr): errorCount += 1.0 35 print "\nthe total number of errors is: %d" % errorCount 36 print "\nthe total error rate is: %f" % (errorCount/float(mTest)) 37 handwritingClassTest()

技术分享技术分享

Python 学习笔记(Machine Learning In Action)K-近邻算法识别手写数字

标签:

原文地址:http://www.cnblogs.com/wlc297984368/p/5766191.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!