Python3學習筆記【機器學習】【k-近鄰算法】【手寫識別系統】

import kNN # dating program file in previous blog
from numpy import *
import operator
import os

def img2vector(filename):
    # Make the 32x32 txt file to a 1024 list
    returnVector = zeros((1,1024))
    fr = open(filename)
    for i in range(32):
        lineStr = fr.readline()
        for j in range(32):
            returnVector[0,32*i+j] = int(lineStr[j])
    return returnVector

testVector = img2vector('testDigits/0_13.txt')
print(testVector[0,0:31])
print(testVector[0,32:63])

def handwritingClassTest():
    hwLabels = []
    trainingFileList = os.listdir("trainingDigits")
    # os.listdir () : is used to return a list of files or 
    # folder names contained in the specified folder.
    LengthTFList = len(trainingFileList)
    traningMat = zeros((LengthTFList,1024))
    for i in range(LengthTFList):
        fileNameStr = trainingFileList[i]
        fileStr = fileNameStr.split('.')[0]
        # use '.' as a splitter to cut the fileName to some parts
        # give the first part to fileStr
        classNumStr = int(fileStr.split('_')[0])
        # the training file name just like '0_3.txt'
        # we need to get the object number '0' and the training data file
        # order '3'
        hwLabels.append(classNumStr)
        traningMat[i,:] = img2vector('trainingDigits/%s'%fileNameStr)
    testFileList = os.listdir('testDigits')
    errorCount = 0.0
    lengthTestFL = len(testFileList)
    for i in range(lengthTestFL):
        fileNameStr = testFileList[i]
        fileStr = fileNameStr.split('.')[0]
        classNumStr = int(fileStr.split('_')[0])
        vectorUnderTest = img2vector('testDigits/%s'%fileNameStr)
        classifiyResult = kNN.classfiy0(vectorUnderTest,traningMat,hwLabels,3)
        print('The classifier return : %d , the real answser is : %d' 
              % (classifiyResult,classNumStr))
        if (classifiyResult != classNumStr) :
            errorCount += 1.0
    print('The total error num is : %d' % errorCount)
    print('Error radio is : %f'%(errorCount/lengthTestFL))
    
handwritingClassTest()
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章