当前位置:   article > 正文

k近邻算法-手写数字识别系统

k近邻算法-手写数字识别系统

构造一个能识别数字 0 到 9 的基于 KNN 分类器的手写数字识别系统。

需要识别的数字是存储在文本文件中的具有相同的色彩和大小: 宽高是 32 像素 * 32 像素的黑白图像。

在控制台输入 mspaint,打开画图工具把像素调至32 x 32

选择单色位图

  1. def getImageArr(path):
  2. imageArr = cv2.imread(path)
  3. imageArr = imageArr[:, :, 0] / 255
  4. imageArr = imageArr.astype(np.int32)
  5. where_0 = np.where(imageArr == 0)
  6. where_1 = np.where(imageArr == 1)
  7. imageArr[where_0] = 1
  8. imageArr[where_1] = 0
  9. return imageArr

解析得到01010的格式

解析题上给的数据集

  1. def img2vector(filename):
  2. returnVect = np.zeros((1,1024))
  3. fr = open(filename)
  4. for i in range(32):
  5. lineStr = fr.readline()
  6. for j in range(32):
  7. returnVect[0,32*i+j] = int(lineStr[j])
  8. return returnVect

写上k近邻算法

  1. def classifyFun(inX, dataList, dataLabels, k):
  2. dataListLen = dataList.shape[0]
  3. difMat = np.tile(inX, (dataListLen, 1)) - dataList
  4. sqDifMat = difMat ** 2
  5. sqDifMatSum = sqDifMat.sum(axis = 1)
  6. DifDist = sqDifMatSum ** 0.5
  7. # 排序
  8. DifDistSortIdx = DifDist.argsort()
  9. classCount = {}
  10. for i in range(k):
  11. getLabel = dataLabels[DifDistSortIdx[i]]
  12. classCount[getLabel] = classCount.get(getLabel, 0) + 1
  13. classifyAns = sorted(classCount.items(), key = operator.itemgetter(1),reverse = True)
  14. print(classifyAns)
  15. return classifyAns[0][0]
  1. def ClassTrain():
  2. digLabels = []
  3. trainingFileList = os.listdir("trainingDigits")
  4. digCount = len(trainingFileList)
  5. trainingMat = np.zeros((digCount, 32 * 32))
  6. for i in range(digCount):
  7. fileNameStr = trainingFileList[i]
  8. fileStr = fileNameStr.split('.')[0]
  9. classNum = int(fileStr.split('_')[0])
  10. digLabels.append(classNum)
  11. trainingMat[i, :] = img2vector('trainingDigits/'+fileNameStr)
  12. return trainingMat, digLabels

最后进行一个调用

  1. if __name__ == "__main__":
  2. imageIn = getImageArr("5.bmp")
  3. trainingMat, digLabels = ClassTrain()
  4. classifierResult = classifyFun(imageIn.flatten(), trainingMat, digLabels, 10)
  5. plt.imshow(imageIn)
  6. print(classifierResult)

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/1008061
推荐阅读
相关标签
  

闽ICP备14008679号