{"cells":[{"metadata":{"_uuid":"bd738a42d89dcb22bdaeb65473adafaccff8b849","_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":"8f30e32079145409351f96dacb5a347109ba1de1"},"cell_type":"code","source":"train <- read_csv('../input/train.csv')\ntest <- read_csv('../input/test.csv')\n#merchants <- read_csv('../input/merchants.csv')\nhistorical_transactions <- read_csv('../input/historical_transactions.csv')\nnew_merchant_transactions <- read_csv('../input/new_merchant_transactions.csv')\nprint('train:');dim(train)\nprint('test:');dim(test)\n#print('merchants:');dim(merchants)\nprint('historical_transactions:');dim(historical_transactions)\nprint('new_merchant_transactions:');dim(new_merchant_transactions)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e03d9969f0eee0499e4ccac848ea8f6b25bf5dc6"},"cell_type":"code","source":"str(train)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e4c187a7ea4d96d2624194823bae5f29b6fdc5de"},"cell_type":"code","source":"str(test)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"949242b986dda07db826a91c941a9b73c6c66b90"},"cell_type":"code","source":"head(train)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"a35e47569b708504721789e532a6d46445c2685d"},"cell_type":"markdown","source":"card_id\t:                    Unique card identifier\n\nfirst_active_month :\t'YYYY-MM', month of first purchase\n\nfeature_1 :\t              Anonymized card categorical feature\n\nfeature_2 :\t             Anonymized card categorical feature\n\nfeature_3 :             Anonymized card categorical feature\n\ntarget :\t                   Loyalty numerical score calculated 2 months after historical and evaluation period"},{"metadata":{"trusted":true,"_uuid":"be67f57c07af707675b31126396f3bf40dc71dd6"},"cell_type":"code","source":"names(train)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"76bab3cd9205d5dbb487c53d68a40c6389112f73"},"cell_type":"code","source":"# convert categorical attributes to factor type\ndata_cat <- train[,c(1:5)]\ndata_num <- train[,-c(1:5)]\n\ndata_cat <- data.frame(apply(data_cat, 2,function(x){as.factor(x)}))\ntrain <- cbind(data_cat,data_num)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3eab95d36eaf3d6f77afb6b78c6afd111e83e365"},"cell_type":"code","source":"names(test)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"17da63398550498345ee9d1d6177643088cf4a8f"},"cell_type":"code","source":"# convert categorical attributes to factor type\ntest <- data.frame(apply(test, 2,function(x){as.factor(x)}))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"38ea340818abdb8cda2d6b83f44bf4ed96eae615"},"cell_type":"code","source":"summary(train)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9101eae1b5fc8fc59c0415fcd9abd6686314d5b5"},"cell_type":"code","source":"head(historical_transactions)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4680bfcdc7510664b7991ec9015bd5659b550b56"},"cell_type":"code","source":"head(new_merchant_transactions)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a7fb46eaaa31062e01bc5640ea25fa95ab9763fa"},"cell_type":"code","source":"historical_transactions <- rbind(historical_transactions,new_merchant_transactions)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e9fc2b65c7309d3610de6b7426d79fa22c0aea23"},"cell_type":"code","source":"transactions <- data.frame(card_id=unique(historical_transactions$card_id))\ndim(transactions)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a500a58df04a0ca719048dd6476b9ab54b2c6ba4"},"cell_type":"code","source":"most_frequent_category <- function(x) {\n  categories <- table(x)\n  return(names(categories[which.max(categories)]))\n}","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b5521b46b9c822ae9bf79570ed9ed371332662ed"},"cell_type":"code","source":"temp<-historical_transactions %>%\n  group_by(card_id) %>%\n  summarise(most_frequent_state_of_transaction = most_frequent_category(state_id))\ntransactions <- merge(transactions,temp,by=\"card_id\",sort = FALSE,all.x=TRUE)\ntransactions$most_frequent_state_of_transaction <- as.factor(transactions$most_frequent_state_of_transaction)\ntransactions$most_frequent_state_of_transaction[transactions$most_frequent_state_of_transaction == '-1'] <- most_frequent_category(historical_transactions$state_id)\ntransactions$most_frequent_state_of_transaction <- factor(transactions$most_frequent_state_of_transaction)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"df4978c371a7aac603f8f1751b82bbe27e762377"},"cell_type":"code","source":"temp<-historical_transactions %>%\n  group_by(card_id) %>%\n  summarise(most_frequent_merchant_category = most_frequent_category(merchant_category_id))\ntransactions <- merge(transactions,temp,by=\"card_id\",sort = FALSE,all.x=TRUE)\ntransactions$most_frequent_merchant_category <- as.factor(transactions$most_frequent_merchant_category)\ntransactions$most_frequent_merchant_category[transactions$most_frequent_merchant_category == '-1'] <- most_frequent_category(historical_transactions$merchant_category_id)\ntransactions$most_frequent_merchant_category <- factor(transactions$most_frequent_merchant_category)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c74fe69414038a7d3a58e0d7c2d6e108c4225e47"},"cell_type":"code","source":"temp<-historical_transactions %>%\n  group_by(card_id) %>%\n  summarise(most_frequent_merchant_category_group = most_frequent_category(subsector_id))\ntransactions <- merge(transactions,temp,by=\"card_id\",sort = FALSE,all.x=TRUE)\ntransactions$most_frequent_merchant_category_group <- as.factor(transactions$most_frequent_merchant_category_group)\ntransactions$most_frequent_merchant_category_group[transactions$most_frequent_merchant_category_group == '-1'] <- most_frequent_category(historical_transactions$subsector_id)\ntransactions$most_frequent_merchant_category_group <- factor(transactions$most_frequent_merchant_category_group)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6a5dafe79c8f0ac9d474734ea4a32e571a3ae40d"},"cell_type":"code","source":"temp<-historical_transactions %>%\n  filter(authorized_flag == \"Y\") %>%\n  group_by(card_id) %>%\n  summarise(last_purchase_month = max(purchase_date, na.rm = TRUE))\ntransactions <- merge(transactions,temp,by=\"card_id\",sort = FALSE,all.x=TRUE)\ntransactions$last_purchase_month <- format(transactions$last_purchase_month, \"%b-%y\")\ntransactions$last_purchase_month <- as.factor(transactions$last_purchase_month)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3bb877d5ba86549086bfcfa2a6fee0b161348b0e"},"cell_type":"code","source":"temp<-historical_transactions %>%\n  filter(authorized_flag == \"Y\") %>%\n  group_by(card_id) %>%\n  summarise(authorized_flag_Y = n())\ntransactions <- merge(transactions,temp,by=\"card_id\",sort = FALSE,all.x=TRUE)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"44331fddde7862c7e634a79f279a6787c33b35ed"},"cell_type":"code","source":"temp<-historical_transactions %>%\n  filter(authorized_flag == \"N\") %>%\n  group_by(card_id) %>%\n  summarise(authorized_flag_N = n())\ntransactions <- merge(transactions,temp,by=\"card_id\",sort = FALSE,all.x=TRUE)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7facee6bc70cdf940262a45f98d1ab6b452d1d2a"},"cell_type":"code","source":"temp<-historical_transactions %>%\n  filter(authorized_flag == \"Y\") %>%\n  group_by(card_id) %>%\n  summarise(avg_purchase_amount = mean(purchase_amount, na.rm = TRUE))\ntransactions <- merge(transactions,temp,by=\"card_id\",sort = FALSE,all.x=TRUE)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"fd69a5e66bf9a700e805000f5cba9d9832a064b3"},"cell_type":"code","source":"temp<-historical_transactions %>%\n  filter(authorized_flag == \"Y\") %>%\n  group_by(card_id) %>%\n  summarise(min_purchase_amount = min(purchase_amount, na.rm = TRUE))\ntransactions <- merge(transactions,temp,by=\"card_id\",sort = FALSE,all.x=TRUE)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d383a2c49910a4c181880fa02602ba2a54aad0ac"},"cell_type":"code","source":"temp<-historical_transactions %>%\n  filter(authorized_flag == \"Y\") %>%\n  group_by(card_id) %>%\n  summarise(max_purchase_amount = max(purchase_amount, na.rm = TRUE))\ntransactions <- merge(transactions,temp,by=\"card_id\",sort = FALSE,all.x=TRUE)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6e779ccfbedb0b82ee8f2f4634082415f073b3ed"},"cell_type":"code","source":"temp<-historical_transactions %>%\n  filter(authorized_flag == \"N\") %>%\n  group_by(card_id) %>%\n  summarise(avg_purchase_amount_denied = mean(purchase_amount, na.rm = TRUE))\ntransactions <- merge(transactions,temp,by=\"card_id\",sort = FALSE,all.x=TRUE)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"236920cdec26f622027f6ba29090e035e67760f0"},"cell_type":"code","source":"temp<-historical_transactions %>%\n  filter(authorized_flag == \"N\") %>%\n  group_by(card_id) %>%\n  summarise(max_purchase_amount_denied = max(purchase_amount, na.rm = TRUE))\ntransactions <- merge(transactions,temp,by=\"card_id\",sort = FALSE,all.x=TRUE)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8db6f205b264e85e18605118ad5c2401eaed2700"},"cell_type":"code","source":"temp<-historical_transactions %>%\n  filter(authorized_flag == \"Y\" & category_1 == \"N\") %>%\n  group_by(card_id) %>%\n  summarise(category_1_N = n())\ntransactions <- merge(transactions,temp,by=\"card_id\",sort = FALSE,all.x=TRUE)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e50d4a207ca8e3f3b6e9a8dcd45e7a5a75df4bad"},"cell_type":"code","source":"temp<-historical_transactions %>%\n  filter(authorized_flag == \"Y\" & category_1 == \"Y\") %>%\n  group_by(card_id) %>%\n  summarise(category_1_Y = n())\ntransactions <- merge(transactions,temp,by=\"card_id\",sort = FALSE,all.x=TRUE)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"10a3a3dca1b2c1d2b2117996e2768ad703944ecd"},"cell_type":"code","source":"temp<-historical_transactions %>%\n  filter(authorized_flag == \"Y\" & category_3 == \"A\") %>%\n  group_by(card_id) %>%\n  summarise(category_3_A = n())\ntransactions <- merge(transactions,temp,by=\"card_id\",sort = FALSE,all.x=TRUE)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7d33aaa089cd742fb9dd15b855340c878e1bef7a"},"cell_type":"code","source":"temp<-historical_transactions %>%\n  filter(authorized_flag == \"Y\" & category_3 == \"B\") %>%\n  group_by(card_id) %>%\n  summarise(category_3_B = n())\ntransactions <- merge(transactions,temp,by=\"card_id\",sort = FALSE,all.x=TRUE)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8095de0e692a235c47950842b33c26c857debbba"},"cell_type":"code","source":"temp<-historical_transactions %>%\n  filter(authorized_flag == \"Y\" & category_3 == \"C\") %>%\n  group_by(card_id) %>%\n  summarise(category_3_C = n())\ntransactions <- merge(transactions,temp,by=\"card_id\",sort = FALSE,all.x=TRUE)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b18f7353718c5c110b4d2ef64bc88a501d7abfa5"},"cell_type":"code","source":"temp<-historical_transactions %>%\n  filter(authorized_flag == \"Y\" & category_2 == \"1\") %>%\n  group_by(card_id) %>%\n  summarise(category_2_1 = n())\ntransactions <- merge(transactions,temp,by=\"card_id\",sort = FALSE,all.x=TRUE)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ed1a36871f93150967541aa124c902b161a27f29"},"cell_type":"code","source":"temp<-historical_transactions %>%\n  filter(authorized_flag == \"Y\" & category_2 == \"2\") %>%\n  group_by(card_id) %>%\n  summarise(category_2_2 = n())\ntransactions <- merge(transactions,temp,by=\"card_id\",sort = FALSE,all.x=TRUE)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1f3cc145809929302ccc16977ac6c195242152b0"},"cell_type":"code","source":"temp<-historical_transactions %>%\n  filter(authorized_flag == \"Y\" & category_2 == \"3\") %>%\n  group_by(card_id) %>%\n  summarise(category_2_3 = n())\ntransactions <- merge(transactions,temp,by=\"card_id\",sort = FALSE,all.x=TRUE)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b98737720a34be89b2a92b1179ede3556628c2ff"},"cell_type":"code","source":"temp<-historical_transactions %>%\n  filter(authorized_flag == \"Y\" & category_2 == \"4\") %>%\n  group_by(card_id) %>%\n  summarise(category_2_4 = n())\ntransactions <- merge(transactions,temp,by=\"card_id\",sort = FALSE,all.x=TRUE)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5aa663443c11e0187d70fbdcc0ff73b3d8fcc90c"},"cell_type":"code","source":"temp<-historical_transactions %>%\n  filter(authorized_flag == \"Y\" & category_2 == \"5\") %>%\n  group_by(card_id) %>%\n  summarise(category_2_5 = n())\ntransactions <- merge(transactions,temp,by=\"card_id\",sort = FALSE,all.x=TRUE)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"948d9158dbb3790db19a93cbd69fa9f32bd2ec21"},"cell_type":"code","source":"temp<-historical_transactions %>%\n  filter(authorized_flag == \"Y\") %>%\n  group_by(card_id) %>%\n  summarise(avg_installments = mean(installments, na.rm = TRUE))\ntransactions <- merge(transactions,temp,by=\"card_id\",sort = FALSE,all.x=TRUE)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f37f1683be6a3c5c4d8622c2590f04384066a275"},"cell_type":"code","source":"temp<-historical_transactions %>%\n  group_by(card_id) %>%\n  summarise(month_lag = min(month_lag, na.rm = TRUE))\ntransactions <- merge(transactions,temp,by=\"card_id\",sort = FALSE,all.x=TRUE)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5e46e9ca47acf25e2f2f93d6693f0013471cef97"},"cell_type":"code","source":"library(Hmisc)\n# Set the counts to 0 for the NAs\ntransactions$authorized_flag_Y <- impute(transactions$authorized_flag_Y,0)\ntransactions$authorized_flag_N <- impute(transactions$authorized_flag_N,0)\ntransactions$avg_purchase_amount_denied <- impute(transactions$avg_purchase_amount_denied,0)\ntransactions$max_purchase_amount_denied <- impute(transactions$max_purchase_amount_denied,0)\ntransactions$category_1_N <- impute(transactions$category_1_N,0)\ntransactions$category_1_Y <- impute(transactions$category_1_Y,0)\ntransactions$category_3_A <- impute(transactions$category_3_A,0)\ntransactions$category_3_B <- impute(transactions$category_3_B,0)\ntransactions$category_3_C <- impute(transactions$category_3_C,0)\ntransactions$category_2_1 <- impute(transactions$category_2_1,0)\ntransactions$category_2_2 <- impute(transactions$category_2_2,0)\ntransactions$category_2_3 <- impute(transactions$category_2_3,0)\ntransactions$category_2_4 <- impute(transactions$category_2_4,0)\ntransactions$category_2_5 <- impute(transactions$category_2_5,0)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"980cb883d0af10e756e418590c9c208e76c80157"},"cell_type":"code","source":"head(transactions)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f2a2a2e39ad2de63a1a8ec9b3135472139578493"},"cell_type":"code","source":"train <- merge(train,transactions,by=\"card_id\",sort = FALSE,all.x=TRUE)\ndim(train)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8a6eefeba95deaddaee6e21c798362ca0f2d5650"},"cell_type":"code","source":"test <- merge(test,transactions,by=\"card_id\",sort = FALSE,all.x=TRUE)\ndim(test)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9e69d7bb58fa7201aded8689ff49f53781ac97b2"},"cell_type":"code","source":"### Check for null or missing values\ncbind(sort(colSums(is.na(train)),decreasing=TRUE))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b7906eeaea1498aa5af6cd8af944f3373af80b74"},"cell_type":"code","source":"### Check for null or missing values\ncbind(sort(colSums(is.na(test)),decreasing=TRUE))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3a157b82def082c43bce23d2da3576d5f300eb8b"},"cell_type":"code","source":"test$first_active_month <- impute(test$first_active_month,'2017-01')\ntest$first_active_month <- factor(test$first_active_month)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"eb7d54b1d2c3c2d361dca0f6706b33121f7cdcf4"},"cell_type":"code","source":"summary(train)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ba5fd69f6eb6b7453d99e146214fc46baa062894"},"cell_type":"code","source":"avg <- plyr::ddply(train, \"first_active_month\", summarise, grp.avg=mean(target))\nhead(avg)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e0487890a65f5da00d881c5775f79a8dcaa654cf"},"cell_type":"code","source":"ggplot(train, aes(x=target, fill=first_active_month)) +\n  geom_density() + geom_vline(data=avg, aes(xintercept=grp.avg, color=first_active_month),\n             linetype=\"dashed\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9e39b81326efe07a3fc28b63425dd115e5d12d90"},"cell_type":"code","source":"avg <- plyr::ddply(train, \"feature_1\", summarise, grp.avg=mean(target))\nhead(avg)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"50ce85c789fbc9a8568c893d231f5a320d10d734"},"cell_type":"code","source":"ggplot(train, aes(x=target, fill=feature_1)) +\n  geom_density() + geom_vline(data=avg, aes(xintercept=grp.avg, color=feature_1),\n             linetype=\"dashed\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e54c42f8eeae3bf088f91972be7b7c20b48bc26d"},"cell_type":"code","source":"avg <- plyr::ddply(train, \"feature_2\", summarise, grp.avg=mean(target))\nhead(avg)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"249961b497c1ff7b333b4e67062377928cfd95bb"},"cell_type":"code","source":"ggplot(train, aes(x=target, fill=feature_2)) +\n  geom_density() + geom_vline(data=avg, aes(xintercept=grp.avg, color=feature_2),\n             linetype=\"dashed\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7cc66461c7a6decd0ab25ba2f90cf123d0056ffd"},"cell_type":"code","source":"avg <- plyr::ddply(train, \"feature_3\", summarise, grp.avg=mean(target))\nhead(avg)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"cec99c070c8f1fb1e26c878363567954daacc17e"},"cell_type":"code","source":"ggplot(train, aes(x=target, fill=feature_3)) +\n  geom_density() + geom_vline(data=avg, aes(xintercept=grp.avg, color=feature_3),\n             linetype=\"dashed\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2c2f54f0be0555d0669ffe428298c02fa2d452c3"},"cell_type":"code","source":"avg <- plyr::ddply(train, \"most_frequent_state_of_transaction\", summarise, grp.avg=mean(target))\nhead(avg)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2e0a3a225e994ef158b3c61d8bd1254b334f39c9"},"cell_type":"code","source":"ggplot(train, aes(x=target, fill=most_frequent_state_of_transaction)) +\n  geom_density() + geom_vline(data=avg, aes(xintercept=grp.avg, color=most_frequent_state_of_transaction),\n             linetype=\"dashed\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3f27d85a750cd73dbef7caf3945d8dbaf8b03ef4"},"cell_type":"code","source":"avg <- plyr::ddply(train, \"most_frequent_merchant_category_group\", summarise, grp.avg=mean(target))\nhead(avg)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"321adf1872ee743357e87077690a5b50c5f40701"},"cell_type":"code","source":"ggplot(train, aes(x=target, fill=most_frequent_merchant_category_group)) +\n  geom_density() + geom_vline(data=avg, aes(xintercept=grp.avg, color=most_frequent_merchant_category_group),\n             linetype=\"dashed\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"174bc99fb50151497b099230d64fc0ce6f8933ae"},"cell_type":"code","source":"avg <- plyr::ddply(train, \"last_purchase_month\", summarise, grp.avg=mean(target))\nhead(avg)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"bcedf234faa5b15f963654a67779f0596b66e6b9"},"cell_type":"code","source":"ggplot(train, aes(x=target, fill=last_purchase_month)) +\n  geom_density() + geom_vline(data=avg, aes(xintercept=grp.avg, color=last_purchase_month),\n             linetype=\"dashed\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"06ca99cc5e7fb4ce8d732acf2bef41ee3d42ec99"},"cell_type":"code","source":"ggplot(train, aes(authorized_flag_Y, target)) +\n  geom_point()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e700722de48fe3225d45b74f906de9c704b6b736"},"cell_type":"code","source":"ggplot(train, aes(authorized_flag_N, target)) +\n  geom_point()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"13bcdbdeaf36a0fff81dd4f2ad5e89c841c303ac"},"cell_type":"code","source":"ggplot(train, aes(avg_purchase_amount, target)) +\n  geom_point()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e72caee60c4cdcf0fe747dd1eae21e9725519a90"},"cell_type":"code","source":"# Split Data into Train and test - 70:30\nlibrary(caret)\nset.seed(7)\ntrain_test_split<-createDataPartition(train$target, 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":"85fa9b5c9e1d2e952e52ce1c599e784b08ba9800"},"cell_type":"code","source":"library(Matrix)\ntrain_data_sparse_matrix <- sparse.model.matrix(~., data = train_data[,-c(1,6)])[,-1]\ntest_data_sparse_matrix <- sparse.model.matrix(~., data = test_data[,-c(1,6)])[,-1]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4d3950f07fe00c1389da30cc0e481e08e6ccaf0c"},"cell_type":"code","source":"library(xgboost)\nX_train <- xgb.DMatrix(train_data_sparse_matrix)\ny_train <- train_data$target\nX_test <- xgb.DMatrix(test_data_sparse_matrix)\ny_test <- test_data$target","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"95e11ba8186d38900899e5575405f4f76d489909"},"cell_type":"code","source":"xgb_trcontrol <- trainControl(\n  method = \"cv\",\n  number = 5,  \n  allowParallel = TRUE,\n  verboseIter = FALSE,\n  returnData = FALSE\n)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"cb1b4b1252af02c97bb8dc96f04fce17cf4e840a"},"cell_type":"code","source":"xgbGrid <- expand.grid(nrounds = c(400,600,800),\n                       max_depth = c(5, 7, 10),\n                       colsample_bytree = seq(0.5, 0.9, length.out = 5),\n                       eta = c(0.01,0.02), #c(0.03,0.05,1),\n                       gamma = 0, #c(0.01,0.1,0.6),\n                       min_child_weight = 100, #c(0,1,10),\n                       subsample = 0.85 #c(0.5,0.8,1)\n                      )","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b9da038e5c8bf3f91cdb11c8d565d9f97602b189"},"cell_type":"code","source":"set.seed(0) \nxgb_model <- train(\n  X_train, y_train,  \n  trControl = xgb_trcontrol,\n  tuneGrid = xgbGrid,\n  tuneLength = 10,\n  method = \"xgbTree\"\n)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"72dea0bf756fb78ecd1e22c0595b7379a231a849"},"cell_type":"code","source":"xgb_model$bestTune","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"11251133ead2b7bdbee8044ff4a0546bb883ecce"},"cell_type":"code","source":"summary(xgb_model)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a93ef4d59d0d8fde966be4e6a04fb89b20853fbc"},"cell_type":"code","source":"# predict values for test split\npredicted <- predict(xgb_model, X_test)\nresiduals <- y_test - predicted\nRMSE <- sqrt(mean(residuals^2))\nprint(RMSE)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4d6affd842508e2b1b9aa93e41bc5b31e86ac52e"},"cell_type":"code","source":"test_sparse_matrix <- sparse.model.matrix(~., data = test[,-c(1)])[,-1]\ntest_X <- xgb.DMatrix(test_sparse_matrix)\n# predict values for test data\npred <- predict(xgb_model, test_X)\ntest$target <- pred","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"022bcadda8be2505b91f0b5a324e92fb60800943"},"cell_type":"code","source":"submission <- read_csv('../input/sample_submission.csv',col_names=TRUE)\nhead(submission)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3617e4a3128932e8a4132f2d395aeead6981a695"},"cell_type":"code","source":"submission <- submission[,-c(2)]\nsubmission <- merge(submission,test[,c(1,29)],by=\"card_id\",sort = FALSE,all.x=TRUE)\nhead(submission)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"621d3f7a1a8c937d009454867b4ee69e8e425f9d"},"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}