{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "975c570b-062e-1d6e-182c-d58b5cc0a7d9"
      },
      "outputs": [],
      "source": [
        "import matplotlib.pyplot as plt\n",
        "import numpy as np\n",
        "import pandas as pd\n",
        "from sklearn.ensemble import BaggingClassifier\n",
        "from sklearn import cross_validation"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "7491456a-4077-3f3b-76c9-3777ce723721"
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "024a1310-4251-2383-6222-8727cc1f8db1"
      },
      "outputs": [],
      "source": [
        "def showDigit (pixelArray):\n",
        "    pixelMatrix = pixelArray.reshape( (28, 28) )\n",
        "    plt.imshow(pixelMatrix, cmap='Greys')\n",
        "    plt.show()\n",
        "\n",
        "def decisionTreeWithBagging(X, Y):\n",
        "    clf = BaggingClassifier(max_samples=0.5, max_features=0.5)\n",
        "    clf = clf.fit(X, Y)\n",
        "    return clf\n",
        "\n",
        "def prepareSubmission(name, clf, test):\n",
        "    prediction = clf.predict(test)\n",
        "    df = pd.DataFrame(prediction, columns = ['Label'])\n",
        "    df.index += 1 \n",
        "    df.to_csv(name, index_label = \"ImageId\")\n",
        "\n",
        "def crossValidation(clf, X, Y):\n",
        "    scores = cross_validation.cross_val_score(clf, X, Y, cv=5)\n",
        "    print(\"Accuracy: %0.2f (+/- %0.2f)\" % (scores.mean(), scores.std() * 2))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "ea2474d4-75dc-cc94-137d-e79e36a79a0a"
      },
      "outputs": [],
      "source": [
        "# load data\n",
        "train = pd.read_csv(\"../input/train.csv\")\n",
        "test = pd.read_csv(\"../input/test.csv\")"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "c6b3bb61-0bc8-22c9-b2da-4f9ee3585e89"
      },
      "outputs": [],
      "source": [
        "# extract labels and features \n",
        "Y = train.ix[:,0].as_matrix()\n",
        "X = train.ix[0:,1:].as_matrix()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "27300a08-bf57-f388-35c6-b90b241950cd"
      },
      "outputs": [],
      "source": [
        "# prepare classifier\n",
        "tree = decisionTreeWithBagging(X, Y)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "09edf05f-3946-6cb1-560d-2d5cc9e1b177"
      },
      "outputs": [],
      "source": [
        "# check accuracy\n",
        "crossValidation(tree, X, Y)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "f9cfdb62-41eb-551c-736a-69246aa387ba"
      },
      "outputs": [],
      "source": [
        "# prepareSubmission\n",
        "prepareSubmission('decisionTree.bagging.csv', tree, test)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "96f1ef3f-e933-3c14-ca05-29f41c0a558d"
      },
      "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
}