python使用KNN算法手寫體識別
更新時間:2018年02月01日 09:47:32 作者:一笑丶奈何
這篇文章主要為大家詳細介紹了python使用KNN算法手寫體識別,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了用KNN算法手寫體識別的具體代碼,供大家參考,具體內(nèi)容如下
#!/usr/bin/python #coding:utf-8 import numpy as np import operator import matplotlib import matplotlib.pyplot as plt import os ''''' KNN算法 1. 計算已知類別數(shù)據(jù)集中的每個點依次執(zhí)行與當前點的距離。 2. 按照距離遞增排序。 3. 選取與當前點距離最小的k個點 4. 確定前k個點所在類別的出現(xiàn)頻率 5. 返回前k個點出現(xiàn)頻率最高的類別作為當前點的預(yù)測分類 ''' ''''' inX為要分類的向量 dataSet為訓(xùn)練樣本 labels為標簽向量 k為最近鄰的個數(shù) ''' def classify0(inX , dataSet , labels , k): dataSetSize = dataSet.shape[0]#dataSetSize為訓(xùn)練樣本的個數(shù) diffMat = np.tile(inX , (dataSetSize , 1)) - dataSet#將inX擴展為dataSetSize行,1列 sqDiffMat = diffMat**2 sqDistances = sqDiffMat.sum(axis=1) distances = sqDistances**0.5 sortedDistIndicies = distances.argsort()#返回的是元素從小到大排序后,該元素原來的索引值的序列 classCount = {} for i in range(k): voteIlabel = labels[sortedDistIndicies[i]]#voteIlabel為類別 classCount[voteIlabel] = classCount.get(voteIlabel,0)+1#如果之前這個voteIlabel是有的,那么就返回字典里這個voteIlabel里的值,如果沒有就返回0 sortedClassCount = sorted(classCount.iteritems(),key=operator.itemgetter(1),reverse=True)#key=operator.itemgetter(1)的意思是按照字典里的第一個排序,{A:1,B:2},要按照第1個(AB是第0個),即‘1'‘2'排序。reverse=True是降序排序 print sortedClassCount return sortedClassCount[0][0] ''''' 將圖像轉(zhuǎn)換為1*1024的向量 ''' def img2vector(filename): returnVect = np.zeros((1,1024)) fr = open(filename) for i in range(32): line = fr.readline() for j in range(32): returnVect[0,i*32+j] = int(line[j] ) return returnVect ''''' 手寫體識別系統(tǒng)測試 ''' def handwritingClassTest(trainFilePath,testFilePath): hwLabels = [] trainingFileList = os.listdir(trainFilePath) m=len(trainingFileList) trainSet = np.zeros((m,1024)) for i in range(m): filename = trainingFileList[i] classNum = filename.split('.')[0] classNum = int(classNum.split('_')[0]) hwLabels.append(classNum) trainSet[i] = img2vector( os.path.join(trainFilePath,filename) ) testFileList = os.listdir(testFilePath) errorCount = 0 mTest = len(testFileList) for i in range(mTest): filename = trainingFileList[i] classNum = filename.split('.')[0] classNum = int(classNum.split('_')[0]) vectorUnderTest = img2vector(os.path.join(trainFilePath, filename)) classifyNum = classify0(vectorUnderTest,trainSet,hwLabels,10) print "the classifier came back with : %d , the real answer is : %d"% (classifyNum , classNum) if(classifyNum != classNum) : errorCount+=1 print ("\nthe total number of error is : %d"%errorCount) print ("\nthe error rate is : %f"%(float(errorCount)/mTest)) handwritingClassTest()
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python超有趣實例通過冒泡排序來實現(xiàn)LOL厄斐琉斯控槍
冒泡排序是一種簡單的排序算法,它也是一種穩(wěn)定排序算法。其實現(xiàn)原理是重復(fù)掃描待排序序列,并比較每一對相鄰的元素,當該對元素順序不正確時進行交換。一直重復(fù)這個過程,直到?jīng)]有任何兩個相鄰元素可以交換,就表明完成了排序2022-05-05Python中搜索和替換文件中的文本的實現(xiàn)(四種)
本文主要介紹了Python中搜索和替換文件中的文本的實現(xiàn),文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-10-10Python如何操作office實現(xiàn)自動化及win32com.client的運用
這篇文章主要介紹了Python如何操作office實現(xiàn)自動化及win32com.client的運用,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04matplotlib 多個圖像共用一個colorbar的實現(xiàn)示例
這篇文章主要介紹了matplotlib 多個圖像共用一個colorbar的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09