fromNumPyImport*Importoperator fromOsImportListdirdefclassify0 (InX, DataSet, labels, k): Datasetsize=Dataset.shape[0] Diffmat= Tile (InX, (datasetsize,1))-DataSet Sqdiffmat= Diffmat * * 2sqdistances= Sqdiffmat.sum (Axis=1) Distances= sqdistances * * 0.5sorteddistindicies=distances.argsort () ClassCount= {} forIinchRange (k): Voteilabel=Labels[sorteddistindicies[i]] Classcount[voteilabel]= Classcount.get (voteilabel,0) + 1Sortedclasscount= Sorted (Classcount.items (), Key=operator.itemgetter (1), reverse=True)returnSortedclasscount[0][0]defimg2vector (filename): Returnvect= Zeros ((1,1024)) #print (returnvect)FR =open (filename) forIinchRange (32): Linestr=Fr.readline () forJinchRange (32): Returnvect[0,32*I+J] =Int (linestr[j])returnReturnvectdefhandwritingclasstest (): Hwlabels=[] trainingfilelist= Listdir ('trainingdigits') M=Len (trainingfilelist) Trainingmat= Zeros ((m,1024)) forIinchRange (m): Filenamestr=Trainingfilelist[i] Filestr= Filenamestr.split ('.') [0] Classnumstr= Int (Filestr.split ('_') [0]) hwlabels.append (CLASSNUMSTR) trainingmat[i,:]= Img2vector ('trainingdigits/%s'%filenamestr) Testfilelist= Listdir ('testdigits') Errorcount= 0.0mtest=Len (testfilelist) 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))if(Classifierresult! =classnumstr): Errorcount+ = 1Print("The total number of errors is:%d"%errorcount)Print("The total error rate is:%f"% (errorcount/float (mtest))) Handwritingclasstest ()
Test set + training set data address: https://i.cnblogs.com/Files.aspx
Knn.rar
KNN algorithm for handwritten numerals