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)