使用KNN演算法實現手寫數字識別
阿新 • • 發佈:2021-01-09
1.文字檔案資料
等等
2.將其3232的二進位制影象轉換為11024的向量
3.測試演算法
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
'''=================================================
@Project -> File :KNN -> kNN
@IDE :PyCharm
@Author :zgq
@Date :2021/1/7 14:15
@Desc :
=================================================='''
from numpy import *
import operator #運算子模組
import matplotlib
import matplotlib.pyplot as plt
from os import listdir
def classify0(inX,dataSet,labels,k):
#inx用於分類的輸入向量
#訓練樣本集dataset
#lables標籤
#k最近鄰數目
#距離計算
dataSetSize=dataSet.shape[0] #dataset有幾行
diffMat=tile(inX,(dataSetSize,1))-dataSet #輸入向量重複了已有資料集的行數,一起減掉,出來一個新的矩陣,每個數字都記錄當前新樣本該維度與每個樣本差值
sqDiffMat=diffMat**2
sqDistances=sqDiffMat.sum(axis=1) #所有橫軸元素加和
distances=sqDistances**0.5 #到此處時 distance為一個一位列陣列,記錄每條樣本與新樣本的距離
sortedDistIndicies= distances.argsort() #對distance進行升序排序
classCount={} #DICT型別
for i in range(k): #尋找距離最小的K個點
voteIlabel = labels[sortedDistIndicies[ i]] #返回距離排序中前K條資料的標籤
classCount[voteIlabel]=classCount.get(voteIlabel,0)+1
#classCount.get(voteIlabel,0) 字典獲取vouteIlabel值,沒有的話返回0
#此處for迴圈將距離最近的K個數據標籤進行統計:每次for迴圈第一步,將第i個標籤記錄到voteIlable中,第二部將該標籤出現後再dict中次數加一
sortedClassCount=sorted(classCount.items(),key=operator.itemgetter(1),reverse=True)
return sortedClassCount[0][0]
#將img資料轉換為向量
def img2vector(filename):
returnVect=zeros((1,1024))
fr=open(filename)
for i in range(32):
lineStr=fr.readline()
for j in range(32):
returnVect[0,32*i+j]=int(lineStr[j])
return returnVect
#手寫數字識別系統的測試程式碼
def handwritingClassTest():
hwLabels=[]
trainingFileList=listdir('trainingDigits') #listdir可以列出給定目錄的檔名
m=len(trainingFileList)
trainingMat=zeros((m,1024))
for i in range(m):
fileNameStr=trainingFileList[i] #獲取當前第i個檔名
fileStr=fileNameStr.split('.')[0] #先用點來切分,切分為0_0和txt [0_0,txt]取第0項
classNumStr=int(fileStr.split('_')[0])
hwLabels.append(classNumStr) #將所有的標籤按照順序新增到了hwLables中
trainingMat[i,:]=img2vector('trainingDigits/%s' % fileNameStr) #順便將每一個檔案都轉為向量存入trainingMat中
testFileList=listdir('testDigits') #將測試檔案的名字作為列表給予testFilelist
errorCount=0.0
mTest=len(testFileList) #取test的集的總數
for i in range(mTest):
fileNameStr=testFileList[i]
fileStr=fileNameStr.split('.')[0]
classNumStr=int(fileStr.split('_')[0])
vectorUnderTest=img2vector('testDigits/%s' % fileNameStr) #拿出一條測試集資料構成測試向量
classifierResult=classify0(vectorUnderTest,trainingMat,hwLabels,3) #此處訓練集樣本和標籤行數是對齊的
print("the classifier came back with : %d,the real answer is : %d" %(classifierResult,classNumStr))
if (classifierResult!=classNumStr):
errorCount=errorCount+1.0
print("\n the total number of errors is :%d" % errorCount)
print("\n the total error rate is: %f" % (errorCount/float(mTest)))
handwritingClassTest()
測試結果:
the classifier came back with : 0,the real answer is : 0
the classifier came back with : 0,the real answer is : 0
the classifier came back with : 0,the real answer is : 0
the classifier came back with : 0,the real answer is : 0
the classifier came back with : 0,the real answer is : 0
the classifier came back with : 0,the real answer is : 0
……
the classifier came back with : 1,the real answer is : 1
the classifier came back with : 7,the real answer is : 1
the classifier came back with : 1,the real answer is : 1
……
the classifier came back with : 8,the real answer is : 8
the classifier came back with : 6,the real answer is : 8
the classifier came back with : 8,the real answer is : 8
the classifier came back with : 8,the real answer is : 8
the classifier came back with : 8,the real answer is : 8
the classifier came back with : 8,the real answer is : 8
the classifier came back with : 8,the real answer is : 8
the classifier came back with : 8,the real answer is : 8
the classifier came back with : 8,the real answer is : 8
the classifier came back with : 8,the real answer is : 8
the classifier came back with : 8,the real answer is : 8
the classifier came back with : 8,the real answer is : 8
the classifier came back with : 8,the real answer is : 8
the classifier came back with : 8,the real answer is : 8
the classifier came back with : 3,the real answer is : 8
the classifier came back with : 8,the real answer is : 8
……
the total number of errors is :10
the total error rate is: 0.010571