{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "9ccbae5e-99b8-6dfd-60e6-2be22b7a261f"
      },
      "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",
        "import matplotlib.pyplot as plt\n",
        "%matplotlib inline\n",
        "\n",
        "from keras.models import Sequential\n",
        "from keras.layers import Dense , Dropout , Lambda, Flatten\n",
        "from keras.optimizers import Adam ,RMSprop\n",
        "from sklearn.model_selection import train_test_split\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": "3835e159-d89b-7d47-52e8-56aa7433815b"
      },
      "outputs": [],
      "source": [
        "# create the training & test sets, skipping the header row with [1:]\n",
        "train = pd.read_csv(\"../input/train.csv\")\n",
        "\n",
        "test_images = (pd.read_csv(\"../input/test.csv\").values).astype('float32')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "de6fb098-0105-72ff-d172-8ce04f63b152"
      },
      "outputs": [],
      "source": [
        "train_images = (train.ix[:,1:].values).astype('float32')\n",
        "train_labels = train.ix[:,0].values.astype('int32')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "753789db-d333-4d20-48a7-453859ed5651"
      },
      "outputs": [],
      "source": [
        "train_images.shape"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "1dfd9f9c-7953-af4b-426f-dc1977b6c4c1"
      },
      "outputs": [],
      "source": [
        "\n",
        "\n",
        "train_images = train_images.reshape(train_images.shape[0],  28, 28)\n",
        "\n",
        "for i in range(6, 9):\n",
        "    plt.subplot(330 + (i+1))\n",
        "    plt.imshow(train_images[i], cmap=plt.get_cmap('gray'))\n",
        "    plt.title(train_labels[i]);"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "38210f85-7ccc-1d61-a894-95a44517c9ce"
      },
      "outputs": [],
      "source": [
        "train_images = train_images.reshape((42000, 28 * 28))\n",
        "\n",
        "test_images.shape"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "6fe3a1dd-09d1-5054-afb8-5da2dbf4f32a"
      },
      "outputs": [],
      "source": [
        "train_images = train_images / 255\n",
        "test_images = test_images / 255"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "d92c4def-cfab-d2e2-36e8-b66be57fbca2"
      },
      "outputs": [],
      "source": [
        "from keras.utils.np_utils import to_categorical\n",
        "train_labels = to_categorical(train_labels)\n",
        "num_classes = train_labels.shape[1]\n",
        "num_classes"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "b9c61103-2c4e-61fc-31fc-265a723caec4"
      },
      "outputs": [],
      "source": [
        "plt.title(train_labels[9])\n",
        "plt.plot(train_labels[9])\n",
        "plt.xticks(range(10));"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "e6f5b5aa-b0a7-fd74-15f8-567f122cfe77"
      },
      "outputs": [],
      "source": [
        "# fix random seed for reproducibility\n",
        "seed = 43\n",
        "np.random.seed(seed)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "6844576a-ac38-ddd5-0d1f-ebb4cd82f351"
      },
      "outputs": [],
      "source": [
        "from keras.models import Sequential\n",
        "from keras.layers import Dense , Dropout\n",
        "\n",
        "model=Sequential()\n",
        "model.add(Dense(32,activation='relu',input_dim=(28 * 28)))\n",
        "model.add(Dense(16,activation='relu'))\n",
        "model.add(Dense(10,activation='softmax'))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "625993c8-1deb-3983-6d72-12cf63faca98"
      },
      "outputs": [],
      "source": [
        "from keras.optimizers import RMSprop\n",
        "model.compile(optimizer=RMSprop(lr=0.001),\n",
        " loss='categorical_crossentropy',\n",
        " metrics=['accuracy'])"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "7699cff4-5f8c-c11c-2a72-78f6d62f634a"
      },
      "outputs": [],
      "source": [
        "history=model.fit(train_images, train_labels, validation_split = 0.05, \n",
        "            nb_epoch=25, batch_size=64)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "b49df2bd-90a5-d71e-a99e-b3fd22eb546d"
      },
      "outputs": [],
      "source": [
        "history_dict = history.history\n",
        "history_dict.keys()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "53696f8a-1f8b-fdfc-831e-f720c969c921"
      },
      "outputs": [],
      "source": [
        "import matplotlib.pyplot as plt\n",
        "%matplotlib inline\n",
        "loss_values = history_dict['loss']\n",
        "val_loss_values = history_dict['val_loss']\n",
        "epochs = range(1, len(loss_values) + 1)\n",
        "\n",
        "# \"bo\" is for \"blue dot\"\n",
        "plt.plot(epochs, loss_values, 'bo')\n",
        "# b+ is for \"blue crosses\"\n",
        "plt.plot(epochs, val_loss_values, 'b+')\n",
        "plt.xlabel('Epochs')\n",
        "plt.ylabel('Loss')\n",
        "\n",
        "plt.show()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "cf368b85-6326-47d3-7314-02500f0cb9d0"
      },
      "outputs": [],
      "source": [
        "plt.clf()   # clear figure\n",
        "acc_values = history_dict['acc']\n",
        "val_acc_values = history_dict['val_acc']\n",
        "\n",
        "plt.plot(epochs, acc_values, 'bo')\n",
        "plt.plot(epochs, val_acc_values, 'b+')\n",
        "plt.xlabel('Epochs')\n",
        "plt.ylabel('Accuracy')\n",
        "\n",
        "plt.show()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "06a6768a-91a0-e4cc-c55e-23984c953d47"
      },
      "outputs": [],
      "source": [
        "model = Sequential()\n",
        "model.add(Dense(64, activation='relu',input_dim=(28 * 28)))\n",
        "model.add(Dense(128, activation='relu'))\n",
        "model.add(Dropout(0.15))\n",
        "model.add(Dense(64, activation='relu'))\n",
        "model.add(Dropout(0.15))\n",
        "model.add(Dense(10, activation='softmax'))\n",
        "\n",
        "\n",
        "model.compile(optimizer=RMSprop(lr=0.0001), loss='categorical_crossentropy',\n",
        " metrics=['accuracy'])\n",
        "\n",
        "history=model.fit(train_images, train_labels, \n",
        "            nb_epoch=15, batch_size=64)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "20d909c6-91f0-635a-1991-e1b991edda4f"
      },
      "outputs": [],
      "source": [
        "predictions = model.predict_classes(test_images, verbose=0)\n",
        "\n",
        "submissions=pd.DataFrame({\"ImageId\": list(range(1,len(predictions)+1)),\n",
        "                         \"Label\": predictions})\n",
        "submissions.to_csv(\"DR.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.6.0"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}