高级规则

Python 中的购物篮分析

Isaiah Hull

Visiting Associate Professor of Finance, BI Norwegian Business School

购物篮分析概览

  • 购物篮分析的标准流程。
    1. 生成大量规则。
    2. 用指标筛选规则。
    3. 运用直觉与常识。
Python 中的购物篮分析

生成规则

  • 规则数量随商品数指数增长。
    • 大多数规则无用。
  • 必须先做初步筛选。
    • 第 3 章用 Apriori 算法讲解
Python 中的购物篮分析

筛选如何进行?

ID 前件 后件 支持度 提升度
1 哈利·波特 饥饿游戏 0.001 1.01
2 饥饿游戏 暮光之城 0.020 1.23
3 傲慢与偏见 霍比特人 0.030 1.05
4 霍比特人 暮光之城 0.015 0.85
5 哈利·波特 霍比特人 0.010 1.07
Python 中的购物篮分析

多指标筛选

ID 前件 后件 支持度 Zhang 指标
1 哈利·波特 饥饿游戏 0.001 -0.05
2 饥饿游戏 暮光之城 0.020 0.17
3 傲慢与偏见 霍比特人 0.030 0.06
4 霍比特人 暮光之城 0.015 -0.04
5 哈利·波特 霍比特人 0.010 0.34
Python 中的购物篮分析

执行多指标筛选

print(rules.head())
  antecedents consequents  antecedent support  ... support  confidence  ... conviction  
0 Potter      Hunger       0.48                ...  0.12  ... 0.26      ...  0.92
1 Potter      Hunger       0.32                ...  0.12  ... 0.26      ...  0.92 
2 Twilight    Hunger       0.26                ...  0.09  ... 0.35      ...  1.04
3 Hunger      Twilight     0.32                ...  0.09  ... 0.28      ...  1.03 
4 Mockingbird Hunger       0.48                ...  0.10  ... 0.20      ...  0.85
Python 中的购物篮分析

执行多指标筛选

# 选择后件支持度较低的规则子集。
rules = rules[rules['consequent support'] < 0.05]
print(len(rules))
12
# 选择提升度 > 1.5 的规则子集。
rules = rules[rules['lift'] > 1.5]
print(len(rules))
2
Python 中的购物篮分析

Passons à la pratique !

Python 中的购物篮分析

Preparing Video For Download...