Machine learning Combat NOTE-K neighbor algorithm 3 (handwriting recognition system)

Source: Internet
Author: User

1 Preparing data: Converting an image to a test vector
There are two kinds of data sets, the training data set and the test data set, respectively, there are 2000, 900.
We will convert a 32*32 binary image matrix to a vector of 1 x 1024 so that the classifier used in the first two sections can process the digital image information.
Code:








return returnVect


Effect:

Test algorithm
Code:

Def handwritingtest ():
Hwlabels = []
Trainingfilelist = Os.listdir (' trainingdigits ')
Trainingfilelength = Len (trainingfilelist)
Trainingmat = Zeros ((trainingfilelength,1024))
For I in Range (trainingfilelength):
FILENAMESTR = Trainingfilelist[i]
ClassName = Filenamestr.split ('_') [0]
Hwlabels.append (int (className))
Filevector = Img2vector (' trainingdigits/' + filenamestr)
Trainingmat[i,:] = Filevector
Testfilelist = Os.listdir (' testdigits ')
Testfilelength = Len (testfilelist)
Errorcount = 0.0
For I in Range (testfilelength):
FILENAMESTR = Testfilelist[i]
className = Int (Filenamestr.split ('_') [0])
Filevector = Img2vector (' testdigits/' + filenamestr)
TestResult = Classify0 (filevector,trainingmat,hwlabels,3)
Print ("The classifier came back with:%d, the real answer is:%d"% (testresult,classname))
if (testresult! = className):
errorcount+=1.0
Errorrate = Errorcount/float (testfilelength)
Print ("The Errorrate is:%f"% errorrate)

Results:

Change K to 4, 5 respectively:


You can see that the error rate is increasing

Machine learning Combat NOTE-K neighbor algorithm 3 (handwriting recognition system)

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.