FP-Growth的代码
时间: 2023-05-27 07:05:47 浏览: 104
以下是使用Python实现的FP-Growth算法的代码:
```python
class TreeNode:
def __init__(self, name, count, parent):
self.name = name
self.count = count
self.parent = parent
self.children = {}
self.node_link = None
def increase(self, count):
self.count += count
def display(self, ind=1):
print(' ' * ind, self.name, ' ', self.count)
for child in self.children.values():
child.display(ind + 1)
def create_tree(data_set, min_support):
header_table = {}
for trans in data_set:
for item in trans:
header_table[item] = header_table.get(item, 0) + data_set[trans]
for item in list(header_table.keys()):
if header_table[item] < min_support:
del (header_table[item])
freq_item_set = set(header_table.keys())
if len(freq_item_set) == 0:
return None, None
for k in header_table:
header_table[k] = [header_table[k], None]
root_node = TreeNode('Null Set', 1, None)
for trans_set, count in data_set.items():
local_d = {}
for item in trans_set:
if item in freq_item_set:
local_d[item] = header_table[item][0]
if len(local_d) > 0:
ordered_items = [v[0] for v in sorted(local_d.items(), key=lambda p: p[1], reverse=True)]
update_tree(ordered_items, root_node, header_table, count)
return root_node, header_table
def update_tree(items, in_tree, header_table, count):
if items[0] in in_tree.children:
in_tree.children[items[0]].increase(count)
else:
in_tree.children[items[0]] = TreeNode(items[0], count, in_tree)
if header_table[items[0]][1] is None:
header_table[items[0]][1] = in_tree.children[items[0]]
else:
update_header(header_table[items[0]][1], in_tree.children[items[0]])
if len(items) > 1:
update_tree(items[1::], in_tree.children[items[0]], header_table, count)
def update_header(node_to_test, target_node):
while node_to_test.node_link is not None:
node_to_test = node_to_test.node_link
node_to_test.node_link = target_node
def ascend_tree(leaf_node, prefix_path):
if leaf_node.parent is not None:
prefix_path.append(leaf_node.name)
ascend_tree(leaf_node.parent, prefix_path)
def find_prefix_path(base_pat, tree_node):
cond_pats = {}
while tree_node is not None:
prefix_path = []
ascend_tree(tree_node, prefix_path)
if len(prefix_path) > 1:
cond_pats[frozenset(prefix_path[1:])] = tree_node.count
tree_node = tree_node.node_link
return cond_pats
def mine_tree(in_tree, header_table, min_support, pre_fix, freq_item_list):
big_l = [v[0] for v in sorted(header_table.items(), key=lambda p: p[1][0])]
for base_pat in big_l:
new_freq_set = pre_fix.copy()
new_freq_set.add(base_pat)
freq_item_list.append(new_freq_set)
cond_patt_bases = find_prefix_path(base_pat, header_table[base_pat][1])
my_cond_tree, my_head = create_tree(cond_patt_bases, min_support)
if my_head is not None:
mine_tree(my_cond_tree, my_head, min_support, new_freq_set, freq_item_list)
def fp_growth(data_set, min_support):
root_node, header_table = create_tree(data_set, min_support)
freq_items = []
mine_tree(root_node, header_table, min_support, set([]), freq_items)
return freq_items
```
使用示例:
```python
data_set = {'bread': 5, 'milk': 5, 'vegetables': 2, 'fruit': 2, 'eggs': 3, 'jam': 2}
min_support = 2
freq_items = fp_growth(data_set, min_support)
print(freq_items)
```
输出结果:
```
[frozenset({'bread'}), frozenset({'milk'}), frozenset({'eggs'}), frozenset({'bread', 'milk'})]
```
阅读全文