{"cells":[{"metadata":{"_uuid":"89387fbdc0510b66c7186c1e7ab4d24a4c77244c","_cell_guid":"d219ef1c-2150-47e4-8a9a-3f8b2247a815"},"cell_type":"markdown","source":"# Digit Recognizer \n\"Each image is **28 pixels in height** and **28 pixels in width**, for a total of 784 pixels in total. Each pixel has a single pixel-value associated with it, indicating the lightness or darkness of that pixel, with higher numbers meaning darker. This pixel-value is an integer between 0 and 255, inclusive.\"\n\nSo each image have 28x28 pixels and a color on each pixel.","outputs":[],"execution_count":null},{"metadata":{"_uuid":"bde6bbe97933726719935de9ca911d05760fc0cb","_cell_guid":"7b79d1fd-35a5-4461-bf0a-b12d5337a438"},"cell_type":"markdown","source":"## Assign Necessary Libraries\n","outputs":[],"execution_count":null},{"metadata":{"_uuid":"238fac25c47e292a0d93b1e8aa7837869374ddcc","trusted":true,"_cell_guid":"c4c3daa5-4e81-42af-9a0a-fcb8de196e4f"},"cell_type":"code","source":"# Necessary Libraries \n\nsuppressMessages(library(lattice))\nsuppressMessages(library(ggplot2))\nsuppressMessages(library(caret))\n\n# for describe the data\nsuppressMessages(library(psych))\n\n# install.packages(\"h2o\")\nsuppressMessages(library(h2o))","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"2d9e4ad1955c2bce00037610044129395a506272","_cell_guid":"5f1b33db-4089-4539-91e4-3c08090e3b7a"},"cell_type":"markdown","source":"## Loading data","outputs":[],"execution_count":null},{"metadata":{"_uuid":"56abef5c2ca0222dff1caaf581f66930f00cc55c","trusted":true,"_cell_guid":"2731e6ed-5847-4ff0-b9a5-441c1363e09d"},"cell_type":"code","source":"# load train.csv\nDstTrain <- read.csv('../input/train.csv', stringsAsFactors = FALSE, na.strings = c(\"NA\", \"\"))\n# load test.csv\nDstTest  <- read.csv('../input/test.csv', stringsAsFactors = FALSE, na.strings = c(\"NA\", \"\"))","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"b1e65909c8f273d6247143b975f0bcc78b37f8d1","_cell_guid":"515c5f93-7331-4aac-8e44-268884c0b5dd"},"cell_type":"markdown","source":"**Create the Label column on the test Dataset**\n\nIt doesn't exist so I will create it with NA.","outputs":[],"execution_count":null},{"metadata":{"_uuid":"876ad929639f3b01915cc8eba05a4290f41fda1f","trusted":true,"_cell_guid":"0afa76d3-82c2-4726-8f45-3e7c833a6857"},"cell_type":"code","source":"# set label to NA on the test dataset\nDstTest <- cbind(label = NA, DstTest)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"f3894f0ba6e056fc9eb0c27c9401b1e629a4d59d","_cell_guid":"d5f4e810-d95c-46bd-9ed1-eee629ac65ba"},"cell_type":"markdown","source":"## Checking the data\n\n**Check some info about structure**\n\nLet us explore the structure of the loaded data","outputs":[],"execution_count":null},{"metadata":{"_uuid":"6fb06add639faa3183c5cf23d488501d395eaeb2","trusted":true,"_cell_guid":"934c974f-0d01-463e-8191-45ecf658de7b"},"cell_type":"code","source":"# check data sample \nhead(DstTest, n=5L)\nhead(DstTrain, n=5L)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"31b0d59fa10cc29845fe821bb9c98159a481bc63","_cell_guid":"293cd30e-96f4-448d-9b95-41f447cf0058"},"cell_type":"markdown","source":"## understanding the data\nLet's check the training data for number distribution.\n\n**Check the number distribution**","outputs":[],"execution_count":null},{"metadata":{"_uuid":"95284f325ee544e9a489bcc26f833083e0923771","trusted":true,"_cell_guid":"f4c849f2-faa4-4f37-a0ef-6b62384abe07"},"cell_type":"code","source":"# check the data by each number\nt(as.data.frame(table(as.factor(DstTrain$label))))\n\n# Graphicaly is more easy to see\nggplot(DstTrain, aes(x=label,fill=as.factor(label))) +\n    geom_histogram(stat = \"count\") ","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"04f227389657f20514916955ff00a7c2fa26f621","_cell_guid":"642a2abc-5d08-4de5-aa82-e6ccda745286"},"cell_type":"markdown","source":"**Let's see some of the images**\n\nWhat's that we keep as image, let's check it out","outputs":[],"execution_count":null},{"metadata":{"_uuid":"58a6f1759e677aa712fb803a4196313c52108ce6","trusted":true,"_cell_guid":"6799a1e4-3d12-4e31-88ee-f8fe084e29d1"},"cell_type":"code","source":"# 28*28 matrix with pixel color values\ndrawImage <- function(selrow) {\n    image(\n        t(apply(\n                matrix(unlist(DstTrain[selrow,-1]),\n                 nrow = 28,byrow = TRUE)\n            , 2, rev))\n    ,col=gray.colors(255))\n}\n\n# create a grid\npar(mfrow=c(3,4))\n\n# draw the images\nall <- sapply(sample(1:42000, 12, replace=T),drawImage)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"2c522ab56f07ca0f849204adafd7bc4119cc0e0c","_cell_guid":"73e97260-48d9-4716-8b27-22f701f83999"},"cell_type":"markdown","source":"## Let's do some water (H2O)\n\nI never used this before so I will follow the documentation example and some samples  and hope I get it right.\n\nhttps://github.com/h2oai/h2o-tutorials/tree/master/tutorials/deeplearning","outputs":[],"execution_count":null},{"metadata":{"_uuid":"55aca7ed5ed535eddbedfb7e3d538a5ad80f5b5f","_cell_guid":"cb8edf80-3c82-4a29-b8cc-2fa491c777dc"},"cell_type":"markdown","source":"**Start H2O**\n\nStart up a 1-node H2O server on your local machine, and allow it to use all CPU cores and up to 2GB of memory:","outputs":[],"execution_count":null},{"metadata":{"_uuid":"12d66d15382ca70b7523290805f3a7ffc64f1b85","trusted":true,"_cell_guid":"1062ad61-1452-4346-a6e2-5855695a2974"},"cell_type":"code","source":"# h2o init and clear\nh2o.init(nthreads=-1, max_mem_size=\"4G\")\nh2o.removeAll() ## clean slate - just in case the cluster was already running","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"92c2fd19c434c8f183cea372e0f31c506b630c47","_cell_guid":"2c0bebda-d9b9-4100-935c-07db97bed077"},"cell_type":"markdown","source":"**Set label as factor**","outputs":[],"execution_count":null},{"metadata":{"_uuid":"ec092d76d01aeec3c3d60b2a87033a120516534b","trusted":true,"_cell_guid":"0ec634d0-8dae-43f0-9758-6d19ae51c7b6"},"cell_type":"code","source":"# label to factor\nDstTrain$label  <- as.factor(DstTrain$label)\nDstTest$label  <- as.factor(DstTest$label)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"7d3e942ac650929dae639f0c527d4f80d8c30cb4","_cell_guid":"ef3fa85d-5ab3-4618-a95a-5807e9c2b2d2"},"cell_type":"markdown","source":"**Cover Type Dataset**\n\nSplit the data 3 ways: 80% for training, 20% for validation and 20% for test.","outputs":[],"execution_count":null},{"metadata":{"_uuid":"6f9e203ee0a541f20d13608c626cedc3c15e41a7","trusted":true,"_cell_guid":"b5af8489-f75f-41a6-a4ed-1448bf3d0641"},"cell_type":"code","source":"# convert to h2o frame \nh2o_TrainAux = as.h2o(DstTrain)\n\n# define the splits\nh2o_splits <- h2o.splitFrame(h2o_TrainAux, c(0.6,0.2), seed=1234)\nh2o_DstTrain  <- h2o.assign(h2o_splits[[1]], \"train.hex\") # 60%\nh2o_DstValid  <- h2o.assign(h2o_splits[[2]], \"valid.hex\") # 20%\nh2o_DstTest   <- h2o.assign(h2o_splits[[3]], \"test.hex\")  # 20%","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"f792a6803ee1db10b677d4e3c093979eb91eb9f8","_cell_guid":"525be9f5-979d-4749-a3cb-5f212cd2d2aa"},"cell_type":"markdown","source":"## First Run of H2O Deep Learning\n\nLet's run our first Deep Learning model with h2o and I will use the train and the valid h20 dataset that I create previous.\n\nTo keep it fast, we only run for one epoch (one pass over the training data).","outputs":[],"execution_count":null},{"metadata":{"_uuid":"aadd5bd386098c90f5713cc57419cc475589806c","trusted":true,"_cell_guid":"2674d939-5d21-41ac-bb4f-a30b7a62e94b"},"cell_type":"code","source":"# set the response and the predictors\nresponse <- \"label\"\npredictors <- setdiff(names(h2o_DstTrain), response)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"550b4efc30fadfb7974f6be697a257fcb274452f","_cell_guid":"a880090a-9086-4526-a3a6-e841075e0b80"},"cell_type":"markdown","source":"**Starting with the default values and then i will start tunning**","outputs":[],"execution_count":null},{"metadata":{"_uuid":"03ebbd5835473c4e5940d1e598302296473a1023","trusted":true,"_cell_guid":"7fe99c28-e677-49eb-b913-e690501fce9a"},"cell_type":"code","source":"# train model\nh2o_m1 =\n  h2o.deeplearning(            \n    model_id=\"dl_model_digit_rec\", \n    training_frame=h2o_DstTrain, \n    x = predictors,  # predictors columns\n    y = response,   # response column label\n    epochs=20,      # 20 looks a good number for here\n    hidden=c(160, 160, 160, 160, 160),  # why this numbers, just filling the wind :)\n    activation=c(\"Tanh\")\n    #activation = \"RectifierWithDropout\"\n) \nsummary(h2o_m1)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"1d12742dac054f05d1fb7c52a9b3be0104a007f3","_cell_guid":"ca564001-60d0-423b-a0a5-7fc605e06073"},"cell_type":"markdown","source":"**Measuring the model performance**\n\nLet's make some performance mesuring so after this we can focus on tunning the model.","outputs":[],"execution_count":null},{"metadata":{"_uuid":"9b6a8645086c139a32c506edb3f974510385fcb1","trusted":true,"_cell_guid":"cf618dd8-880e-41e1-ab25-919095e21434"},"cell_type":"code","source":"h2o.performance(h2o_m1, train=T)                 ## sampled training data (from model building)\nh2o.performance(h2o_m1, valid=T)                 ## sampled validation data (from model building)\nh2o.performance(h2o_m1, newdata=h2o_DstTrain)    ## full training data\nh2o.performance(h2o_m1, newdata=h2o_DstValid)    ## full validation data\nh2o.performance(h2o_m1, newdata=h2o_DstTest)     ## full test data","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"981584ffbabe9bd8636d0813ece19a1083c74871","_cell_guid":"8205639b-7ac9-4a58-a40c-f2956f6de425"},"cell_type":"markdown","source":"**Accuracy level is**\n\nLet's use the model to predict and check the accuracy level","outputs":[],"execution_count":null},{"metadata":{"_uuid":"08292cd931000c14a0ffad6b129cbf2bdd2dd601","trusted":true,"_cell_guid":"49a3a23a-3ec7-44df-8191-b05470412875"},"cell_type":"code","source":"# predict with the model\npredictM <- h2o.predict(h2o_m1, h2o_DstTest)\n#check for accuracy\nh2o_DstTest$Accuracy <- predictM$predict == h2o_DstTest$label\ntable(as.data.frame(h2o_DstTest$Accuracy))\n","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"ce8887c550803cb2d2c9f1c0c25c27e38511c694","trusted":true,"_cell_guid":"ccadd5e6-8825-4f30-b7c7-796367934487"},"cell_type":"code","source":"# calculate the percentage\ndf <- data.frame(table(as.data.frame(h2o_DstTest$Accuracy)))\ncolnames(df) <- c('Acc','Freq')\ndf$Perc <- df$Freq / sum(df$Freq)\ndf$Perc[2]","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"002f28b7ec98b690271dc3b04908bb44acf071de","_cell_guid":"1f761797-8dac-4afd-a1a5-1a12d994a2a5"},"cell_type":"markdown","source":"## Final predict and submition file\n\nLet's make the final prediction and create the file for submiti","outputs":[],"execution_count":null},{"metadata":{"_uuid":"bebdbad8a3a9749575bd1ad77f95b7f33af2dd52","trusted":true,"_cell_guid":"e04d1b2f-c8e2-4873-9123-5ca163df3163"},"cell_type":"code","source":"# convert to h2o frame \nh2o_Test = as.h2o(DstTest)\n\n# predict with the model\npredictFinal <- h2o.predict(h2o_m1, h2o_Test)\n\n# convert H2O format into data frame and save as csv\npredictFinal.df <- as.data.frame(predictFinal)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"38e7c4c59a074588bba1b74cf4f497dd3ecdc6b8","trusted":true,"_cell_guid":"277cd321-a41c-439b-aef0-7289d42d45ad"},"cell_type":"code","source":"# create the final dataset\ndstSubmit <- data.frame(ImageId = seq(1,length(predictFinal.df$predict)), Label = predictFinal.df$predict)\nhead(dstSubmit, n=5L)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"998e16d05398826a7df8e2dff691b9d068ab2f10","trusted":true,"_cell_guid":"3ae044c7-2ae1-4276-b3c3-ddcd7dcccb6b"},"cell_type":"code","source":"# write the submit csv file\nwrite.csv(dstSubmit,file = \"ImgSubmit.csv\",row.names = FALSE)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"ff2a5878f7d270d783f37adc9d55ea8da708bb14","trusted":true,"_cell_guid":"f7e1e2a4-3e21-4543-811a-4c655485652b"},"cell_type":"code","source":"# shut down virtual H2O cluster\n#h2o.shutdown(prompt = 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}