一、什么是购物篮分析
经典问题:顾客买了面包,多大可能会买牛奶?买了啤酒的人,会不会买尿布?
业务场景:电商推荐、零售促销、货架摆放、捆绑销售。
一句话总结:关联规则挖掘"经常一起买"的商品组合,核心是支持度 + 置信度 + 提升度三大指标,提升度 > 1 才算真关联。
二、环境要求
| 依赖库 | 版本要求 |
|---|---|
| Python | 3.14+ |
| scikit-learn | 1.9+ |
| pandas | 3.0+ |
| numpy | 2.4+ |
| jupyter | 1.1+ |
| notebook | 7.5+ |
| nbconvert | 7.17+ |
三、数据预处理:事务 → One-Hot
原始数据:每行一笔交易,商品空格分隔。
transactions=pd.DataFrame({"products":["bread eggs","bread eggs milk","milk cheese","bread butter cheese","eggs milk","bread milk butter cheese"]})转换为 One-Hot 矩阵:
| 交易 | bread | butter | cheese | eggs | milk |
|---|---|---|---|---|---|
| 0 | 1 | 0 | 0 | 1 | 0 |
| 1 | 1 | 0 | 0 | 1 | 1 |
| 2 | 0 | 0 | 1 | 0 | 1 |
| 3 | 1 | 1 | 1 | 0 | 0 |
| 4 | 0 | 0 | 0 | 1 | 1 |
| 5 | 1 | 1 | 1 | 0 | 1 |
工业方案:mlxtend.preprocessing.TransactionEncoder一行搞定。
frommlxtend.preprocessingimportTransactionEncoder te=TransactionEncoder()te_arr=te.fit(transactions['products'].apply(lambdax:x.split())).transform(...)四、关联规则三大核心指标
1. 支持度 Support(A)
公式:Support(A) = count(A) / N
含义:A 在多少比例的交易中出现(流行程度)。
support=df.sum()/len(df)# bread: 0.6667 → 4/6 笔交易有 bread2. 置信度 Confidence(A→B)
公式:Confidence(A→B) = Support(A∪B) / Support(A)
含义:买 A 的人里,有多大比例也买了 B(条件概率 P(B|A))。
conf_cheese_bread=(len(df.query("cheese==1 and bread==1"))/len(df.query("cheese==1")))# 0.6667 → 买 cheese 的人 2/3 也买了 bread3. 提升度 Lift(A→B)
公式:Lift(A→B) = Confidence(A→B) / Support(B)
含义:A 是否"真"对 B 有促进(过滤伪关联)。
| Lift 值 | 含义 | 业务建议 |
|---|---|---|
| > 1 | A 提升 B 出现概率(正相关) | ✅ 可作推荐规则 |
| = 1 | A 与 B 独立 | ❌ 不可用 |
| < 1 | A 抑制 B(负相关) | ⚠️ 反推荐 |
五、为什么必须看提升度
反例:假设买手机壳 → 买手机置信度 90%,看似很强。
真相:90% 的人都会买手机,与买不买手机壳无关。手机本身就是高频商品,置信度高是"假象"。
提升度公式:Confidence / Support(B)把"高频商品干扰"剔除。
经验值:Lift > 1.5 才算强关联,Lift > 2 才有商业价值。
六、手工计算完整流程
# 1. 单品支持度support=df.sum()/len(df)# 2. 多品组合支持度sup_butter_cheese=len(df.query("butter==1 and cheese==1"))/len(df)# 3. 置信度conf_butter_cheese=(len(df.query("butter==1 and cheese==1"))/len(df.query("butter==1")))# 4. 提升度lift_butter_cheese=conf_butter_cheese/sup_butter_cheese# 2.0 → 买 butter 的人买 cheese 的概率是平均值的 2 倍七、工业实现路径
原始日志 → 事务矩阵 → 频繁项集 → 关联规则 → 业务筛选
| 步骤 | 工具 |
|---|---|
| 频繁项集挖掘 | Apriori / FP-Growth |
| 规则生成 | mlxtend.fpgrowth / apriori |
| 业务筛选 | 提升度 > 1 + 业务回查 |
常用库:mlxtend
frommlxtend.frequent_patternsimportapriori,association_rules# 1. 挖掘频繁项集(支持度 ≥ 0.01)frequent_itemsets=apriori(df,min_support=0.01,use_colnames=True)# 2. 生成关联规则(置信度 ≥ 0.6)rules=association_rules(frequent_itemsets,metric="confidence",min_threshold=0.6)# 3. 按提升度筛选(Lift > 1.5)strong_rules=rules[rules["lift"]>1.5].sort_values("lift",ascending=False)八、阈值经验值
| 阈值 | 推荐值 | 含义 |
|---|---|---|
min_support | 0.01 ~ 0.05 | 至少 1-5% 交易出现 |
min_confidence | ≥ 0.6 | 60% 以上的可能性 |
min_lift | > 1(>1.5 更强) | 过滤伪关联 |
调参思路:
min_support过高:漏掉长尾规则;过低:组合爆炸min_confidence过高:规则过少;过低:噪声多min_lift是核心筛选器,必看
九、业务应用模板
业务问题:超市希望提升客单价 ↓ 挖掘关联规则(支持度 ≥ 0.02,置信度 ≥ 0.5,提升度 ≥ 1.5) ↓ 筛选"买了 A 必买 B"的强规则 ↓ 三种落地方式: 1. 捆绑销售(A+B 组合优惠) 2. 货架摆放(A 旁放 B) 3. 推荐系统(买了 A 推荐 B) ↓ A/B 测试验证转化率提升十、关键要点
- 三大指标缺一不可:单独看置信度会被"高频商品"误导
- 提升度是核心:Lift > 1 才有真关联,Lift > 1.5 才强
- 支持度是过滤器:过滤掉罕见的"巧合"
- 小数据集手工实现:大数据用 mlxtend 的 Apriori/FP-Growth
- 业务验证不可省:统计上显著的规则未必有商业价值
十一、常见陷阱
- ❌ 只看置信度:"啤酒→尿布"经典案例(伪关联)就会漏掉
- ❌ min_support 设太低:项集爆炸,内存溢出
- ❌ 忽略提升度:推荐无效规则,转化率不升反降
- ❌ 样本量太小:< 1000 笔交易,关联规则不可靠
- ❌ 强规则直接上线:必须 A/B 测试验证
十二、完整实战示例(mlxtend)
importpandasaspdfrommlxtend.frequent_patternsimportapriori,association_rules# 假设 df 已经是 One-Hot 编码的事务矩阵# 1. 频繁项集freq=apriori(df,min_support=0.05,use_colnames=True)# 2. 关联规则rules=association_rules(freq,metric="lift",min_threshold=1.2)# 3. Top 10 强规则top10=rules.sort_values("lift",ascending=False).head(10)print(top10[["antecedents","consequents","support","confidence","lift"]])输出示例:
| antecedents | consequents | support | confidence | lift |
|---|---|---|---|---|
| {butter} | {cheese} | 0.333 | 1.000 | 2.000 |
| {bread, milk} | {butter} | 0.167 | 0.800 | 2.400 |
| … | … | … | … | … |
参考资料:ant-exercises-sklearn: scikit-learn 编程练习 100 例