The KNN algorithm can be installed in Python using the pip Install command, and I have reviewed the installed KNN algorithm before implementation, including the processing of the handwritten data set. I just realized the basic recognition method, limited ability, no data processing method.
Computer is too slag, do not own training data set.
The selected dataset is already processed.
If you want to handle the data set manually, it is recommended to mnist. I want to write algorithms to deal with pictures.
#!/usr/bin/env python#-*-coding:utf-8-*-#AUTHOR:GJTImportNumPy as NP fromOsImportListdir fromSklearn.neighborsImportKneighborsclassifier as KNNdefimg2vector (filename): Returnvet= Np.zeros ((1,1024)) Fr=open (filename) forIinchRange (32): Liststr=Fr.readline () forJinchRange (32): Returnvet[0,32*I+J] =Int (liststr[j])returnReturnvetdefhandwritingclasstest (): Hwlabels=[] trainingfilelist= Listdir ('trainingdigits') M=Len (trainingfilelist) Trainingmat= Np.zeros ((m,1024)) forIinchRange (m): Filenamestr=Trainingfilelist[i] Classnumber= Int (Filenamestr.split ('_') [0]) hwlabels.append (Classnumber) trainingmat[i,:]= Img2vector ('trainingdigits/%s'%(FILENAMESTR)) Neigh= KNN (n_neighbors=3,algorithm='Auto') Neigh.fit (trainingmat,hwlabels) testfilelist= Listdir ("testdigits") Errorcount= 0.0mtest=Len (testfilelist) forIinchRange (mtest): Filenamestr=Testfilelist[i] Classnumber= Int (Filenamestr.split ("_") [0]) Vectorundertest= Img2vector ('testdigits/%s'%(FILENAMESTR)) Classifierresult=neigh.predict (vectorundertest)Print("category return result%d\t true result%d"%(Classifierresult, classnumber))if(Classnumber! =classifierresult): Errorcount+ = 1.0Print("total wrong%d \ t error rate is%f%%"% (Errorcount, errorcount/mtest * 100))if __name__=="__main__": Handwritingclasstest ()
The effect is still considerable ...
Refer to the "machine learning Combat"
We do not produce code, just code for porters.
。。。。。。。。。。。。。。。
The KNN algorithm realizes the handwriting distinction