{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "628766d5-5757-3c61-b6bf-e35b99fbc07e"
      },
      "source": ""
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "f4e2d10d-f18a-e628-4e46-4b3cdafd7512"
      },
      "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": "markdown",
      "metadata": {
        "_cell_guid": "c7aec2d1-4467-b69a-60ff-f3d176962405"
      },
      "source": [
        "First of all, i will import the data sets as a panda dataframe."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "7016e29c-8dd0-ed23-252f-7ffd68c51484"
      },
      "outputs": [],
      "source": [
        "train = pd.read_csv('../input/train.csv', header = 0)\n",
        "test = pd.read_csv('../input/test.csv', header = 0)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "0a88502c-77b2-3c7e-6c2b-df44465c5068"
      },
      "outputs": [],
      "source": [
        "# Image soze \n",
        "img_rows, img_cols = 28, 28\n",
        "# Number of pixels\n",
        "num_pixels = img_rows * img_cols\n",
        "# Size of mini batch\n",
        "batch_size = 128\n",
        "# Number of epochs\n",
        "nb_epoch = 20"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "f0476d89-26b2-6309-221b-549f701d658c"
      },
      "source": [
        "For this simple exercise, i will try to modify the training set by choosing boolean values for the pixels : if a pixel has a non-zero value, I will make it 1. It will help decrease computational time without, I guess, affecting too much the outcome."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "86172888-94c9-a026-6c3b-7d786dead8ab"
      },
      "outputs": [],
      "source": [
        "from keras.utils import np_utils\n",
        "from keras.models import Sequential\n",
        "from keras.layers import Dense, Dropout"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "7986e471-b58d-b03c-62a8-dcfd0146627b"
      },
      "outputs": [],
      "source": [
        "# creating a vector of labels, then a matrix of labels\n",
        "Y = train['label']\n",
        "Y = np_utils.to_categorical(Y)\n",
        "\n",
        "nb_classes = Y.shape[1]"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "63b3b714-84c0-0cb7-dd8e-f46399ec5eac"
      },
      "outputs": [],
      "source": [
        "print(Y)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "3aaa4f65-1a4f-863c-0171-6633d7eb9922"
      },
      "outputs": [],
      "source": [
        "train = train.drop(['label'], axis=1)\n",
        "train = train.values\n",
        "print(train)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "6b783e37-3990-f5fd-3468-1f32f24c5604"
      },
      "outputs": [],
      "source": [
        "test = test.values\n",
        "print(test)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "0f825b46-eadb-7274-6716-77831b452bb0"
      },
      "outputs": [],
      "source": [
        "nb_classes = Y.shape[1]"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "3ab16647-f1fb-5509-1a08-8ede5a2847ed"
      },
      "outputs": [],
      "source": [
        "# Creating a sequetial model\n",
        "model = Sequential()\n",
        "# Adding the input layer\n",
        "model.add(Dense(785, input_dim = num_pixels, init=\"normal\", activation=\"relu\"))\n",
        "model.add(Dropout(0.2))\n",
        "# Adding the output layer\n",
        "model.add(Dense(nb_classes, init=\"normal\", activation=\"softmax\"))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "c6b7d2aa-a36c-d57c-ec50-c5eeb1f2b30d"
      },
      "outputs": [],
      "source": [
        "# Compiling the model\n",
        "model.compile(loss=\"categorical_crossentropy\", optimizer=\"adam\", metrics=[\"accuracy\"])"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "cd43f95a-0148-f69e-c0af-75b7e3247034"
      },
      "outputs": [],
      "source": [
        "# Printing the model summary\n",
        "print(model.summary())"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "686d6cb8-95bf-3df0-dc20-819b55f9eff0"
      },
      "outputs": [],
      "source": [
        "# Fitting the model\n",
        "model.fit(train, Y, batch_size=batch_size, nb_epoch=nb_epoch, verbose=1)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "602d19df-85f0-f762-11ee-a9f639240961"
      },
      "outputs": [],
      "source": [
        "# Making predictions\n",
        "predictions = model.predict(test)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "c1a0e2a9-4dd1-5244-d169-245981a1ba22"
      },
      "outputs": [],
      "source": [
        "# Converting the categories to the labels\n",
        "predictions = np_utils.categorical_probas_to_classes(predictions)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "db01376a-f6c3-ef21-c2c8-faade6a31fde"
      },
      "outputs": [],
      "source": [
        "print(predictions)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "ae443478-fac6-610f-eb52-b2795560b548"
      },
      "outputs": [],
      "source": [
        "# Writing data to the output\n",
        "out = np.column_stack((range(1, predictions.shape[0]+1), predictions))\n",
        "np.savetxt(\"submission_v1.csv\", out, header=\"ImageId,Label\", comments=\"\", fmt=\"%d,%d\")"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "0bc433cc-86e3-8611-d647-fa57a4029430"
      },
      "outputs": [],
      "source": [
        "print(out)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "effef2bf-e6df-e45e-c305-237aea5d48b0"
      },
      "outputs": [],
      "source": [
        "print(check_output([\"ls\", \"../input\"]).decode(\"utf8\"))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "4348be17-4603-cbb7-42bc-6fe1457651ad"
      },
      "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
}