python2.7.5下KNN file2matrix错误解决
2014-09-08 19:14
148 查看
使用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()
结果能正常执行了
依照机器学习实战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 出版社此书的论坛
相关文章推荐
- 〖Android〗arm-linux-androideabi-gdb报 libpython2.6.so.1.0: cannot open shared object file错误的解决方法
- 解决Python3.6,发送POST请求错误提示:POST data should be bytes, an iterable of bytes, or a file object. It canno
- 解决 Python.h:没有那个文件或目录 错误的方法( Python.h:No such file or directory )
- python错误解决:SyntaxError: Non-ASCII character '\xd3' in file crawler.py
- 从C调用Python脚本unableto load the file system codec ImportError错误解决方法
- python3.5连接MySQL出现Cannot open include file: 'config-win.h': No such file or directory 错误解决办法
- Python出现"Non-ASCII character '\xe6' in file"错误解决方法
- psutil安装错误"Python.h: No such file or directory"解决
- 打开sln文件出现 “the selected file is not a valid visual studio solution file”错误提示解决办法
- FCKeditor出现"this connector is disabled Please check the"editor/filemanager/connectors/aspx/config.aspx"错误的解决办法
- Cannot open include file: 'iostream.h'错误解决方法
- ServiceMix 中使用file组件时出现无效的property的错误问题的解决
- phpmyadmin提示错误Wrong permissions on configuration file, should not be world writable!解决方法
- FCKeditor出现"this connector is disabled Please check the"editor/filemanager/connectors/aspx/config.aspx"错误的解决办法
- MySQL 错误Incorrect key file for table ******.MYI; try to repair it的解决
- Windows下解决 MySQL-python-1.2.3b1 的安装编译错误
- 解决 MySQL-python-1.2.3b1 的安装错误
- ORA-01251: Unknown File Header Version read for file number 9 错误解决
- python错误TypeError: 'module' object is not callable 的解决方法
- 编译错误error C2370: 'THIS_FILE' : redefinition的解决方法