{"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/fork-of-building-the-model/train_agg.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":"ad8880d94ee2d080a9f60bbded702b9fb48e37f2"},"cell_type":"code","source":"str(train)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"84de90845282fb2cc60de11389ad0916a8f73bf9","_kg_hide-output":true},"cell_type":"code","source":"# correct the data types\ndata_cat <- train[,c(1:21,30,31)]\ndata_num <- train[,-c(1:21,30,31)]\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":"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":"library(chron)\ntest$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":"fdd83fcadea6966d4506553bbdb560d58555b475"},"cell_type":"code","source":"#levels(train$browser) <- c(levels(train$browser),\n#                          setdiff(levels(test$browser),levels(train$browser)))\n#levels(train$operatingSystem) <- c(levels(train$operatingSystem),\n#                          setdiff(levels(test$operatingSystem),levels(train$operatingSystem)))\n#levels(train$country) <- c(levels(train$country),\n#                          setdiff(levels(test$country),levels(train$country)))\n#levels(train$region) <- c(levels(train$region),\n#                          setdiff(levels(test$region),levels(train$region)))\n#levels(train$metro) <- c(levels(train$metro),\n#                          setdiff(levels(test$metro),levels(train$metro)))\n#levels(train$city) <- c(levels(train$city),\n#                          setdiff(levels(test$city),levels(train$city)))\n#levels(train$campaign) <- c(levels(train$campaign),\n#                          setdiff(levels(test$campaign),levels(train$campaign)))\n#levels(train$source) <- c(levels(train$source),\n#                          setdiff(levels(test$source),levels(train$source)))\n#levels(train$adContent) <- c(levels(train$adContent),\n#                          setdiff(levels(test$adContent),levels(train$adContent)))\n#levels(train$adwordsClickInfo.page) <- c(levels(train$adwordsClickInfo.page),\n#                          setdiff(levels(test$adwordsClickInfo.page),levels(train$adwordsClickInfo.page)))\n#levels(train$adwordsClickInfo.slot) <- c(levels(train$adwordsClickInfo.slot),\n#                          setdiff(levels(test$adwordsClickInfo.slot),levels(train$adwordsClickInfo.slot)))","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":"4f905050710465fa54781493cba130cad0599112"},"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(1)],\n  n.trees = 3500,\n  interaction.depth = 5,\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":"8da4a88974dfdbccaf7dc96f4acf1e519e85e705"},"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":"0efe05bb57d894e0a7b547071e02e35e27aeb4f9"},"cell_type":"code","source":"# get MSE and compute RMSE\nsqrt(min(gbm.fit$cv.error))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9eeec3114c0a7303b97cad083c8898b4fe7bcf3c"},"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}