详解python实现FP-TREE进行关联规则挖掘(带有FP树显示功能)附源代码下载(1)
程序使用PYTHON3.2实现,要生成每一步树的图片,请安装一个绘图库PIL(Python Image Library)
源代码下载地址:点击打开链接
打开源代码后可以在sample.py找到样本如下:
items=('chips','eggs','bread','milk','beer','popcorn','butter') sample=[ ['milk','eggs','bread','chips'], ['eggs','popcorn','chips','beer'], ['eggs','bread','chips'], ['milk','eggs','bread','popcorn','chips','beer'], ['milk','bread','beer'], ['eggs','bread','beer'], ['milk','bread','chips'], ['milk','eggs','bread','butter','chips'], ['milk','eggs','butter','chips'] ]
这就本文用来挖掘的样本案例,该样本的FP树如下图所示
对支持度最小为3的关联规则进行挖掘,以 beer 项的挖掘过程为例,请看下面每一步的FP树图:
beer 的条件模式基:
beer和bread 的条件模式基:
beer和eggs 的条件模式基:
最后的程序对样本的挖掘结果如下(右边数字是频集的支持度):
('chips', 'eggs') 6
('milk', 'chips') 5
('milk', 'bread') 5
('chips', 'bread') 5
('bread', 'eggs') 5
('milk', 'eggs', 'chips') 4
('milk', 'eggs') 4
('chips', 'eggs', 'bread') 4
('milk', 'chips', 'bread') 4
('milk', 'eggs', 'chips', 'bread') 3
('beer', 'bread') 3
('beer', 'eggs') 3
('milk', 'eggs', 'bread') 3
('milk', 'chips') 5
('milk', 'bread') 5
('chips', 'bread') 5
('bread', 'eggs') 5
('milk', 'eggs', 'chips') 4
('milk', 'eggs') 4
('chips', 'eggs', 'bread') 4
('milk', 'chips', 'bread') 4
('milk', 'eggs', 'chips', 'bread') 3
('beer', 'bread') 3
('beer', 'eggs') 3
('milk', 'eggs', 'bread') 3
在以后的博客中,我会详细把源代码解读给大家,敬请期待!