本章,主要是讲了通过FP树的方法来寻找频繁项集,请注意,这种方法不能用于发现关联规则。FP树的构建过程可以直接看书中的图12-3和表12-2,图12-3很好地说明了FP树的构建过程。
由于我使用的是python3,所以需要对书中的代码进行一些小修改。
提示错误:
RuntimeError: dictionary changed size during iteration
对应函数createTree里的语句:
for k in headerTable.keys():
if headerTable[k] < minSup:
del(headerTable[k])
这条语句的作用是遍历字典headerTable,删除字典中小于minSup的数据,这条语句在python2中是没有错误的,因为python2中字典是支持遍历的同时改变字典的大小的,但在python3中不支持,应该改成如下形式:
for k in list(headerTable):
if headerTable[k] < minSup:
del(headerTable[k])
将字典转为列表类型就可以了。