장바구니 분석이란?

Python으로 배우는 Market Basket Analysis

Isaiah Hull

Visiting Associate Professor of Finance, BI Norwegian Business School

서점 레이아웃 선택

 

이 이미지는 소설과 전기 코너가 함께, 시와 역사 코너가 함께 배치된 서점 레이아웃을 보여줍니다.

 

이 이미지는 소설과 시 코너가 함께, 전기와 역사 코너가 함께 배치된 서점 레이아웃을 보여줍니다.

Python으로 배우는 Market Basket Analysis

거래 데이터 살펴보기

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

 

  • TID = 각 거래의 고유 ID

 

  • Transaction = 함께 구매된 고유 항목 집합
Python으로 배우는 Market Basket Analysis

장바구니 분석이란?

  1. 함께 자주 구매되는 상품 식별

    • 전기와 역사
    • 소설과 시
  2. 이 결과로 추천 구성

    • 전기와 역사 코너를 함께 배치
    • 소설과 역사는 떨어뜨려 배치
Python으로 배우는 Market Basket Analysis

장바구니 분석의 활용 사례

  1. 넷플릭스 스타일 추천 엔진 구축
  2. 전자상거래 상품 추천 개선
  3. 소매점 교차판매 강화
  4. 재고 관리 개선
  5. 상향판매(업셀) 촉진
Python으로 배우는 Market Basket Analysis

장바구니 분석 활용

TID Transaction
11 fiction, biography
12 fiction, biography
13 history, biography
... ...
19 fiction, biography
20 fiction, biography
... ...
  • 장바구니 분석
    • 연관 규칙 구성
    • 함께 자주 구매되는 상품 식별
  • 연관 규칙
    • {선행} $\rightarrow$ {결과}
      • {fiction} $\rightarrow$ {biography}
Python으로 배우는 Market Basket Analysis

데이터 불러오기

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를 참고하십시오.

Python으로 배우는 Market Basket Analysis

거래 목록 만들기

 

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

# Convert DataFrame into list of strings.
transactions = list(transactions)
Python으로 배우는 Market Basket Analysis

아이템셋 개수 세기

# 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으로 배우는 Market Basket Analysis

추천 제안하기

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

이 이미지는 소설과 시 코너가 함께, 전기와 역사 코너가 함께 배치된 서점 레이아웃을 보여줍니다.

Python으로 배우는 Market Basket Analysis

연습해 봅시다!

Python으로 배우는 Market Basket Analysis

Preparing Video For Download...