电子工业出版社,2015年第3版
P153,增加了buildtree
函数后执行出错,报错为:
---------------------------------------------------------------------------
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'
而如果使用python2执行是没问题的,所以怀疑是2和3之间的兼容问题。查看了网友列出来的一些2与3之间的差异后,初步确定是这个:
出处:http://chenqx.github.io/2014/11/10/Key-differences-between-Python-2-7-x-and-Python-3-x/
解法方法:
(还在尝试)