{"cells":[{"metadata":{"_uuid":"6159aa8d3dca32a938e005722ac6a7130b000512","_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":"92ab4d49eb3de822a04436122fad47ee73f2f613"},"cell_type":"code","source":"library(readr)\nlibrary(data.table)\nlibrary(datasets)\nlibrary(dplyr)\nlibrary(lubridate)\n# library(reshape)\nlibrary(ggplot2)\nlibrary(ggthemes)\nlibrary(plotly)\nlibrary(Amelia)\nlibrary(caTools)\nlibrary(class)\nlibrary(scales)\nlibrary(psych)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"82cdf8f6cffc78f9f88451c3d280320a5d2fb609"},"cell_type":"code","source":"item_categories <- read_csv(\"../input/item_categories.csv\")\nitems <- read_csv(\"../input/items.csv\")\nshops <- read_csv(\"../input/shops.csv\")\nsales_train <- read_csv(\"../input/sales_train.csv\")\ntest <- read_csv(\"../input/test.csv\")\nsample_submission <- read_csv(\"../input/sample_submission.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"36f02b6e33fe7f878bafdfc14b4b92289927e722"},"cell_type":"code","source":"sapply(item_categories, class)\nsapply(items, class)\nsapply(shops, class)\nsapply(sales_train, class)\nsapply(test, class)\nsapply(sample_submission, class)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e49eb1540015af7ccd70ae0f4eb6401e5912fc1a"},"cell_type":"code","source":"head(sales_train)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f25d6354a08e822f1e8785332c8a431600a74328"},"cell_type":"code","source":"dim(sales_train)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1fafb6da3e11618f9569d7270c7a9cb2e1af1432"},"cell_type":"code","source":"dim(test)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2189f947b0b8de6a98ffc5edba4fa05508b8379d"},"cell_type":"code","source":"sales_train$date <- gsub(\"[.]\", \"/\", sales_train$date)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"305e24694a62f5eec5fc7747a6d7af6505d86e48"},"cell_type":"code","source":"sales_train$date <- as.Date(sales_train$date, \"%d/%m/%Y\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"030187a4b3df7c09b5ef0cc413e6ec5c070fe543"},"cell_type":"code","source":"sales_train","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"32c34a4e39c28b0077ce5255f714427e3b8171d2"},"cell_type":"code","source":"sales_train = merge(sales_train, items[,c(\"item_id\", \"item_category_id\")], by = \"item_id\", all.x = T)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1251bc578b0e308c53e4aa31c88202c8b3397e07"},"cell_type":"code","source":"month_sales_train<-sales_train %>%\n  group_by(date_block_num, shop_id,item_id,item_category_id,item_price) %>%\n  summarise(month_sale=sum(item_cnt_day))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"bb0ffc3422713219dbc6527396fb6af67e5d1eb7"},"cell_type":"code","source":"item_prices<-sales_train %>%\n  group_by(item_id,item_price)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b99f24f642861e513bb816b0b305cc9cfd54e05c"},"cell_type":"code","source":"item_prices$date<-NULL\nitem_prices$date_block_num<-NULL\nitem_prices$item_cnt_day<-NULL\nitem_prices$item_category_id<-NULL","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"638f8191c6a13d634061d24b3e6b2c4aa843e895"},"cell_type":"code","source":"item_prices<-data.frame(item_prices)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2d3b2ffb2e8912c1f52d543b9aac93a53b833f9f"},"cell_type":"code","source":"item_prices<-unique(item_prices)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ed48cf629ed26705ca06fb12c4a43a8893a3aed5"},"cell_type":"code","source":"item_prices","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"19149b91661ab4a46c609f1d74ad799dee9ab4d6"},"cell_type":"code","source":"month_sales_train","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d3ce5802fb787ba4a6f5703b26b1cf3ea80d686d"},"cell_type":"code","source":"dim(month_sales_train) #      2935849 6 ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ea4b5ef2cb057992b2f3813d81b536bc362890af"},"cell_type":"code","source":"glimpse(month_sales_train)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"40e8e38a8ecd2291fc8788a266d4a00961a5741c"},"cell_type":"code","source":"str(month_sales_train)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3443bc8037ac8006da2a515becfe6ff84667a942"},"cell_type":"code","source":"summary(month_sales_train)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f585067b3c0365993c6bf94548702e889f2ac62f"},"cell_type":"code","source":"describe(month_sales_train)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"84d7c13f08023127f0c7a2ed46d10add741716f2"},"cell_type":"code","source":"cor(month_sales_train)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"dbc28541d846c863f88556b66b3324cad51538a9"},"cell_type":"code","source":"library(corrplot)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5e9649f643ded748b3fa182040b2f1be3fed22c3"},"cell_type":"code","source":"my_data<-cor(month_sales_train)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"bdfaaece1624f4a8636623d51900b4877c916d3f"},"cell_type":"code","source":"corrplot(my_data,type = \"lower\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d06678b780a61a8d39299c1ea1310213466ea830"},"cell_type":"code","source":"corrplot(my_data,method = \"number\",type = \"lower\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ad5f2b6bf40f8bb2e700374cc20a4be6607c2437"},"cell_type":"code","source":"test = merge(test, items[,c(\"item_id\", \"item_category_id\")], by = \"item_id\", all.x = T)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ed90a00879a87bd57ba4639f728e0763e05fa1cb"},"cell_type":"code","source":"dim(test)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"bc268e78bf89518506737549af827a1f77d02702"},"cell_type":"code","source":"test = merge(test, item_prices[,c(\"shop_id\",\"item_id\", \"item_price\")], by.x = c(\"shop_id\", \"item_id\"),by.y = c(\"shop_id\", \"item_id\"), all.x = T)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1d0b511eddd6975c206c54426746a833ed61b878"},"cell_type":"code","source":"dim(test)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ccec1de2a5f912b9b82a7ba9d7d4916e4a051772"},"cell_type":"code","source":"new_test<-test","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"57271f714a54d0dcf9d023fd290fd1fbe89e94a0"},"cell_type":"code","source":"new_test<-new_test[order(test$ID),c(1,2,3,4,5)]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a50a1dcbe3595888173c4d99bf41393be38cbd4a"},"cell_type":"code","source":"my_test_1<-new_test %>%\n  group_by(ID,shop_id,item_id,item_category_id) %>%\n  summarise(item_price=mean(item_price))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b76dfc5cd2189f79a6e47973958895df03ebb32b"},"cell_type":"code","source":"my_test_1","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e80e710ab47c77f003c58912192ede13eba86197"},"cell_type":"code","source":"dim(my_test_1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2ec1c575b51f114785510b9ae80290e38ce71a20"},"cell_type":"code","source":"my_test_2<-my_test_1","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"caced6bc9799ddc5401aaa8444d7781a7de9fa00"},"cell_type":"code","source":"dim(my_test_2)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"49f24f3c6259ee11d6c6d2def02e5824ab88660a"},"cell_type":"code","source":"my_test_2$ID<-34","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0442ce4d0efa6c741ff326981a48a3572ca5d627"},"cell_type":"code","source":"my_test_2","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0374ef629d6b8009f3de262f6aa5f60e6b80026b"},"cell_type":"code","source":"u = mean(my_test_2$item_price, na.rm = TRUE)  ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a0c28e127fe0d0518f7682e4d95d7727e55c4f94"},"cell_type":"code","source":"my_test_2[is.na(my_test_2)] <- u","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9d7b4f2c887781b9a5054b0fc809b25cb12ba736"},"cell_type":"code","source":"linear_model = lm(formula = month_sale ~ shop_id + item_id+item_category_id+item_price,\n                  data = month_sales_train) ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"45fd46026ae0103e0333d633dcbe4293d5c6b005"},"cell_type":"code","source":"result = predict(linear_model, my_test_2[,c(\"ID\", \"shop_id\",\"item_id\", \"item_category_id\", \"item_price\")]) ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8d3b3c340d66ba8dea17f4c05cb57a07acae5ee7"},"cell_type":"code","source":"result_1=predict(linear_model, month_sales_train[,c(\"date_block_num\", \"shop_id\",\"item_id\", \"item_category_id\", \"item_price\")])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2b023e5046496f310e8634dfa8fe855259d9354e"},"cell_type":"code","source":"rss <- sum((result_1 - month_sales_train$month_sale) ^ 2)  ## residual sum of squares\ntss <- sum((month_sales_train$month_sale - mean(month_sales_train$month_sale)) ^ 2)  ## total sum of squares\nrsq <- 1 - rss/tss\nrsq","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1b24915467a6bb65b366725366fa35ff587f4281"},"cell_type":"code","source":"submission =  data.frame(ID = my_test_1$ID,\n                         item_cnt_month = result)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d2174b236cc55d4383c981b09a9a6a8dac2ca37b"},"cell_type":"code","source":"head(submission)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d06c542662651ca6fce56d0437705d7ff2655553"},"cell_type":"code","source":"write.csv(submission, file = \"sub1.csv\", row.names = F)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1eb63a59471f1e0c376db37eb5a7327b8e789ff2"},"cell_type":"code","source":"# GBM Model\nlibrary(gbm) \nstart =  Sys.time()\ngbm_model  =  gbm(month_sale ~ shop_id + item_id+item_category_id+item_price,\n                  data = month_sales_train,\n                  shrinkage = 0.01,\n                  distribution = \"gaussian\",\n                  n.trees = 5000,\n                  interaction.depth = 3, \n                  bag.fraction = 0.7,\n                  train.fraction = 0.8,\n                  # cv.folds = 5,\n                  n.cores = NULL,\n                  verbose = T)\n\nend = Sys.time()\nprint(end - start)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b5f352693fe0bb570c3acc4a262278d9db8bf7af"},"cell_type":"code","source":"result2 = predict(gbm_model,newdata = my_test_2[,c(\"ID\", \"shop_id\",\"item_id\", \"item_category_id\", \"item_price\")], n.trees = 5000 )","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ff275389073a3406fece6d60dac4cd74684b071f"},"cell_type":"code","source":"sub2 = data.frame(ID = my_test_1$ID, \n                  item_cnt_month =  result2)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c0188611e8a9193959a358bb11e8d0e0d6bd0afc"},"cell_type":"code","source":"write.csv(sub2, \"submission.csv\", row.names = F)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f3ada5f9c2ea4a004340f7618115a8813a56ee71"},"cell_type":"code","source":"gbm.perf(gbm_model)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"628952b3ed928f7e77170d501770cb5874eb36fb"},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"R","language":"R","name":"ir"},"language_info":{"mimetype":"text/x-r-source","name":"R","pygments_lexer":"r","version":"3.4.2","file_extension":".r","codemirror_mode":"r"}},"nbformat":4,"nbformat_minor":1}