• 机器学习实战笔记-K近邻算法3(手写识别系统)


    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:
    这里写图片描述
    这里写图片描述
    可以发现错误率逐渐增高

  • 相关阅读:
    关于使用HttpModule模块处理登录验证示例
    Request.Url.Query 和 Request.QueryString的区别
    使用Trace.axd 调试ASP.NET
    asp.net网站 页面缓存
    WCF系列之.net(3.0/3.5)Rest使用示例
    解决TFS无法上传Nuget下载的DLL问题
    WCF系列之.net(4.0)使用示例
    WCF系列之.net(3.0/3.5)使用示例
    批量删除MSSQL被挂JS木马语句
    查询你的数据库有没有被木马注入的可能!
  • 原文地址:https://www.cnblogs.com/kevincong/p/7801901.html
Copyright © 2020-2023  润新知