{"cells":[{"metadata":{},"cell_type":"markdown","source":"# Introduction"},{"metadata":{},"cell_type":"markdown","source":"# Packages"},{"metadata":{"_uuid":"051d70d956493feee0c6d64651c6a088724dca2a","_execution_state":"idle","trusted":true},"cell_type":"code","source":"pacman::p_load(\n    # tidyverse,\n    dplyr, tidyr, forcats,\n    data.table,\n    parsnip,\n    recipes, embed\n)\n\nlist.files(path = \"../input\", recursive = TRUE)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# EDA"},{"metadata":{"trusted":true},"cell_type":"code","source":"# import data into R\n\ntrain_df <- data.table::fread('../input/cat-in-the-dat-ii/train.csv')\ntest_df <- data.table::fread('../input/cat-in-the-dat-ii/test.csv')\nsub <- data.table::fread('../input/cat-in-the-dat-ii/sample_submission.csv')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Model"},{"metadata":{"trusted":true},"cell_type":"code","source":"features <- setdiff(names(train_df), c(\"id\", \"target\"))\n\ndata <- dplyr::bind_rows(\n    train_df, \n    mutate(test_df),\n    .id = \"DT\"\n)\n\ndata <- data %>% \n    mutate_all( ~ ifelse(.x == \"\", NA, .x) ) %>% \n    mutate_at( features,  factor ) %>% \n    mutate_at( features,  forcats::fct_explicit_na )\n\n# check that all features are factors\nall( sapply(data[, features], class) == \"factor\" )\n\n# give back to train_df, test_df\ntrain_df <- filter(data, DT==1) %>% \n    select(-DT) %>% \n    mutate(target = factor(target))\ntest_df <- filter(data, DT==2) %>% \n    select(-DT)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"max_num_terms <- function(variable, df, limit = 50){\n  nuniq <- select(df, {{variable}} ) %>% n_distinct()\n  min( ceiling(nuniq/2), limit  )\n}\n\nstep_embed2 <- function(recipe, variable, df){\n  embed::step_embed(recipe,\n             {{variable}} ,\n             outcome = all_outcomes(),\n             num_terms = max_num_terms({{variable}}, df),\n             hidden_units = 0,\n             options = embed_control(epochs = 5, validation_split = 0.2)\n  )\n}\n\nbase_recipe <- recipes::recipe(target ~ ., train_df) %>% \n    recipes::update_role(id, new_role = \"id variable\")\n\nfor(feat in features){\n    base_recipe <- base_recipe %>% \n        step_embed2({{feat}}, data)\n}\nbase_recipe","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"message(\"=== Starting prepping the recipe ===\")\ntrain_prepped <- recipes::prep(base_recipe, training = train_df)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"head(juice(train_prepped))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"es <- keras::callback_early_stopping(\n  monitor='val_loss', min_delta=0.001, patience=5,\n  verbose=1, mode='max', restore_best_weights=TRUE\n)\n\nrlr = keras::callback_reduce_lr_on_plateau(\n    monitor='val_loss', factor=0.5,\n    patience=3, min_lr=1e-6, mode='max', verbose=1\n)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"message(\"=== Starting fitting the model ===\")\n\n# parsnip\nmlp_fit <- parsnip::mlp(\"classification\", hidden_units = 300, epochs = 100) %>% \n    parsnip::set_engine(\"keras\",\n                        verbose = 1,\n                        callbacks = list(es, rlr),\n                        batch_size = 1024) %>% \n    parsnip::fit(target ~ . -id, recipes::juice(train_prepped) )\n\nmlp_fit","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Submission"},{"metadata":{"trusted":true},"cell_type":"code","source":"# sample_submission\nhead(sub)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"message(\"=== Preparing submission file ===\")\n\n# our actual submission\ntest_prepped <- recipes::bake(train_prepped, test_df)\n\nsubmission <- test_prepped %>% \n    select(id) %>% \n    mutate(target = predict(mlp_fit, new_data = test_prepped, type = \"prob\") %>% pull(.pred_1),\n          target = replace_na(target, 0) )\nhead(submission)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# write the .csv submission file\ndata.table::fwrite(submission, \"submission.csv\")\n\nmessage(\"=== Submission file exported ===\")","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}