The KNN algorithm realizes the handwriting distinction

Source: Internet
Author: User

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

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.