{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "bb11f314-dc4f-a41d-0bdc-50d776cbbce4"
      },
      "source": [
        "## Data exploration\n",
        "## Model building exercise\n",
        "- One-hidden layer MLP"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "64de1912-edba-bffe-ff1a-1b699eae9c3c"
      },
      "outputs": [],
      "source": [
        "import os\n",
        "os.listdir('../input') # ['test.csv', 'train.csv']"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "e7416372-a9a8-734f-f6e1-fc0764eec640"
      },
      "outputs": [],
      "source": [
        "import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "433d186c-4dcb-8344-bfbe-04a5d67fd896"
      },
      "outputs": [],
      "source": [
        "trainDF = pd.read_csv('../input/train.csv')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "d6a99529-a6f7-f493-aacf-7a82f8a96f8c"
      },
      "outputs": [],
      "source": [
        "trainDF.columns"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "31a46724-229d-e2c7-3862-de494cdd5b43"
      },
      "outputs": [],
      "source": [
        "Y,X=trainDF.iloc[:,0].values,trainDF.iloc[:,range(1,785)].values"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "a672263e-ef0b-8774-b962-cca2e7d9f736"
      },
      "outputs": [],
      "source": [
        "Y.shape,X.shape"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "b9f5e9c6-4f0f-da92-e885-8698c7044d02"
      },
      "outputs": [],
      "source": [
        "from keras.models import Sequential\n",
        "from keras.layers import Dense\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "4060ea04-a6b0-3b3a-c7dd-7baf5c2755c3"
      },
      "outputs": [],
      "source": [
        "model = Sequential()\n",
        "model.add(Dense(30,input_dim=784,activation='relu'))\n",
        "model.add(Dense(10,activation='softmax'))\n",
        "model.summary()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "95888ada-8905-05ee-6911-3c7539220829"
      },
      "outputs": [],
      "source": [
        "30*785+31*10"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "4167e26d-6d33-1d99-cd54-a064f885ab65"
      },
      "outputs": [],
      "source": [
        "model.compile(optimizer='sgd',loss='categorical_crossentropy',metrics=['accuracy'])"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "5c04af8e-c5a5-1903-ca50-5a95ef9ac3d5"
      },
      "outputs": [],
      "source": ""
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "49ac36aa-9804-7114-f0d2-2340795f9e29"
      },
      "outputs": [],
      "source": [
        "from keras.utils import to_categorical\n",
        "Y_ = to_categorical(Y)\n",
        "history = model.fit(X,Y_,epochs=2,batch_size=128,verbose=1)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "5cc41022-fd2a-b98b-e56b-4513b4b0ab85"
      },
      "outputs": [],
      "source": [
        "history.history"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "5436dd3d-f5af-2c02-ea61-a756e623ec01"
      },
      "outputs": [],
      "source": [
        "history = model.fit(X,Y_,epochs=3,batch_size=128,verbose=1)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "cd4e93e4-a973-a5b9-a9f7-85d0b5d8c6e3"
      },
      "outputs": [],
      "source": [
        "history.history"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "8d1f505d-9e1e-4061-ef17-035dd8578326"
      },
      "outputs": [],
      "source": [
        "history = model.fit(X,Y_,epochs=3,batch_size=128,verbose=1,validation_split=.3)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "c2b276f0-55c4-465b-2ead-96ccf350530a"
      },
      "outputs": [],
      "source": [
        "history.history"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "fc61e552-6c60-9be5-8f3c-c9899752b3f5"
      },
      "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
}