마켓 바스켓 분석이란?

R로 배우는 Market Basket Analysis

Christopher Bruffaerts

Statistician

마트의 여러 바스켓

가게엔 무엇이 있나?

all_products_emoticons

바스켓 1: {"Bread", "Cheese"}

바스켓 2: {"Bread", "Wine" , "Cheese"}

여러 바스켓

고객 100명이 마트에 온다면, 함께 사는 아이템의 연관을 찾을 수 있을까?

예시: Bread와 Cheese

bread_cheese

결과: “if this, then that”

R로 배우는 Market Basket Analysis

마켓 바스켓 활용 분야

여러 바스켓에서 학습하기

baskets

다양한 적용 분야

  • 이커머스: “이 상품을 산 고객은 다음도 샀습니다”
  • 리테일: 함께 “번들/진열”되는 아이템
  • 소셜 미디어: 친구·연결 추천
  • 동영상/영화 추천
R로 배우는 Market Basket Analysis

R에서 여러 바스켓

여러 바스켓이 있는 데이터셋을 만듭니다!

my_baskets = data.frame(
  "Basket" = c(1,1,1,1, 2,2,2, 3,3, 4,4,4, 5,5, 6,6, 7,7),
  "Product" = c("Bread", "Cheese", "Cheese", "Cheese",
                "Bread", "Butter", "Wine",
                "Butter", "Butter",
                "Butter", "Wine", "Wine",
                "Butter", "Cheese",
                "Cheese", "Wine",
                "Wine", "Wine")
)

내 바스켓 미리보기

head(my_baskets)
  Basket Product
1      1   Bread
2      1  Cheese
3      1  Cheese
4      1  Cheese
5      2   Bread
6      2  Butter
R로 배우는 Market Basket Analysis

우리 바스켓엔 무엇이 있나?

질문

  • 고유 상품 수는?
n_distinct(my_baskets$Product)
[1] 4
  • 바스켓 수는?
n_distinct(my_baskets$Basket)
[1] 7
  • 각 바스켓의 상품 개수는?
df_basket =
  my_baskets %>%
  group_by(Basket) %>%
  summarize(
    n_total = n(),
    n_items = n_distinct(Product))
  Basket n_total n_items
   <dbl>   <int>   <int>
1      1       4       2
2      2       3       3
R로 배우는 Market Basket Analysis

바스켓 크기는 어느 정도일까?

평균 바스켓 크기

basket_size %>% 
  summarize(
    avg_total_items = mean(n_total), 
    avg_dist_items = mean(n_items))
# A tibble: 1 x 2
  avg_total_items avg_dist_items
            <dbl>          <dbl>
1            2.57           1.86

바스켓 크기 분포

# 고유 상품 분포
ggplot(df_basket, aes(n_items)) +
  geom_bar()

distribution_basket

R로 배우는 Market Basket Analysis

바스켓의 특정 상품

어떤 상품을 볼까?

  • 모든 바스켓에서 해당 상품의 총 등장 횟수

  • 그 상품을 포함한 바스켓 수

예시:

cheese

R에서 Cheese 필터링

# Cheese를 포함한 바스켓 수
my_baskets %>%
  filter(Product == "Cheese")  %>%
  summarize(
    n_tot_items = n(),
    n_basket_item = n_distinct(Basket))
  n_tot_items n_basket_item
1           5             3
R로 배우는 Market Basket Analysis

연관 규칙 마이닝

연관 규칙 마이닝: 아이템 집합에서 자주 함께 나타나는 연관을 찾기.

emoticons_arrows

규칙 추출 예:

  • {Bread} $\rightarrow$ {Butter}
  • {Bread, Cheese} $\rightarrow$ {Wine}
R로 배우는 Market Basket Analysis

다음에는 무엇을 하나요?

남은 과정의 아젠다:

  • 2장: 마켓 바스켓 분석의 지표와 기법
  • 3장: 마켓 바스켓 분석의 시각화
  • 4장: 사례 연구: MovieLens 영화 추천

movie_lens_logo

R로 배우는 Market Basket Analysis

바스켓으로 연습해 봅시다!

R로 배우는 Market Basket Analysis

Preparing Video For Download...