Market basket analysis คืออะไร?

Market Basket Analysis ด้วย Python

Isaiah Hull

Visiting Associate Professor of Finance, BI Norwegian Business School

เลือกผังร้านหนังสือ

 

ภาพแสดงผังร้านหนังสือที่จัดให้หมวดนิยายและชีวประวัติอยู่ใกล้กัน และหมวดกวีนิพนธ์กับประวัติศาสตร์อยู่ใกล้กัน

 

ภาพแสดงผังร้านหนังสือที่จัดให้หมวดนิยายและกวีนิพนธ์อยู่ใกล้กัน และหมวดชีวประวัติกับประวัติศาสตร์อยู่ใกล้กัน

Market Basket Analysis ด้วย Python

สำรวจข้อมูลธุรกรรม

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

 

  • TID = รหัส ID เฉพาะของแต่ละธุรกรรม

 

  • Transaction = ชุดสินค้าที่ซื้อร่วมกันในครั้งเดียว
Market Basket Analysis ด้วย Python

Market basket analysis คืออะไร?

  1. ระบุสินค้าที่มักถูกซื้อร่วมกัน

    • ชีวประวัติและประวัติศาสตร์
    • นิยายและกวีนิพนธ์
  2. สร้างคำแนะนำจากผลที่ได้

    • จัดหมวดชีวประวัติและประวัติศาสตร์ไว้ใกล้กัน
    • แยกหมวดนิยายและประวัติศาสตร์ออกจากกัน
Market Basket Analysis ด้วย Python

กรณีการใช้งาน market basket analysis

  1. สร้างระบบแนะนำแบบ Netflix
  2. ปรับปรุงคำแนะนำสินค้าในร้านค้าออนไลน์
  3. Cross-sell สินค้าในร้านค้าปลีก
  4. บริหารจัดการสินค้าคงคลังได้ดีขึ้น
  5. Upsell สินค้า
Market Basket Analysis ด้วย Python

การใช้ market basket analysis

TID Transaction
11 fiction, biography
12 fiction, biography
13 history, biography
... ...
19 fiction, biography
20 fiction, biography
... ...
  • Market basket analysis
    • สร้าง association rules
    • ระบุสินค้าที่มักถูกซื้อร่วมกัน
  • Association rules
    • {antecedent} $\rightarrow$ {consequent}
      • {fiction} $\rightarrow$ {biography}
Market Basket Analysis ด้วย 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 Cheat Sheet

Market Basket Analysis ด้วย Python

สร้างชุดธุรกรรม

 

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

# Convert DataFrame into list of strings.
transactions = list(transactions)
Market Basket Analysis ด้วย Python

นับจำนวน itemset

# Print the first transaction.
print(transactions[0])
['biography', 'history']
# Count the number of transactions that contain biography and fiction.
transactions.count(['biography', 'fiction'])
218
Market Basket Analysis ด้วย Python

สรุปคำแนะนำ

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

ภาพแสดงผังร้านหนังสือที่จัดให้หมวดนิยายและกวีนิพนธ์อยู่ใกล้กัน และหมวดชีวประวัติกับประวัติศาสตร์อยู่ใกล้กัน

Market Basket Analysis ด้วย Python

มาฝึกกันเถอะ!

Market Basket Analysis ด้วย Python

Preparing Video For Download...