Machine learning Practical notes--handwritten recognition system based on KNN algorithm

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.