在上上篇”k-近邻分类算法“添加如下代码
#将32*32图像矩阵转换为1*1024向量
def img2vector(filename):
returnVect = zeros((1, 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
#手写数字识别系统的测试代码
def handwritingClassTest():
hwLabels = []
trainingFileList = os.listdir('trainingDigits') #获取文件夹目录
m = len(trainingFileList) #文件夹下文件数
traingMat = zeros((m, 1024))
for i in range(m): #从文件名解析分类数字
fileNameStr = trainingFileList[i]
fileStr = fileNameStr.split('.')[0]
classNumStr = int(fileStr.split('_')[0])
hwLabels.append(classNumStr)
traingMat[i,:] = img2vector('trainingDigits/%s' %fileNameStr)
testFileList = os.listdir('testDigits')
errorCount = 0.0
mTest = len(testFileList)
for i in range(mTest):
fileNameStr = testFileList[i]
fileStr = fileNameStr.split('.')[0]
classNumStr = int(fileStr.split('_')[0])
vectorUnderTest = img2vector('testDigits/%s' %fileNameStr)
classifierResult = classify0(vectorUnderTest, traingMat, 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))
测试
>>> import KNN
>>> handwritingClassTest()
结果
... ...
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
错误率约1.2%