什么是购物篮分析?

R 中的购物篮分析

Christopher Bruffaerts

Statistician

超市中的多个篮子

商店里有什么?

所有商品_表情

篮子 1:{"Bread", "Cheese"}

篮子 2:{"Bread", "Wine" , "Cheese"}

多个篮子

若有 100 位顾客进店,能否找到一起出现的商品关联?

示例:Bread 和 Cheese

面包_奶酪

结果:"若此,则彼"

R 中的购物篮分析

购物篮分析的应用

从多个篮子中学习

篮子

不同应用

  • 电商:"购买此商品的用户还购买了"
  • 零售:常被"捆绑或摆放在一起"的商品
  • 社交媒体:好友与连接推荐
  • 视频与电影推荐
R 中的购物篮分析

在 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 中的购物篮分析

篮子里有什么?

问题

  • 有多少个不同的商品?
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 中的购物篮分析

篮子有多大?

平均篮子大小

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()

分布_篮子

R 中的购物篮分析

篮子中的特定商品

在看哪个商品?

  • 某商品在所有篮子中出现了多少次?

  • 有多少个篮子包含该商品?

示例:

奶酪

在 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 中的购物篮分析

关联规则挖掘

关联规则挖掘:在一组商品中发现经常共同出现的关联。

表情_箭头

规则提取示例:

  • {Bread} $\rightarrow$ {Butter}
  • {Bread, Cheese} $\rightarrow$ {Wine}
R 中的购物篮分析

接下来是什么?

课程剩余部分计划:

  • 第 2 章:购物篮分析的指标与技术
  • 第 3 章:购物篮分析的可视化
  • 第 4 章:案例研究:MovieLens 电影推荐

movielens_标识

R 中的购物篮分析

让我们玩转篮子!

R 中的购物篮分析

Preparing Video For Download...