最简单的度量

Python 中的购物篮分析

Isaiah Hull

Visiting Associate Professor of Finance, BI Norwegian Business School

度量与剪枝

  • 度量是评估规则表现的指标。
    • {humor} $\rightarrow$ {poetry}
      • 0.81
    • {fiction} $\rightarrow$ {travel}
      • 0.23
  • 剪枝是用度量丢弃规则。
    • 保留:{humor} $\rightarrow$ {poetry}
    • 丢弃:{fiction} $\rightarrow$ {travel}
Python 中的购物篮分析

最简单的度量

  • 支持度衡量包含某项集的交易占比。

 

$$\frac{\text{number of transactions with items(s)}}{\text{number of transactions}}$$

 

$$\frac{\text{number of transactions with milk}}{\text{total transactions}}$$

Python 中的购物篮分析

language 的支持度

TID 交易
0 travel, humor, fiction
1 humor, language
2 humor, biography, cooking
3 cooking, language
4 travel

 

{language} 的支持度 = 2 / 10 = 0.2

TID 交易
5 poetry, health, travel, history
6 humor
7 travel
8 poetry, fiction, humor
9 fiction, biography
Python 中的购物篮分析

{Humor} $\rightarrow$ {Language} 的支持度

TID 交易
0 travel,humor,fiction
1 humor,language
2 humor,biography,cooking
3 cooking,language
4 travel

 

{language} $\rightarrow$ {humor} 的支持度 = 0.1

TID 交易
5 poetry,health,travel,history
6 humor
7 travel
8 poetry,fiction,humor
9 fiction,biography
Python 中的购物篮分析

准备数据

print(transactions)
[['travel', 'humor', 'fiction'],
...
['fiction', 'biography']]
from mlxtend.preprocessing import TransactionEncoder
# Instantiate transaction encoder
encoder = TransactionEncoder().fit(transactions)
Python 中的购物篮分析

准备数据

# One-hot encode itemsets by applying fit and transform
onehot = encoder.transform(transactions)
# Convert one-hot encoded data to DataFrame
onehot = pd.DataFrame(onehot, columns = encoder.columns_)
print(onehot)
   biography  cooking  ...  poetry  travel
0  False      False   ...   False    True
...
9  True       False   ...   False    False
Python 中的购物篮分析

计算单个商品的支持度

print(onehot.mean())
biography    0.2
cooking      0.2
fiction      0.3
health       0.1
history      0.1
humor        0.5
language     0.2
poetry       0.2
travel       0.4
dtype: float64
Python 中的购物篮分析

计算多商品的支持度

import numpy as np

# Define itemset that contains fiction and poetry
onehot['fiction+poetry'] = np.logical_and(onehot['fiction'],onehot['poetry'])

print(onehot.mean())
biography         0.2
cooking           0.2
...               ...
travel            0.4
fiction+poetry    0.1
dtype: float64
Python 中的购物篮分析

Vamos praticar!

Python 中的购物篮分析

Preparing Video For Download...