{"cells":[{"metadata":{"_uuid":"2001fdf7d3e68128d71ff7f685a03dad920ab112"},"cell_type":"markdown","source":"**Introduction**\n\nUse train and test to emulate the version 2 situation. We estimate that the objective funtion will be calculated over only 2000-3000 "},{"metadata":{"trusted":true,"_uuid":"460b1916f27758f36367f060695ddecbb238e5c4"},"cell_type":"code","source":"# Importing packages silently\nsuppressPackageStartupMessages({\n    # Data Manipulation: on data. -frames ( or -tables) because standard R is too slow\n    library(tidyverse) # I don't like it, but...\n    library(data.table) # I don't like it either, but...\n    library(splitstackshape) # obscure packge to reshape nested frames\n    library(lubridate) # Because standard date manipulation is never good enough\n\n    # JSON from Jeroen Ooms\n    library(jsonlite)\n\n    # ML from everywhere\n    library(xgboost)\n    library(lightgbm)\n    library(catboost)\n})\n## -----------------------------------------------------\n## options - to prevent some unwanted side effects\noptions(stringsAsFactors= FALSE)\noptions(digits= 4)\n\nuntibble <- function(tibble) {\n  if(\"tbl\" %in%  class(tibble)) {\n    return(as.data.frame(unclass(tibble)))\n  } else return(tibble)\n}\n\nfast_csv <- function(...){\n    require(readr)\n    tib <- suppressMessages(read_csv(...))\n    return(untibble(tib))\n}\n\nrmse <- Metrics::rmse","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"dfc50041cfc198f2c3736cfc379957a17b34f181"},"cell_type":"code","source":"# make a data-set like Olivier's \n\nn.time <- Sys.time()\ncat(\"\\n### Start...\")\n\ntrain <- fast_csv(\"../input/train.csv\")\ntest <- fast_csv(\"../input/test.csv\")\nboth <- rbind(train, test)\nboth <- cbind(id=1: nrow(both), both)\nl.train <- nrow(train)\n\nb_device <- paste(\"[\", paste(both$device, collapse = \",\"), \"]\") %>% fromJSON(flatten = TRUE)\nb_geoNetwork <- paste(\"[\", paste(both$geoNetwork, collapse = \",\"), \"]\") %>% fromJSON(flatten = TRUE)\nb_totals <- paste(\"[\", paste(both$totals, collapse = \",\"), \"]\") %>% fromJSON(flatten = TRUE)\nb_trafficSource <- paste(\"[\", paste(both$trafficSource, collapse = \",\"), \"]\") %>% fromJSON(flatten = TRUE)\nboth <- both %>%\n  cbind(b_device, b_geoNetwork, b_totals, b_trafficSource) %>%\n  select(-device, -geoNetwork, -totals, -trafficSource)\nrm(train); rm(test); rm(b_device); rm(b_geoNetwork); rm(b_totals); rm(b_trafficSource)\ncat(\"\\n### JSON conversion in...\", Sys.time() - n.time)\n# --------------------------------------","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c7347d4e593c2dd4d88798028fccf66e011452eb"},"cell_type":"code","source":"# delete some columns\nsetDT(both)\nboth[, campaignCode := NULL]\nboth[is.na(both$adwordsClickInfo.isVideoAd), \n     adwordsClickInfo.isVideoAd := FALSE]\nfeats<- copy(names(both)) ## very nasty:  for (nm in names(both)) does not work\nfor (nm in feats){\n  v <-length(unique(both[[nm]]))\n  if (v == 1) both[, (nm):= NULL]\n}\n# Columns that Olivier ddidnt use:\nfeats <- c(\"adwordsClickInfo.page\", \"adwordsClickInfo.slot\"\n           , \"adwordsClickInfo.gclId\", \"adwordsClickInfo.adNetworkType\")\nfor (nm in feats) both[, (nm):= NULL]\n#-----------------------------------------------------------\ncat(\"\\n### Dimensions of data ...\", dim(both)) # should be 31","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"14bd3f12cd4d07387236ad861476675c24ca831c"},"cell_type":"code","source":"# Z: last 2 months\n# X: before 3.5 months\n\nZ<- both$date >= 20180301\nX<- both$date <= 20180115\nte_2 <- both[Z,]\ntr <- both[X, ]\nte_1 <- tr[-(1:l.train), ]\ntr_0 <- tr[1:l.train, ]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"scrolled":true,"_uuid":"2eac406076dc3dade0d72337b24e0a884173d59b"},"cell_type":"code","source":"u1<- unique(te_1$fullVisitorId) \nu2<- unique(te_2$fullVisitorId) \nu3<- unique(tr_0$fullVisitorId)\nset_P <- intersect(u1, u2) ## te_2 in te_1: the set that will be used for private LB\nset_P_in_tr<- intersect(u3,set_P) ## the part of that set in train\ncat(\"\\n### A total of\", length(set_P), \"visitors expected in the Private LB , of which only\"\n    , length(set_P_in_tr), \"will be in train. ###\")\ncat(\"\\n### This is\", round(100*length(set_P_in_tr)/length(set_P), 1), \"percent ###\\n\")","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"fcf570b48bdb48737be7860430d64e45517abb2f"},"cell_type":"markdown","source":"So we have approx. 2900 \"recuring visitors\" in this emulated test set from the available data in train. But we do not have any reason to belive that more history will lead to many more of them, so this looks like the number of predictions that will really matter. That is only about 8% of all the predictions that we will make, but we do not know who is in that 8%. And unfortunately only 15% _of that 8%_ is also present in the training set ....\n\nAs is the case in the expanded vs 2 data, not all of train set has the target information, but it has other information that the \"real\" test does not have. So we have created a nice enulation of the real problem on a smaller dataset. \n\n**And now?**\n\n(a) The most obvious estimate is the avergage purchase per 2 months.  (b) A slightly more advanced advanced estimate will look at the history of the purchases. \n\nBut even (a) is difficult. We expect 85% of the final (te_2) test data not to be in train. \n\n**So this is a data mining competition**\n\nAll participants that know how to manipulate the applet from Gogle analytics have an advantage on 85% of the data. \n\nSo the target values of the first test V2 period should be published, otherwise this competition is completely unfair. "},{"metadata":{"trusted":true,"_uuid":"8fdccad6a80464c6b7ae5883daf6616424f2eec2"},"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}