• 收集数据:提供文本文件
  • 准备数据:编写函数classify0(), 将图像格式转换为分类器使用的list格式
  • 分析数据:检查数据
  • 训练算法:不适用于k-近邻算法
  • 测试算法:编写函数切分测试样本; 测试样本是已经完成分类的数据,如果预测分类与实际分类不同,则标记为错误
  • 使用算法:
Numpy中有两个常用数据类型,数组(array/ndarray)、矩阵(matrix)
  • matrix是array的特殊形式,matrix只能表示2维的数据,而array可以表示任意维度数据,matrix相当于二维数组。
  • 当matrix的某一维的维度为1时,即可称为向量(m,1)是列向量,(1,n)是行向量
import numpy as np
import os
import operator

准备数据:将图像转换为测试向量

# 将图像处理为向量
def img2vector(filename):
    returnVect = np.zeros((1,1024))
#     print(returnVect)
    with open(filename,'r') as f:
        for i in range(32):
            lineStr = f.readline()
#             print(lineStr)
            for j in range(32):
                returnVect[0,32*i+j] = int(lineStr[j])
        return returnVect
testVector = img2vector("./dataset/testDigits/0_0.txt")
[[0. 0. 0. ... 0. 0. 0.]]
testVector[0,0:120]
array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 1., 0., 0.,
       0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
       0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 1., 1., 1., 1., 1., 0.,
       0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
       0., 0., 0., 0., 0., 0., 0., 1., 1., 1., 1., 1., 1., 1., 1., 1., 0.,
       0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
       0., 0., 0., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 0., 0., 0.,
       0.])
# kNN分类器
def classify0(inX, dataSet, labels, k):
    datsSetSize = dataSet.shape[0]  #行数
    diffMat = np.tile(inX, (datsSetSize,1)) -dataSet #将输入矩阵放大,求差
    sqDiffMat = diffMat ** 2
    sqDistances = sqDiffMat.sum(axis=1) #跨列,按行求和
    distances = sqDistances ** 0.5 # 欧式距离
    sortedDistIndicies = distances.argsort()  # 返回数组值从小到大的索引
    # 选择距离最小的k个点
    classCount={}
    for i in range(k):
        voteIlabel = labels[sortedDistIndicies[i]]
        classCount[voteIlabel]= classCount.get(voteIlabel,0) +1
    # 排序
    sortedClassCount = sorted(classCount.items(),key=operator.itemgetter(1), reverse=True)
    return sortedClassCount[0][0]

测试算法:使用k-近邻算法识别手写数字

def handwriteClassTest():
    hwLabels =[]
    trainingFileList = os.listdir('./dataset/trainingDigits')
    m = len(trainingFileList)
#     print(m)
    trainingMat = np.zeros((m,1024))
    for i in range(m):
        fileNameStr = trainingFileList[i]
#         print(fileNameStr)
        fileStr = fileNameStr.split('.')[0]
#         print(fileStr)
        classNum = int(fileStr.split('_')[0])
        hwLabels.append(classNum)
        trainingMat[i,:] = img2vector('./dataset/trainingDigits/%s' % fileNameStr)


    testFileList = os.listdir('./dataset/testDigits')
    errorCount =0.0
    mTest = len(testFileList)
    for j in range(mTest):
        fileNameStr = testFileList[j]
        fileStr = fileNameStr.split('.')[0]
        classNum = int(fileStr.split('_')[0])
        vectorTest = img2vector('./dataset/testDigits/%s' % fileNameStr)
        classifierResult = classify0(vectorTest, trainingMat, hwLabels, 3)
        print("classifierResult:",classifierResult, "real answer:",classNum)
        if (classifierResult != classNum):
            errorCount += 1.0
    print("正确率:",1.0-errorCount/float(mTest))

