{"cells":[{"metadata":{"_uuid":"8551a56e497ba12a5c8ce967cec88ee87bfa9e2f","_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\nlibrary(rpart) #Decision Tree\nlibrary(gbm) #Gradient Boosting\nlibrary(rpart.plot) #Model Visualization\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":"f6c744f7c7ca159b9550d46003c48642e7866cfe"},"cell_type":"code","source":"train = read_csv(\"../input//train.csv\")\ntest = read_csv(\"../input//test.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"244a5f7a975095b9a9834c324312da94f7591c04"},"cell_type":"code","source":"train %>% \n    head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"48051c0f1f385fba866ad5dcf2edf6907e0d2397"},"cell_type":"code","source":"train = train %>% \n    mutate(label = as.factor(label))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e22552b734d469f534e4c673830191ecc48cc2d2"},"cell_type":"code","source":"formula = label ~ .","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5f19a451d6f4f6ee03cd146b1174316339fc0abd"},"cell_type":"code","source":"set.seed(1256)\ntree = rpart(formula = formula, data = train, method = \"class\")\nrpart.plot(tree)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"dd4c82787cc07f869c9f8c9f6a2776b8f0b1c988"},"cell_type":"code","source":"barplot(tree$variable.importance)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"404bdd79b23d41f0e549cd605943121ce22e0dbd"},"cell_type":"code","source":"rsq.rpart(tree)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"91f6cfbcd27cab7797636ab8da847ccb0069a822"},"cell_type":"code","source":"submit = predict(tree, newdata = test, type=\"class\")\nsubmit %>% \n    head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4adcf270628cc5807d32f30fc225a0aa4ebcb619"},"cell_type":"code","source":"sample = read_csv(\"../input//sample_submission.csv\")\nhead(sample)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c122210b148f36211b292be727d1cab5dc1289c6"},"cell_type":"code","source":"test %>% \n    rowid_to_column(\"ImageId\") %>% \n    add_column(Label = submit) %>% \n    select(ImageId, Label) %>% \n    write_csv(\"submissionBasicDecisionTree.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"68169aae78dc2b50db4d70437bf3328441d411e5"},"cell_type":"code","source":"dir(\".\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a31234ec1d70052092792979f105e95a0646a9e2"},"cell_type":"code","source":"?gbm","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6899eb7cbf881734ca5364910c19cfadfb5dcdf1"},"cell_type":"code","source":"set.seed(3214)\ngbm0 = gbm(formula = formula, data = train, train.fraction = 0.8, \n           interaction.depth = 4, shrinkage = 0.25, cv.folds = 10,\n           distribution = \"multinomial\", n.trees = 100, verbose = F)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"715594a647b94894283a7a7a07631cbc3cac0513"},"cell_type":"code","source":"best.iter_test = gbm.perf(gbm0, method = \"cv\", plot.it=FALSE)\nbest.iter_test","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ed27d1fd1d78c62508c6aabab4ca5c162790b3b3"},"cell_type":"code","source":"gbm0.predict = predict(gbm0, test, type = \"response\", n.trees = best.iter_test)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"181122094e2163bc219f4f3e3fe870c1340ce490"},"cell_type":"code","source":"preds = as_tibble(gbm0.predict)\nhead(preds)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0783133b93b6dd32ec1f993fd61b633ec539cd3e"},"cell_type":"code","source":"predictions = as_tibble(colnames(preds)[max.col(preds,ties.method=\"first\")]) %>% \n    mutate(value = as.character(floor(as.integer(value))))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c258077295c5ea245a211e1dafcd97ba890c9c5a"},"cell_type":"code","source":"test %>% \n    rowid_to_column(\"ImageId\")  %>% \n    bind_cols(predictions) %>% \n    rename(Label = value) %>% \n    select(ImageId, Label) %>% \n    write_csv(\"submissionGBM.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b8a975a647eb6f6d6763686f6b241e4d3abd0cf5"},"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}