{"cells":[{"metadata":{"_execution_state":"idle","_uuid":"d87ee7f52ef3cdc72c973ede882f5a973a8ff26d","_cell_guid":"a07b17a7-4215-4a31-8263-89cbda180862"},"cell_type":"markdown","source":"# Reading data\n\nThe starting point is to create a simple model using only the training sample provided:"},{"metadata":{"_uuid":"5a94da321915f57cb9852c5f3280a780febe1c1c","trusted":false,"_cell_guid":"0cc24d98-028f-4592-bf76-56229c0dff1c"},"cell_type":"code","source":"train <- read.csv(\"../input/train_sample.csv\")\ntrain$is_attributed <- as.factor(train$is_attributed)\nlevels(train$is_attributed) <- c(\"zeros\", \"ones\")\ntrain$click_time <- as.POSIXct(as.character(train$click_time))","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"4655778dcd0af1f4ac62929ee2efdf115955d0ac","_cell_guid":"3fa63b09-c045-4e37-8ff2-fd67d23065ea"},"cell_type":"markdown","source":"# Extracting time informations\n\nSince there was the feature **click_time** completely ignored in the baseline model, the first step could be to extract some information out of it. Using **lubridate**:"},{"metadata":{"_uuid":"4a5bd8f3d5d18fd2d9ad02312f12e14c0c71a5f0","trusted":false,"_cell_guid":"9d540e15-e1d7-48d5-8bf6-fe25f32cfece"},"cell_type":"code","source":"library(dplyr)\nlibrary(lubridate)\n\ntrain <- train %>% \nmutate(day = day(click_time),\n       hour = hour(click_time),\n       minute = minute(click_time),\n       time_since = as.integer(click_time - as.POSIXct(\"2017-11-07 00:00:00\")))\nhead(train)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"dc9469f88cc55d474e987c9325d3228ef29ee56a","_cell_guid":"5dc70981-46ac-4293-851b-5a518ca7841b"},"cell_type":"markdown","source":"# A simple model - **Rpart**\n\nExcluding the **attributed_time** and optimizing against the area under the curve using the **caret** package:"},{"metadata":{"_uuid":"f20f3a09de663a7907ec68d67dfe50d63e35a685","_cell_guid":"e8eed627-7b77-4907-8274-370e84258903"},"cell_type":"markdown","source":""},{"metadata":{"_uuid":"5871b9b118bc8c8fce940f7ed154858d92769c31","trusted":false,"_cell_guid":"7dcd0aba-fdcc-436a-bf45-fe1d27672dc7"},"cell_type":"code","source":"library(caret)\nlibrary(dplyr)\nlibrary(parallel)\nlibrary(doParallel)\n\ncores <- detectCores()\nregisterDoParallel(cores = cores)\n\nctrl <- trainControl(method = \"cv\", \n                     number = 20, \n                     selectionFunction = \"best\",\n                     allowParallel = TRUE,\n                     summaryFunction = twoClassSummary,\n                     classProbs = TRUE\n                    ) \nfit.rpart <- train(is_attributed ~ ., \n                   data = train %>% select(-click_time, -attributed_time), \n                   method = \"rpart\", \n                   trControl = ctrl,\n                   metric = \"ROC\")\nregisterDoSEQ()\nprint(fit.rpart)\nplot(fit.rpart)\nplot(varImp(fit.rpart))","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"98890beb3899c1746b83155cb5566cfbd0d514a9","_cell_guid":"33128334-97ac-451f-8ab3-5364ef6b1962"},"cell_type":"markdown","source":"# Fine tuning the model\n\nExploring a larger set of values for the complexity parameter **cp**"},{"metadata":{"_uuid":"52d64b3798c54240f973561738446696b0d29ce7","trusted":false,"_cell_guid":"68e5a3ed-4fa8-4829-a00d-ee99fe2f1ea1"},"cell_type":"code","source":"cores <- detectCores()\nregisterDoParallel(cores = cores)\n\nctrl <- trainControl(method = \"cv\", \n                     number = 20, \n                     selectionFunction = \"best\",\n                     allowParallel = TRUE,\n                     summaryFunction = twoClassSummary,\n                     classProbs = TRUE\n                    ) \ngrid <- expand.grid(cp = 10^seq(-10, -2, by = 1))\nfit.rpart <- train(is_attributed ~ ., \n                   data = train %>% select(-click_time, -attributed_time), \n                   method = \"rpart\", \n                   trControl = ctrl,\n                   metric = \"ROC\",\n                   tuneGrid = grid)\nregisterDoSEQ()\nprint(fit.rpart)\nplot(fit.rpart)\nplot(varImp(fit.rpart))","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"a3c7217fa3bd84cb0e48558a8ef7f7408bcdceb8","_cell_guid":"c9a208c3-a09f-4195-921a-8782a9b3984d"},"cell_type":"markdown","source":"Finally:"},{"metadata":{"_uuid":"b66ef826ff4827a38b5af80d370557121b87d08c","trusted":false,"_cell_guid":"cc83042e-da4a-45e9-98c4-a6723d819947"},"cell_type":"code","source":"cores <- detectCores()\nregisterDoParallel(cores = cores)\n\nctrl <- trainControl(method = \"cv\", \n                     number = 20, \n                     selectionFunction = \"best\",\n                     allowParallel = TRUE,\n                     summaryFunction = twoClassSummary,\n                     classProbs = TRUE\n                    ) \ngrid <- expand.grid(cp = c(0.00005, 0.000075, 0.00015, 0.00020, 0.00025, 0.00030, 0.00035))\nfit.rpart <- train(is_attributed ~ ., \n                   data = train %>% select(-click_time, -attributed_time), \n                   method = \"rpart\", \n                   trControl = ctrl,\n                   metric = \"ROC\",\n                   tuneGrid = grid)\nregisterDoSEQ()\nprint(fit.rpart)\nplot(fit.rpart)\nplot(varImp(fit.rpart))","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"f0b0e8bff8249a865620d5f296192b839a2ab1b2","_cell_guid":"7065f834-236b-4e97-b859-bd29977c1f65"},"cell_type":"markdown","source":"# Xgboost\n\nChanging classifier:"},{"metadata":{"_uuid":"c8ec325745b2d62f9b2e35384fee08698d421b7e","trusted":true,"_cell_guid":"09cfbe4e-570d-4fd6-9b55-a06913839893"},"cell_type":"code","source":"cores <- detectCores()\nregisterDoParallel(cores = cores)\n\nctrl <- trainControl(method = \"cv\", \n                     number = 20, \n                     selectionFunction = \"best\",\n                     allowParallel = TRUE,\n                     summaryFunction = twoClassSummary,\n                     classProbs = TRUE\n                    ) \ngrid <- expand.grid(eta = 0.1, \n                    max_depth = 3, \n                    gamma = 0,\n                    nrounds = 400, \n                    colsample_bytree = 0.8,\n                    min_child_weight = 0,\n                    subsample = 1.0)\nfit.xgb <- train(is_attributed ~ ., \n                 data = train %>% select(-click_time, -attributed_time), \n                 method = \"xgbTree\", \n                 trControl = ctrl,\n                 metric = \"ROC\",\n                 tuneGrid = grid)\nregisterDoSEQ()\nprint(fit.xgb)\nplot(varImp(fit.xgb))","execution_count":3,"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}