FP-growth算法代码
时间: 2024-01-02 11:02:12 浏览: 141
以下是Python实现的FP-growth算法代码:
```python
class TreeNode:
def __init__(self, name, count, parent):
self.name = name
self.count = count
self.parent = parent
self.children = {}
self.next = None
def increment(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_fptree(dataset, minsup):
"""
根据数据集创建FP树
:param dataset: 数据集,格式为列表的列表
:param minsup: 最小支持度
:return: FP树,头指针表
"""
header_table = {}
for trans in dataset:
for item in trans:
header_table[item] = header_table.get(item, 0) + dataset[trans]
keys = list(header_table.keys())
for k in keys:
if header_table[k] < minsup:
del (header_table[k])
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]
tree = TreeNode('Null Set', 1, None)
for trans, count in dataset.items():
local_D = {}
for item in trans:
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, tree, header_table, count)
return tree, header_table
def update_tree(items, in_tree, header_table, count):
"""
更新FP树
:param items: 项集
:param in_tree: 当前子树
:param header_table: 头指针表
:param count: 频数
:return: None
"""
if items[0] in in_tree.children:
in_tree.children[items[0]].increment(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.next is not None:
node_to_test = node_to_test.next
node_to_test.next = 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):
"""
查找某项的前缀路径
:param base_pat: 项
:param tree_node: 树节点
:return: 前缀路径列表
"""
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.next
return cond_pats
def mine_tree(in_tree, header_table, minsup, pre_fix, freq_item_list):
"""
递归挖掘FP树
:param in_tree: 当前子树
:param header_table: 头指针表
:param minsup: 最小支持度
:param pre_fix: 前缀
:param freq_item_list: 频繁项列表
:return: None
"""
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_fptree(cond_patt_bases, minsup)
if my_head is not None:
mine_tree(my_cond_tree, my_head, minsup, new_freq_set, freq_item_list)
```
其中,`create_fptree`函数用于创建FP树和头指针表,`update_tree`函数用于更新FP树,`find_prefix_path`函数用于查找某项的前缀路径,`mine_tree`函数用于递归挖掘FP树,最终返回频繁项集列表。
阅读全文