python使用KNN算法手寫體識(shí)別
本文實(shí)例為大家分享了用KNN算法手寫體識(shí)別的具體代碼,供大家參考,具體內(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. 計(jì)算已知類別數(shù)據(jù)集中的每個(gè)點(diǎn)依次執(zhí)行與當(dāng)前點(diǎn)的距離。
2. 按照距離遞增排序。
3. 選取與當(dāng)前點(diǎn)距離最小的k個(gè)點(diǎn)
4. 確定前k個(gè)點(diǎn)所在類別的出現(xiàn)頻率
5. 返回前k個(gè)點(diǎn)出現(xiàn)頻率最高的類別作為當(dāng)前點(diǎn)的預(yù)測(cè)分類
'''
'''''
inX為要分類的向量
dataSet為訓(xùn)練樣本
labels為標(biāo)簽向量
k為最近鄰的個(gè)數(shù)
'''
def classify0(inX , dataSet , labels , k):
dataSetSize = dataSet.shape[0]#dataSetSize為訓(xùn)練樣本的個(gè)數(shù)
diffMat = np.tile(inX , (dataSetSize , 1)) - dataSet#將inX擴(kuò)展為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#如果之前這個(gè)voteIlabel是有的,那么就返回字典里這個(gè)voteIlabel里的值,如果沒有就返回0
sortedClassCount = sorted(classCount.iteritems(),key=operator.itemgetter(1),reverse=True)#key=operator.itemgetter(1)的意思是按照字典里的第一個(gè)排序,{A:1,B:2},要按照第1個(gè)(AB是第0個(gè)),即‘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
'''''
手寫體識(shí)別系統(tǒng)測(cè)試
'''
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()
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python爬蟲之對(duì)CSDN榜單進(jìn)行分析
這篇文章主要介紹了Python爬蟲之對(duì)CSDN榜單進(jìn)行分析,文章有詳細(xì)代碼,簡(jiǎn)單易懂,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2023-01-01
Python自動(dòng)化短視頻生成腳本實(shí)現(xiàn)熱門視頻流水線生產(chǎn)
有粉絲和說,最近在網(wǎng)上看到一些視頻營(yíng)銷號(hào)一天能發(fā)布幾百條短視頻, 感覺是批量生成的,能不能用Python做個(gè)自動(dòng)化短視頻生成腳本呢?今天就帶大家一起實(shí)現(xiàn)熱門視頻批量流水線生產(chǎn)2021-09-09
Python如何通過百度翻譯API實(shí)現(xiàn)翻譯功能
這篇文章主要介紹了Python如何通過百度翻譯API實(shí)現(xiàn)翻譯功能,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04
在Linux下使用Python的matplotlib繪制數(shù)據(jù)圖的教程
這篇文章主要介紹了在Linux下使用Python的matplotlib繪制數(shù)據(jù)圖的教程,matplotlib基于Numpy進(jìn)行科學(xué)計(jì)算上的延伸,需要的朋友可以參考下2015-06-06
python通過urllib2爬網(wǎng)頁上種子下載示例
這篇文章主要介紹了通過urllib2、re模塊抓種子下載的示例,需要的朋友可以參考下2014-02-02
python二進(jìn)制串轉(zhuǎn)字符串的方法詳解
這篇文章主要介紹了python二進(jìn)制串轉(zhuǎn)字符串的方法詳解,使用json,可以自動(dòng)檢測(cè)編碼,但需要注意的是,它返回的是python對(duì)象,不一定是字符串,具體是什么對(duì)象要視原始內(nèi)容而定,需要的朋友可以參考下2023-11-11
python調(diào)用百度API實(shí)現(xiàn)人臉識(shí)別
這篇文章主要介紹了python調(diào)用百度API實(shí)現(xiàn)人臉識(shí)別,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11

