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í)行與當(dāng)前點的距離。
2. 按照距離遞增排序。
3. 選取與當(dāng)前點距離最小的k個點
4. 確定前k個點所在類別的出現(xiàn)頻率
5. 返回前k個點出現(xiàn)頻率最高的類別作為當(dāng)前點的預(yù)測分類
'''
'''''
inX為要分類的向量
dataSet為訓(xùn)練樣本
labels為標(biāo)簽向量
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)熱門視頻流水線生產(chǎn)
有粉絲和說,最近在網(wǎng)上看到一些視頻營銷號一天能發(fā)布幾百條短視頻, 感覺是批量生成的,能不能用Python做個自動化短視頻生成腳本呢?今天就帶大家一起實現(xiàn)熱門視頻批量流水線生產(chǎn)2021-09-09
在Linux下使用Python的matplotlib繪制數(shù)據(jù)圖的教程
這篇文章主要介紹了在Linux下使用Python的matplotlib繪制數(shù)據(jù)圖的教程,matplotlib基于Numpy進行科學(xué)計算上的延伸,需要的朋友可以參考下2015-06-06
python通過urllib2爬網(wǎng)頁上種子下載示例
這篇文章主要介紹了通過urllib2、re模塊抓種子下載的示例,需要的朋友可以參考下2014-02-02
python調(diào)用百度API實現(xiàn)人臉識別
這篇文章主要介紹了python調(diào)用百度API實現(xiàn)人臉識別,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11

