{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "c4af1343-c879-0137-9ffd-8e338351bd04"
      },
      "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": "3ccdbb4f-3f3a-8aa1-a355-ac01eca0ce5f"
      },
      "outputs": [],
      "source": [
        "from keras.models import Sequential\n",
        "from keras.utils import np_utils\n",
        "from keras.layers.core import Dense, Activation, Dropout"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "16982267-f3b8-9311-be26-1c0ca8e4fe05"
      },
      "outputs": [],
      "source": [
        "import pandas as pd\n",
        "import numpy as np"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "0a985371-01b1-0988-692a-f9ed6c8ef898"
      },
      "outputs": [],
      "source": [
        "# Read data\n",
        "train = pd.read_csv('../input/train.csv')\n",
        "labels = train.ix[:,0].values.astype('int32')\n",
        "X_train = (train.ix[:,1:].values).astype('float32')\n",
        "X_test = (pd.read_csv('../input/test.csv').values).astype('float32')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "26607689-1b38-a64d-f7a3-5dc4ca76f263"
      },
      "outputs": [],
      "source": [
        "# convert list of labels to binary class matrix\n",
        "y_train = np_utils.to_categorical(labels) "
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "a605db0f-75ee-9257-0bf8-a4a9342c198e"
      },
      "outputs": [],
      "source": [
        "# pre-processing: divide by max and substract mean\n",
        "scale = np.max(X_train)\n",
        "X_train /= scale\n",
        "X_test /= scale\n",
        "\n",
        "mean = np.std(X_train)\n",
        "X_train -= mean\n",
        "X_test -= mean"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "dc086737-c9d5-7f56-eeff-758bf887f8c9"
      },
      "outputs": [],
      "source": [
        "input_dim = X_train.shape[1]\n",
        "nb_classes = y_train.shape[1]"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "0ea33b05-6b6c-32d7-d97a-ecd6ceedc7fa"
      },
      "outputs": [],
      "source": [
        "input_dim"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "e76b6745-6276-b7d0-c53b-f40bb87e4aad"
      },
      "outputs": [],
      "source": [
        "nb_classes"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "e1c38457-2fc7-7c06-60ca-7e7cc4beaf88"
      },
      "outputs": [],
      "source": [
        "# Here's a Deep Dump MLP (DDMLP)\n",
        "model = Sequential()\n",
        "model.add(Dense(128, input_dim=input_dim))\n",
        "model.add(Activation('relu'))\n",
        "model.add(Dropout(0.15))\n",
        "model.add(Dense(128))\n",
        "model.add(Activation('relu'))\n",
        "model.add(Dropout(0.15))\n",
        "model.add(Dense(nb_classes))\n",
        "model.add(Activation('softmax'))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "f2cf6425-962a-e06f-1dc7-550b660563a4"
      },
      "outputs": [],
      "source": [
        "# we'll use categorical xent for the loss, and RMSprop as the optimizer\n",
        "model.compile(loss='categorical_crossentropy', optimizer='rmsprop',metrics=['accuracy'])\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "b212c8db-ecda-2ddb-5d0d-f5e3de495d9c"
      },
      "outputs": [],
      "source": [
        "print(\"Training...\")\n",
        "model.fit(X_train, y_train, nb_epoch=10, batch_size=16, validation_split=0.1, verbose=2)\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "ccf5df4e-9bea-f36b-328a-154097040c12"
      },
      "outputs": [],
      "source": [
        "print(\"Generating test predictions...\")\n",
        "preds = model.predict_classes(X_test, verbose=0)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "be81220a-43fb-f8ef-96c9-c4cdc844b382"
      },
      "outputs": [],
      "source": [
        "def write_preds(preds, fname):\n",
        "    pd.DataFrame({\"ImageId\": list(range(1,len(preds)+1)), \"Label\": preds}).to_csv(fname, index=False, header=True)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "389e65a3-ef43-ce94-8771-2d037c094067"
      },
      "outputs": [],
      "source": [
        "write_preds(preds, \"keras-mlp.csv\")"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "45318a68-9a7b-d770-0c58-d0fc91222972"
      },
      "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
}