使用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月前
|
机器学习/深度学习 算法 Python
请解释Python中的随机森林算法以及如何使用Sklearn库实现它。
【2月更文挑战第28天】【2月更文挑战第101篇】请解释Python中的随机森林算法以及如何使用Sklearn库实现它。
|
24天前
|
机器学习/深度学习 算法 搜索推荐
Machine Learning机器学习之决策树算法 Decision Tree(附Python代码)
Machine Learning机器学习之决策树算法 Decision Tree(附Python代码)
|
1月前
|
机器学习/深度学习 算法 数据挖掘
请解释Python中的决策树算法以及如何使用Sklearn库实现它。
决策树是监督学习算法,常用于分类和回归问题。Python的Sklearn库提供了决策树实现。以下是一步步创建决策树模型的简要步骤:导入所需库,加载数据集(如鸢尾花数据集),划分数据集为训练集和测试集,创建`DecisionTreeClassifier`,训练模型,预测测试集结果,最后通过`accuracy_score`评估模型性能。示例代码展示了这一过程。
|
1月前
|
机器学习/深度学习 算法 数据可视化
请解释Python中的K-means聚类算法以及如何使用Sklearn库实现它。
【2月更文挑战第29天】【2月更文挑战第104篇】请解释Python中的K-means聚类算法以及如何使用Sklearn库实现它。
|
2天前
|
算法 数据可视化 Python
Python用MCMC马尔科夫链蒙特卡洛、拒绝抽样和Metropolis-Hastings采样算法
Python用MCMC马尔科夫链蒙特卡洛、拒绝抽样和Metropolis-Hastings采样算法
13 6
|
3天前
|
机器学习/深度学习 算法 搜索推荐
Python用机器学习算法进行因果推断与增量、增益模型Uplift Modeling智能营销模型
Python用机器学习算法进行因果推断与增量、增益模型Uplift Modeling智能营销模型
30 12
|
9天前
|
算法 数据可视化 Python
Python贝叶斯推断Metropolis-Hastings(M-H)MCMC采样算法的实现
Python贝叶斯推断Metropolis-Hastings(M-H)MCMC采样算法的实现
14 0
|
9天前
|
数据可视化 算法 数据挖掘
PYTHON实现谱聚类算法和改变聚类簇数结果可视化比较
PYTHON实现谱聚类算法和改变聚类簇数结果可视化比较
|
10天前
|
算法 数据可视化 Python
Python中LARS和Lasso回归之最小角算法Lars分析波士顿住房数据实例
Python中LARS和Lasso回归之最小角算法Lars分析波士顿住房数据实例
15 0
|
10天前
|
机器学习/深度学习 算法 Python
使用Python实现集成学习算法:Bagging与Boosting
使用Python实现集成学习算法:Bagging与Boosting
20 0