{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "f118fa16-f277-9a7d-1a19-0c6f92ce2b7b"
      },
      "outputs": [],
      "source": [
        "library('ggplot2')\n",
        "library('reshape2')\n",
        "library('randomForest')\n",
        "options(scipen=999)       # No scientific notation for numbers\n",
        "\n",
        "## Multiplot function for ggplot2 http://www.cookbook-r.com/Graphs/Multiple_graphs_on_one_page_%28ggplot2%29/\n",
        "multiplot <- function(..., plotlist=NULL, file, cols=1, layout=NULL) {\n",
        "  require(grid)\n",
        "  # Make a list from the ... arguments and plotlist\n",
        "  plots <- c(list(...), plotlist)\n",
        "  numPlots = length(plots)\n",
        "  # If layout is NULL, then use 'cols' to determine layout\n",
        "  if (is.null(layout)) {\n",
        "    # Make the panel\n",
        "    # ncol: Number of columns of plots\n",
        "    # nrow: Number of rows needed, calculated from # of cols\n",
        "    layout <- matrix(seq(1, cols * ceiling(numPlots/cols)),\n",
        "                    ncol = cols, nrow = ceiling(numPlots/cols))\n",
        "  }\n",
        " if (numPlots==1) {\n",
        "    print(plots[[1]])\n",
        "  } else {\n",
        "    # Set up the page\n",
        "    grid.newpage()\n",
        "    pushViewport(viewport(layout = grid.layout(nrow(layout), ncol(layout))))\n",
        "    # Make each plot, in the correct location\n",
        "    for (i in 1:numPlots) {\n",
        "      # Get the i,j matrix positions of the regions that contain this subplot\n",
        "      matchidx <- as.data.frame(which(layout == i, arr.ind = TRUE))\n",
        "\n",
        "      print(plots[[i]], vp = viewport(layout.pos.row = matchidx$row,\n",
        "                                      layout.pos.col = matchidx$col))\n",
        "    }\n",
        "  }\n",
        "}\n",
        "\n",
        "plotting <- function(name) {\n",
        "plots <- list()  # new empty list\n",
        "for (i in 1:10) {\n",
        "intensity <- as.numeric(name[i,-1])/max(as.numeric(name[i,-1]))\n",
        "tmp <- (matrix(intensity,nrow = colnumber,ncol = colnumber))\n",
        "tmp2 <- melt(tmp)\n",
        "p1 = ggplot(tmp2, aes(Var1,-Var2, fill=value)) + geom_raster()+ guides(fill=FALSE) + xlab(\"\") +ylab(\"\")+\n",
        "        theme(strip.background = element_blank(),\n",
        "              strip.text.x = element_blank(),\n",
        "              axis.text.x = element_blank(),\n",
        "              axis.text.y = element_blank(),\n",
        "              axis.ticks = element_blank(),\n",
        "              axis.line = element_blank())\n",
        "plots[[i]] <- p1  # add each plot into plot list\n",
        "}\n",
        "layout <- matrix(c(1:10), nrow=2,byrow=5)\n",
        "multiplot(plotlist = plots, layout = layout)\n",
        "}\n",
        "\n",
        "colnumber <- 28\n",
        "train <- read.csv(\"../input/train.csv\")\n",
        "test <- read.csv(\"../input/test.csv\")\n",
        "\n",
        "# The first colomn of digits, the rest of the rows  with the information about pixels\n",
        "labels <- train[,1]\n",
        "features <- train[,-1]\n",
        "\n",
        "# Median, mean and SD of each digit\n",
        "means <- aggregate(features, list(labels), mean)\n",
        "medians <- aggregate(features, list(labels), median)\n",
        "stds <- aggregate(features, list(labels), sd)\n",
        "\n",
        "\n",
        "\n",
        "\n",
        "\n",
        "\n",
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "23ed44f5-184a-bae1-51d1-04813a33ad79"
      },
      "source": [
        "### Median"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "7b69a0b7-2cae-97c2-fdfa-e6ba9ea776da"
      },
      "outputs": [],
      "source": [
        "plotting(medians)\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "96d7de79-30ec-e356-aaaa-d5f317816a62"
      },
      "source": [
        "### Mean"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "e7f39732-c4dc-f247-95fe-220fd2cee0dc"
      },
      "outputs": [],
      "source": [
        "plotting(means)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "43a6af6b-a3ab-6107-71e7-dd4ceeba24e0"
      },
      "source": [
        "### SD"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "638acd27-78fd-8f3a-7e59-ca5ba0b1a017"
      },
      "outputs": [],
      "source": [
        "plotting(stds)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "d41cec67-81d7-920c-3581-d77b1d5436c2"
      },
      "source": [
        "## Predicting using Random Forest"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "767059e4-e0ea-f137-75e2-f3d379c2d832"
      },
      "outputs": [],
      "source": [
        "set.seed(123)\n",
        "\n",
        "numTrain <- 10000\n",
        "\n",
        "rows <- sample(1:nrow(train), numTrain)\n",
        "labels <- as.factor(labels[rows])\n",
        "train<- train[rows,-1]\n",
        "\n",
        "rf <- randomForest(train, labels, xtest=test, ntree=100)\n",
        "rf\n",
        "predictions <- data.frame(ImageId=1:nrow(test), Label=levels(labels)[rf$test$predicted])\n",
        "head(predictions)\n",
        "\n",
        "write.csv(predictions, paste0(\"Prediction\", format(Sys.time(), \"%d-%b-%Y %H.%M\"), \".csv\"), row.names=F,quote=F)"
      ]
    }
  ],
  "metadata": {
    "_change_revision": 0,
    "_is_fork": false,
    "kernelspec": {
      "display_name": "R",
      "language": "R",
      "name": "ir"
    },
    "language_info": {
      "codemirror_mode": "r",
      "file_extension": ".r",
      "mimetype": "text/x-r-source",
      "name": "R",
      "pygments_lexer": "r",
      "version": "3.3.2"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}