使用python实现FP-Growth算法

简介: 使用python实现FP-Growth算法

FP-Growth(Frequent Pattern Growth)是一种用于发现频繁项集的数据挖掘算法,通常用于关联规则挖掘。下面是一个简单的Python实现FP-Growth算法的示例:

 

```python
from collections import defaultdict
 
class FPNode:
    def __init__(self, item, count, parent):
        self.item = item
        self.count = count
        self.parent = parent
        self.children = {}
        self.next = None
 
    def increment_count(self, count):
        self.count += count
 
def build_tree(transactions, min_support):
    header_table = defaultdict(int)
    for transaction in transactions:
        for item in transaction:
            header_table[item] += 1
    header_table = {k: v for k, v in header_table.items() if v >= min_support}
    
    if len(header_table) == 0:
        return None, None
 
    root = FPNode(None, 0, None)
    for transaction in transactions:
        transaction = [item for item in transaction if item in header_table]
        transaction.sort(key=lambda item: header_table[item], reverse=True)
        current_node = root
        for item in transaction:
            if item in current_node.children:
                current_node.children[item].increment_count(1)
            else:
                new_node = FPNode(item, 1, current_node)
                current_node.children[item] = new_node
                if header_table[item] == 1:
                    update_fp_tree(new_node, header_table)
            current_node = current_node.children[item]
 
    return root, header_table
 
def update_fp_tree(node, header_table):
    while node.next is not None:
        node = node.next
    node.next = header_table[node.item]
 
def find_frequent_patterns(tree, header_table, prefix, frequent_patterns, min_support):
    for item, count in header_table.items():
        new_prefix = prefix.copy()
        new_prefix.add(item)
        frequent_patterns.add(frozenset(new_prefix))
        conditional_patterns = get_conditional_patterns(item, header_table)
        conditional_tree, conditional_header = build_tree(conditional_patterns, min_support)
        if conditional_tree is not None:
            find_frequent_patterns(conditional_tree, conditional_header, new_prefix, frequent_patterns, min_support)
 
def get_conditional_patterns(item, header_table):
    conditional_patterns = []
    node = header_table[item]
    while node is not None:
        prefix_path = []
        current_node = node.parent
        while current_node.item is not None:
            prefix_path.append(current_node.item)
            current_node = current_node.parent
        if len(prefix_path) > 0:
            conditional_patterns.append(prefix_path)
        node = node.next
    return conditional_patterns
 
def fp_growth(transactions, min_support):
    tree, header_table = build_tree(transactions, min_support)
    frequent_patterns = set()
    find_frequent_patterns(tree, header_table, set(), frequent_patterns, min_support)
    return frequent_patterns
 
# 示例数据
transactions = [
    ['A', 'B', 'D'],
    ['B', 'C', 'E'],
    ['A', 'B', 'D', 'E'],
    ['A', 'B', 'C', 'E']
]
 
min_support = 2
 
frequent_patterns = fp_growth(transactions, min_support)
for pattern in frequent_patterns:
    print(pattern)
```

 

这是一个简单的FP-Growth算法的Python实现示例。您可以根据需要进行调整和扩展。这段代码可以帮助您理解FP-Growth算法的基本原理和实现方式。

相关文章
|
1月前
|
算法 搜索推荐 JavaScript
基于python智能推荐算法的全屋定制系统
本研究聚焦基于智能推荐算法的全屋定制平台网站设计,旨在解决消费者在个性化定制中面临的选择难题。通过整合Django、Vue、Python与MySQL等技术,构建集家装设计、材料推荐、家具搭配于一体的一站式智能服务平台,提升用户体验与行业数字化水平。
|
1月前
|
存储 监控 算法
监控电脑屏幕的帧数据检索 Python 语言算法
针对监控电脑屏幕场景,本文提出基于哈希表的帧数据高效检索方案。利用时间戳作键,实现O(1)级查询与去重,结合链式地址法支持多条件检索,并通过Python实现插入、查询、删除操作。测试表明,相较传统列表,检索速度提升80%以上,存储减少15%,具备高实时性与可扩展性,适用于大规模屏幕监控系统。
114 5
|
2月前
|
存储 算法 调度
【复现】【遗传算法】考虑储能和可再生能源消纳责任制的售电公司购售电策略(Python代码实现)
【复现】【遗传算法】考虑储能和可再生能源消纳责任制的售电公司购售电策略(Python代码实现)
183 26
|
2月前
|
机器学习/深度学习 算法 机器人
【机器人路径规划】基于D*算法的机器人路径规划(Python代码实现)
【机器人路径规划】基于D*算法的机器人路径规划(Python代码实现)
181 0
|
2月前
|
机器学习/深度学习 算法 机器人
【机器人路径规划】基于改进型A*算法的机器人路径规划(Python代码实现)
【机器人路径规划】基于改进型A*算法的机器人路径规划(Python代码实现)
216 0
|
2月前
|
机器学习/深度学习 编解码 算法
【机器人路径规划】基于迪杰斯特拉算法(Dijkstra)的机器人路径规划(Python代码实现)
【机器人路径规划】基于迪杰斯特拉算法(Dijkstra)的机器人路径规划(Python代码实现)
315 4
|
2月前
|
机器学习/深度学习 算法 机器人
【机器人路径规划】基于A*算法的机器人路径规划研究(Python代码实现)
【机器人路径规划】基于A*算法的机器人路径规划研究(Python代码实现)
441 4
|
2月前
|
机器学习/深度学习 算法 机器人
【机器人路径规划】基于深度优先搜索(Depth-First-Search,DFS)算法的机器人路径规划(Python代码实现)
【机器人路径规划】基于深度优先搜索(Depth-First-Search,DFS)算法的机器人路径规划(Python代码实现)
238 3
|
2月前
|
算法 机器人 定位技术
【机器人路径规划】基于流场寻路算法(Flow Field Pathfinding)的机器人路径规划(Python代码实现)
【机器人路径规划】基于流场寻路算法(Flow Field Pathfinding)的机器人路径规划(Python代码实现)
148 4
机器学习/深度学习 算法 自动驾驶
484 0

推荐镜像

更多