kNN_handwriting
import numpy as np
from os import listdir
import operator
def img2vector(filename):
fr = open(filename)
lines = fr.readlines()
returnVect = np.zeros((1,1024))
for i in range(32):
line = lines[i]
for j in range(32):
returnVect[0,32*i+j] = int(line[j])
return returnVect
def classify(inX, dataset, labels, k):
datasetSize = dataset.shape[0]
diffMat = np.tile(inX,(datasetSize,1)) - dataset
sqDiffMat = diffMat**2
sqDistance = np.sum(sqDiffMat, axis=1)
distances = sqDistance**0.5
sortedDistIndicies = np.argsort(distances)
classCount = {}
for i in range(k):
voteIlable = labels[sortedDistIndicies[i]]
classCount[voteIlable] = classCount.get(voteIlable,0)+1
sortedDistIndicies = sorted(classCount.items(),key=operator.itemgetter(1),reverse=True)
return sortedDistIndicies[0][0]
def handwritingClassTest():
hwLabels = []
trainingFileList = listdir('trainingDigits')
m = len(trainingFileList)
trainingMat = np.zeros((m,1024))
for i in range(m):
fileNameStr = trainingFileList[i]
filestr = fileNameStr.split('.')[0]
classNumStr = filestr.split('_')[0]
hwLabels.append(classNumStr)
trainingMat[i,:] = img2vector('trainingDigits/%s' % fileNameStr)
errorCount = 0.0
testFileList = listdir('testDigits')
mTest = len(testFileList)
for i in range(mTest):
fileNameStr = testFileList[i]
filestr = fileNameStr.split('.')[0]
classNumStr = filestr.split('_')[0]
vectorunderTest = img2vector('testDigits/%s' % fileNameStr)
classifierResult = classify(vectorunderTest, trainingMat, hwLabels, 3)
print "the classfier came back with: %s, the real answer is: %s" % (classifierResult, classNumStr)
if(classifierResult!=classNumStr):
errorCount+=1
print "the total error rate is: %f" % (errorCount/float(mTest))
posted on 2021-08-28 18:37 Yan12345678 阅读(57) 评论(0) 编辑 收藏 举报