购物篮简介

R 中的购物篮分析

Christopher Bruffaerts

Statistician

概览

购物篮课程

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

movie_lens_logo

R 中的购物篮分析

什么是购物篮?

购物篮 = 商品集合

商品

  1. 超市里的商品
  2. 在线网站的商品
  3. DataCamp 课程
  4. 用户看过的电影

supermarket

购物篮示例:

  1. 你在超市的购物篮

  2. 你的亚马逊购物车

  3. 你在 DataCamp 的课程

  4. 你在 Netflix 看过的电影

R 中的购物篮分析

超市示例

商店里有哪些?

all_products_emoticons

今天准备买什么?

  • 一条面包
  • 三块奶酪

bread_3cheese

R 中的购物篮分析

R 中的超市示例

商店里有哪些?

store = c("Bread", "Butter",
          "Cheese", "Wine")
set.seed(1234)
n_items = 4
my_basket = data.frame(
                TID = rep(1,n_items),
                Product = sample(
                    store, n_items, 
                    replace = TRUE))

R 输出

my_basket
  TID Product
1   1   Bread
2   1  Cheese
3   1  Cheese
4   1  Cheese
R 中的购物篮分析

我的购物篮里有什么?

我的原始购物篮

每个购买的商品一条记录

  TID Product
1   1   Bread
2   1  Cheese
3   1  Cheese
4   1  Cheese

我的调整后购物篮

每个不同商品一条记录

# A tibble: 2 x 3
    TID Product Quantity
  <dbl> <fct>      <int>
1     1 Bread          1
2     1 Cheese         3
R 中的购物篮分析

我的 R 购物篮里有什么?

重塑购物篮数据

# 调整我的购物篮
my_basket = my_basket %>%
                add_count(Product) %>%
                unique() %>% 
                rename(Quantity = n)
# 不同商品数量
n_distinct(my_basket$Product)
2
# 购物篮总量
my_basket %>% summarize(sum(Quantity))
4
R 中的购物篮分析

可视化我的购物篮商品

可视化购物篮中的商品

# 绘制商品 
ggplot(my_basket,
    aes(x=reorder(Product, Quantity),
        y = Quantity)) +
          geom_col() +
        coord_flip() +
        xlab("Items") +
        ggtitle("Summary of items
                in my basket")

distribution_basket_bread_cheese

R 中的购物篮分析

为何关注我的购物篮?

问题:篮子内的商品之间有关系吗?

baskets

回到示例

  1. 你在超市的购物篮, 例如:意大利面和番茄酱

  2. 你的亚马逊购物车, 例如:手机和手机壳

  3. 你在 DataCamp 的课程, 例如:"Introduction to R"和"Intermediate R"

R 中的购物篮分析

购物愉快!

R 中的购物篮分析

Preparing Video For Download...