{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "7386e70c-257a-89e5-8574-f0042f8bb3db"
      },
      "source": [
        "A single-layer perceptron on the Digits Datas"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "b3ed196e-0069-e12b-4655-345551056cdb"
      },
      "outputs": [],
      "source": [
        "import numpy as np\n",
        "import pandas as pd\n",
        "\n",
        "from keras.models import Sequential\n",
        "from keras.layers import Dense, Dropout\n",
        "from keras.optimizers import adam\n",
        "from keras.wrappers.scikit_learn import KerasClassifier\n",
        "from keras.utils.np_utils import to_categorical\n",
        "\n",
        "\n",
        "from sklearn.cross_validation import StratifiedKFold, train_test_split\n",
        "from sklearn.ensemble import RandomForestClassifier\n",
        "from sklearn.preprocessing import StandardScaler\n",
        "from sklearn.decomposition import PCA"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "c5fd712f-488a-eea4-b4b8-ff3be815a8dd"
      },
      "outputs": [],
      "source": [
        "train = pd.read_csv(\"../input/train.csv\")\n",
        "test  = pd.read_csv(\"../input/test.csv\")"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "619424cc-a50e-86a0-8f1f-4533e9f11b21"
      },
      "outputs": [],
      "source": [
        "X_train = train.loc[:,\"pixel0\":\"pixel783\"]\n",
        "y_train = train.label\n",
        "X_test = test.loc[:,\"pixel0\":\"pixel783\"]"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "4d89006b-d0fd-6ac0-7f88-fadaba312dcf"
      },
      "outputs": [],
      "source": [
        "y_dummy_train = to_categorical(y_train)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "fc7667d5-26eb-1372-3cc7-cc5237a6da91"
      },
      "outputs": [],
      "source": [
        "y_dummy_train.shape"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "5e2ad804-e2a7-3b06-757a-92263f3dd706"
      },
      "outputs": [],
      "source": [
        "#pca = PCA(n_components = 100)\n",
        "#X_pca = pca.fit_transform(X_train)\n",
        "X_pca = StandardScaler().fit_transform(X_train)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "71d66dd4-a91e-0217-5efc-a90973dbc4f3"
      },
      "outputs": [],
      "source": [
        "#X_test_pca = pca.transform(X_test)\n",
        "X_test_pca = StandardScaler().fit_transform(X_test)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "70f10c8a-3f68-c605-ce26-1c6873ed24a7"
      },
      "outputs": [],
      "source": [
        "#building the keras model:\n",
        "model = Sequential()\n",
        "model.add(Dense(800, input_dim=784, init='normal', activation=\"relu\"))\n",
        "model.add(Dropout(0.2))\n",
        "model.add(Dense(10, activation=\"softmax\"))\n",
        "model.compile(loss='categorical_crossentropy', optimizer='adam', metrics = [\"accuracy\"])"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "e01a4f9c-0bb7-a130-aa15-a4dbb2b32834"
      },
      "outputs": [],
      "source": [
        "hist = model.fit(X_pca, y_dummy_train, nb_epoch=20, batch_size=64, verbose = 0) #validation_split=0.2)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "b863faf8-dcf6-ba11-a260-a21c112236f6"
      },
      "outputs": [],
      "source": [
        "#scores = pd.DataFrame(hist.history)\n",
        "#scores.loc[:5,[\"acc\", \"val_acc\"]].plot()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "6a3223d5-a30d-0591-145d-9d798a24f1cb"
      },
      "outputs": [],
      "source": [
        "#scores.val_acc.max()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "525e3b9f-1fe9-e3aa-9a54-ea1382264352"
      },
      "outputs": [],
      "source": [
        "preds = model.predict_classes(X_test_pca)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "eae812a9-75af-ed79-bf5f-670d0a7613bb"
      },
      "outputs": [],
      "source": [
        "solution = pd.DataFrame({\"ImageId\": np.arange(1,(len(preds)+1)),  \"Label\":preds})"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "19427472-aee4-d260-9633-8b32c070886e"
      },
      "outputs": [],
      "source": [
        "solution.to_csv(\"solution.csv\", index=False)"
      ]
    }
  ],
  "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.5.2"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}