机器学习实战一 kNN错误及修正

出现错误1

FileNotFoundError: [Errno 2] No such file or directory: 'datingTestSet.txt'
datingDataMat,datingLabels = kNN.file2matrix('datingTestSet.txt')

一定注意:
datingTestSet.txt要与本py文件在同一个文件夹下
机器学习实战一 kNN错误及修正_第1张图片

出现错误2

ValueError: invalid literal for int() with base 10: 'largeDoses'

原因:
本书的实例代码出错了,此处应该要用datingTestSet2.txt,因为datingTestSet2.txt的末尾列为数字,而datingTestSet.txt末尾列为字符串。

出现错误3

 File "H:\PycharmProject\kNN-master\机器学习实战(中文版+英文版+源代码)\机器学习实战源代码\machinelearninginaction\Ch02\kNN.py", line 30, in classify0
    sortedClassCount = sorted(classCount.iteritems(), key=operator.itemgetter(1), reverse=True)
AttributeError: 'dict' object has no attribute 'iteritems'

出错原因

python2.x版本有iteritems,而python3.x将它删除了,将其改为items即可

kNN.classify0([0,0],group,labels,3)

出现错误4

 File "H:\PycharmProject\kNN-master\机器学习实战(中文版+英文版+源代码)\机器学习实战源代码\machinelearninginaction\Ch02\kNN.py", line 92, in classifyPerson
    classifierResult = classify0((inArr-minVals)/ranges,normMat,datingLabels,3)
TypeError: unsupported operand type(s) for -: 'set' and 'float'

将下面这行代码改正:

 inArr = array({ffMiles,percentTats,iceCream})

错误改正

inArr = [ffMiles,percentTats,iceCream]

参考文献

https://blog.csdn.net/qq_44940689/article/details/120895909
https://blog.csdn.net/michaelhan3/article/details/74017111

你可能感兴趣的:(机器学习,机器学习,python)