什么是购物篮分析?

Python 中的购物篮分析

Isaiah Hull

Visiting Associate Professor of Finance, BI Norwegian Business School

选择书店布局

 

此图展示书店布局:小说与传记分区在一起,诗歌与历史分区在一起。

 

此图展示书店布局:小说与诗歌分区在一起,传记与历史分区在一起。

Python 中的购物篮分析

探索交易数据

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

 

  • TID = 每笔交易的唯一 ID。

 

  • Transaction = 一起购买的唯一商品集合。
Python 中的购物篮分析

什么是购物篮分析?

  1. 识别经常一起购买的商品。

    • 传记 与 历史
    • 小说 与 诗歌
  2. 基于此构建推荐。

    • 将传记与历史区域放在一起。
    • 将小说与历史分开。
Python 中的购物篮分析

购物篮分析的用例

  1. 构建类 Netflix 的推荐引擎。
  2. 提升电商商品推荐。
  3. 实现零售场景中的交叉销售。
  4. 改进库存管理。
  5. 促进加购/加价销售。
Python 中的购物篮分析

使用购物篮分析

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

此图展示书店布局:小说与诗歌分区在一起,传记与历史分区在一起。

Python 中的购物篮分析

Passons à la pratique !

Python 中的购物篮分析

Preparing Video For Download...