1 准备数据:将图像转换为测试向量
这次数据集还是有两种,训练数据集和测试数据集,分别有2000个,900个。
我们将把一个32*32的二进制图像矩阵转换为1 x 1024的向量,这样前两节使用的分类器就可以处理数字图像信息了。
代码:
def img2vector(filename):
returnVect = zeros((1,1024))
file = open(filename)
for i in range(32):
line = file.readline()
for j in range(32):
returnVect[0,i*32+j] = line[j]
return returnVect
效果截图:
测试算法
代码:
def handWritingTest():
hwLabels = []
trainingFileList = os.listdir(‘trainingDigits‘)
trainingFileLength = len(trainingFileList)
trainingMat = zeros((trainingFileLength,1024))
for i in range(trainingFileLength):
fileNameStr = trainingFileList[i]
className = fileNameStr.split(‘_‘)[0]
hwLabels.append(int(className))
fileVector = img2vector(‘trainingDigits/‘ + fileNameStr)
trainingMat[i,:] = fileVector
testFileList = os.listdir(‘testDigits‘)
testFileLength = len(testFileList)
errorCount = 0.0
for i in range(testFileLength):
fileNameStr = testFileList[i]
className = int(fileNameStr.split(‘_‘)[0])
fileVector = img2vector(‘testDigits/‘ + fileNameStr)
testResult = classify0(fileVector,trainingMat,hwLabels,3)
print("the classifier came back with: %d, the real answer is: %d" % (testResult,className))
if(testResult != className):
errorCount+=1.0
errorRate = errorCount/float(testFileLength)
print("the errorRate is : %f" % errorRate)
结果截图:
分别将k改为4,5:
可以发现错误率逐渐增高
时间: 2024-09-28 02:27:39