基于FP-Growth算法的关联规则挖掘
使用Pyfpgrowth进行关联规则挖掘
Pyfpgrowth是一个用于实现FP-Growth算法的Python库,可以高效地挖掘频繁项集和关联规则。
安装:
pip install pyfpgrowth
使用:
import pyfpgrowth
# 示例交易数据
transactions = [[1, 2, 5], [2, 4], [2, 3], [1, 2, 4], [1, 3], [2, 3], [1, 2, 3, 5], [1, 2, 3]]
# 寻找支持度大于等于3的频繁项集
patterns = pyfpgrowth.find_frequent_patterns(transactions, 3)
# 生成置信度大于等于0.8的关联规则
rules = pyfpgrowth.generate_association_rules(patterns, 0.8)
print("频繁项集:", patterns)
print("关联规则:", rules)
代码说明:
transactions
: 包含交易数据的列表,每个交易表示为一个项目ID列表。find_frequent_patterns(transactions, support)
: 返回支持度大于等于support
的频繁项集。generate_association_rules(patterns, confidence)
: 从频繁项集中生成置信度大于等于confidence
的关联规则。
20.15KB
文件大小:
评论区