{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "2a7cfb5e-ae0f-ab13-8dcd-4e1486f1cf31"
      },
      "outputs": [],
      "source": [
        "# Import libraries\n",
        "\n",
        "import numpy as np\n",
        "import pandas as pd\n",
        "from sklearn.preprocessing import StandardScaler\n",
        "from sklearn import svm\n",
        "from sklearn.neural_network import MLPClassifier"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "0e242afe-adbf-d1ad-aa25-5734fe4a5400"
      },
      "outputs": [],
      "source": [
        "# Import data\n",
        "\n",
        "train_df = pd.read_csv('../input/train.csv')\n",
        "test_df = pd.read_csv('../input/test.csv')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "ec93d700-cb7a-5531-4108-5cde2c5c8377"
      },
      "outputs": [],
      "source": [
        "# Split training data into features and labels dataframes\n",
        "\n",
        "train_features = train_df.drop(['label'],1)\n",
        "train_labels = train_df['label']"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "1ca5f255-64f6-a29c-6ce8-175106b47725"
      },
      "outputs": [],
      "source": [
        "# Convert to np array and convert int to float\n",
        "\n",
        "X_train_array = np.array(train_features)\n",
        "X_train_array = X_train_array.astype('float')\n",
        "\n",
        "y_train_array = np.array(train_labels)\n",
        "y_train_array = y_train_array.astype('float')\n",
        "\n",
        "\n",
        "X_test_array = np.array(test_df)\n",
        "X_test_array = X_test_array.astype('float')\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "2c7fbe79-7ddb-4d91-b2a7-d44a03224e94"
      },
      "outputs": [],
      "source": [
        "# Scale the data\n",
        "\n",
        "scaler = StandardScaler().fit(X_train_array)\n",
        "X_train = scaler.transform(X_train_array)\n",
        "X_test = scaler.transform(X_test_array)\n",
        "\n",
        "# The labels are not scaled\n",
        "\n",
        "y_train = y_train_array"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "9f0b27c1-8cc7-7d2e-983f-d645d54b5fd4"
      },
      "outputs": [],
      "source": [
        "# Choose classifier\n",
        "\n",
        "#clf = svm.SVC(kernel='linear')\n",
        "\n",
        "clf = MLPClassifier(solver='lbfgs', hidden_layer_sizes=(600), random_state=1)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "a4764265-ccb8-3e41-aa26-3a97ccd0abd1"
      },
      "outputs": [],
      "source": [
        "# Check size of test dataset\n",
        "\n",
        "print(len(X_test))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "39bd9814-e6e2-ed42-68f0-de28b6afc4e2"
      },
      "outputs": [],
      "source": [
        "# Fit classifier on training dataset\n",
        "\n",
        "clf.fit(X_train, y_train)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "3da7388a-5599-d3c2-d106-78172f105fdd"
      },
      "outputs": [],
      "source": [
        "# Predict using the test dataset\n",
        "\n",
        "predictions = clf.predict(X_test)\n",
        "\n",
        "predictions = predictions.astype('int')\n",
        "\n",
        "print(predictions)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "8541ce3b-319d-b25e-2191-b6f79075dce1"
      },
      "outputs": [],
      "source": [
        "# Create ImageId column for submission\n",
        "\n",
        "ImageId = []\n",
        "for i in range(1, 28001):\n",
        "    ImageId.append(i)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "3e7872be-97b9-aac5-ceed-18b53b703f3e"
      },
      "outputs": [],
      "source": [
        "# Create dataframe for submission\n",
        "\n",
        "submission = pd.DataFrame({\n",
        "    'ImageId': ImageId,\n",
        "    'Label': predictions\n",
        "})\n",
        "\n",
        "print(submission)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "e0c9d5f4-186e-c641-9ed8-d12ea2043625"
      },
      "outputs": [],
      "source": [
        "# Submit\n",
        "\n",
        "submission.to_csv('kaggle_mnist.csv', index=False)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "673b7bce-d5b3-cf32-895c-a96ddf85b178"
      },
      "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.5.2"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}