在机器学习中,**提升度(Lift)置信度(Confidence)**是关联规则挖掘中的两个重要指标,主要用于衡量项集之间的关联性。它们的核心思想是通过统计方法分析数据中的模式,常用于购物篮分析、推荐系统等领域。以下是它们的原理、数学公式以及Python实现方法的详细说明。


1. 核心概念

1.1 置信度(Confidence)
  • 定义:置信度表示在包含项集 XX 的事务中,同时包含项集 YY 的比例。它衡量规则 X→YX→Y 的可信度。
  • 数学公式

    Confidence(X→Y)=Support(X∪Y)Support(X)Confidence(X→Y)=Support(X)Support(X∪Y)​

    • Support(X∪Y)Support(X∪Y):项集 XX 和 YY 同时出现的概率。
    • Support(X)Support(X):项集 XX 出现的概率。
1.2 提升度(Lift)
  • 定义:提升度衡量项集 XX 和 YY 之间的相关性。它比较了规则 X→YX→Y 的置信度与 YY 的全局出现概率。
  • 数学公式

    Lift(X→Y)=Confidence(X→Y)Support(Y)=Support(X∪Y)Support(X)⋅Support(Y)Lift(X→Y)=Support(Y)Confidence(X→Y)​=Support(X)⋅Support(Y)Support(X∪Y)​

    • 提升度的意义
      • Lift > 1:XX 和 YY 正相关(购买 XX 会提高购买 YY 的概率)。
      • Lift = 1:XX 和 YY 独立(无关联)。
      • Lift < 1:XX 和 YY 负相关(购买 XX 会降低购买 YY 的概率)。

2. Python 实现

在 Python 中,可以使用 mlxtend 库的 aprioriassociation_rules 模块来计算置信度和提升度。以下是一个完整的示例代码。

2.1 数据准备

假设我们有一个购物篮数据集,每个事务(Transaction)包含多个商品。

from mlxtend.preprocessing import TransactionEncoder
import pandas as pd

# 示例数据集:每个子列表代表一个事务
data = [
    ['牛奶', '面包', '黄油'],
    ['牛奶', '面包'],
    ['牛奶', '黄油'],
    ['面包', '黄油'],
    ['牛奶', '面包', '黄油', '啤酒']
]

# 转换为二进制矩阵
te = TransactionEncoder()
te_ary = te.fit(data).transform(data)
df = pd.DataFrame(te_ary, columns=te.columns_)
print(df)

深色版本

   牛奶  面包  黄油  啤酒
0  True  True  True  False
1  True  True  False  False
2  True  False  True  False
3  False  True  True  False
4  True  True  True  True
2.2 生成频繁项集

使用 Apriori 算法找出满足最小支持度的频繁项集:

python

深色版本

from mlxtend.frequent_patterns import apriori

# 设置最小支持度阈值为 0.6
frequent_itemsets = apriori(df, min_support=0.6, use_colnames=True)
print(frequent_itemsets)

输出结果:

深色版本

   support     itemsets
0     0.80      (牛奶)
1     0.80      (面包)
2     0.80      (黄油)
3     0.60  (牛奶, 面包)
4     0.60  (牛奶, 黄油)
5     0.60  (面包, 黄油)
6     0.40  (牛奶, 面包, 黄油)
2.3 生成关联规则

计算置信度和提升度:

from mlxtend.frequent_patterns import association_rules

# 生成关联规则,设置最小置信度为 0.7
rules = association_rules(frequent_itemsets, metric="confidence", min_threshold=0.7)
print(rules[['antecedents', 'consequents', 'support', 'confidence', 'lift']])

输出结果:

  antecedents consequents  support  confidence       lift
0      (牛奶)      (面包)     0.60      0.7500  1.125000
1      (牛奶)      (黄油)     0.60      0.7500  1.125000
2      (面包)      (牛奶)     0.60      0.7500  1.125000
3      (面包)      (黄油)     0.60      0.7500  1.125000
4      (黄油)      (牛奶)     0.60      0.7500  1.125000
5      (黄油)      (面包)     0.60      0.7500  1.125000

3. 关键步骤解析

  1. 数据预处理

    • 将事务数据转换为二进制矩阵(使用 TransactionEncoder)。
    • 每个商品作为一列,事务中存在该商品则标记为 True,否则为 False
  2. Apriori 算法

    • 通过扫描数据多次,生成所有满足最小支持度的频繁项集。
    • 支持度阈值 (min_support) 决定了频繁项集的筛选标准。
  3. 关联规则生成

    • 使用 association_rules 函数计算规则的置信度和提升度。
    • metric 参数指定评估指标(如 confidence 或 lift)。
    • min_threshold 参数设置阈值,过滤弱关联规则。

4. 应用场景

  • 零售行业:分析商品之间的关联性,优化商品摆放和促销策略。
  • 推荐系统:基于用户历史行为生成个性化推荐。
  • 医疗领域:挖掘疾病之间的关联性,辅助诊断。
Logo

DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。

更多推荐