This paper mainly uses K-nearest neighbor classifier to implement handwriting recognition system, training data set about 2000 samples, each number has about 200 samples, each sample is saved in a TXT file, the handwriting image itself is a 32x32 two value image, as shown in:
First, we need to format the image as a vector to convert a 32x32 binary image matrix through the Img2vector () function to the 1x1024 vector:
def img2vector (filename): returnvect = Zeros ((1,1024)) fr = open (filename) for i in range (+): linestr = Fr.readline () for J in Range (+): returnvect[0,32*i+j] = Int (linestr[j]) return Returnvect
The test code for the handwritten digital recognition system:
Def handwritingclasstest (): Hwlabels = [] trainingfilelist = Listdir (' trainingdigits ') #load the training Set m = Len (trainingfilelist) Trainingmat = Zeros ((m,1024)) for I in Range (m): Filenamestr = Trainingfilel Ist[i] Filestr = Filenamestr.split ('. ') [0] #take off. txt classnumstr = int (Filestr.split ('_') [0]) Hwlabels.append (CLASSNUMSTR) Training Mat[i,:] = Img2vector (' trainingdigits/%s '% filenamestr) testfilelist = Listdir (' testdigits ') #iterate through T He test set errorcount = 0.0 mtest = Len (testfilelist) for I in Range (mtest): Filenamestr = Testfilelist[i ] Filestr = Filenamestr.split ('. ') [0] #take off. txt classnumstr = int (Filestr.split ('_') [0]) Vectorundertest = Img2vector (' testdigits/%s ' % filenamestr) Classifierresult = Classify0 (Vectorundertest, Trainingmat, Hwlabels, 3) print "The Classifie R came back with:%d, the real answer is:%d "% (classifierResult, Classnumstr) if (classifierresult! = classnumstr): Errorcount + = 1.0 print "\nthe total number of errors is:%d '% errorcount print ' \nthe total error rate is:%f "% (Errorcount/float (mtest))
In the Python command prompt, enter knn.handwritingclasstest () to test the output of the function:
Note: The relevant code of this article is derived from Peter Harringtor's "machine learning Combat"
Machine learning Practical notes--handwritten recognition system based on KNN algorithm