handwriteClassTest()
classifierResult: 4 real answer: 4
classifierResult: 4 real answer: 4
classifierResult: 3 real answer: 3
classifierResult: 9 real answer: 9
classifierResult: 0 real answer: 0
classifierResult: 0 real answer: 0
classifierResult: 9 real answer: 9
classifierResult: 7 real answer: 7
classifierResult: 7 real answer: 7
classifierResult: 0 real answer: 0
classifierResult: 3 real answer: 3
classifierResult: 2 real answer: 2
classifierResult: 2 real answer: 2
classifierResult: 5 real answer: 5
classifierResult: 5 real answer: 5
classifierResult: 5 real answer: 5
classifierResult: 2 real answer: 2
classifierResult: 6 real answer: 6
classifierResult: 6 real answer: 6
classifierResult: 9 real answer: 9
classifierResult: 8 real answer: 8
classifierResult: 1 real answer: 8
classifierResult: 1 real answer: 1
classifierResult: 8 real answer: 8
classifierResult: 1 real answer: 1
classifierResult: 3 real answer: 8
classifierResult: 9 real answer: 9
classifierResult: 6 real answer: 6
classifierResult: 6 real answer: 6
classifierResult: 5 real answer: 5
classifierResult: 2 real answer: 2
classifierResult: 5 real answer: 5
classifierResult: 5 real answer: 5
classifierResult: 2 real answer: 2
classifierResult: 2 real answer: 2
classifierResult: 9 real answer: 9
classifierResult: 3 real answer: 3
classifierResult: 0 real answer: 0
classifierResult: 7 real answer: 7
classifierResult: 7 real answer: 7
classifierResult: 0 real answer: 0
classifierResult: 9 real answer: 9
classifierResult: 9 real answer: 9
classifierResult: 0 real answer: 0
classifierResult: 3 real answer: 3
classifierResult: 4 real answer: 4
classifierResult: 4 real answer: 4
classifierResult: 4 real answer: 4
classifierResult: 3 real answer: 3
classifierResult: 4 real answer: 4
classifierResult: 3 real answer: 3
classifierResult: 1 real answer: 1
classifierResult: 9 real answer: 9
classifierResult: 0 real answer: 0
classifierResult: 0 real answer: 0
classifierResult: 7 real answer: 7
classifierResult: 9 real answer: 9
classifierResult: 7 real answer: 7
classifierResult: 0 real answer: 0
classifierResult: 7 real answer: 7
classifierResult: 9 real answer: 9
classifierResult: 0 real answer: 0
classifierResult: 2 real answer: 2
classifierResult: 5 real answer: 5
classifierResult: 2 real answer: 2
classifierResult: 5 real answer: 5
classifierResult: 5 real answer: 5
classifierResult: 2 real answer: 2
classifierResult: 2 real answer: 2
classifierResult: 6 real answer: 6
classifierResult: 6 real answer: 6
classifierResult: 1 real answer: 1
classifierResult: 1 real answer: 1
classifierResult: 8 real answer: 8
classifierResult: 6 real answer: 6
classifierResult: 9 real answer: 9
classifierResult: 8 real answer: 8
classifierResult: 8 real answer: 8
classifierResult: 9 real answer: 9
classifierResult: 1 real answer: 1
classifierResult: 6 real answer: 6
classifierResult: 8 real answer: 8
classifierResult: 6 real answer: 6
classifierResult: 1 real answer: 1
classifierResult: 6 real answer: 6
classifierResult: 2 real answer: 2
classifierResult: 5 real answer: 5
classifierResult: 2 real answer: 2
classifierResult: 5 real answer: 5
classifierResult: 2 real answer: 2
classifierResult: 2 real answer: 2
classifierResult: 5 real answer: 5
classifierResult: 9 real answer: 9
classifierResult: 7 real answer: 7
classifierResult: 7 real answer: 7
classifierResult: 0 real answer: 0
classifierResult: 0 real answer: 0
classifierResult: 9 real answer: 9
classifierResult: 7 real answer: 7
classifierResult: 7 real answer: 9
classifierResult: 0 real answer: 0
classifierResult: 1 real answer: 1
classifierResult: 3 real answer: 3
classifierResult: 4 real answer: 4
classifierResult: 4 real answer: 4
classifierResult: 3 real answer: 3
classifierResult: 9 real answer: 3
classifierResult: 4 real answer: 4
classifierResult: 4 real answer: 4
classifierResult: 4 real answer: 4
classifierResult: 3 real answer: 3
classifierResult: 7 real answer: 7
classifierResult: 9 real answer: 9
classifierResult: 7 real answer: 7
classifierResult: 0 real answer: 0
classifierResult: 0 real answer: 0
classifierResult: 7 real answer: 7
classifierResult: 9 real answer: 9
classifierResult: 9 real answer: 9
classifierResult: 0 real answer: 0
classifierResult: 0 real answer: 0
classifierResult: 5 real answer: 5
classifierResult: 2 real answer: 2
classifierResult: 5 real answer: 5
classifierResult: 2 real answer: 2
classifierResult: 2 real answer: 2
classifierResult: 5 real answer: 5
classifierResult: 5 real answer: 5
classifierResult: 2 real answer: 2
classifierResult: 8 real answer: 8
classifierResult: 1 real answer: 1
classifierResult: 9 real answer: 9
classifierResult: 8 real answer: 8
classifierResult: 6 real answer: 6
classifierResult: 1 real answer: 1
classifierResult: 8 real answer: 8
classifierResult: 6 real answer: 6
classifierResult: 6 real answer: 6
classifierResult: 8 real answer: 8
classifierResult: 6 real answer: 6
classifierResult: 1 real answer: 1
classifierResult: 8 real answer: 8
classifierResult: 9 real answer: 9
classifierResult: 8 real answer: 8
classifierResult: 1 real answer: 1
classifierResult: 2 real answer: 2
classifierResult: 5 real answer: 5
classifierResult: 2 real answer: 2
classifierResult: 5 real answer: 5
classifierResult: 2 real answer: 2
classifierResult: 5 real answer: 5
classifierResult: 5 real answer: 5
classifierResult: 2 real answer: 2
classifierResult: 0 real answer: 0
classifierResult: 9 real answer: 9
classifierResult: 0 real answer: 0
classifierResult: 0 real answer: 0
classifierResult: 9 real answer: 9
classifierResult: 7 real answer: 7
classifierResult: 7 real answer: 7
classifierResult: 0 real answer: 0
classifierResult: 9 real answer: 9
classifierResult: 7 real answer: 7
classifierResult: 4 real answer: 4
classifierResult: 3 real answer: 3
classifierResult: 4 real answer: 4
classifierResult: 3 real answer: 3
classifierResult: 4 real answer: 4
classifierResult: 3 real answer: 3
classifierResult: 4 real answer: 4
classifierResult: 4 real answer: 4
classifierResult: 7 real answer: 7
classifierResult: 7 real answer: 7
classifierResult: 4 real answer: 4
classifierResult: 0 real answer: 0
classifierResult: 9 real answer: 9
classifierResult: 9 real answer: 9
classifierResult: 0 real answer: 0
classifierResult: 7 real answer: 7
classifierResult: 0 real answer: 0
classifierResult: 5 real answer: 5
classifierResult: 5 real answer: 5
classifierResult: 2 real answer: 2
classifierResult: 2 real answer: 2
classifierResult: 5 real answer: 5
classifierResult: 2 real answer: 2
classifierResult: 2 real answer: 2
classifierResult: 9 real answer: 9
classifierResult: 8 real answer: 8
classifierResult: 1 real answer: 1
classifierResult: 8 real answer: 8
classifierResult: 6 real answer: 6
classifierResult: 6 real answer: 6
classifierResult: 7 real answer: 7
classifierResult: 7 real answer: 7
classifierResult: 6 real answer: 6
classifierResult: 6 real answer: 6
classifierResult: 1 real answer: 1
classifierResult: 8 real answer: 8
classifierResult: 8 real answer: 8
classifierResult: 9 real answer: 9
classifierResult: 2 real answer: 2
classifierResult: 5 real answer: 5
classifierResult: 2 real answer: 2
classifierResult: 2 real answer: 2
classifierResult: 2 real answer: 2
classifierResult: 5 real answer: 5
classifierResult: 5 real answer: 5
classifierResult: 7 real answer: 7
classifierResult: 0 real answer: 0
classifierResult: 9 real answer: 9
classifierResult: 0 real answer: 0
classifierResult: 0 real answer: 0
classifierResult: 9 real answer: 9
classifierResult: 7 real answer: 7
classifierResult: 4 real answer: 4
classifierResult: 7 real answer: 7
classifierResult: 4 real answer: 4
classifierResult: 4 real answer: 4
classifierResult: 3 real answer: 3
classifierResult: 4 real answer: 4
classifierResult: 4 real answer: 4
classifierResult: 3 real answer: 3
classifierResult: 3 real answer: 3
classifierResult: 4 real answer: 4
classifierResult: 8 real answer: 8
classifierResult: 3 real answer: 3
classifierResult: 4 real answer: 4
classifierResult: 4 real answer: 4
classifierResult: 0 real answer: 0
classifierResult: 7 real answer: 7
classifierResult: 9 real answer: 9
classifierResult: 0 real answer: 0
classifierResult: 9 real answer: 9
classifierResult: 7 real answer: 7
classifierResult: 0 real answer: 0
classifierResult: 2 real answer: 2
classifierResult: 2 real answer: 2
classifierResult: 5 real answer: 5
classifierResult: 5 real answer: 5
classifierResult: 6 real answer: 6
classifierResult: 1 real answer: 1
classifierResult: 8 real answer: 8
classifierResult: 6 real answer: 6
classifierResult: 8 real answer: 8
classifierResult: 1 real answer: 1
classifierResult: 1 real answer: 1
classifierResult: 8 real answer: 8
classifierResult: 6 real answer: 6
classifierResult: 8 real answer: 8
classifierResult: 0 real answer: 0
classifierResult: 0 real answer: 0
classifierResult: 8 real answer: 8
classifierResult: 6 real answer: 6
classifierResult: 1 real answer: 1
classifierResult: 6 real answer: 6
classifierResult: 8 real answer: 8
classifierResult: 8 real answer: 8
classifierResult: 1 real answer: 1
classifierResult: 6 real answer: 6
classifierResult: 1 real answer: 8
classifierResult: 6 real answer: 6
classifierResult: 1 real answer: 1
classifierResult: 5 real answer: 5
classifierResult: 2 real answer: 2
classifierResult: 5 real answer: 5
classifierResult: 2 real answer: 2
classifierResult: 7 real answer: 7
classifierResult: 0 real answer: 0
classifierResult: 9 real answer: 9
classifierResult: 0 real answer: 0
classifierResult: 0 real answer: 0
classifierResult: 1 real answer: 9
classifierResult: 7 real answer: 7
classifierResult: 4 real answer: 4
classifierResult: 4 real answer: 4
classifierResult: 3 real answer: 3
classifierResult: 8 real answer: 8
classifierResult: 3 real answer: 3
classifierResult: 4 real answer: 4
classifierResult: 4 real answer: 4
classifierResult: 3 real answer: 3
classifierResult: 4 real answer: 4
classifierResult: 4 real answer: 4
classifierResult: 4 real answer: 4
classifierResult: 3 real answer: 3
classifierResult: 8 real answer: 8
classifierResult: 3 real answer: 3
classifierResult: 4 real answer: 4
classifierResult: 4 real answer: 4
classifierResult: 0 real answer: 0
classifierResult: 9 real answer: 9
classifierResult: 0 real answer: 0
classifierResult: 7 real answer: 7
classifierResult: 2 real answer: 2
classifierResult: 2 real answer: 2
classifierResult: 5 real answer: 5
classifierResult: 2 real answer: 2
classifierResult: 6 real answer: 6
classifierResult: 6 real answer: 6
classifierResult: 8 real answer: 8
classifierResult: 1 real answer: 1
classifierResult: 1 real answer: 1
classifierResult: 8 real answer: 8
classifierResult: 6 real answer: 6
classifierResult: 8 real answer: 8
classifierResult: 8 real answer: 8
classifierResult: 6 real answer: 6
classifierResult: 1 real answer: 1
classifierResult: 8 real answer: 8
classifierResult: 8 real answer: 8
classifierResult: 1 real answer: 1
classifierResult: 6 real answer: 6
classifierResult: 6 real answer: 6
classifierResult: 2 real answer: 2
classifierResult: 5 real answer: 5
classifierResult: 2 real answer: 2
classifierResult: 2 real answer: 2
classifierResult: 7 real answer: 7
classifierResult: 9 real answer: 9
classifierResult: 0 real answer: 0
classifierResult: 0 real answer: 0
classifierResult: 4 real answer: 4
classifierResult: 4 real answer: 4
classifierResult: 3 real answer: 3
classifierResult: 8 real answer: 8
classifierResult: 3 real answer: 3
classifierResult: 4 real answer: 4
classifierResult: 4 real answer: 4
classifierResult: 3 real answer: 3
classifierResult: 8 real answer: 8
classifierResult: 3 real answer: 3
classifierResult: 4 real answer: 4
classifierResult: 4 real answer: 4
classifierResult: 6 real answer: 6
classifierResult: 4 real answer: 4
classifierResult: 4 real answer: 4
classifierResult: 7 real answer: 7
classifierResult: 0 real answer: 0
classifierResult: 0 real answer: 0
classifierResult: 9 real answer: 9
classifierResult: 5 real answer: 5
classifierResult: 2 real answer: 2
classifierResult: 2 real answer: 2
classifierResult: 1 real answer: 1
classifierResult: 8 real answer: 8
classifierResult: 8 real answer: 8
classifierResult: 1 real answer: 1
classifierResult: 6 real answer: 6
classifierResult: 6 real answer: 6
classifierResult: 8 real answer: 8
classifierResult: 8 real answer: 8
classifierResult: 1 real answer: 1
classifierResult: 6 real answer: 6
classifierResult: 1 real answer: 1
classifierResult: 6 real answer: 6
classifierResult: 8 real answer: 8
classifierResult: 6 real answer: 6
classifierResult: 6 real answer: 6
classifierResult: 8 real answer: 8
classifierResult: 1 real answer: 1
classifierResult: 1 real answer: 1
classifierResult: 8 real answer: 8
classifierResult: 2 real answer: 2
classifierResult: 2 real answer: 2
classifierResult: 5 real answer: 5
classifierResult: 0 real answer: 0
classifierResult: 9 real answer: 9
classifierResult: 0 real answer: 0
classifierResult: 7 real answer: 7
classifierResult: 4 real answer: 4
classifierResult: 4 real answer: 4
classifierResult: 6 real answer: 6
classifierResult: 4 real answer: 4
classifierResult: 4 real answer: 4
classifierResult: 3 real answer: 3
classifierResult: 8 real answer: 8
classifierResult: 3 real answer: 3
classifierResult: 3 real answer: 3
classifierResult: 8 real answer: 8
classifierResult: 3 real answer: 3
classifierResult: 4 real answer: 4
classifierResult: 4 real answer: 4
classifierResult: 3 real answer: 3
classifierResult: 4 real answer: 4
classifierResult: 4 real answer: 4
classifierResult: 4 real answer: 4
classifierResult: 7 real answer: 7
classifierResult: 9 real answer: 9
classifierResult: 9 real answer: 9
classifierResult: 0 real answer: 0
classifierResult: 0 real answer: 0
classifierResult: 7 real answer: 7
classifierResult: 4 real answer: 4
classifierResult: 5 real answer: 5
classifierResult: 2 real answer: 2
classifierResult: 2 real answer: 2
classifierResult: 5 real answer: 5
classifierResult: 2 real answer: 2
classifierResult: 1 real answer: 1
classifierResult: 8 real answer: 8
classifierResult: 6 real answer: 6
classifierResult: 8 real answer: 8
classifierResult: 1 real answer: 1
classifierResult: 8 real answer: 8
classifierResult: 6 real answer: 6
classifierResult: 6 real answer: 6
classifierResult: 1 real answer: 1
classifierResult: 8 real answer: 8
classifierResult: 6 real answer: 6
classifierResult: 6 real answer: 6
classifierResult: 8 real answer: 8
classifierResult: 6 real answer: 6
classifierResult: 1 real answer: 1
classifierResult: 6 real answer: 6
classifierResult: 8 real answer: 8
classifierResult: 8 real answer: 8
classifierResult: 1 real answer: 1
classifierResult: 1 real answer: 1
classifierResult: 6 real answer: 6
classifierResult: 1 real answer: 8
classifierResult: 2 real answer: 2
classifierResult: 2 real answer: 2
classifierResult: 5 real answer: 5
classifierResult: 5 real answer: 5
classifierResult: 2 real answer: 2
classifierResult: 4 real answer: 4
classifierResult: 0 real answer: 0
classifierResult: 7 real answer: 7
classifierResult: 9 real answer: 9
classifierResult: 0 real answer: 0
classifierResult: 9 real answer: 9
classifierResult: 7 real answer: 7
classifierResult: 4 real answer: 4
classifierResult: 4 real answer: 4
classifierResult: 4 real answer: 4
classifierResult: 4 real answer: 4
classifierResult: 3 real answer: 3
classifierResult: 3 real answer: 3
classifierResult: 8 real answer: 8
classifierResult: 4 real answer: 4
classifierResult: 3 real answer: 3
classifierResult: 8 real answer: 8
classifierResult: 4 real answer: 4
classifierResult: 4 real answer: 4
classifierResult: 3 real answer: 3
classifierResult: 3 real answer: 3
classifierResult: 4 real answer: 4
classifierResult: 3 real answer: 3
classifierResult: 6 real answer: 6
classifierResult: 3 real answer: 3
classifierResult: 0 real answer: 0
classifierResult: 7 real answer: 7
classifierResult: 9 real answer: 9
classifierResult: 9 real answer: 9
classifierResult: 7 real answer: 7
classifierResult: 7 real answer: 7
classifierResult: 0 real answer: 0
classifierResult: 4 real answer: 4
classifierResult: 2 real answer: 2
classifierResult: 5 real answer: 5
classifierResult: 2 real answer: 2
classifierResult: 5 real answer: 5
classifierResult: 2 real answer: 2
classifierResult: 6 real answer: 6
classifierResult: 1 real answer: 1
classifierResult: 8 real answer: 8
classifierResult: 6 real answer: 6
classifierResult: 8 real answer: 8
classifierResult: 1 real answer: 1
classifierResult: 1 real answer: 1
classifierResult: 8 real answer: 8
classifierResult: 6 real answer: 6
classifierResult: 0 real answer: 0
classifierResult: 1 real answer: 1
classifierResult: 1 real answer: 1
classifierResult: 1 real answer: 1
classifierResult: 7 real answer: 1
classifierResult: 0 real answer: 0
classifierResult: 1 real answer: 1
classifierResult: 6 real answer: 6
classifierResult: 8 real answer: 8
classifierResult: 8 real answer: 8
classifierResult: 1 real answer: 1
classifierResult: 6 real answer: 6
classifierResult: 8 real answer: 8
classifierResult: 6 real answer: 6
classifierResult: 1 real answer: 1
classifierResult: 2 real answer: 2
classifierResult: 5 real answer: 5
classifierResult: 5 real answer: 5
classifierResult: 2 real answer: 2
classifierResult: 2 real answer: 2
classifierResult: 4 real answer: 4
classifierResult: 7 real answer: 7
classifierResult: 0 real answer: 0
classifierResult: 7 real answer: 7
classifierResult: 9 real answer: 9
classifierResult: 0 real answer: 0
classifierResult: 9 real answer: 9
classifierResult: 7 real answer: 7
classifierResult: 3 real answer: 3
classifierResult: 6 real answer: 6
classifierResult: 3 real answer: 3
classifierResult: 3 real answer: 3
classifierResult: 4 real answer: 4
classifierResult: 4 real answer: 4
classifierResult: 3 real answer: 3
classifierResult: 8 real answer: 8
classifierResult: 4 real answer: 4
classifierResult: 4 real answer: 4
classifierResult: 4 real answer: 4
classifierResult: 3 real answer: 3
classifierResult: 3 real answer: 3
classifierResult: 6 real answer: 6
classifierResult: 4 real answer: 4
classifierResult: 3 real answer: 3
classifierResult: 9 real answer: 9
classifierResult: 9 real answer: 9
classifierResult: 0 real answer: 0
classifierResult: 7 real answer: 7
classifierResult: 7 real answer: 7
classifierResult: 4 real answer: 4
classifierResult: 2 real answer: 2
classifierResult: 5 real answer: 5
classifierResult: 5 real answer: 5
classifierResult: 2 real answer: 2
classifierResult: 6 real answer: 6
classifierResult: 6 real answer: 6
classifierResult: 8 real answer: 8
classifierResult: 1 real answer: 1
classifierResult: 1 real answer: 1
classifierResult: 8 real answer: 8
classifierResult: 8 real answer: 8
classifierResult: 0 real answer: 0
classifierResult: 1 real answer: 1
classifierResult: 1 real answer: 1
classifierResult: 1 real answer: 1
classifierResult: 1 real answer: 1
classifierResult: 0 real answer: 0
classifierResult: 8 real answer: 8
classifierResult: 1 real answer: 1
classifierResult: 8 real answer: 8
classifierResult: 8 real answer: 8
classifierResult: 1 real answer: 1
classifierResult: 6 real answer: 6
classifierResult: 6 real answer: 6
classifierResult: 2 real answer: 2
classifierResult: 5 real answer: 5
classifierResult: 5 real answer: 5
classifierResult: 2 real answer: 2
classifierResult: 4 real answer: 4
classifierResult: 7 real answer: 7
classifierResult: 7 real answer: 7
classifierResult: 9 real answer: 9
classifierResult: 0 real answer: 0
classifierResult: 9 real answer: 9
classifierResult: 4 real answer: 4
classifierResult: 3 real answer: 3
classifierResult: 6 real answer: 6
classifierResult: 3 real answer: 3
classifierResult: 3 real answer: 3
classifierResult: 4 real answer: 4
classifierResult: 4 real answer: 4
classifierResult: 3 real answer: 3
classifierResult: 3 real answer: 3
classifierResult: 4 real answer: 4
classifierResult: 4 real answer: 4
classifierResult: 4 real answer: 4
classifierResult: 3 real answer: 3
classifierResult: 6 real answer: 6
classifierResult: 7 real answer: 7
classifierResult: 7 real answer: 7
classifierResult: 9 real answer: 9
classifierResult: 0 real answer: 0
classifierResult: 9 real answer: 9
classifierResult: 4 real answer: 4
classifierResult: 5 real answer: 5
classifierResult: 5 real answer: 5
classifierResult: 2 real answer: 2
classifierResult: 2 real answer: 2
classifierResult: 1 real answer: 1
classifierResult: 8 real answer: 8
classifierResult: 8 real answer: 8
classifierResult: 1 real answer: 1
classifierResult: 6 real answer: 6
classifierResult: 6 real answer: 6
classifierResult: 1 real answer: 1
classifierResult: 1 real answer: 1
classifierResult: 0 real answer: 0
classifierResult: 0 real answer: 0
classifierResult: 1 real answer: 1
classifierResult: 1 real answer: 1
classifierResult: 6 real answer: 6
classifierResult: 6 real answer: 6
classifierResult: 8 real answer: 8
classifierResult: 1 real answer: 1
classifierResult: 1 real answer: 1
classifierResult: 8 real answer: 8
classifierResult: 2 real answer: 2
classifierResult: 2 real answer: 2
classifierResult: 5 real answer: 5
classifierResult: 5 real answer: 5
classifierResult: 4 real answer: 4
classifierResult: 0 real answer: 0
classifierResult: 9 real answer: 9
classifierResult: 9 real answer: 9
classifierResult: 7 real answer: 7
classifierResult: 7 real answer: 7
classifierResult: 6 real answer: 6
classifierResult: 4 real answer: 4
classifierResult: 4 real answer: 4
classifierResult: 4 real answer: 4
classifierResult: 3 real answer: 3
classifierResult: 3 real answer: 3
classifierResult: 3 real answer: 3
classifierResult: 3 real answer: 3
classifierResult: 4 real answer: 4
classifierResult: 4 real answer: 4
classifierResult: 3 real answer: 3
classifierResult: 4 real answer: 4
classifierResult: 6 real answer: 6
classifierResult: 7 real answer: 7
classifierResult: 0 real answer: 0
classifierResult: 7 real answer: 7
classifierResult: 9 real answer: 9
classifierResult: 9 real answer: 9
classifierResult: 0 real answer: 0
classifierResult: 9 real answer: 9
classifierResult: 4 real answer: 4
classifierResult: 5 real answer: 5
classifierResult: 5 real answer: 5
classifierResult: 2 real answer: 2
classifierResult: 2 real answer: 2
classifierResult: 5 real answer: 5
classifierResult: 2 real answer: 2
classifierResult: 1 real answer: 1
classifierResult: 8 real answer: 8
classifierResult: 6 real answer: 6
classifierResult: 8 real answer: 8
classifierResult: 1 real answer: 1
classifierResult: 8 real answer: 8
classifierResult: 6 real answer: 6
classifierResult: 6 real answer: 6
classifierResult: 1 real answer: 1
classifierResult: 1 real answer: 1
classifierResult: 1 real answer: 1
classifierResult: 0 real answer: 0
classifierResult: 0 real answer: 0
classifierResult: 1 real answer: 1
classifierResult: 6 real answer: 6
classifierResult: 1 real answer: 1
classifierResult: 6 real answer: 6
classifierResult: 8 real answer: 8
classifierResult: 8 real answer: 8
classifierResult: 1 real answer: 1
classifierResult: 1 real answer: 1
classifierResult: 6 real answer: 6
classifierResult: 8 real answer: 8
classifierResult: 2 real answer: 2
classifierResult: 2 real answer: 2
classifierResult: 5 real answer: 5
classifierResult: 5 real answer: 5
classifierResult: 2 real answer: 2
classifierResult: 5 real answer: 5
classifierResult: 4 real answer: 4
classifierResult: 9 real answer: 9
classifierResult: 9 real answer: 9
classifierResult: 0 real answer: 0
classifierResult: 9 real answer: 9
classifierResult: 7 real answer: 7
classifierResult: 7 real answer: 7
classifierResult: 0 real answer: 0
classifierResult: 6 real answer: 6
classifierResult: 4 real answer: 4
classifierResult: 4 real answer: 4
classifierResult: 3 real answer: 3
classifierResult: 3 real answer: 3
classifierResult: 4 real answer: 4
classifierResult: 3 real answer: 3
classifierResult: 4 real answer: 4
classifierResult: 5 real answer: 5
classifierResult: 3 real answer: 3
classifierResult: 3 real answer: 3
classifierResult: 1 real answer: 1
classifierResult: 9 real answer: 9
classifierResult: 4 real answer: 4
classifierResult: 0 real answer: 0
classifierResult: 0 real answer: 0
classifierResult: 4 real answer: 4
classifierResult: 9 real answer: 9
classifierResult: 7 real answer: 7
classifierResult: 7 real answer: 7
classifierResult: 3 real answer: 3
classifierResult: 9 real answer: 9
classifierResult: 7 real answer: 7
classifierResult: 7 real answer: 7
classifierResult: 2 real answer: 2
classifierResult: 2 real answer: 2
classifierResult: 6 real answer: 5
classifierResult: 5 real answer: 5
classifierResult: 5 real answer: 5
classifierResult: 2 real answer: 2
classifierResult: 5 real answer: 5
classifierResult: 5 real answer: 5
classifierResult: 6 real answer: 6
classifierResult: 1 real answer: 1
classifierResult: 8 real answer: 8
classifierResult: 1 real answer: 1
classifierResult: 7 real answer: 7
classifierResult: 7 real answer: 7
classifierResult: 8 real answer: 8
classifierResult: 1 real answer: 1
classifierResult: 1 real answer: 1
classifierResult: 6 real answer: 6
classifierResult: 5 real answer: 5
classifierResult: 2 real answer: 2
classifierResult: 5 real answer: 5
classifierResult: 5 real answer: 5
classifierResult: 5 real answer: 5
classifierResult: 3 real answer: 5
classifierResult: 2 real answer: 2
classifierResult: 2 real answer: 2
classifierResult: 7 real answer: 7
classifierResult: 7 real answer: 7
classifierResult: 3 real answer: 3
classifierResult: 9 real answer: 9
classifierResult: 7 real answer: 7
classifierResult: 7 real answer: 7
classifierResult: 4 real answer: 4
classifierResult: 0 real answer: 0
classifierResult: 9 real answer: 9
classifierResult: 9 real answer: 9
classifierResult: 0 real answer: 0
classifierResult: 4 real answer: 4
classifierResult: 1 real answer: 1
classifierResult: 3 real answer: 3
classifierResult: 3 real answer: 3
classifierResult: 5 real answer: 5
classifierResult: 4 real answer: 4
classifierResult: 5 real answer: 5
classifierResult: 4 real answer: 4
classifierResult: 3 real answer: 3
classifierResult: 3 real answer: 3
classifierResult: 4 real answer: 4
classifierResult: 1 real answer: 1
classifierResult: 9 real answer: 9
classifierResult: 0 real answer: 0
classifierResult: 4 real answer: 4
classifierResult: 4 real answer: 4
classifierResult: 0 real answer: 0
classifierResult: 7 real answer: 7
classifierResult: 9 real answer: 9
classifierResult: 7 real answer: 7
classifierResult: 0 real answer: 0
classifierResult: 7 real answer: 7
classifierResult: 9 real answer: 9
classifierResult: 9 real answer: 9
classifierResult: 3 real answer: 3
classifierResult: 7 real answer: 7
classifierResult: 7 real answer: 7
classifierResult: 2 real answer: 2
classifierResult: 5 real answer: 5
classifierResult: 2 real answer: 2
classifierResult: 5 real answer: 5
classifierResult: 5 real answer: 5
classifierResult: 2 real answer: 2
classifierResult: 5 real answer: 5
classifierResult: 5 real answer: 5
classifierResult: 5 real answer: 5
classifierResult: 8 real answer: 8
classifierResult: 9 real answer: 9
classifierResult: 6 real answer: 6
classifierResult: 1 real answer: 1
classifierResult: 1 real answer: 1
classifierResult: 8 real answer: 8
classifierResult: 6 real answer: 6
classifierResult: 1 real answer: 1
classifierResult: 7 real answer: 7
classifierResult: 7 real answer: 7
classifierResult: 1 real answer: 1
classifierResult: 1 real answer: 1
classifierResult: 6 real answer: 6
classifierResult: 8 real answer: 8
classifierResult: 6 real answer: 6
classifierResult: 1 real answer: 1
classifierResult: 9 real answer: 9
classifierResult: 8 real answer: 8
classifierResult: 5 real answer: 5
classifierResult: 5 real answer: 5
classifierResult: 5 real answer: 5
classifierResult: 5 real answer: 5
classifierResult: 2 real answer: 2
classifierResult: 5 real answer: 5
classifierResult: 2 real answer: 2
classifierResult: 2 real answer: 2
classifierResult: 5 real answer: 5
classifierResult: 7 real answer: 7
classifierResult: 7 real answer: 7
classifierResult: 9 real answer: 9
classifierResult: 3 real answer: 3
classifierResult: 9 real answer: 9
classifierResult: 7 real answer: 7
classifierResult: 7 real answer: 7
classifierResult: 0 real answer: 0
classifierResult: 0 real answer: 0
classifierResult: 4 real answer: 4
classifierResult: 9 real answer: 9
classifierResult: 7 real answer: 7
classifierResult: 9 real answer: 9
classifierResult: 4 real answer: 4
classifierResult: 0 real answer: 0
classifierResult: 1 real answer: 1
classifierResult: 3 real answer: 3
classifierResult: 4 real answer: 4
classifierResult: 3 real answer: 3
classifierResult: 4 real answer: 4
classifierResult: 5 real answer: 5
classifierResult: 3 real answer: 3
classifierResult: 4 real answer: 4
classifierResult: 3 real answer: 3
classifierResult: 5 real answer: 5
classifierResult: 4 real answer: 4
classifierResult: 3 real answer: 3
classifierResult: 1 real answer: 1
classifierResult: 7 real answer: 7
classifierResult: 9 real answer: 9
classifierResult: 7 real answer: 7
classifierResult: 0 real answer: 0
classifierResult: 0 real answer: 0
classifierResult: 7 real answer: 7
classifierResult: 9 real answer: 9
classifierResult: 9 real answer: 9
classifierResult: 4 real answer: 4
classifierResult: 0 real answer: 0
classifierResult: 7 real answer: 7
classifierResult: 3 real answer: 3
classifierResult: 9 real answer: 9
classifierResult: 5 real answer: 5
classifierResult: 2 real answer: 2
classifierResult: 5 real answer: 5
classifierResult: 2 real answer: 2
classifierResult: 2 real answer: 2
classifierResult: 5 real answer: 5
classifierResult: 5 real answer: 5
classifierResult: 5 real answer: 5
classifierResult: 5 real answer: 5
classifierResult: 6 real answer: 8
classifierResult: 1 real answer: 1
classifierResult: 1 real answer: 1
classifierResult: 6 real answer: 6
classifierResult: 8 real answer: 8
classifierResult: 6 real answer: 6
classifierResult: 7 real answer: 7
classifierResult: 7 real answer: 7
classifierResult: 6 real answer: 6
classifierResult: 8 real answer: 8
classifierResult: 1 real answer: 1
classifierResult: 6 real answer: 6
classifierResult: 8 real answer: 8
classifierResult: 1 real answer: 1
classifierResult: 5 real answer: 5
classifierResult: 5 real answer: 5
classifierResult: 5 real answer: 5
classifierResult: 2 real answer: 2
classifierResult: 5 real answer: 5
classifierResult: 2 real answer: 2
classifierResult: 5 real answer: 5
classifierResult: 5 real answer: 5
classifierResult: 2 real answer: 2
classifierResult: 3 real answer: 3
classifierResult: 9 real answer: 9
classifierResult: 7 real answer: 7
classifierResult: 9 real answer: 9
classifierResult: 0 real answer: 0
classifierResult: 4 real answer: 4
classifierResult: 0 real answer: 0
classifierResult: 9 real answer: 9
classifierResult: 7 real answer: 7
classifierResult: 7 real answer: 7
classifierResult: 0 real answer: 0
classifierResult: 9 real answer: 9
classifierResult: 7 real answer: 7
classifierResult: 1 real answer: 1
classifierResult: 4 real answer: 4
classifierResult: 3 real answer: 3
classifierResult: 5 real answer: 5
classifierResult: 3 real answer: 3
classifierResult: 3 real answer: 3
classifierResult: 4 real answer: 4
classifierResult: 3 real answer: 3
classifierResult: 3 real answer: 3
classifierResult: 4 real answer: 4
classifierResult: 5 real answer: 5
classifierResult: 1 real answer: 1
classifierResult: 7 real answer: 7
classifierResult: 7 real answer: 7
classifierResult: 4 real answer: 4
classifierResult: 0 real answer: 0
classifierResult: 9 real answer: 9
classifierResult: 9 real answer: 9
classifierResult: 0 real answer: 0
classifierResult: 7 real answer: 7
classifierResult: 7 real answer: 7
classifierResult: 9 real answer: 9
classifierResult: 3 real answer: 3
classifierResult: 5 real answer: 5
classifierResult: 5 real answer: 5
classifierResult: 2 real answer: 2
classifierResult: 2 real answer: 2
classifierResult: 5 real answer: 5
classifierResult: 5 real answer: 5
classifierResult: 5 real answer: 5
classifierResult: 1 real answer: 1
classifierResult: 1 real answer: 1
classifierResult: 8 real answer: 8
classifierResult: 6 real answer: 6
classifierResult: 7 real answer: 7
classifierResult: 7 real answer: 7
classifierResult: 6 real answer: 6
classifierResult: 1 real answer: 1
classifierResult: 8 real answer: 8
classifierResult: 1 real answer: 1
classifierResult: 5 real answer: 5
classifierResult: 5 real answer: 5
classifierResult: 5 real answer: 5
classifierResult: 2 real answer: 2
classifierResult: 2 real answer: 2
classifierResult: 5 real answer: 5
classifierResult: 5 real answer: 5
classifierResult: 9 real answer: 9
classifierResult: 3 real answer: 3
classifierResult: 7 real answer: 7
classifierResult: 7 real answer: 7
classifierResult: 9 real answer: 9
classifierResult: 0 real answer: 0
classifierResult: 0 real answer: 0
classifierResult: 4 real answer: 4
classifierResult: 9 real answer: 9
classifierResult: 7 real answer: 7
classifierResult: 7 real answer: 7
classifierResult: 1 real answer: 1
classifierResult: 5 real answer: 5
classifierResult: 4 real answer: 4
classifierResult: 3 real answer: 3
classifierResult: 3 real answer: 3
正确率: 0.9883720930232558

代码示意:

  • 这段代码的主要功能是从os模块中导入函数listdir,它可以列 出给定目录的文件名。
  • 将trainingDigits目录中的文件内容存储在列表中 ,然后可以得到目录中有多少文件,并将其存储在变量m中。
  • 接着,代码创建一个m行1024列的训练矩阵,该矩阵的每行数 据存储一个图像。
  • 我们可以从文件名中解析出分类数字 。该目录下的文件按照规则命名,如文件 9_45.txt的分类是9,它是数字9的第45个实例。
  • 然后我们可以将类代码存储在hwLabels向量中,使 用前面讨论的img2vector函数载入图像。
  • 在下一步中,我们对testDigits目录中的文件执行相似的操作,不同之处是我们并不将这个目录下的文件载入矩阵中,而是使用classify0()函数测试该 目录下的每个文件



Artificial Intelligence   Machine Learning   Algorithm      Machine Learning Algorithm k-近邻

本博客所有文章除特别声明外,均采用 CC BY-SA 3.0协议 。转载请注明出处!