{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "d6f9290b-6da2-edd8-45f0-0e5c33170268"
      },
      "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": "8b36ac46-41ca-f055-c75f-0d97ca5767d1"
      },
      "outputs": [],
      "source": [
        "import numpy as np\n",
        "import pandas as pd\n",
        "import matplotlib.pyplot as plt\n",
        "%matplotlib inline\n",
        "from keras.models import Sequential\n",
        "from keras.layers import Dropout, Dense, Lambda, Flatten\n",
        "from keras.optimizers import Adam, RMSprop\n",
        "from keras.utils.np_utils import to_categorical\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"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "9cb1db83-c17f-4daf-9363-17dbb953e379"
      },
      "outputs": [],
      "source": [
        "train = pd.read_csv(\"../input/train.csv\")\n",
        "\n",
        "test_images = (pd.read_csv(\"../input/test.csv\").values).astype('float32')\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "6a4748e1-c69a-824f-a90a-d1f995f87bf3"
      },
      "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": "a3dccd09-d2a3-0baf-e4ac-ff69b3ddb5c0"
      },
      "outputs": [],
      "source": [
        "train_images = train_images.reshape((42000, 28 * 28))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "295b2a55-e212-b822-b6c0-dc2e8fac76d0"
      },
      "outputs": [],
      "source": [
        "train_images = train_images / 255\n",
        "test_images = test_images / 255\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "1bbd84ee-9b72-4b83-8d77-3b4b0e764ba9"
      },
      "source": [
        "one hot encoding: the labels are converted to , say [0,1,0,0,0,0,0,0,0,0] for 2\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "b5b072e9-2030-c725-8fbd-56a8b7a010c4"
      },
      "outputs": [],
      "source": [
        "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": "3b705728-b623-ab69-66e1-810e88b8f3ff"
      },
      "outputs": [],
      "source": [
        "DESIGNING NEURAL NETWORK"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "9e7d76c7-61c7-bc40-efb8-40b926039270"
      },
      "outputs": [],
      "source": [
        "seed=43\n",
        "np.random.seed(seed)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "dd9ffe57-c1d3-cdca-c5aa-d2548be7a938"
      },
      "outputs": [],
      "source": [
        "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": "a0a7ec22-19e2-a0f4-7427-b49d33674c49"
      },
      "outputs": [],
      "source": [
        "model.compile(optimizer=RMSprop(lr=0.001),loss='categorical_crossentropy',metrics=['accuracy'])"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "d4a24d59-15eb-e7d2-09b8-91c9d081836c"
      },
      "outputs": [],
      "source": [
        "history=model.fit(train_images, train_labels, validation_split = 0.05, nb_epoch=15, batch_size=64)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "837efeb0-0e48-8a8f-7382-bb5133f41946"
      },
      "outputs": [],
      "source": [
        "history_dict = history.history\n",
        "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()\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "d66488b0-bd5f-357a-7c13-a08280cb695a"
      },
      "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)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "c209079f-78bd-30ea-98b9-f56ee7fda37e"
      },
      "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
}