[機器學習實戰-ch09]TypeError: unsupported operand type(s) for /: 'map' and 'int'
阿新 • • 發佈:2019-01-22
在執行這一段命令的最後一行時出現了問題
>>>reload(regTrees)
<module 'regTrees' from 'regTrees.pyc'>
>>> from numpy import *
The data from figure 9.1 is stored in a file called ex00.txt.
>>> myDat=regTrees.loadDataSet('ex00.txt')
>>> myMat = mat(myDat)
>>> regTrees.createTree(myMat)
查了一下網上的方法,發現loadDataSet()函式裡面的map方法和int不相容,網上解決方法是在map外面加一個list,像這樣:
結果試了下,還是不行!!!
然後自己改了下程式碼,發現執行OK了!!!,如下:
def loadDataSet(fileName): #general function to parse tab -delimited floats
dataMat = [] #assume last column is target value
fr = open(fileName)
for line in fr.readlines():
curLine = line.strip().split('\t')
fltLine = []
for i in curLine:
fltLine.append(float(i))
dataMat.append(fltLine)
return dataMat