# 手写识别系统
#准备数据:将图片转换为测试向量
def img2vector(filename):
returnvect = zero((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
#测试算法:使用K近邻算法识别手写数字
def handwritingclasstest():
from os import listdir
hwlabels = []#存储类别标签
trainingfilelist = listdir('trainingDigits')#读取trainingDigits文件夹下的文件的内容
m = len(trainingfilelist)#获取文件数目
trainingmat = zeros((m,1024))#书上的二进制图像矩阵是32*32的,将其转化成1*1024的向量
#trainingDigits下的文件用于训练,然后再对testdigits中的进行测试
for i in range(m):#遍历每一个文件,将每一个文件转换成1*1024的矩阵
filenamestr = trainingfilelist[i]#type是列表
filestr = filenamestr.split('.')[0]#去掉.txt
classnumstr = int(filestr.split('_')[0])
hwlabels.append(classnumstr)
trainingmat[i,:] = img2vector('trainingDigits/%s'%filenamestr)
testfilelist = 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('trainingDigits/%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("errors is: %d"%errorcount)
print("errors rate is: %f"%(errorcount/float(mtest)))
日常复习KNN近邻算法补充
最新推荐文章于 2023-04-15 17:01:33 发布