python使用KNN算法手寫(xiě)體識(shí)別
本文實(shí)例為大家分享了用KNN算法手寫(xiě)體識(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()#返回的是元素從小到大排序后,該元素原來(lái)的索引值的序列
classCount = {}
for i in range(k):
voteIlabel = labels[sortedDistIndicies[i]]#voteIlabel為類別
classCount[voteIlabel] = classCount.get(voteIlabel,0)+1#如果之前這個(gè)voteIlabel是有的,那么就返回字典里這個(gè)voteIlabel里的值,如果沒(méi)有就返回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
'''''
手寫(xiě)體識(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判斷字符串以什么結(jié)尾的實(shí)例方法
在本篇文章里小編給大家整理了關(guān)于python判斷字符串以什么結(jié)尾的實(shí)例方法 ,需要的朋友們可以學(xué)習(xí)參考下。2020-09-09
Python超有趣實(shí)例通過(guò)冒泡排序來(lái)實(shí)現(xiàn)LOL厄斐琉斯控槍
冒泡排序是一種簡(jiǎn)單的排序算法,它也是一種穩(wěn)定排序算法。其實(shí)現(xiàn)原理是重復(fù)掃描待排序序列,并比較每一對(duì)相鄰的元素,當(dāng)該對(duì)元素順序不正確時(shí)進(jìn)行交換。一直重復(fù)這個(gè)過(guò)程,直到?jīng)]有任何兩個(gè)相鄰元素可以交換,就表明完成了排序2022-05-05
基于Python輕松制作一個(gè)股票K線圖網(wǎng)站
在當(dāng)今這個(gè)人手一個(gè)?Web?服務(wù)的年代,GUI?程序還是沒(méi)有?Web?服務(wù)來(lái)的香啊。所以本文將用Python制作一個(gè)簡(jiǎn)單的股票K線圖網(wǎng)站,感興趣的可以了解一下2022-09-09
Python中搜索和替換文件中的文本的實(shí)現(xiàn)(四種)
本文主要介紹了Python中搜索和替換文件中的文本的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10
Python如何操作office實(shí)現(xiàn)自動(dòng)化及win32com.client的運(yùn)用
這篇文章主要介紹了Python如何操作office實(shí)現(xiàn)自動(dòng)化及win32com.client的運(yùn)用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04
matplotlib 多個(gè)圖像共用一個(gè)colorbar的實(shí)現(xiàn)示例
這篇文章主要介紹了matplotlib 多個(gè)圖像共用一個(gè)colorbar的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09

