The application of "machine learning" K-nearest neighbor algorithm in handwritten numeral recognition

Source: Internet
Author: User

In this article, we briefly introduce the principle of the K-nearest neighbor algorithm and a simple example, today we will introduce a simple application, because the principle of use is roughly the same, there is no too much explanation.

To be descriptive, convert the handwritten numbers image to a TXT file, as shown in (three graphs are 5, 6, 8, respectively):

To use the K-nearest neighbor algorithm, you need to have enough sample data and test data, I put in two folders (Trainingdigits and testdigits), can be downloaded here (Http://pan.baidu.com/s/1i3osO7N) use

Here, each number has a 32x32 0 or 1, can be considered a dimension of 1024 points, that is, the use of the KNN algorithm, which is only attached to the handwritten number of the test function code, code and the total need for other functions in the previous article, in addition, it is important to note that because to get a list of file classes, Need to add the from OS import listdir to the header in the file

1 defhandwritingclasstest ():2Hwlabels = []3Trainingfilelist = Listdir ('trainingdigits')4m =Len (trainingfilelist)5Trainingmat = Zeros ((M, 1024))6      forIinchRange (m):7Filenamestr =Trainingfilelist[i]8Filestr = Filenamestr.split ('.') [0]9classnumstr = Int (Filestr.split ('_') [0])Ten hwlabels.append (CLASSNUMSTR) OneTrainingmat[i,:] = Img2vector ('trainingdigits/%s'%filenamestr) ATestfilelist = Listdir ('testdigits') -Errorcount = 0.0 -Mtest =Len (testfilelist) the      forIinchRange (mtest): -Filenamestr =Testfilelist[i] -Filestr = Filenamestr.split ('.') [0] -classnumstr = Int (Filestr.split ('_') [0]) +Vectorundertest = Img2vector ('testdigits/%s'%filenamestr) -Classifierresult = Classify0 (Vectorundertest, Trainingmat, Hwlabels, 3) +         Print "The classifier came back with:%d, the real answer is:%d"%(Classifierresult, CLASSNUMSTR) A         if(Classifierresult! = classnumstr): Errorcount + = 1.0 at     Print "\nthe total number of errors is:%d"%Errorcount -     Print "\nthe total error rate is:%f"% (Errorcount/float (mtest))

Test results such as:

The application of "machine learning" K-nearest neighbor algorithm in handwritten numeral recognition

Related Article

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.