{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "261bf68a-af0d-1b78-1fe5-35b907b3f682"
      },
      "source": [
        "Digit Recognizer  with Keras \n",
        "\n",
        "Importing the required Library "
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "7a1fd04f-4fe3-f483-9b78-296a1f10e172"
      },
      "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": "55b5cc44-879b-b4be-40bb-9f2b8f640371"
      },
      "outputs": [],
      "source": [
        "%matplotlib inline\n",
        "import numpy as np\n",
        "import pandas as pd\n",
        "import matplotlib.pyplot as plt\n",
        "import matplotlib\n",
        "from sklearn import metrics\n",
        "from sklearn.neighbors import NearestNeighbors\n",
        "\n",
        "from keras.models import Sequential, Model\n",
        "from keras.layers import Dense, Dropout, Convolution2D, MaxPooling2D, Flatten, Input\n",
        "from keras.optimizers import adam\n",
        "from keras.utils.np_utils import to_categorical\n",
        "\n",
        "import seaborn as sns\n",
        "\n",
        "%config InlineBackend.figure_format = 'retina'"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "9db37b91-1caa-c136-c67d-adc2a1527a1a"
      },
      "outputs": [],
      "source": [
        "train = pd.read_csv(\"../input/train.csv\")"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "cbb51fd4-9e68-c76d-7568-371e423bd0a4"
      },
      "outputs": [],
      "source": [
        "X_train = train.iloc[:,1:].values\n",
        "X_train = X_train.reshape(X_train.shape[0], 28, 28, 1) #reshape to rectangular\n",
        "X_train = X_train/255 #pixel values are 0 - 255 - this makes puts them in the range 0 - 1\n",
        "\n",
        "y_train = train[\"label\"].values"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "33864bff-f52c-9680-9a5e-b2aebfc9ea3e"
      },
      "outputs": [],
      "source": [
        "y_ohe = to_categorical(y_train)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "2b68a3dd-20d4-ed8f-0686-8d1f1a30afe5"
      },
      "outputs": [],
      "source": [
        "model = Sequential()\n",
        "\n",
        "model.add(Convolution2D(20, 5, 5, input_shape = (28, 28, 1), activation=\"relu\"))\n",
        "model.add(MaxPooling2D(pool_size=(2,2)))\n",
        "model.add(Dropout(0.5))\n",
        "\n",
        "model.add(Convolution2D(40, 5, 5, activation=\"relu\"))\n",
        "model.add(MaxPooling2D(pool_size=(2,2)))\n",
        "model.add(Dropout(0.5))\n",
        "\n",
        "model.add(Flatten())\n",
        "model.add(Dense(100, activation = \"relu\"))\n",
        "model.add(Dropout(0.5))\n",
        "model.add(Dense(100, activation = \"relu\"))\n",
        "model.add(Dense(10, activation=\"softmax\"))\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "de5c663a-dc00-14aa-ed6d-9169cbe1b5ea"
      },
      "outputs": [],
      "source": [
        "model.compile(loss='categorical_crossentropy', \n",
        "              optimizer = adam(lr=0.001), metrics = [\"accuracy\"])"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "e26ede33-a5cf-b10f-62d8-0417ec6fa715"
      },
      "outputs": [],
      "source": [
        "hist = model.fit(X_train, y_ohe,\n",
        "          validation_split = 0.05, batch_size = 128, epochs = 8)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "70c83969-70c5-da98-e33c-e24cf88f4fa5"
      },
      "outputs": [],
      "source": [
        "model.save_weights(\"model.h5\")"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "4a4db34f-4c3f-26a3-c642-a77f6d47dbde"
      },
      "outputs": [],
      "source": [
        "model.load_weights(\"model.h5\")"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "07573ace-d0b8-f54c-57d5-56f1e213cc18"
      },
      "outputs": [],
      "source": [
        "loss_and_metrics = model.evaluate(X_train, y_ohe, batch_size=128)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "61f297e7-a3f4-4e18-da64-73e171a0b68e"
      },
      "outputs": [],
      "source": [
        "#getting the 2D output:\n",
        "output = model.get_layer(\"dense_3\").output\n",
        "extr = Model(model.input, output)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "1e686595-d701-5e27-7b45-e19b0815605e"
      },
      "outputs": [],
      "source": [
        "matplotlib.rcParams['figure.figsize'] = (10.0, 10.0)\n",
        "\n",
        "X_proj = extr.predict(X_train[:10000])\n",
        "X_proj.shape\n",
        "\n",
        "proj = pd.DataFrame(X_proj[:,:2])\n",
        "proj.columns = [\"comp_1\", \"comp_2\"]\n",
        "proj[\"labels\"] = y_train[:10000]\n",
        "\n",
        "sns.lmplot(\"comp_1\", \"comp_2\",hue = \"labels\", data = proj, fit_reg=False)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "76f10e8b-a4e0-b7d6-a906-60c20f3f8468"
      },
      "outputs": [],
      "source": [
        "test = pd.read_csv(\"../input/test.csv\")"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "a7595197-7267-9f01-4511-b435efd4532b"
      },
      "outputs": [],
      "source": [
        "test = test.iloc[:,:].values\n",
        "test = test.reshape(test.shape[0], 28, 28, 1)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "6c825917-4247-13b8-d88c-7161155e73d6"
      },
      "outputs": [],
      "source": [
        "pred = model.predict_classes(test,verbose=0)\n",
        "\n",
        "submissions=pd.DataFrame({\"ImageId\": list(range(1,len(pred)+1)),\n",
        "                         \"Label\": pred})"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "ad045a71-0be4-a9ab-d4ce-53fc75f8a0c3"
      },
      "outputs": [],
      "source": [
        "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
}