{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "918f6cd0-a1c3-4cba-df8d-a490d0cb0279"
      },
      "source": [
        "This is my solution to the Digits recognition competition using a simple deep learning model in Keras. This was my first time building a model in keras, so i'm excited that it performed pretty well. I hope this notebook will be helpful in getting other people who are new to deep learning started. If you are new to deep learning, I recommend the DataCamp course https://www.datacamp.com/courses/deep-learning-in-python ; I learned everything I needed for this notebook in that course. Enjoy!"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "c8baf9b2-0365-0e09-8e38-002cd5b0bbfa"
      },
      "outputs": [],
      "source": [
        "# import libraries\n",
        "import numpy as np # \n",
        "import pandas as pd # \n",
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "b17dec30-15ad-f53a-1099-589dd4723233"
      },
      "outputs": [],
      "source": [
        "# load the training data and inspect\n",
        "digits = pd.read_csv('../input/train.csv')\n",
        "#digits = pd.read_csv('data/train.csv')\n",
        "digits.head()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "cc371177-8cf9-0c92-fe06-32067c1181eb"
      },
      "outputs": [],
      "source": [
        "# let's look at the structure of one row (image)\n",
        "a = digits.iloc[3,1:].values\n",
        "b = a.reshape((28,28))\n",
        "print(b)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "fbf2a389-f8fb-a6f2-fcee-4fa3534d53c9"
      },
      "outputs": [],
      "source": [
        ""
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "ecc80d9a-2044-cf08-58a8-547fa97088bc"
      },
      "outputs": [],
      "source": [
        "# plot one of the images\n",
        "import matplotlib.pyplot as plt\n",
        "plt.imshow(b)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "8aac8289-9884-a064-6f10-f36014fdeabf"
      },
      "outputs": [],
      "source": [
        "# split the training data into predictors (X) and targets (y)\n",
        "X=digits.iloc[:,1:].values\n",
        "y=digits.iloc[:,0].values\n",
        "\n",
        "# Scale the data to be between 0 and 1 (* this makes big difference in results!*)\n",
        "X=np.divide(X,255.)\n",
        "\n",
        "# create dummie variables for the target\n",
        "from keras.utils.np_utils import to_categorical\n",
        "y = to_categorical(y)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "b2cddfe1-d323-68a2-1ca8-640093b1ea60"
      },
      "outputs": [],
      "source": [
        "# Now import keras libraries and start building model\n",
        "import keras\n",
        "from keras.models import Sequential\n",
        "from keras.layers import Dense\n",
        "from keras.callbacks import EarlyStopping"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "28b4f1bd-403c-ace4-7855-c102384b5c7a"
      },
      "outputs": [],
      "source": [
        "model = Sequential()\n",
        "model.add(Dense(100,activation='relu',input_shape=(784,)))\n",
        "model.add(Dense(15,activation='relu'))\n",
        "model.add(Dense(10,activation='softmax'))\n",
        "model.compile(optimizer='adam',loss='categorical_crossentropy',metrics=['accuracy'])\n",
        "# the EarlyStopping stops the model training if it does not improve after 5 epochs\n",
        "history=model.fit(X,y,validation_split=0.3,callbacks=[EarlyStopping(patience=5)],epochs=50)\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "122599f6-5206-6b16-1abc-337ccfc0187b"
      },
      "outputs": [],
      "source": [
        "# plot loss vs # model epochs trained\n",
        "plt.plot(history.history['val_loss'])\n",
        "plt.plot(history.history['loss'])\n",
        "plt.grid()\n",
        "plt.xlabel('epoch')\n",
        "plt.ylabel('loss')\n",
        "plt.legend(['valid','train'])"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "d238cf49-4116-a1da-2651-329547deee6a"
      },
      "outputs": [],
      "source": [
        "# plot accuracy vs # epochs\n",
        "plt.plot(history.history['val_acc'])\n",
        "plt.plot(history.history['acc'])\n",
        "plt.legend(['valid','train'])\n",
        "plt.grid()\n",
        "plt.xlabel('epoch')\n",
        "plt.ylabel('accuracy')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "cc82ba6d-eb69-3a9b-f66e-4f4b6427b84d"
      },
      "outputs": [],
      "source": [
        "# predict on test set and write csv file for submission\n",
        "#test = pd.read_csv('data/test.csv')\n",
        "test = pd.read_csv('../input/test.csv')\n",
        "test.head()\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "fe85bc41-42c8-e3d5-c414-00dd7b05258b"
      },
      "outputs": [],
      "source": [
        "preds = model.predict_classes( np.divide(test.values,255.) )\n",
        "results = pd.DataFrame({'Label':preds})\n",
        "results.head()\n",
        "results.index+=1\n",
        "results = results.reindex(results.index.rename('ImageId'))\n",
        "results.head()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "2c1ff5b4-fc91-800d-c23b-637ae122671d"
      },
      "outputs": [],
      "source": [
        "# results.to_csv('results.csv', 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.6.0"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}