{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "e4fceeb7-9afc-3cc3-bfe0-69b8617b193a"
      },
      "source": [
        "Digit Recognizer by datobig"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "dfedfdce-f47d-d4cf-661e-9ef9eb13e1ac"
      },
      "outputs": [],
      "source": [
        "# This Python 3 environment comes with many helpful analytics libraries installed\n",
        "# It is defined by the kaggle/python docker image: https://github.com/kaggle/docker-python\n",
        "# For example, here's several helpful packages to load in \n",
        "\n",
        "import numpy as np # linear algebra\n",
        "import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n",
        "\n",
        "# Input data files are available in the \"../input/\" directory.\n",
        "# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory\n",
        "\n",
        "from subprocess import check_output\n",
        "print(check_output([\"ls\", \"../input\"]).decode(\"utf8\"))\n",
        "\n",
        "# Any results you write to the current directory are saved as output."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "993f298b-7bdd-8fa0-5aa4-927ecd4efba0"
      },
      "outputs": [],
      "source": [
        "from keras.models import Sequential\n",
        "from keras.layers import Dense, Dropout, Activation, Flatten\n",
        "from keras.layers import Convolution2D, MaxPooling2D\n",
        "from keras.utils import np_utils\n",
        "from keras import backend as K"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "04e7d76d-d510-3967-1381-824c7aa3c87c"
      },
      "outputs": [],
      "source": [
        "training_data = pd.read_csv('../input/train.csv')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "df37107d-41d4-0d34-d738-c7c926786578"
      },
      "outputs": [],
      "source": [
        "testing_data = pd.read_csv('../input/test.csv')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "4bfb21e9-e1e1-cc1e-267f-33be90ed1f65"
      },
      "outputs": [],
      "source": [
        "training_data.shape"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "3bf1703a-674f-fa84-7ebb-3109997df8fa"
      },
      "outputs": [],
      "source": [
        "testing_data.shape"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "5268bb4f-5440-443d-3d76-990afb498704"
      },
      "outputs": [],
      "source": [
        "training_data[:1]"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "3ff240de-5806-4d94-1300-41cbdf6a8bc6"
      },
      "outputs": [],
      "source": [
        "testing_data[:1]"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "c5680006-fe5c-9e70-fc3c-89662cd8e159"
      },
      "outputs": [],
      "source": [
        "y_train = training_data[\"label\"].astype('int32')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "743e47b4-1322-6c7f-0b39-9989dda7919e"
      },
      "outputs": [],
      "source": [
        "y_train.head"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "7c1e0452-73fd-7fc2-7605-dae7b4a93f25"
      },
      "outputs": [],
      "source": [
        "y_train = np_utils.to_categorical(y_train)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "d66fff45-44b1-024b-1104-2d725e6938d7"
      },
      "outputs": [],
      "source": [
        "y_train.shape"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "e958a769-ee1c-8893-67fc-a044171257aa"
      },
      "outputs": [],
      "source": [
        "y_train"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "ff44c1aa-842e-2fd9-1200-240050691eac"
      },
      "outputs": [],
      "source": [
        "X_train = training_data.ix[:,1:].values.astype('float32')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "5344b887-372c-9441-5199-69bc319ce5b3"
      },
      "outputs": [],
      "source": [
        "X_train.shape"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "ee5d7c4c-dda7-854d-c205-a5e63b29c416"
      },
      "outputs": [],
      "source": [
        "X_test = testing_data.ix[:,0:].values.astype('float32')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "4fad7d70-9cdc-873b-6e14-bd91b4aeec4b"
      },
      "outputs": [],
      "source": [
        "X_test.shape"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "5b0758d4-1824-986f-a323-7a1d58bd1900"
      },
      "outputs": [],
      "source": [
        "img_rows, img_cols = 28, 28"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "4e8a96a3-3236-07ff-0b18-2dfc40aef44c"
      },
      "outputs": [],
      "source": [
        "X_train = X_train.reshape(X_train.shape[0], 1, img_rows, img_cols)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "da7d2186-3574-08e7-52ed-64f7678b835e"
      },
      "outputs": [],
      "source": [
        ""
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "0921c392-7c3a-ee33-8dab-d98668f835c9"
      },
      "outputs": [],
      "source": [
        "X_train.shape"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "bc425a90-6536-9de2-5dc2-d14a712f4c94"
      },
      "outputs": [],
      "source": [
        "from keras import backend as K"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "0a8919cf-ea07-e601-32fd-3fa7bcfce78b"
      },
      "outputs": [],
      "source": [
        "K.image_dim_ordering()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "de590d77-7196-2022-15ad-0eed6f5cb7b0"
      },
      "outputs": [],
      "source": [
        "X_test_plot = X_test.reshape(X_test.shape[0],img_rows,img_cols).astype('float32')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "7ba1a160-8dd3-c70a-f1db-7df2ecd95082"
      },
      "outputs": [],
      "source": [
        "X_test_plot.shape"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "f881d31e-553d-ff6d-71e9-7a18a3e77e39"
      },
      "outputs": [],
      "source": [
        "import matplotlib.pyplot as plt\n",
        "# plot 4 images as gray scale\n",
        "plt.subplot(221)\n",
        "plt.imshow(X_test_plot[10], cmap=plt.get_cmap('gray'))\n",
        "plt.subplot(222)\n",
        "plt.imshow(X_test_plot[21], cmap=plt.get_cmap('gray'))\n",
        "plt.subplot(223)\n",
        "plt.imshow(X_test_plot[32], cmap=plt.get_cmap('gray'))\n",
        "plt.subplot(224)\n",
        "plt.imshow(X_test_plot[43], cmap=plt.get_cmap('gray'))\n",
        "# show the plot\n",
        "plt.show()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "a4493e5b-7eeb-bf21-30ac-9b70e39541b8"
      },
      "outputs": [],
      "source": [
        "X_test.shape"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "a6b2c5cd-4c45-d89f-6b6d-a5389262cd24"
      },
      "outputs": [],
      "source": [
        "# normalize inputs from 0-255 to 0-1\n",
        "X_train = X_train / 255\n",
        "X_test = X_test / 255"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "345dd359-0fa4-d5eb-629f-6932f042b7c6"
      },
      "outputs": [],
      "source": [
        "def large_model():\n",
        "    model = Sequential()\n",
        "    model.add(Convolution2D(30, 5, 5, border_mode='valid', input_shape=(1, 28, 28), activation='relu'))\n",
        "    model.add(MaxPooling2D(pool_size=(2, 2)))\n",
        "    model.add(Convolution2D(15, 3, 3, activation='relu'))\n",
        "    model.add(MaxPooling2D(pool_size=(2, 2)))\n",
        "    model.add(Dropout(0.2))\n",
        "    model.add(Flatten())\n",
        "    model.add(Dense(256, activation='relu'))\n",
        "    model.add(Dense(128, activation='relu'))\n",
        "    model.add(Dense(num_classes, activation='softmax'))\n",
        "\n",
        "    model.compile(loss='categorical_crossentropy', optimizer='nadam', metrics=['accuracy'])\n",
        "    return model"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "8ff89556-1b80-7899-f7ea-7e3bb10c4521"
      },
      "outputs": [],
      "source": [
        "K.set_image_dim_ordering('th')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "7a09c724-f98b-80dc-4441-a2b3f72b1730"
      },
      "outputs": [],
      "source": [
        "num_classes = y_train.shape[1]"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "ae5d20b1-209f-139f-4aea-31347b0c4f0a"
      },
      "outputs": [],
      "source": [
        "model = large_model()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "7b70ae3a-e32d-0f24-9211-6329cf4ba8ac"
      },
      "outputs": [],
      "source": [
        "model.fit(X_train, y_train, validation_split=0.1, nb_epoch=2, batch_size=200,verbose=2 )"
      ]
    }
  ],
  "metadata": {
    "_change_revision": 0,
    "_is_fork": false,
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.5.2"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}