def datingClassTest():
hoRatio = 0.10#测试数据占的比重
datingDataMat,datingLabels = file2matrix('datingTestSet2.txt')
normMat,ranges,minVals = autoNorm(datingDataMat)#数值归一化
m = normMat.shape[0]
numTestVecs = int (m*hoRatio)
errorCount = 0.0
for i in range(numTestVecs):
classifierResult = classify0(normMat[1,:],normMat[numTestVecs:m,:],\
datingLabels[numTestVecs:m],3)
print ("the classfier came back with: %d, the real answer is: %d"\
%(classifierResult,datingLabels[i]))#3.6的print要在""外面加括号 print("####")
if(classifierResult != datingLabels[i]): errorCount += 1.0
print ("the total error rate is: %f" % (errorCount/float(numTestVecs)))
Traceback (most recent call last):
File "<pyshell#26>", line 1, in <module>
kNN.datingClassTest()
File "E:\Python\kNN.py", line 52, in datingClassTest
datingDataMat,datingLabels = file2matrix('datingTestSet.txt')
File "E:\Python\kNN.py", line 25, in file2matrix
fr = open(filename)
FileNotFoundError: [Errno 2] No such file or directory: 'datingTestSet.txt'
代码部分错误’datingTestSet.txt’应改为’datingTestSet2.txt’
如果把文件名改为’datingTestSet.txt’
ValueError: invalid literal for int() with base 10: 'largeDoses'
如果把代码改为为datingTestSet2.txt’
TypeError: %d format: a number is required, not list
检查了发现在代码标签的时候少加了个i,传的是个列表