Skip to content

第7章 buildtree类型错误 #1

@xjr7670

Description

@xjr7670

请问一下,中文版P151,在执行

tree=treepredict.buildtree(treepredict.my_data)

这里,用python3不会报类型错误吗?
我直接按书中的代码执行是会报错的,用你这里的代码也是。一样的错误:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-27-d4c8cc29c32a> in <module>()
----> 1 treepredict.buildtree(treepredict.my_data)

/home/cavin/Code/BookPractice/Python/ProgrammingCollectiveIntelligence/chapter7/treepredict.py in buildtree(rows, scoref)
    105         for row in rows:
    106             column_values[row[col]] = 1
--> 107         # 接下来根据这一列中的每个值,尝试对数据集进行拆分
    108         for value in column_values.keys():
    109             (set1, set2) = divideset(rows, col, value)

/home/cavin/Code/BookPractice/Python/ProgrammingCollectiveIntelligence/chapter7/treepredict.py in divideset(rows, column, value)
     39         print(row[column], value)
     40         split_function = lambda row: row[column] == value
---> 41 
     42     # 将数据集拆分成两个集合,并返回
     43     set1 = [row for row in rows if split_function(row)]

/home/cavin/Code/BookPractice/Python/ProgrammingCollectiveIntelligence/chapter7/treepredict.py in <listcomp>(.0)
     39         print(row[column], value)
     40         split_function = lambda row: row[column] == value
---> 41 
     42     # 将数据集拆分成两个集合,并返回
     43     set1 = [row for row in rows if split_function(row)]

/home/cavin/Code/BookPractice/Python/ProgrammingCollectiveIntelligence/chapter7/treepredict.py in <lambda>(row)
     34     split_function = None
     35     if isinstance(value, int) or isinstance(value, float):
---> 36         print(row[column], value)
     37         split_function = lambda row: row[column] >= value
     38     else:

TypeError: '>=' not supported between instances of 'str' and 'int'

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions