{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "5d6af6ae-bc4c-dc55-cb3d-539b8fbc59e7"
      },
      "outputs": [],
      "source": [
        "import numpy as np \n",
        "import pandas as pd \n",
        "\n",
        "from subprocess import check_output\n",
        "print(check_output([\"ls\", \"../input\"]).decode(\"utf8\"))\n",
        "\n",
        "import numpy\n",
        "from keras.datasets import mnist\n",
        "from keras.models import Sequential\n",
        "from keras.layers import Dense\n",
        "from keras.layers import Dropout\n",
        "from keras.layers import Flatten\n",
        "from keras.layers.convolutional import Convolution2D\n",
        "from keras.layers.convolutional import MaxPooling2D\n",
        "from keras.utils import np_utils\n",
        "from sklearn.preprocessing import StandardScaler\n",
        "from matplotlib import pyplot as plt\n",
        "from matplotlib import cm"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "322fe72b-a9f5-5560-440a-e8ad06164228"
      },
      "outputs": [],
      "source": [
        "seed = 18\n",
        "numpy.random.seed(seed)\n",
        "\n",
        "train = pd.read_csv(\"../input/train.csv\")\n",
        "target = train[\"label\"]\n",
        "\n",
        "y_train = train[\"label\"]\n",
        "X_train = train.drop(\"label\",1)\n",
        "\n",
        "y_train = y_train[2000:]\n",
        "y_valid = y_train[:2000]\n",
        "\n",
        "X_train = X_train[2000:]\n",
        "X_valid = X_train[:2000]"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "38c721b3-2bba-62e2-4c7c-b0d3db796b29"
      },
      "outputs": [],
      "source": [
        "X_train = StandardScaler().fit(X_train).transform(X_train)\n",
        "X_valid = StandardScaler().fit(X_valid).transform(X_valid)\n",
        "\n",
        "X_train = X_train.reshape(X_train.shape[0], 28, 28, 1).astype('float32')\n",
        "X_valid = X_valid.reshape(X_valid.shape[0], 28, 28, 1).astype('float32')\n",
        "\n",
        "for i in range(9):\n",
        "    plt.subplot(331+i)\n",
        "    plt.imshow(X_train.reshape(-1,1,28,28)[i][0], cmap=cm.binary)\n",
        "plt.show()\n",
        "print(target[2000:2009])"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "e7755d22-b9c6-7ab8-4949-03950c9d8e16"
      },
      "outputs": [],
      "source": [
        "# normalize inputs from 0-255 to 0-1\n",
        "X_train = X_train / 255\n",
        "X_valid = X_valid / 255\n",
        "# one hot encode outputs\n",
        "y_train = np_utils.to_categorical(y_train)\n",
        "y_valid = np_utils.to_categorical(y_valid)\n",
        "num_classes = y_valid.shape[1]"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "c42d818b-cf60-4016-be22-37abdf8169db"
      },
      "outputs": [],
      "source": [
        "def larger_model():\n",
        "\t# create model\n",
        "\tmodel = Sequential()\n",
        "\tmodel.add(Convolution2D(30, 5, 5, border_mode='valid', input_shape=(28, 28, 1), activation='relu'))\n",
        "\tmodel.add(MaxPooling2D(pool_size=(2, 2)))\n",
        "\tmodel.add(Convolution2D(15, 3, 3, activation='relu'))\n",
        "\tmodel.add(MaxPooling2D(pool_size=(2, 2)))\n",
        "\tmodel.add(Dropout(0.2))\n",
        "\tmodel.add(Flatten())\n",
        "\tmodel.add(Dense(128, activation='relu'))\n",
        "\tmodel.add(Dense(50, activation='relu'))\n",
        "\tmodel.add(Dense(num_classes, activation='softmax'))\n",
        "\t# Compile model\n",
        "\tmodel.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])\n",
        "\treturn model"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "7dfc1e7d-17be-c814-8f3c-6943e6a2503f"
      },
      "outputs": [],
      "source": [
        "# build the model\n",
        "model = larger_model()\n",
        "# Fit the model\n",
        "model.fit(X_train, y_train, validation_data=(X_valid, y_valid), nb_epoch=25, batch_size=200, verbose=2)\n",
        "# Final evaluation of the model\n",
        "scores = model.evaluate(X_valid, y_valid, verbose=0)\n",
        "print(\"Classification Error: %.2f%%\" % (100-scores[1]*100))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "13790e63-8985-7c69-1074-f7ed51c61578"
      },
      "outputs": [],
      "source": [
        "test = pd.read_csv(\"../input/test.csv\")\n",
        "test = StandardScaler().fit(test).transform(test)\n",
        "test = test.reshape(test.shape[0], 28, 28, 1).astype('float32')\n",
        "test = test / 255"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "29709811-df90-fa67-2e6b-faf337dd6176"
      },
      "outputs": [],
      "source": [
        "submission = model.predict_classes(test, verbose=2)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "bb400ab7-db16-9ad5-9c8d-510333a8881e"
      },
      "outputs": [],
      "source": [
        "pd.DataFrame({\"ImageId\": list(range(1,len(test)+1)), \n",
        "              \"Label\": submission}).to_csv('submission.csv', index=False,header=True)"
      ]
    }
  ],
  "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
}