欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

[机器学习实战-ch09]TypeError: unsupported operand type(s) for /: 'map' and 'int'

程序员文章站 2022-03-05 12:20:41
...

在执行这一段命令的最后一行时出现了问题

>>>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)

[机器学习实战-ch09]TypeError: unsupported operand type(s) for /: 'map' and 'int'

查了一下网上的方法,发现loadDataSet()函数里面的map方法和int不兼容,网上解决方法是在map外面加一个list,像这样:
[机器学习实战-ch09]TypeError: unsupported operand type(s) for /: 'map' and 'int'

结果试了下,还是不行!!!

然后自己改了下代码,发现运行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