{"metadata":{"kernelspec":{"name":"ir","display_name":"R","language":"R"},"language_info":{"name":"R","codemirror_mode":"r","pygments_lexer":"r","mimetype":"text/x-r-source","file_extension":".r","version":"4.0.5"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Recommend Items Purchased Together in R\n\nMy R version of [Recommend Items Purchased Together](https://www.kaggle.com/code/cdeotte/recommend-items-purchased-together-0-021)","metadata":{}},{"cell_type":"code","source":"library(tidyverse)","metadata":{"_uuid":"051d70d956493feee0c6d64651c6a088724dca2a","_execution_state":"idle","execution":{"iopub.status.busy":"2022-04-28T11:47:44.098736Z","iopub.execute_input":"2022-04-28T11:47:44.101176Z","iopub.status.idle":"2022-04-28T11:47:45.595888Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transaction_train <- read_csv(\"../input/h-and-m-personalized-fashion-recommendations/transactions_train.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-04-28T11:47:45.599560Z","iopub.execute_input":"2022-04-28T11:47:45.632691Z","iopub.status.idle":"2022-04-28T11:48:32.919428Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"K <- 12\n\nmax_dat <- max(transaction_train$t_dat)","metadata":{"execution":{"iopub.status.busy":"2022-04-28T11:48:32.922136Z","iopub.execute_input":"2022-04-28T11:48:32.923815Z","iopub.status.idle":"2022-04-28T11:48:34.218724Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 1. Recommend Most Often Previously Purchased Items","metadata":{}},{"cell_type":"code","source":"customer_max_dat <- transaction_train %>%\n  group_by(customer_id) %>%\n  summarise(max_dat = max(t_dat), .groups = \"drop\")","metadata":{"execution":{"iopub.status.busy":"2022-04-28T11:48:34.221899Z","iopub.execute_input":"2022-04-28T11:48:34.223703Z","iopub.status.idle":"2022-04-28T11:49:42.263866Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dday <- 7\n\ntransaction_recently <- transaction_train %>%\n  inner_join(customer_max_dat) %>%\n  mutate(diff_dat = max_dat - t_dat) %>%\n  filter(diff_dat < dday)","metadata":{"execution":{"iopub.status.busy":"2022-04-28T11:49:42.266525Z","iopub.execute_input":"2022-04-28T11:49:42.268186Z","iopub.status.idle":"2022-04-28T11:50:01.483250Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transaction_recently %>% head","metadata":{"execution":{"iopub.status.busy":"2022-04-28T11:50:01.498533Z","iopub.execute_input":"2022-04-28T11:50:01.499920Z","iopub.status.idle":"2022-04-28T11:50:01.530440Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"purchased_counts <- transaction_recently %>%\n  count(customer_id, article_id)","metadata":{"execution":{"iopub.status.busy":"2022-04-28T11:50:01.532735Z","iopub.execute_input":"2022-04-28T11:50:01.534108Z","iopub.status.idle":"2022-04-28T11:52:27.841266Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"purchased_counts %>% arrange(desc(n)) %>% head","metadata":{"execution":{"iopub.status.busy":"2022-04-28T11:52:27.844217Z","iopub.execute_input":"2022-04-28T11:52:27.846011Z","iopub.status.idle":"2022-04-28T11:52:28.340209Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 2. Recommend Items Purchased Together","metadata":{}},{"cell_type":"code","source":"days_train <- 7\n\ncustomer_article <- transaction_train %>%\n  filter(t_dat > max_dat - days_train) %>%\n  distinct(customer_id, article_id)\n\npurchased_together_counts <- customer_article %>%\n  inner_join(customer_article, by = \"customer_id\", suffix = c(\"1\", \"2\")) %>%\n  filter(article_id1 != article_id2) %>%\n  count(article_id1, article_id2, name = \"count\") %>%\n  filter(count > 1)","metadata":{"execution":{"iopub.status.busy":"2022-04-28T11:56:37.605188Z","iopub.execute_input":"2022-04-28T11:56:37.607365Z","iopub.status.idle":"2022-04-28T11:57:00.649607Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"purchased_together_counts %>% head","metadata":{"execution":{"iopub.status.busy":"2022-04-28T11:57:10.034273Z","iopub.execute_input":"2022-04-28T11:57:10.036259Z","iopub.status.idle":"2022-04-28T11:57:10.062983Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"most_purchased_together <- purchased_together_counts %>%\n  group_by(article_id1) %>%\n  slice_max(order_by = count, n = 1, with_ties = FALSE)","metadata":{"execution":{"iopub.status.busy":"2022-04-28T11:58:10.325546Z","iopub.execute_input":"2022-04-28T11:58:10.327499Z","iopub.status.idle":"2022-04-28T11:58:12.077296Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"most_purchased_together %>% head","metadata":{"execution":{"iopub.status.busy":"2022-04-28T11:58:16.065350Z","iopub.execute_input":"2022-04-28T11:58:16.067265Z","iopub.status.idle":"2022-04-28T11:58:16.098429Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 3. Recommend Last Week's Most Popular Items","metadata":{}},{"cell_type":"code","source":"most_popular_articles <- function(transaction, k) {\n  transaction %>%\n    count(article_id, sort = TRUE) %>%\n    head(k) %>%\n    pull(article_id)\n}","metadata":{"execution":{"iopub.status.busy":"2022-04-28T08:06:35.100982Z","iopub.execute_input":"2022-04-28T08:06:35.102574Z","iopub.status.idle":"2022-04-28T08:06:35.11503Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"popular_articles <- transaction_train %>%\n  filter(t_dat > max_dat - 7) %>%\n  most_popular_articles(K)","metadata":{"execution":{"iopub.status.busy":"2022-04-28T08:06:35.11756Z","iopub.execute_input":"2022-04-28T08:06:35.119147Z","iopub.status.idle":"2022-04-28T08:06:35.814792Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"popular_articles","metadata":{"execution":{"iopub.status.busy":"2022-04-28T08:06:35.817544Z","iopub.execute_input":"2022-04-28T08:06:35.819102Z","iopub.status.idle":"2022-04-28T08:06:35.836766Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Write Submission CSV","metadata":{}},{"cell_type":"code","source":"recommendations <- purchased_counts %>%\n  rename(purchased = article_id, n_purchase = n) %>%\n  left_join(most_purchased_together, by = c(\"purchased\" = \"article_id1\")) %>%\n  rename(purchased_together = article_id2) %>%\n  group_by(customer_id) %>%\n  slice_max(order_by = n_purchase, n = K, with_ties = FALSE) %>%\n  summarise(purchased = list(purchased), purchased_together = list(purchased_together))","metadata":{"execution":{"iopub.status.busy":"2022-04-28T08:06:35.840561Z","iopub.execute_input":"2022-04-28T08:06:35.842391Z","iopub.status.idle":"2022-04-28T08:12:38.764539Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"recommendations %>% head","metadata":{"execution":{"iopub.status.busy":"2022-04-28T08:13:03.71489Z","iopub.execute_input":"2022-04-28T08:13:03.716833Z","iopub.status.idle":"2022-04-28T08:13:03.74437Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_submission <- read_csv(\"../input/h-and-m-personalized-fashion-recommendations/sample_submission.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-04-28T08:14:16.865204Z","iopub.execute_input":"2022-04-28T08:14:16.866878Z","iopub.status.idle":"2022-04-28T08:14:19.264194Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"my_submission <- sample_submission %>%\n  select(customer_id) %>%\n  left_join(recommendations) %>%\n  rowwise() %>%\n  mutate(prediction = str_c(head(c(purchased,\n                                   na.omit(purchased_together),\n                                   popular_articles),\n                                 K),\n                            collapse = \" \")) %>%\n  select(customer_id, prediction)","metadata":{"execution":{"iopub.status.busy":"2022-04-28T08:32:57.634777Z","iopub.execute_input":"2022-04-28T08:32:57.812547Z","iopub.status.idle":"2022-04-28T08:33:50.995658Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"my_submission %>% head","metadata":{"execution":{"iopub.status.busy":"2022-04-28T08:35:15.142196Z","iopub.execute_input":"2022-04-28T08:35:15.145091Z","iopub.status.idle":"2022-04-28T08:35:15.17847Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"my_submission %>% write_csv(\"submission.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-04-28T08:35:46.034894Z","iopub.execute_input":"2022-04-28T08:35:46.036698Z","iopub.status.idle":"2022-04-28T08:35:47.1791Z"},"trusted":true},"execution_count":null,"outputs":[]}]}