マーケットバスケット分析とは?

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

Isaiah Hull

Visiting Associate Professor of Finance, BI Norwegian Business School

書店レイアウトの選定

 

この画像は、書店のレイアウトで fiction と biography が一緒、poetry と history が一緒に配置されている様子を示します。

 

この画像は、書店のレイアウトで fiction と poetry が一緒、biography と history が一緒に配置されている様子を示します。

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

取引データの把握

TID 取引
1 biography, history
2 fiction
3 biography, poetry
4 fiction, history
5 biography
... ...
75000 fiction, poetry

 

  • TID = 各取引に対応する一意のID。

 

  • Transaction = 一緒に購入された一意のアイテムの集合。
Python で学ぶマーケットバスケット分析

マーケットバスケット分析とは?

  1. よく同時購入される商品を特定。

    • Biography と history
    • Fiction と poetry
  2. 結果に基づき推奨を作成。

    • Biography と history の棚を近接配置。
    • Fiction と history は離して配置。
Python で学ぶマーケットバスケット分析

マーケットバスケット分析の活用例

  1. Netflix 風のレコメンドエンジンを構築。
  2. ECサイトのおすすめを改善。
  3. 小売でクロスセルを促進。
  4. 在庫管理を改善。
  5. アップセルを促進。
Python で学ぶマーケットバスケット分析

マーケットバスケット分析の活用

TID 取引
11 fiction, biography
12 fiction, biography
13 history, biography
... ...
19 fiction, biography
20 fiction, biography
... ...
  • マーケットバスケット分析
    • アソシエーションルールを構築
    • よく同時購入される商品を特定
  • アソシエーションルール
    • {前件} $\rightarrow$ {後件}
      • {fiction} $\rightarrow$ {biography}
Python で学ぶマーケットバスケット分析

データの読み込み

import pandas as pd

# Load transactions from pandas.
books = pd.read_csv("datasets/bookstore.csv")

# Print the header
print(books.head(2))
TID         Transaction                 
0    biography, history
1               fiction

復習用に、Pandas チートシートをご参照ください。

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

トランザクションの作成

 

# Split transaction strings into lists.
transactions = books['Transaction'].apply(lambda t: t.split(','))

# Convert DataFrame into list of strings.
transactions = list(transactions)
Python で学ぶマーケットバスケット分析

アイテムセットのカウント

# Print the first transaction.
print(transactions[0])
['biography', 'history']
# Count the number of transactions that contain biography and fiction.
transactions.count(['biography', 'fiction'])
218
Python で学ぶマーケットバスケット分析

推奨の提示

# Count the number of transactions that contain fiction and poetry.
transactions.count(['fiction', 'poetry'])
5357

この画像は、書店のレイアウトで fiction と poetry が一緒、biography と history が一緒に配置されている様子を示します。

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

Passons à la pratique !

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

Preparing Video For Download...