{"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":"code","source":"# This R environment comes with many helpful analytics packages installed\n# It is defined by the kaggle/rstats Docker image: https://github.com/kaggle/docker-rstats\n# For example, here's a helpful package to load\n\nlibrary(tidyverse) # metapackage of all tidyverse packages\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nlist.files(path = \"../input\")\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"051d70d956493feee0c6d64651c6a088724dca2a","_execution_state":"idle","execution":{"iopub.status.busy":"2022-03-08T03:57:21.891636Z","iopub.execute_input":"2022-03-08T03:57:21.931950Z","iopub.status.idle":"2022-03-08T03:57:22.948088Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"library(data.table)\nlibrary(dplyr)\nlibrary(ggplot2)\nlibrary(wesanderson)","metadata":{"execution":{"iopub.status.busy":"2022-03-08T03:57:22.950730Z","iopub.execute_input":"2022-03-08T03:57:22.952206Z","iopub.status.idle":"2022-03-08T03:57:23.095100Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\narticle <- fread(\"../input/h-and-m-personalized-fashion-recommendations/articles.csv\", header = TRUE)\n\ncustomer <- fread(\"../input/h-and-m-personalized-fashion-recommendations/customers.csv\", header = TRUE)\n\ntransaction_train <- fread(\"../input/h-and-m-personalized-fashion-recommendations/transactions_train.csv\", header = TRUE)","metadata":{"execution":{"iopub.status.busy":"2022-03-08T03:57:23.098873Z","iopub.execute_input":"2022-03-08T03:57:23.100443Z","iopub.status.idle":"2022-03-08T03:58:05.265095Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### EDA - 1. Data Cleaning ","metadata":{}},{"cell_type":"code","source":"summary(article)\n","metadata":{"execution":{"iopub.status.busy":"2022-03-08T03:58:05.267810Z","iopub.execute_input":"2022-03-08T03:58:05.269305Z","iopub.status.idle":"2022-03-08T03:58:05.355851Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"summary(customer)\n","metadata":{"execution":{"iopub.status.busy":"2022-03-08T03:58:05.358886Z","iopub.execute_input":"2022-03-08T03:58:05.360429Z","iopub.status.idle":"2022-03-08T03:58:05.652957Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"library(tidyr)","metadata":{"execution":{"iopub.status.busy":"2022-03-08T03:58:05.655554Z","iopub.execute_input":"2022-03-08T03:58:05.657002Z","iopub.status.idle":"2022-03-08T03:58:05.670149Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Delete NA  from age, fashion news frequency in Customer data \n","metadata":{}},{"cell_type":"code","source":"sum(is.na(customer$age))","metadata":{"execution":{"iopub.status.busy":"2022-03-08T03:58:05.673170Z","iopub.execute_input":"2022-03-08T03:58:05.674551Z","iopub.status.idle":"2022-03-08T03:58:05.692430Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customer <- customer %>% drop_na(club_member_status, fashion_news_frequency)\n","metadata":{"execution":{"iopub.status.busy":"2022-03-08T03:58:05.694827Z","iopub.execute_input":"2022-03-08T03:58:05.696230Z","iopub.status.idle":"2022-03-08T03:58:06.768162Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customer <- customer %>% drop_na(age)\n\nhead(customer)","metadata":{"execution":{"iopub.status.busy":"2022-03-08T03:58:06.770573Z","iopub.execute_input":"2022-03-08T03:58:06.771927Z","iopub.status.idle":"2022-03-08T03:58:06.926628Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sum(is.na(customer$fashion_news_frequency))\n","metadata":{"execution":{"iopub.status.busy":"2022-03-08T03:58:06.930241Z","iopub.execute_input":"2022-03-08T03:58:06.931836Z","iopub.status.idle":"2022-03-08T03:58:06.957369Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Transaction data cleaning ","metadata":{}},{"cell_type":"code","source":"summary(transaction_train)\n","metadata":{"execution":{"iopub.status.busy":"2022-03-08T03:58:06.961142Z","iopub.execute_input":"2022-03-08T03:58:06.962824Z","iopub.status.idle":"2022-03-08T03:58:16.818071Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transaction_train$t_dat <- as.Date(transaction_train$t_dat)\n","metadata":{"execution":{"iopub.status.busy":"2022-03-08T03:58:16.820411Z","iopub.execute_input":"2022-03-08T03:58:16.821737Z","iopub.status.idle":"2022-03-08T03:58:20.362919Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### EDA 2. Product distribution ","metadata":{}},{"cell_type":"code","source":"index <- article %>% group_by(Categories = index_group_name) %>% summarise(Count = n()) %>% mutate(Total = sum(Count), percent = Count/Total)\n","metadata":{"execution":{"iopub.status.busy":"2022-03-08T03:58:20.365456Z","iopub.execute_input":"2022-03-08T03:58:20.366841Z","iopub.status.idle":"2022-03-08T03:58:20.412495Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"index","metadata":{"execution":{"iopub.status.busy":"2022-03-08T03:58:20.416354Z","iopub.execute_input":"2022-03-08T03:58:20.418012Z","iopub.status.idle":"2022-03-08T03:58:20.441835Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Top 10 product ","metadata":{}},{"cell_type":"code","source":"productType <- article %>% group_by(product_name =product_type_name) %>% summarise(Count = n()) %>% arrange(desc(Count)) %>% head(10)\n","metadata":{"execution":{"iopub.status.busy":"2022-03-08T03:58:20.444398Z","iopub.execute_input":"2022-03-08T03:58:20.445821Z","iopub.status.idle":"2022-03-08T03:58:20.475977Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"productType","metadata":{"execution":{"iopub.status.busy":"2022-03-08T03:58:20.478657Z","iopub.execute_input":"2022-03-08T03:58:20.480166Z","iopub.status.idle":"2022-03-08T03:58:20.503103Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Visualization ","metadata":{}},{"cell_type":"code","source":"ggplot(aes(x= \"\", y= percent, fill =Categories), data= index) +geom_bar(stat = \"identity\", position = position_fill()) +\n  coord_polar(\"y\") + geom_text(aes(x= 1.25, label =Count ), position = position_fill(vjust= 0.5)) + theme(axis.title.x = element_blank(), \n                                                                                                          axis.title.y = element_blank(), plot.title = element_text())  + \nscale_fill_manual(values=wes_palette(n=5, name=\"Darjeeling2\"))","metadata":{"execution":{"iopub.status.busy":"2022-03-08T03:58:20.506016Z","iopub.execute_input":"2022-03-08T03:58:20.507610Z","iopub.status.idle":"2022-03-08T03:58:21.294696Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### majority of article categories is ladieswear and baby/children cloth. \n","metadata":{}},{"cell_type":"code","source":"ggplot(data= productType ,aes(x= product_name, y= Count)) + geom_bar(stat = \"identity\", fill = \"Blue\")\n","metadata":{"execution":{"iopub.status.busy":"2022-03-08T03:58:21.298737Z","iopub.execute_input":"2022-03-08T03:58:21.301004Z","iopub.status.idle":"2022-03-08T03:58:21.531163Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### The most frequent product types are Trousers and Dress Sweater and T-shirts are following. \n","metadata":{}},{"cell_type":"markdown","source":"### 1-2 customer demograhpic \n","metadata":{}},{"cell_type":"code","source":"member <- customer %>% group_by(club_member_status) %>% drop_na(club_member_status) %>% summarise(Count = n())\n","metadata":{"execution":{"iopub.status.busy":"2022-03-08T03:58:21.533715Z","iopub.execute_input":"2022-03-08T03:58:21.535161Z","iopub.status.idle":"2022-03-08T03:58:21.786031Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### When I run the code first, there was \"\" status, so I converted \"\" to NA and rerun the code ","metadata":{}},{"cell_type":"code","source":"customer$fashion_news_frequency[customer$fashion_news_frequency == \"\"] <- NA\ncustomer$club_member_status[customer$club_member_status == \"\"] <- NA\n","metadata":{"execution":{"iopub.status.busy":"2022-03-08T03:58:21.788636Z","iopub.execute_input":"2022-03-08T03:58:21.790086Z","iopub.status.idle":"2022-03-08T03:58:21.999375Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"member","metadata":{"execution":{"iopub.status.busy":"2022-03-08T03:58:22.002708Z","iopub.execute_input":"2022-03-08T03:58:22.004634Z","iopub.status.idle":"2022-03-08T03:58:22.025034Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"frequency <- customer %>% group_by(fashion_news_frequency) %>% summarise(Count =n())\n","metadata":{"execution":{"iopub.status.busy":"2022-03-08T03:58:22.027560Z","iopub.execute_input":"2022-03-08T03:58:22.028996Z","iopub.status.idle":"2022-03-08T03:58:22.086625Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"frequency\n","metadata":{"execution":{"iopub.status.busy":"2022-03-08T03:58:22.089240Z","iopub.execute_input":"2022-03-08T03:58:22.090738Z","iopub.status.idle":"2022-03-08T03:58:22.112040Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### We can see we have two type of none, \"NONE\" and \"None\" , so I change \"NONE\" -> None\n","metadata":{}},{"cell_type":"code","source":"customer$fashion_news_frequency[customer$fashion_news_frequency == \"NONE\"] <- \"None\"\n","metadata":{"execution":{"iopub.status.busy":"2022-03-08T03:58:22.114555Z","iopub.execute_input":"2022-03-08T03:58:22.115984Z","iopub.status.idle":"2022-03-08T03:58:22.247179Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"frequency","metadata":{"execution":{"iopub.status.busy":"2022-03-08T03:58:22.436491Z","iopub.execute_input":"2022-03-08T03:58:22.438186Z","iopub.status.idle":"2022-03-08T03:58:22.458459Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"agegroup <- customer %>%  group_by(agegroup = cut(age, breaks = seq(16,75, by =5)),\n                                   club_member_status, fashion_news_frequency) %>% drop_na(agegroup) %>% summarise(Count =n())\n","metadata":{"execution":{"iopub.status.busy":"2022-03-08T03:58:22.461544Z","iopub.execute_input":"2022-03-08T03:58:22.462928Z","iopub.status.idle":"2022-03-08T03:58:22.898534Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ggplot(aes(x =agegroup ,y = Count, fill = fashion_news_frequency), data = agegroup)+geom_bar(stat = \"identity\") +scale_fill_manual(values = wes_palette(n=3, name=\"Moonrise1\"))\noptions(scipen = 100)\n","metadata":{"execution":{"iopub.status.busy":"2022-03-08T03:58:22.901033Z","iopub.execute_input":"2022-03-08T03:58:22.902531Z","iopub.status.idle":"2022-03-08T03:58:23.209248Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ggplot(aes(x =agegroup ,y = Count, fill = club_member_status), data = agegroup)+geom_bar(stat = \"identity\") +\nscale_fill_manual(values = wes_palette(n=3, name=\"Moonrise1\"))","metadata":{"execution":{"iopub.status.busy":"2022-03-08T03:58:23.211942Z","iopub.execute_input":"2022-03-08T03:58:23.213465Z","iopub.status.idle":"2022-03-08T03:58:24.754853Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### aged 20-30 is the major customers in Most age group activate club members, but mostly don't get fashion news frequently. \n","metadata":{}},{"cell_type":"markdown","source":"### EDA-3. Transaction Tran ","metadata":{}},{"cell_type":"code","source":"dim(transaction_train)","metadata":{"execution":{"iopub.status.busy":"2022-03-08T03:58:24.757572Z","iopub.execute_input":"2022-03-08T03:58:24.759078Z","iopub.status.idle":"2022-03-08T03:58:24.775200Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Let's explore one of sample customer's transaction train data","metadata":{}},{"cell_type":"code","source":"library(lubridate)\n","metadata":{"execution":{"iopub.status.busy":"2022-03-08T03:58:24.777735Z","iopub.execute_input":"2022-03-08T03:58:24.779144Z","iopub.status.idle":"2022-03-08T03:58:24.791793Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#### Customer ID =  00007d2de826758b65a93dd24ce629ed66842531df6699338c5570910a014cc2\t\t\n\n\ncustomer_item <- transaction_train %>% select(t_dat, customer_id, article_id)\n","metadata":{"execution":{"iopub.status.busy":"2022-03-08T03:58:24.794247Z","iopub.execute_input":"2022-03-08T03:58:24.795674Z","iopub.status.idle":"2022-03-08T03:58:24.812546Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"head(customer_item)","metadata":{"execution":{"iopub.status.busy":"2022-03-08T03:59:24.688257Z","iopub.execute_input":"2022-03-08T03:59:24.689962Z","iopub.status.idle":"2022-03-08T03:59:24.716171Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customer_1 <- customer_item %>% filter(customer_id == \"00007d2de826758b65a93dd24ce629ed66842531df6699338c5570910a014cc2\") %>% group_by(Date = floor_date(t_dat, \"month\"), article_id)  %>% summarise(Count = n())\n","metadata":{"execution":{"iopub.status.busy":"2022-03-08T03:59:08.318795Z","iopub.execute_input":"2022-03-08T03:59:08.321534Z","iopub.status.idle":"2022-03-08T03:59:10.225802Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"head(customer_1)\n","metadata":{"execution":{"iopub.status.busy":"2022-03-08T03:59:32.458154Z","iopub.execute_input":"2022-03-08T03:59:32.460107Z","iopub.status.idle":"2022-03-08T03:59:32.487955Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Customer ID =  00007d2de826758b65a93dd24ce629ed66842531df6699338c5570910a014cc2\tbought total 110 items from Sep 2018 to 2020 May.\n\n### Article type ","metadata":{}},{"cell_type":"code","source":"\narticletype <- article %>% select(article_id, prod_name, product_type_name, product_group_name, index_name, colour_group_name) \n\ncustomer_1_article <- merge(x =customer_1, y= articletype, by= \"article_id\") %>% arrange((Date))\n\nhead(customer_1_article)\n","metadata":{"execution":{"iopub.status.busy":"2022-03-08T03:59:45.728816Z","iopub.execute_input":"2022-03-08T03:59:45.730794Z","iopub.status.idle":"2022-03-08T03:59:45.784688Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"unique(customer_1_article$colour_group_name)","metadata":{"execution":{"iopub.status.busy":"2022-03-08T03:59:56.994657Z","iopub.execute_input":"2022-03-08T03:59:56.996549Z","iopub.status.idle":"2022-03-08T03:59:57.016743Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"unique(customer_1_article$index_name)\n","metadata":{"execution":{"iopub.status.busy":"2022-03-08T04:00:09.344707Z","iopub.execute_input":"2022-03-08T04:00:09.346528Z","iopub.status.idle":"2022-03-08T04:00:09.365440Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"unique(customer_1_article$product_group_name)\n","metadata":{"execution":{"iopub.status.busy":"2022-03-08T04:00:16.750594Z","iopub.execute_input":"2022-03-08T04:00:16.752618Z","iopub.status.idle":"2022-03-08T04:00:16.923260Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"unique(customer_1_article$colour_group_name)\n","metadata":{"execution":{"iopub.status.busy":"2022-03-08T04:00:24.402735Z","iopub.execute_input":"2022-03-08T04:00:24.404332Z","iopub.status.idle":"2022-03-08T04:00:24.420683Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Explore customer's favorite color ","metadata":{}},{"cell_type":"code","source":"\nfreq_color_1 <- customer_1_article %>% group_by(color = colour_group_name) %>% summarise(Count = n())\n\nhead(freq_color_1)","metadata":{"execution":{"iopub.status.busy":"2022-03-08T04:01:05.843065Z","iopub.execute_input":"2022-03-08T04:01:05.844804Z","iopub.status.idle":"2022-03-08T04:01:05.881461Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ggplot(aes(x= color, y= Count), data =freq_color_1) +geom_bar(stat = \"identity\", fill= \"orange\")\n","metadata":{"execution":{"iopub.status.busy":"2022-03-08T04:01:14.878873Z","iopub.execute_input":"2022-03-08T04:01:14.880543Z","iopub.status.idle":"2022-03-08T04:01:15.097472Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### most frequent color that customer A purchase is Black, following white, lightpink\n","metadata":{}},{"cell_type":"markdown","source":"#### Let's see what type of cloth item that customer A purchase","metadata":{}},{"cell_type":"code","source":"\nfreq_item_1 <- customer_1_article %>% group_by(group = product_group_name) %>% summarise(Count = n()) \n\nhead(freq_item_1)\n","metadata":{"execution":{"iopub.status.busy":"2022-03-08T04:04:10.893142Z","iopub.execute_input":"2022-03-08T04:04:10.895425Z","iopub.status.idle":"2022-03-08T04:04:10.933822Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ggplot(aes(x= group, y= Count), data =freq_item_1) +geom_bar(stat = \"identity\", fill = \"orange\")\n","metadata":{"execution":{"iopub.status.busy":"2022-03-08T04:04:21.923759Z","iopub.execute_input":"2022-03-08T04:04:21.925568Z","iopub.status.idle":"2022-03-08T04:04:22.313883Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"freq_producttype_1 <- customer_1_article %>% group_by(type = product_type_name) %>% summarise(Count = n())%>% arrange(desc(Count))\n\n\nhead(freq_producttype_1)","metadata":{"execution":{"iopub.status.busy":"2022-03-08T04:04:45.765792Z","iopub.execute_input":"2022-03-08T04:04:45.767517Z","iopub.status.idle":"2022-03-08T04:04:45.803716Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ggplot(data = freq_producttype_1, aes(x=type, y= Count)) +geom_bar(stat =\"identity\", fill= \"orange\")\n","metadata":{"execution":{"iopub.status.busy":"2022-03-08T04:04:58.798114Z","iopub.execute_input":"2022-03-08T04:04:58.799838Z","iopub.status.idle":"2022-03-08T04:04:59.014695Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### top items that customer 1 bought is underwear bottom and bra, following blouse and skirt. \n","metadata":{}},{"cell_type":"markdown","source":"### Buying pattern \n","metadata":{}},{"cell_type":"code","source":"customer_1_article\n","metadata":{"execution":{"iopub.status.busy":"2022-03-08T04:05:50.444740Z","iopub.execute_input":"2022-03-08T04:05:50.446805Z","iopub.status.idle":"2022-03-08T04:05:50.698316Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nfreq_date_1 <- customer_1_article %>% group_by(Date) %>% summarise(Count =sum(Count))\n\nfreq_date_1","metadata":{"execution":{"iopub.status.busy":"2022-03-08T04:06:07.793781Z","iopub.execute_input":"2022-03-08T04:06:07.795511Z","iopub.status.idle":"2022-03-08T04:06:07.825483Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ggplot(aes(x= Date, y= Count), data = freq_date_1) +geom_point()+geom_line()\n","metadata":{"execution":{"iopub.status.busy":"2022-03-08T04:06:19.215834Z","iopub.execute_input":"2022-03-08T04:06:19.217621Z","iopub.status.idle":"2022-03-08T04:06:19.433719Z"},"trusted":true},"execution_count":null,"outputs":[]}]}