最も簡単な指標

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{items を含む取引数}}{\text{取引総数}}$$

 

$$\frac{\text{milk を含む取引数}}{\text{取引総数}}$$

Python で学ぶマーケットバスケット分析

language のサポート

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

 

{language} のサポート = 2 / 10 = 0.2

TID Transaction
5 poetry, health, travel, history
6 humor
7 travel
8 poetry, fiction, humor
9 fiction, biography
Python で学ぶマーケットバスケット分析

{Humor} $\rightarrow$ {Language} のサポート

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

 

{language} $\rightarrow$ {humor} のサポート = 0.1

TID Transaction
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...