新手学习PYTHON中KNN算法的手写识别出现问题 求助
展开全部
参考了其他博主的代码 想试着运行 然后去理解。结果一直报错,希望大神帮帮忙。
import numpy as np
import os
import kNN
def img2vector(filename):
"""函数将以文本格式出现的32*32的0-1图片,转变成一维特征数组,返回一维数组
Keyword argument:
filename -- 文本格式的图片文件
"""
imgvect = np.zeros((1, 1024))
fr = open(filename)
for i in range(32):
linestr = fr.readline()
for j in range(32):
imgvect[0, 32*i + j] = int(linestr[j])
return imgvect
def handwriteClassfiy(testfile, trainfile, k):
"""函数将trainfile中的文本图片转换成样本特征集和样本类型集,用testfile中的测试样本测试,无返回值
Keyword argument:
testfile -- 测试图片目录
trainfile -- 样本图片目录
"""
trainFileList = os.listdir(trainfile)
trainFileSize = len(trainFileList)
labels = []
trainDataSet = np.zeros((trainFileSize, 1024))
for i in range(trainFileSize):
filenameStr = trainFileList[i]
digitnameStr = filenameStr.split('.')[0]
digitLabels = digitnameStr.split('_')[0]
labels.append(digitLabels)
trainDataSet[i, :] = img2vector(trainfile + '/' + filenameStr)
testFileList = os.listdir(testfile)
testNumber = len(testFileList)
errorcount = 0.0
for testname in testFileList:
testdigit = img2vector(testfile + '/' + testname)
classifyresult = kNN.classfiy(testdigit, trainDataSet, labels, k)
testStr = testname.split('.')[0]
testDigitLabel = testStr.split('_')[0]
if classifyresult != testDigitLabel:
errorcount += 1.0
#print('this test real digit is:%s, and the result is: %s' % (testDigitLabel, classifyresult))
print('k = %d, errorRatio is: %f' % (k, errorcount/float(testNumber)))
return
if __name__ == '__main__':
filename = 'C:/Users/lx/Desktop/MachineLearning-master/kNN/use Python and NumPy/testDigits/0_1.txt'
traindir= 'C:/Users/lx/Desktop/MachineLearning-master/kNN/use Python and NumPy/trainingDigits'
testdir = 'C:/Users/lx/Desktop/MachineLearning-master/kNN/use Python and NumPy/testDigits'
handwriteClassfiy(testdir, traindir, 3)
错误提示Traceback (most recent call last):
File "kNN.py", line 56, in <module>
handwriteClassfiy(testdir, traindir, 3)
File "kNN.py", line 43, in handwriteClassfiy
classifyresult = kNN.classfiy(testdigit, trainDataSet, labels, k)
AttributeError: module 'kNN' has no attribute 'classfiy'
你这个文件是不是就叫 kNN.py ?如果是的话那你这个里面根本就没有 classfiy 这个属性,当然会报错。
另外,import kNN 是 import 自己?
import numpy as np
import os
import kNN
def img2vector(filename):
"""函数将以文本格式出现的32*32的0-1图片,转变成一维特征数组,返回一维数组
Keyword argument:
filename -- 文本格式的图片文件
"""
imgvect = np.zeros((1, 1024))
fr = open(filename)
for i in range(32):
linestr = fr.readline()
for j in range(32):
imgvect[0, 32*i + j] = int(linestr[j])
return imgvect
def handwriteClassfiy(testfile, trainfile, k):
"""函数将trainfile中的文本图片转换成样本特征集和样本类型集,用testfile中的测试样本测试,无返回值
Keyword argument:
testfile -- 测试图片目录
trainfile -- 样本图片目录
"""
trainFileList = os.listdir(trainfile)
trainFileSize = len(trainFileList)
labels = []
trainDataSet = np.zeros((trainFileSize, 1024))
for i in range(trainFileSize):
filenameStr = trainFileList[i]
digitnameStr = filenameStr.split('.')[0]
digitLabels = digitnameStr.split('_')[0]
labels.append(digitLabels)
trainDataSet[i, :] = img2vector(trainfile + '/' + filenameStr)
testFileList = os.listdir(testfile)
testNumber = len(testFileList)
errorcount = 0.0
for testname in testFileList:
testdigit = img2vector(testfile + '/' + testname)
classifyresult = kNN.classfiy(testdigit, trainDataSet, labels, k)
testStr = testname.split('.')[0]
testDigitLabel = testStr.split('_')[0]
if classifyresult != testDigitLabel:
errorcount += 1.0
#print('this test real digit is:%s, and the result is: %s' % (testDigitLabel, classifyresult))
print('k = %d, errorRatio is: %f' % (k, errorcount/float(testNumber)))
return
if __name__ == '__main__':
filename = 'C:/Users/lx/Desktop/MachineLearning-master/kNN/use Python and NumPy/testDigits/0_1.txt'
traindir= 'C:/Users/lx/Desktop/MachineLearning-master/kNN/use Python and NumPy/trainingDigits'
testdir = 'C:/Users/lx/Desktop/MachineLearning-master/kNN/use Python and NumPy/testDigits'
handwriteClassfiy(testdir, traindir, 3)
错误提示Traceback (most recent call last):
File "kNN.py", line 56, in <module>
handwriteClassfiy(testdir, traindir, 3)
File "kNN.py", line 43, in handwriteClassfiy
classifyresult = kNN.classfiy(testdigit, trainDataSet, labels, k)
AttributeError: module 'kNN' has no attribute 'classfiy'
你这个文件是不是就叫 kNN.py ?如果是的话那你这个里面根本就没有 classfiy 这个属性,当然会报错。
另外,import kNN 是 import 自己?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询