{"cells":[{"metadata":{"_uuid":"2dda764a5b415839d6b9367ed539356d77c4ab7e","_execution_state":"idle","trusted":true},"cell_type":"code","source":"## Importing packages\n\n# This R environment comes with all of CRAN and many other helpful packages preinstalled.\n# You can see which packages are installed by checking out the kaggle/rstats docker image: \n# https://github.com/kaggle/docker-rstats\n\nlibrary(tidyverse) # metapackage with lots of helpful functions\n\n## Running code\n\n# In a notebook, you can run a single code cell by clicking in the cell and then hitting \n# the blue arrow to the left, or by clicking in the cell and pressing Shift+Enter. In a script, \n# you can run code by highlighting the code you want to run and then clicking the blue arrow\n# at the bottom of this window.\n\n## Reading in files\n\n# You can access files from datasets you've added to this kernel in the \"../input/\" directory.\n# You can see the files added to this kernel by running the code below. \n\nlist.files(path = \"../input\")\n\n## Saving data\n\n# If you save any files or images, these will be put in the \"output\" directory. You \n# can see the output directory by committing and running your kernel (using the \n# Commit & Run button) and then checking out the compiled version of your kernel.","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f8d74066f7faf8165415bef9193b029f70e68c60"},"cell_type":"code","source":"library(data.table)\nlibrary(dplyr)\nlibrary(ggplot2)\nlibrary(knitr)\nlibrary(stringr)\nlibrary(DT)\n\norders <- fread('../input/orders.csv')\nproducts <- fread('../input/products.csv')\norder_products <- fread('../input/order_products__train.csv')\norder_products_prior <- fread('../input/order_products__prior.csv')\naisles <- fread('../input/aisles.csv')\ndepartments <- fread('../input/departments.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6842c13c3606644a6ed86e39dd9237d93cb471ef"},"cell_type":"code","source":"glimpse(orders)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6cdbe95812f5e6c9ff16ae873df4c7290a52131f"},"cell_type":"code","source":"orders <- orders %>% mutate(order_hour_of_day = as.numeric(order_hour_of_day), eval_set = as.factor(eval_set))\nproducts <- products %>% mutate(product_name = as.factor(product_name))\naisles <- aisles %>% mutate(aisle = as.factor(aisle))\ndepartments <- departments %>% mutate(department = as.factor(department))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e5182c575db3c591496e83922b45abe8c6715ad0"},"cell_type":"code","source":"orders %>% \n  ggplot(aes(x=order_hour_of_day)) + \n  geom_histogram(stat=\"count\",fill=\"red\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4066fd20b37ee034ab39798c20de0c3f4335f29e"},"cell_type":"code","source":"orders %>% \n  ggplot(aes(x=order_dow)) + \n  geom_histogram(stat=\"count\",fill=\"red\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d5adf00b3092f23eb38d4ce7843ea58fdcab06c4"},"cell_type":"code","source":"orders %>% \n  ggplot(aes(x=days_since_prior_order)) + \n  geom_histogram(stat=\"count\",fill=\"red\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e544cd6040762d13a6e48a9cdd6eb5357481ff12"},"cell_type":"code","source":"orders %>% filter(eval_set==\"prior\") %>% count(order_number) %>% ggplot(aes(order_number,n)) + geom_line(color=\"red\", size=1)+geom_point(size=2, color=\"red\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4f9e59afdc6985ce1c341aa1a1171afb594e188b"},"cell_type":"code","source":"order_products %>% \n  group_by(order_id) %>% \n  summarize(n_items = last(add_to_cart_order)) %>%\n  ggplot(aes(x=n_items))+\n  geom_histogram(stat=\"count\",fill=\"red\") + \n  geom_rug()+\n  coord_cartesian(xlim=c(0,80))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"85e672fcb2064be9fcf4ee3149a3cbcf28223181"},"cell_type":"code","source":"tmp <- order_products %>% \n  group_by(product_id) %>% \n  summarize(count = n()) %>% \n  top_n(10, wt = count) %>%\n  left_join(select(products,product_id,product_name),by=\"product_id\") %>%\n  arrange(desc(count)) \nkable(tmp)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2632c79977073c41a3c29f8a4e8cb3d22a12d2f7"},"cell_type":"code","source":"tmp %>% \n  ggplot(aes(x=reorder(product_name,-count), y=count))+\n  geom_bar(stat=\"identity\",fill=\"red\")+\n  theme(axis.text.x=element_text(angle=90, hjust=1),axis.title.x = element_blank())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a9b1eb97702ea19d7b50d27ec5f9f2e92e97d54b"},"cell_type":"code","source":"tmp <- order_products %>% \n  group_by(reordered) %>% \n  summarize(count = n()) %>% \n  mutate(reordered = as.factor(reordered)) %>%\n  mutate(proportion = count/sum(count))\nkable(tmp)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4145ee5654a6845e8c1aff0d031cedd6c8bc96f6"},"cell_type":"code","source":"tmp %>% \n  ggplot(aes(x=reordered,y=count,fill=reordered))+\n  geom_bar(stat=\"identity\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"77d6173944e232eb91591cea29536e4d5a85970b"},"cell_type":"code","source":"tmp <-order_products %>% \n  group_by(product_id) %>% \n  summarize(proportion_reordered = mean(reordered), n=n()) %>% \n  filter(n>40) %>% \n  top_n(10,wt=proportion_reordered) %>% \n  arrange(desc(proportion_reordered)) %>% \n  left_join(products,by=\"product_id\")\n\nkable(tmp)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b6c369e1bb1adb8dfedeaf0f8773ea47aaf72beb"},"cell_type":"code","source":"tmp %>% \n  ggplot(aes(x=reorder(product_name,-proportion_reordered), y=proportion_reordered))+\n  geom_bar(stat=\"identity\",fill=\"red\")+\n  theme(axis.text.x=element_text(angle=90, hjust=1),axis.title.x = element_blank())+coord_cartesian(ylim=c(0.85,0.95))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"cdd2aa14d5e34257813db9779fb8534652990f79"},"cell_type":"code","source":"tmp <- order_products %>% \n  group_by(product_id, add_to_cart_order) %>% \n  summarize(count = n()) %>% mutate(pct=count/sum(count)) %>% \n  filter(add_to_cart_order == 1, count>10) %>% \n  arrange(desc(pct)) %>% \n  left_join(products,by=\"product_id\") %>% \n  select(product_name, pct, count) %>% \n  ungroup() %>% \n  top_n(10, wt=pct)\n\nkable(tmp)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4f8d5491bcdd56019c78bf9846f0ae79d4ff28ff"},"cell_type":"code","source":"tmp %>% \n  ggplot(aes(x=reorder(product_name,-pct), y=pct))+\n  geom_bar(stat=\"identity\",fill=\"red\")+\n  theme(axis.text.x=element_text(angle=90, hjust=1),axis.title.x = element_blank())+coord_cartesian(ylim=c(0.4,0.7))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1d74c4191a9e2d971c3c990cf331a2ca458fbb65"},"cell_type":"code","source":"order_products %>% \n  left_join(orders,by=\"order_id\") %>% \n  group_by(days_since_prior_order) %>%\n  summarize(mean_reorder = mean(reordered)) %>%\n  ggplot(aes(x=days_since_prior_order,y=mean_reorder))+\n  geom_bar(stat=\"identity\",fill=\"red\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"092952615af378d16850d26409c27d9dd434a577"},"cell_type":"code","source":"order_products %>% \n  group_by(product_id) %>% \n  summarize(proportion_reordered = mean(reordered), n=n()) %>%\n  ggplot(aes(x=n,y=proportion_reordered))+\n  geom_point()+\n  geom_smooth(color=\"red\")+\n  coord_cartesian(xlim=c(0,2000))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a12d69e1c5bb622ce8a3ace30a860a32607804cf"},"cell_type":"code","source":"products <- products %>% \n    mutate(organic=ifelse(str_detect(str_to_lower(products$product_name),'organic'),\"organic\",\"not organic\"), organic= as.factor(organic))\n    \ntmp <- order_products %>% \n  left_join(products, by=\"product_id\") %>% \n  group_by(organic) %>% \n  summarize(count = n()) %>% \n  mutate(proportion = count/sum(count))\nkable(tmp)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"06a34771bba8b990d95bbe58967474d5be0e1c34"},"cell_type":"code","source":"tmp %>% \n  ggplot(aes(x=organic,y=count, fill=organic))+\n  geom_bar(stat=\"identity\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0190e0d4cda81af4a790b57802705eaed620dec8"},"cell_type":"code","source":"tmp <- order_products %>% left_join(products,by=\"product_id\") %>% group_by(organic) %>% summarize(mean_reordered = mean(reordered))\nkable(tmp)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a8a3a57547ec8faec454a955049ccb23625ae7e2"},"cell_type":"code","source":"tmp %>% \n  ggplot(aes(x=organic,fill=organic,y=mean_reordered))+geom_bar(stat=\"identity\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"52d50241419042e669212f11f069d1dcc0e1fa70"},"cell_type":"code","source":"library(treemap)\n\ntmp <- products %>% group_by(department_id, aisle_id) %>% summarize(n=n())\ntmp <- tmp %>% left_join(departments,by=\"department_id\")\ntmp <- tmp %>% left_join(aisles,by=\"aisle_id\")\n\ntmp2<-order_products %>% \n  group_by(product_id) %>% \n  summarize(count=n()) %>% \n  left_join(products,by=\"product_id\") %>% \n  ungroup() %>% \n  group_by(department_id,aisle_id) %>% \n  summarize(sumcount = sum(count)) %>% \n  left_join(tmp, by = c(\"department_id\", \"aisle_id\")) %>% \n  mutate(onesize = 1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6ba9e03287fc2585c38f83b50087c70ae0a4fef6"},"cell_type":"code","source":"treemap(tmp2,index=c(\"department\",\"aisle\"),vSize=\"onesize\",vColor=\"department\",palette=\"Set3\",title=\"\",sortID=\"-sumcount\", border.col=\"#FFFFFF\",type=\"categorical\", fontsize.legend = 0,bg.labels = \"#FFFFFF\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"bc4dcb5dc11173a69a673783494a6ffcdf649ecf"},"cell_type":"code","source":"treemap(tmp,index=c(\"department\",\"aisle\"),vSize=\"n\",title=\"\",palette=\"Set3\",border.col=\"#FFFFFF\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9fea81005faca2572559ea8aaa1f2b407066a618"},"cell_type":"code","source":"treemap(tmp2,index=c(\"department\",\"aisle\"),vSize=\"sumcount\",title=\"\",palette=\"Set3\",border.col=\"#FFFFFF\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1c7cdef3d2fceb156b735a019f4277ad05dc8174"},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"R","language":"R","name":"ir"},"language_info":{"name":"python","version":"3.6.4","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}