关联规则 Fp-Growth算法实现

Fp-Growth算法实现

实现上次博客例子,设置最小支持度计数为3,3/5=0.6,所以支持度为0.6
关联规则 Fp-Growth算法实现_第1张图片

  • 代码
# 属于太菜了,做个调包侠
from mlxtend.preprocessing import TransactionEncoder
from mlxtend.frequent_patterns import fpgrowth
import pandas as pd

# 数据集
dataset = [
    ['f', 'c', 'a','m','p'],
    ['f', 'c', 'a','b','m'],
    ['f', 'b'],
    ['c', 'b', 'p'],
    ['f','c', 'a', 'm', 'p']
]

# 将数据集转换为 one-hot 编码格式
te = TransactionEncoder()
te_ary = te.fit(dataset).transform(dataset)
df = pd.DataFrame(te_ary, columns=te.columns_)

# 应用 FP-Growth 算法
# min_support 参数指定支持度的阈值
frequent_patterns = fpgrowth(df, min_support=0.6, use_colnames=True)

print(frequent_patterns)

  • 结果

support itemsets
0 0.8 (f)
1 0.8 ©
2 0.6 §
3 0.6 (m)
4 0.6 (a)
5 0.6 (b)
6 0.6 (f, c)
7 0.6 (p, c)
8 0.6 (m, c)
9 0.6 (m, f)
10 0.6 (m, f, c)
11 0.6 (m, a)
12 0.6 (a, c)
13 0.6 (a, f)
14 0.6 (m, a, c)
15 0.6 (m, a, f)
16 0.6 (f, a, c)
17 0.6 (f, m, a, c)

你可能感兴趣的:(算法,机器学习,人工智能)