-
大小: 330KB文件類型: .zip金幣: 2下載: 0 次發(fā)布日期: 2021-05-14
- 語(yǔ)言: 其他
- 標(biāo)簽:
資源簡(jiǎn)介
手寫體數(shù)字識(shí)別數(shù)據(jù)-digits.zip——手寫體數(shù)據(jù)和KNN實(shí)現(xiàn)

代碼片段和文件信息
‘‘‘
kNN:?k?Nearest?Neighbors
Input:??????inX:?vector?to?compare?to?existing?dataset?(1xN)
????????????dataSet:?size?m?data?set?of?known?vectors?(NxM)
????????????labels:?data?set?labels?(1xM?vector)
????????????k:?number?of?neighbors?to?use?for?comparison?(should?be?an?odd?number)
Output:?????the??digit?label
‘‘‘
from?numpy?import?*
import?operator
from?os?import?listdir
#?KNN分類方法
def?classify0(inX?dataSet?labels?k):
????#?距離計(jì)算——使用歐氏距離,計(jì)算兩個(gè)向量點(diǎn)的距離
????dataSetSize?=?dataSet.shape[0]
????diffMat?=?tile(inX?(dataSetSize1))?-?dataSet
????sqDiffMat?=?diffMat**2
????sqDistances?=?sqDiffMat.sum(axis=1)
????distances?=?sqDistances**0.5
????#?把距離從小到大排序
????sortedDistIndicies?=?distances.argsort()
????classCount={}
????for?i?in?range(k):
????????#?統(tǒng)計(jì)距離最?近的?k?個(gè)?數(shù)據(jù)的?標(biāo)簽分類出現(xiàn)次數(shù)
????????voteIlabel?=?labels[sortedDistIndicies[i]]
????????classCount[voteIlabel]?=?classCount.get(voteIlabel0)?+?1
????#?對(duì)?標(biāo)簽分類出現(xiàn)次數(shù)?進(jìn)行??從大到小?排序
????sortedClassCount?=?sorted(classCount.items()?key=operator.itemgetter(1)?reverse=True)
????return?sortedClassCount[0][0]
#?這里把?32?*?32?的?二進(jìn)制圖像矩陣?轉(zhuǎn)換為??1?*?1024?的向量
def?img2vector(filename):
????returnVect?=?zeros((11024))
????fr?=?open(filename)
????for?i?in?range(32):
????????lineStr?=?fr.readline()
????????for?j?in?range(32):
????????????returnVect[032*i+j]?=?int(lineStr[j])
????return?returnVect
def?handwritingClassTest():
????hwLabels?=?[]
????trainingFileList?=?listdir(‘digits/trainingDigits‘)???????????#load?the?training?set
????m?=?len(trainingFileList)
????trainingMat?=?zeros((m1024))
????for?i?in?range(m):
????????fileNameStr?=?trainingFileList[i]
????????#?獲取文件名
????????fileStr?=?fileNameStr.split(‘.‘)[0]?????#take?off?.txt
????????#?獲取標(biāo)簽數(shù)值
????????classNumStr?=?int(fileStr.split(‘_‘)[0])
????????#?待識(shí)別圖片數(shù)字的?標(biāo)簽數(shù)值?保存在??hwLabels?中
????????hwLabels.append(classNumStr)
????????trainingMat[i:]?=?img2vector(‘digits/trainingDigits/%s‘?%?fileNameStr)
????testFileList?=?listdir(‘digits/testDigits‘)????????#iterate?through?the?test?set
????errorCount?=?0.0
????mTest?=?len(testFileList)
????for?i?in?range(mTest):
????????fileNameStr?=?testFileList[i]
????????fileStr?=?fileNameStr.split(‘.‘)[0]?????#take?off?.txt
????????classNumStr?=?int(fileStr.split(‘_‘)[0])
????????vectorUnderTest?=?img2vector(‘digits/testDigits/%s‘?%?fileNameStr)
????????#?調(diào)用KNN分類方法
????????classifierResult?=?classify0(vectorUnderTest?trainingMat?hwLabels?3)
????????print(“the?classifier?came?back?with:?%d?the?real?answer?is:?%d“?%?(classifierResult?classNumStr))
????????if(classifierResult?!=?classNumStr):
????????????errorCount?+=?1.0
????print(“\n?the?total?number?of?errors?is:?%d“?%?errorCount)
????print(“\n?the?total?error?rate?is:?%f“?%?(errorCount/float(mTest)))
????print(“測(cè)試的手寫體數(shù)字個(gè)數(shù)為?%d?“?%?mTest)
#?調(diào)用方法
handwritingClassTest()
?屬性????????????大小?????日期????時(shí)間???名稱
-----------?---------??----------?-----??----
?????文件??????739988??2011-05-04?15:43??數(shù)據(jù)和KNN實(shí)現(xiàn)代碼\digits.zip
?????文件????????3138??2019-05-22?18:11??數(shù)據(jù)和KNN實(shí)現(xiàn)代碼\handIdentify.py
?????目錄???????????0??2019-05-22?18:19??數(shù)據(jù)和KNN實(shí)現(xiàn)代碼\
評(píng)論
共有 條評(píng)論