{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "bd729f60-ce81-47d2-8d01-bfb91f8516c2"
      },
      "outputs": [],
      "source": [
        "import pandas as pd\n",
        "\n",
        "# The competition datafiles are in the directory ../input\n",
        "# Read competition data files:\n",
        "train = pd.read_csv(\"../input/train.csv\")\n",
        "test  = pd.read_csv(\"../input/test.csv\")\n",
        "\n",
        "# Write to the log:\n",
        "print(\"Training set has {0[0]} rows and {0[1]} columns\".format(train.shape))\n",
        "print(\"Test set has {0[0]} rows and {0[1]} columns\".format(test.shape))\n",
        "# Any files you write to the current directory get shown as outputs\n",
        "\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "61241128-6a96-49d5-b433-e6f4cd8e6c04"
      },
      "outputs": [],
      "source": [
        "import numpy as np"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "339c4599-e8bd-4b56-8341-c4fec4536927"
      },
      "outputs": [],
      "source": [
        "def trim_images(images):\n",
        "    cols = set(['pixel' + str(i + j * 28) for i in [0,1,2,3, 24,25,26,27] for j in range(0, 28)])\n",
        "    cols |= set(['pixel' + str(j + i * 28) for i in [0,1,2,3, 24,25,26,27] for j in range(0, 28)])\n",
        "    return images.drop(list(cols), 1)\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "e3ced526-6056-48d2-aafb-185257eade24"
      },
      "outputs": [],
      "source": [
        "images = pd.DataFrame(trim_images(train.drop('label', 1)))\n",
        "labels = train['label']"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "f9f87b91-76e9-4fb7-b052-9dcf47938ae3"
      },
      "outputs": [],
      "source": [
        "from sklearn.ensemble import RandomForestClassifier\n",
        "clf = RandomForestClassifier(bootstrap=False, class_weight='auto', criterion='gini',\n",
        "            max_depth=None, max_features=0.032, max_leaf_nodes=None,\n",
        "            min_samples_leaf=1, min_samples_split=1,\n",
        "            min_weight_fraction_leaf=0.0, n_estimators=140, n_jobs=8,\n",
        "            oob_score=False, random_state=None, verbose=0,\n",
        "            warm_start=False)\n",
        "\n",
        "n_train = int(len(images) * 0.75)\n",
        "\n",
        "clf.fit(images[:n_train], labels[:n_train])\n",
        "\n",
        "len(images[n_train:][clf.predict(images[n_train:]) == labels[n_train:]]) / (len(images) - n_train)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "8eebb4d1-b8a4-42e2-af70-5a2f16f7bcd1"
      },
      "outputs": [],
      "source": [
        "predication = pd.DataFrame(clf.predict(trim_images(test)), columns=['Label'], index=(test.index + 1))\n",
        "predication.to_csv('output.csv', index=True, index_label='ImageId')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "a962d878-ac48-48f6-98b8-770ac14d12e5"
      },
      "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
}