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