使用python2.7.5版本错误解决
依照机器学习实战KNN的代码报错如下:
runloader@runloader-dell:~/ml/Ch02$ python
Python 2.7.5+ (default, Feb 27 2014, 19:37:08)[GCC 4.8.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import kNN
>>> datingDataMat,datingLabels = kNN.file2matrix('datingTestSet.txt')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "kNN.py", line 48, in file2matrix
classLabelVector.append(int(listFromLine[-1]))
ValueError: invalid literal for int() with base 10: 'largeDoses'
KNN源码如下:
def file2matrix(filename):
fr = open(filename)
numberOfLines = len(fr.readlines()) #get the number of lines in the file
returnMat = zeros((numberOfLines,3)) #prepare matrix to return
classLabelVector = [] #prepare labels return
fr = open(filename)
index = 0
for line in fr.readlines():
line = line.strip()
listFromLine = line.split('\t')
returnMat[index,:] = listFromLine[0:3]
classLabelVector.append(int(listFromLine[-1])) ###是因为此处的int导致
index += 1
return returnMat,classLabelVector
修改后代码如下:
def file2matrix(filename):
fr = open(filename)
numberOfLines = len(fr.readlines()) #get the number of lines in the file
returnMat = zeros((numberOfLines,3)) #prepare matrix to return
classLabelVector = [] #prepare labels return
fr = open(filename)
index = 0
for line in fr.readlines():
line = line.strip()
listFromLine = line.split('\t')
returnMat[index,:] = listFromLine[0:3]
classLabelVector.append(listFromLine[-1])
index += 1
return returnMat,classLabelVector
>>> import kNN
>>> datingDataMat,datingLabels = mykNN.file2matrix('datingTestSet.txt')
>>> import matplotlib
>>> import matplotlib.pyplot as plt
>>> fig = plt.figure()
>>> ax = fig.add_subplot(111)
>>> ax.scatter(mat[:,1], mat[:,2], 15.0*array(map(int,lab)),15.0*array(map(int,lab))) ##替换原文中的ax.scatter(datingDataMat[:,1], datingDataMat[:, 2]
<matplotlib.collections.PathCollection object at 0x3c04310>
>>> plt.show()
结果能正常执行了
请查看了harrington本人的回复,Manning 出版社此书的论坛