{"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":"My R version of [Simple Baseline with Local Validation template](https://www.kaggle.com/code/pmanresa6/simple-baseline-with-local-validation-template).\n\nI use [Metrics](https://github.com/benhamner/Metrics/) package to calculate `MAP@12`.","metadata":{}},{"cell_type":"code","source":"install.packages(\"Metrics\", quiet = TRUE)","metadata":{"execution":{"iopub.status.busy":"2022-04-09T10:16:43.302119Z","iopub.execute_input":"2022-04-09T10:16:43.333881Z","iopub.status.idle":"2022-04-09T10:16:52.091181Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"library(tidyverse)","metadata":{"execution":{"iopub.status.busy":"2022-04-09T10:42:30.950731Z","iopub.execute_input":"2022-04-09T10:42:30.953674Z","iopub.status.idle":"2022-04-09T10:42:30.979463Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transactions <- read_csv(\"../input/h-and-m-personalized-fashion-recommendations/transactions_train.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-04-09T10:21:28.209316Z","iopub.execute_input":"2022-04-09T10:21:28.212040Z","iopub.status.idle":"2022-04-09T10:21:44.968829Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"most_popular_articles <- function(transaction, k) {\n  transaction %>%\n    group_by(article_id) %>%\n    summarise(n = n(), .groups = \"drop\") %>%\n    arrange(desc(n)) %>%\n    head(k) %>%\n    pull(article_id)\n}","metadata":{"execution":{"iopub.status.busy":"2022-04-09T10:28:09.639703Z","iopub.execute_input":"2022-04-09T10:28:09.641877Z","iopub.status.idle":"2022-04-09T10:28:09.673451Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"K <- 12\nmax_dat <- max(transactions$t_dat)\n\ndays_trains <- 7 * c(1, 2, 4, 8)\nstart_date_fold <- (max_dat - 6) - 0:14 * 7 * 4","metadata":{"execution":{"iopub.status.busy":"2022-04-09T10:28:53.429102Z","iopub.execute_input":"2022-04-09T10:28:53.430894Z","iopub.status.idle":"2022-04-09T10:29:05.394596Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"days_trains","metadata":{"execution":{"iopub.status.busy":"2022-04-09T10:29:16.851829Z","iopub.execute_input":"2022-04-09T10:29:16.853407Z","iopub.status.idle":"2022-04-09T10:29:16.870156Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"start_date_fold","metadata":{"execution":{"iopub.status.busy":"2022-04-09T10:29:25.900036Z","iopub.execute_input":"2022-04-09T10:29:25.901675Z","iopub.status.idle":"2022-04-09T10:29:25.918871Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results <- start_date_fold %>%\n  map_dfr(function(start_date) {\n    days_trains %>%\n      map_dfr(function(days_train) {\n        transaction_pred <- transactions %>%\n          filter(t_dat >= start_date, t_dat < start_date + 7)\n        transaction_train <- transactions %>%\n          filter(t_dat >= start_date - days_train, t_dat < start_date)\n\n        purchase <- transaction_pred %>%\n          group_by(customer_id) %>%\n          summarise(actual = list(article_id), .groups = \"drop\") %>%\n          mutate(predicted = list(most_popular_articles(transaction_train, K)))\n\n        mapk <- Metrics::mapk(K, pull(purchase, actual), pull(purchase, predicted))\n\n        list(days_train = days_train, start_date = start_date, mapk = mapk)\n      })\n  })","metadata":{"execution":{"iopub.status.busy":"2022-04-09T10:31:17.077636Z","iopub.execute_input":"2022-04-09T10:31:17.079231Z","iopub.status.idle":"2022-04-09T10:38:48.690284Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results %>% head","metadata":{"execution":{"iopub.status.busy":"2022-04-09T10:39:35.292167Z","iopub.execute_input":"2022-04-09T10:39:35.294095Z","iopub.status.idle":"2022-04-09T10:39:35.320449Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results_plot <- results %>%\n  mutate(days_train = factor(days_train))","metadata":{"execution":{"iopub.status.busy":"2022-04-09T10:44:18.330842Z","iopub.execute_input":"2022-04-09T10:44:18.334397Z","iopub.status.idle":"2022-04-09T10:44:18.358850Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ggplot(results_plot, aes(x = days_train, y = mapk)) +\n  geom_boxplot()","metadata":{"execution":{"iopub.status.busy":"2022-04-09T10:44:25.863336Z","iopub.execute_input":"2022-04-09T10:44:25.866073Z","iopub.status.idle":"2022-04-09T10:44:26.186385Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ggplot(results_plot, aes(x = start_date, y = mapk, color = days_train)) +\n  geom_line()","metadata":{"execution":{"iopub.status.busy":"2022-04-09T10:44:36.490921Z","iopub.execute_input":"2022-04-09T10:44:36.492683Z","iopub.status.idle":"2022-04-09T10:44:36.923780Z"},"trusted":true},"execution_count":null,"outputs":[]}]}