{"cells":[{"metadata":{"_uuid":"6cb94db85d38910dd673138acd2ed065c7cf1fad","_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":"ae76201f656cd5e46f917ab3009be3d839589164"},"cell_type":"code","source":"library(data.table)\ntrain = fread(\"../input/preprocessing-of-the-v2-datasets/train_v2_clean.csv\", header = TRUE,\n           colClasses=c(fullVisitorId='character'))\ntest = fread(\"../input/preprocessing-of-the-v2-datasets/test_v2_clean.csv\", header = TRUE,\n           colClasses=c(fullVisitorId='character'))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d621dc54809aa7b309835ef330dea631927eb3fe"},"cell_type":"code","source":"print('train:'); dim(train)\nprint('test:'); dim(test)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"84de90845282fb2cc60de11389ad0916a8f73bf9","_kg_hide-output":true},"cell_type":"code","source":"# correct the data types\ndata_num <- train[,c(4,17:23)]\ndata_cat <- train[,-c(4,17:23)]\ndata_cat <- data.frame(apply(data_cat, 2,function(x){as.factor(x)}))\ndata_num <- data.frame(apply(data_num, 2,function(x){as.numeric(x)}))\ntrain <- cbind(data_cat,data_num)\nstr(train)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8d547e4a5db1ceb6554697dec93ea413230a0dbf","_kg_hide-output":true},"cell_type":"code","source":"library(chron)\ntrain$date <- as.Date(as.character(train$date), format= \"%Y-%m-%d\")\ntrain$dayOfMonth <- as.factor(days(train$date))\ntrain$month <- as.factor(month(train$date))\nstr(train)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6d9d238bf30755ad51cd312ded4ff4bf9adffb3b"},"cell_type":"code","source":"head(train)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d7a293bd99313f767ff008c3ac12b9a76e5ae6a4","_kg_hide-output":true},"cell_type":"code","source":"# correct the data types\ndata_num <- test[,c(4,17:23)]\ndata_cat <- test[,-c(4,17:23)]\ndata_cat <- data.frame(apply(data_cat, 2,function(x){as.factor(x)}))\ndata_num <- data.frame(apply(data_num, 2,function(x){as.numeric(x)}))\ntest <- cbind(data_cat,data_num)\nstr(test)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8970abb24716b702ae230863c8bbc5b3a5fa7326","_kg_hide-output":true},"cell_type":"code","source":"test$date <- as.Date(as.character(test$date), format= \"%Y-%m-%d\")\ntest$dayOfMonth <- as.factor(days(test$date))\ntest$month <- as.factor(month(test$date))\nstr(test)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a93d97b8fec1ebe32307deba86044673c486b772"},"cell_type":"code","source":"# Split Data into Train and test - 70:30\nlibrary(caret)\nset.seed(7)\ntrain_test_split<-createDataPartition(train$transactionRevenue, p=0.7, list = FALSE)\ntrain_data<-train[train_test_split,]\ntest_data<-train[-train_test_split,]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"38feaf13751889f00fd3c2633ec817c7c74c67bb"},"cell_type":"code","source":"library(gbm)\nset.seed(7)\n# train GBM model\ngbm.fit <- gbm(\n  formula = transactionRevenue ~ .,\n  distribution = \"gaussian\",\n  data = train_data[,-c(2,3,15,19,20,26)],\n  n.trees = 3500,\n  interaction.depth = 6,\n  shrinkage = 0.001,\n  cv.folds = 3,\n  n.cores = NULL, # will use all cores by default\n  verbose = FALSE\n  )\nprint(gbm.fit)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"297d8e7ba6a8b6cca12822424f00b9a905c167a8"},"cell_type":"code","source":"# Check performance using 5-fold cross-validation\nbest.iter <- gbm.perf(gbm.fit, method = \"cv\")\nprint(best.iter)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"05b556d3666315b44bda0996a3b58c7163bff43e"},"cell_type":"code","source":"# get MSE and compute RMSE\nsqrt(min(gbm.fit$cv.error))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"676daa7a251bb4fcbfd1d232dae58077f2ba0b96"},"cell_type":"code","source":"summary(gbm.fit)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"eacf762bb7fd2765e25e73df676abdf97826ae30"},"cell_type":"code","source":"# predict values for test split\npred <- predict(gbm.fit, n.trees = best.iter, test_data)\n# results\ncaret::RMSE(pred, test_data$transactionRevenue)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f8b324de9a5a16b92356d869b681ab1ac449ba9d"},"cell_type":"code","source":"# predict values for test data\npred <- predict(gbm.fit, n.trees = best.iter, test)\n# results\ncaret::RMSE(pred, test$transactionRevenue)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0f75fb9320cb9e86fcc517b389d3b4ff24196fd8"},"cell_type":"code","source":"test$target <- ifelse(pred<0,0,pred)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5ffa44740f1c035501b68feca4feabbd46e120f0"},"cell_type":"code","source":"tsf_data <- data.frame(fullVisitorId=unique(test$fullVisitorId))\ndim(tsf_data)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"706b2ee0743d14d190082e318b979c6fd6b4a4d2"},"cell_type":"code","source":"temp<-test %>%\n  group_by(fullVisitorId) %>%\n  summarise(PredictedLogRevenue = log(sum(target)+1))\ntsf_data <- merge(tsf_data,temp,by=\"fullVisitorId\",sort = FALSE,all.x=TRUE)\ndim(tsf_data)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e6968a5f411133d6a6510e394d2e86366f71cbf7"},"cell_type":"code","source":"head(tsf_data)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7f3313aee691649395c4ece17ebc2b2564480f46"},"cell_type":"code","source":"submission_v2 <- read_csv('../input/ga-customer-revenue-prediction/sample_submission_v2.csv',col_names=TRUE)\nhead(submission_v2)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f19b083f79344add2ca7f39612eb6fe918766073"},"cell_type":"code","source":"submission_v2<-submission_v2[,-c(2)]\nsubmission<-merge(submission_v2,tsf_data,by=\"fullVisitorId\",sort = FALSE,all.x=TRUE)\nhead(submission)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2c99e0a0453d80a30a7c31a5c383df1bcdf4a738"},"cell_type":"code","source":"# --- write to csv\nwrite.csv(submission, \"submission.csv\",row.names = FALSE)","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}