{"nbformat": 4, "metadata": {"language_info": {"mimetype": "text/x-r-source", "name": "R", "version": "3.4.1", "file_extension": ".r", "codemirror_mode": "r", "pygments_lexer": "r"}, "kernelspec": {"name": "ir", "display_name": "R", "language": "R"}}, "nbformat_minor": 1, "cells": [{"execution_count": null, "cell_type": "code", "metadata": {"scrolled": true, "_cell_guid": "ca501674-aeef-450f-8fba-0349043cf28f", "_uuid": "0305aff86743bdd8f4c9c0726c32f785f5ba0a2b"}, "outputs": [], "source": ["library(data.table)\n", "library(Matrix)\n", "library(xgboost)\n", "library(caret)\n", "library(dplyr)"]}, {"execution_count": null, "cell_type": "code", "metadata": {"_cell_guid": "e0ffce0a-2c2c-4b67-9b92-f67965afa49c", "_uuid": "242e82ad210b83868e839bebe2ac2f8b298a9d62"}, "outputs": [], "source": ["cat(\"Init\")\n", "set.seed(12345)\n", "PATH <- \"../input/\""]}, {"execution_count": null, "cell_type": "code", "metadata": {"_kg_hide-input": true, "_cell_guid": "cc75af6e-9db4-4a31-9fe6-f19eb026bc6b", "_kg_hide-output": false, "_uuid": "50af2ccd1cff175986ef0deee0f761a2b94ab58e"}, "outputs": [], "source": ["cat(\"Load data\")\n", "train <- fread(paste0(PATH,\"train.csv\"), sep=\",\", na.strings = \"\", stringsAsFactors=T)\n", "transactions <- fread(paste0(PATH,\"transactions.csv\"), sep=\",\", na.strings = \"\", stringsAsFactors=T)\n", "members <- fread(paste0(PATH,\"members.csv\"), sep=\",\", na.strings = \"\", stringsAsFactors=T)\n", "sample_submission_zero <- fread(paste0(PATH,\"sample_submission_zero.csv\"), sep=\",\", na.strings = \"\", stringsAsFactors=T)"]}, {"execution_count": null, "cell_type": "code", "metadata": {"_cell_guid": "80ffb9a8-7154-4388-bc55-12022bc4343c", "collapsed": true, "_uuid": "051bd2b2923acb5c799bde9f2e33853e02dad558"}, "outputs": [], "source": ["cat(\"Combine train and test files\")\n", "sample_submission_zero$is_churn <- NA\n", "data <- rbind(train, sample_submission_zero)\n", "data[,is_duplicate := as.numeric(duplicated(as.character(data$msno)) | duplicated(as.character(data$msno),fromLast=T))]\n", "rm(train);gc()"]}, {"execution_count": null, "cell_type": "code", "metadata": {"scrolled": true, "_cell_guid": "00c016b4-c691-485f-ba86-9376c1966d51", "_uuid": "8a59806c37d605a6eeecd6685e55e8608e55e3bf"}, "outputs": [], "source": ["cat(\"Format gender and remove NA's\")\n", "members[,gender := as.numeric(gender)]\n", "members$gender[is.na(members$gender)] <- 0\n"]}, {"execution_count": null, "cell_type": "code", "metadata": {"_cell_guid": "57201261-f798-44f5-8bb5-d06e3dfba81d", "_uuid": "882dc57f68a2f7b352f78c51d22586008e7e6b27"}, "outputs": [], "source": ["cat(\"Format dates and do some feature engineering\")\n", "members[,\":=\"(reg_fulldate = members$registration_init_time\n", "             ,registration_init_time = as.Date(as.character(registration_init_time), '%Y%m%d')\n", "             ,exp_fulldate = expiration_date\n", "             ,expiration_date = as.Date(as.character(expiration_date), '%Y%m%d'))]\n", "members[,\":=\"(reg_year = year(registration_init_time)\n", "             ,reg_month = month(registration_init_time)\n", "             ,reg_mday = mday(registration_init_time)\n", "             ,reg_wday = wday(registration_init_time)\n", "             ,exp_year = year(expiration_date)\n", "             ,exp_month = month(expiration_date)\n", "             ,exp_mday = mday(expiration_date)\n", "             ,exp_wday = wday(expiration_date)\n", "             ,date_diff = as.numeric(expiration_date - registration_init_time))]\n", "members <- subset(members, select = -c(registration_init_time, expiration_date))"]}, {"execution_count": null, "cell_type": "code", "metadata": {"_cell_guid": "4c821abb-836d-4e5b-b02b-c0e608c77c98", "_uuid": "84f7c1e6fc32c904eeaa7652387fcdd3722c6f43"}, "outputs": [], "source": ["cat(\"Merge data and members\")\n", "data <- merge(data, members, by = \"msno\", all.x = TRUE)\n", "rm(members);gc()\n", "\n", "cat(\"Reduce size of transactions a bit\")\n", "transactions <- transactions[transactions$msno %in% levels(data$msno),]\n", "\n", "cat(\"Get amount of transactions per user\")\n", "transactions[,n_transactions := .N, by = msno]\n", "\n", "cat(\"Get difference between plan price and payment amount\")\n", "transactions[,payment_price_diff := plan_list_price - actual_amount_paid]"]}, {"execution_count": null, "cell_type": "code", "metadata": {"_cell_guid": "f28a9864-a851-42a4-ad3b-30353f62451f", "_uuid": "0bec7a7986c22bea10f5b511447b67d782b5b718"}, "outputs": [], "source": ["cat(\"Aggregate by user, get mean of columns\")\n", "cat(\"I don't think the transaction dates are useful for now, so let's remove them\")\n", "transactions <- transactions[,lapply(.SD,mean,na.rm=T), by = msno, .SDcols = names(transactions)[c(2:6,9:11)]]"]}, {"execution_count": null, "cell_type": "code", "metadata": {"_cell_guid": "a18562fe-ff9a-4a18-913f-6bd0db54ae0c", "_uuid": "3b92eab7d906ffd29265fe885c73c6fe073f88d5"}, "outputs": [], "source": ["cat(\"Merge data and transactions\")\n", "data <- merge(data, transactions, by = \"msno\", all.x = TRUE)\n", "rm(transactions);gc()\n"]}, {"execution_count": null, "cell_type": "code", "metadata": {"_cell_guid": "3088c95e-a7ff-448c-baa0-c8beeb61f8bf", "_uuid": "f21ad0ef84f327ee7ccf32093c659198494ee80e"}, "outputs": [], "source": ["cat(\"Prepare for xgb\")\n", "cvFolds <- createFolds(data$is_churn[!is.na(data$is_churn)], k=5, list=TRUE, returnTrain=FALSE)\n", "varnames <- setdiff(colnames(data), c(\"msno\", \"is_churn\"))\n", "train_sparse <- Matrix(as.matrix(data[!is.na(is_churn), varnames, with=F]), sparse=TRUE)\n", "test_sparse <- Matrix(as.matrix(data[is.na(is_churn), varnames, with=F]), sparse=TRUE)\n", "y_train <- data[!is.na(is_churn),is_churn]\n", "test_ids <- data[is.na(is_churn),msno]\n", "dtrain <- xgb.DMatrix(data=train_sparse, label=y_train)\n", "dtest <- xgb.DMatrix(data=test_sparse)"]}, {"execution_count": null, "cell_type": "code", "metadata": {"_cell_guid": "a0f1e8e0-91bc-4e8a-8c00-de1dfaa07540", "_uuid": "3d681a433f20dbabc71dbcea41417dbe54afdcbf"}, "outputs": [], "source": ["cat(\"Params for xgb\")\n", "param <- list(booster=\"gbtree\",\n", "              objective=\"binary:logistic\",\n", "              eval_metric=\"logloss\",\n", "              eta = .02,\n", "              gamma = 1,\n", "              max_depth = 6,\n", "              min_child_weight = 1,\n", "              subsample = .8,\n", "              colsample_bytree = .8\n", ")"]}, {"execution_count": null, "cell_type": "code", "metadata": {"_cell_guid": "7a8d9789-2e24-402a-8736-1a7ac3cb21bd", "_uuid": "47545f5fce88105c88127cc63de01b9dcb7fc90c"}, "outputs": [], "source": ["cat(\"Xgb cross-validation, uncomment when running locally\")\n", "# xgb_cv <- xgb.cv(data = dtrain,\n", "#                  params = param,\n", "#                  nrounds = 1000,\n", "#                  maximize = FALSE,\n", "#                  prediction = TRUE,\n", "#                  folds = cvFolds,\n", "#                  print_every_n = 10,\n", "#                  early_stopping_round = 50)\n", "# best_iter <- xgb_cv$best_iteration\n", "best_iter <- 1512"]}, {"execution_count": null, "cell_type": "code", "metadata": {"_cell_guid": "e71157ce-bd14-44d7-8045-9c3e8a64321c", "_uuid": "6c196833d2771e978d01c2bbe1e288bf23770405"}, "outputs": [], "source": ["cat(\"xgb model\")\n", "xgb_model <- xgb.train(data = dtrain,\n", "                       params = param,\n", "                       watchlist = list(train = dtrain),\n", "                       nrounds = best_iter,\n", "                       verbose = 1,\n", "                       print_every_n = 100\n", ")"]}, {"execution_count": null, "cell_type": "code", "metadata": {"_cell_guid": "7fb5b277-4a3e-4750-81ac-d28c78cf3267", "_uuid": "2cc586d0bf13eecb5007cc95cb4ca18529a496aa"}, "outputs": [], "source": ["cat(\"Feature importance\")\n", "names <- dimnames(train_sparse)[[2]]\n", "importance_matrix <- xgb.importance(names, model=xgb_model)\n", "xgb.plot.importance(importance_matrix)"]}, {"execution_count": null, "cell_type": "code", "metadata": {"_cell_guid": "ef7b426a-6f9b-4219-a18e-1525b2e3667b", "_uuid": "8597b1b1019df1efd48ddaee81ec884db5f0b271"}, "outputs": [], "source": ["cat(\"Predict and output csv\")\n", "preds <- data.table(msno=test_ids, is_churn=predict(xgb_model,dtest))\n", "preds <- merge(sample_submission_zero[,1], preds, by=\"msno\", all.x=T, sort=F)\n", "write.table(preds, \"submission.csv\", sep=\",\", dec=\".\", quote=FALSE, row.names=FALSE)\n", "\n", "show le"]}]}