{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "901d90d4-3778-8fa2-c6cc-3f76ab6480dd"
      },
      "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": "591b9ff6-359c-d4d2-cc5f-da05c362aaae"
      },
      "outputs": [],
      "source": [
        "train= pd.read_csv(\"../input/train.csv\")\n",
        "train.head(5)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "15a29882-ec73-bb06-2d31-701f087556bc"
      },
      "outputs": [],
      "source": [
        "test= pd.read_csv(\"../input/test.csv\")\n",
        "test.head(5)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "f5c583ac-7161-eac1-b288-f36843cafacc"
      },
      "outputs": [],
      "source": [
        "print(train.shape, test.shape)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "13567f77-a375-7fc2-f7e8-32fca2ed5ce8"
      },
      "source": ""
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "31e9760f-baf0-9d08-0893-df3af952654e"
      },
      "outputs": [],
      "source": [
        "X_train= train.drop(['label'], axis=1)\n",
        "X_train.head(5)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "9bf6c6c9-f610-7bac-1191-0da02b18542b"
      },
      "outputs": [],
      "source": [
        "y_train= train['label']"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "5a245d13-b3fd-74c7-e989-347bb287062e"
      },
      "outputs": [],
      "source": [
        "X_train.shape[0]"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "8281aac0-41e6-25ec-e849-1a5e70f34270"
      },
      "outputs": [],
      "source": [
        "from keras.models import Sequential\n",
        "from keras.layers.core import Dense, Dropout, Activation, Flatten, Reshape\n",
        "from keras.optimizers import SGD, RMSprop\n",
        "from keras.utils import np_utils\n",
        "from keras.regularizers import l2\n",
        "from keras.layers.convolutional import Convolution2D, MaxPooling2D, ZeroPadding2D, AveragePooling2D\n",
        "from keras.callbacks import EarlyStopping\n",
        "from keras.preprocessing.image import ImageDataGenerator\n",
        "from keras.layers.normalization import BatchNormalization\n",
        "from keras import backend as K\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "87d7d95f-7e54-a767-10c8-d1b51db8f758"
      },
      "outputs": [],
      "source": [
        "x_train = train.drop(['label'], axis=1).values.astype('float32')\n",
        "Y_train = train['label'].values.astype('float32')\n",
        "x_test = test.values.astype('float32')\n",
        "\n",
        "img_width, img_height = 28, 28\n",
        "\n",
        "n_train = x_train.shape[0]\n",
        "n_test = x_test.shape[0]\n",
        "\n",
        "n_classes = 10 \n",
        "\n",
        "x_train = x_train.reshape(n_train,1,img_width,img_height)\n",
        "x_test = x_test.reshape(n_test,1,img_width,img_height)\n",
        "\n",
        "x_train = x_train/255 #normalize from [0,255] to [0,1]\n",
        "x_test = x_test/255 \n",
        "\n",
        "y_train = np_utils.to_categorical(Y_train)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "83f9847e-15c2-695d-6d2c-349700c94866"
      },
      "outputs": [],
      "source": [
        "%matplotlib inline\n",
        "import matplotlib.pyplot as plt\n",
        "\n",
        "imgplot = plt.imshow(x_train[100,0,:,:,],cmap='gray')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "5feb51f8-df29-1aad-77d2-2995212675cf"
      },
      "outputs": [],
      "source": [
        "from keras.models import Sequential\n",
        "from keras.layers.convolutional import *\n",
        "from keras.layers.core import Dropout, Dense, Flatten, Activation\n",
        "\n",
        "n_filters = 64\n",
        "filter_size1 = 3\n",
        "filter_size2 = 2\n",
        "pool_size1 = 3\n",
        "pool_size2 = 1\n",
        "n_dense = 128\n",
        "\n",
        "K.set_image_dim_ordering('th')\n",
        "\n",
        "model = Sequential()\n",
        "\n",
        "model.add(Convolution2D(n_filters, filter_size1, filter_size1, batch_input_shape=(None, 1, img_width, img_height), activation='relu', border_mode='valid'))\n",
        "\n",
        "model.add(MaxPooling2D(pool_size=(pool_size1, pool_size1)))\n",
        "\n",
        "model.add(Convolution2D(n_filters, filter_size2, filter_size2, activation='relu', border_mode='valid'))\n",
        "\n",
        "model.add(MaxPooling2D(pool_size=(pool_size2, pool_size2)))\n",
        "\n",
        "model.add(Dropout(0.25))\n",
        "\n",
        "model.add(Flatten())\n",
        "\n",
        "model.add(Dense(n_dense))\n",
        "\n",
        "model.add(Activation('relu'))\n",
        "\n",
        "model.add(Dropout(0.5))\n",
        "\n",
        "model.add(Dense(n_classes))\n",
        "\n",
        "model.add(Activation('softmax'))\n",
        "\n",
        "model.compile(optimizer='adam',\n",
        "              loss='categorical_crossentropy',\n",
        "              metrics=['accuracy'])"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "b52b76ab-4d9a-b8e3-e4d3-d43be6ca5083"
      },
      "outputs": [],
      "source": [
        "model.compile(optimizer='Adam', loss='categorical_crossentropy', metrics=['accuracy'])"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "5c81980c-173c-bf4a-5e71-abde0816faa2"
      },
      "outputs": [],
      "source": [
        "model.fit(x_train,\n",
        "          y_train,\n",
        "          batch_size=128,\n",
        "          nb_epoch=2,verbose=1,\n",
        "          validation_split=.2\n",
        "          )"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "96268201-d16d-b94e-0de9-f3412e493c63"
      },
      "outputs": [],
      "source": [
        "yPred = model.predict_classes(x_test,batch_size=32,verbose=1)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "b107fd4e-a81c-6f00-68c7-0087cd849fff"
      },
      "outputs": [],
      "source": [
        "pd.DataFrame({\"ImageId\": list(range(1,len(x_test)+1)), \n",
        "              \"Label\": yPred}).to_csv('MNIST-submission_1.csv', index=False,header=True)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "2b5da420-14da-fbe3-e9b9-7267b667f867"
      },
      "outputs": [],
      "source": ""
    }
  ],
  "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.6.0"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}