{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "e43ad8a5-4417-a66b-a25f-e8006c35c8f5"
      },
      "source": [
        "Handwritten digit recognizer ( classifier)  analysis"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "4318731e-6a57-d577-f4f3-fee347a76030"
      },
      "outputs": [],
      "source": [
        "# This Python 3 environment comes with many helpful analytics libraries installed\n",
        "# It is defined by the kaggle/python docker image: https://github.com/kaggle/docker-python\n",
        "# For example, here's several helpful packages to load in \n",
        "\n",
        "import numpy as np # linear algebra\n",
        "import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n",
        "\n",
        "# Input data files are available in the \"../input/\" directory.\n",
        "# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory\n",
        "\n",
        "from subprocess import check_output\n",
        "print(check_output([\"ls\", \"../input\"]).decode(\"utf8\"))\n",
        "\n",
        "# Any results you write to the current directory are saved as output."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "1fcb9c10-2fac-96e8-1c7f-278b589bf0d4"
      },
      "outputs": [],
      "source": [
        "train = pd.read_csv(\"../input/train.csv\")\n",
        "X_test = pd.read_csv(\"../input/test.csv\")\n",
        "print(train.shape)\n",
        "print(X_test.shape)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "42111837-7360-246e-7eac-697c751f8d71"
      },
      "outputs": [],
      "source": [
        "#traning samples\n",
        "train.head(2)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "d678b430-141b-bc2a-1d41-aa8044a10be6"
      },
      "outputs": [],
      "source": [
        "#reconstruction\n",
        "from matplotlib import pyplot as plt\n",
        "data =train.ix[0, 'pixel0':'pixel783'].reshape(28,28)\n",
        "plt.subplot(221)\n",
        "plt.imshow(data, cmap=plt.get_cmap('gray'))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "39a35cc1-d7ee-87f0-06ff-b61e66ee0d7e"
      },
      "source": [
        "Each digit is represented by 784(28 by 28) pixels. The first digit 1 is reconstructed as above. Each pixel here is considered as one feature. I will compare the performance of various algorithms on handwritten digit classification"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "d458b399-7198-d616-3826-b819365b9aed"
      },
      "outputs": [],
      "source": [
        "#getting data ready for classifiers\n",
        "X_train = train.ix[:,'pixel0'::]\n",
        "y_train = train.ix[:,'label']\n",
        "np.arange(1,2)\n",
        "#X_test as above"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "786f3d78-40ee-bad0-bde8-e85042a5062e"
      },
      "outputs": [],
      "source": [
        "#RandomForest algorithm\n",
        "from sklearn.ensemble import RandomForestClassifier\n",
        "rfc = RandomForestClassifier(n_estimators = 100, random_state=0)\n",
        "rfc.fit(X_train, y_train)\n",
        "print(\"Accuracy on training set: {:.3f}\".format(rfc.score(X_train, y_train)))\n",
        "y_pred = rfc.predict(X_test)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "0766e7e3-90e0-f85f-e64c-8fb3f3146c57"
      },
      "outputs": [],
      "source": [
        "#GradientBoostingRegressor\n",
        "from sklearn.ensemble import GradientBoostingRegressor\n",
        "grbr = GradientBoostingRegressor(random_state=0)\n",
        "grbr.fit(X_train, y_train)\n",
        "print(\"Accuracy on training set: {:.3f}\".format(grbr.score(X_train, y_train)))\n",
        "y_pred = grbr.predict(X_test)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "2b115588-aa2f-cc17-56ae-9bb5b374f531"
      },
      "outputs": [],
      "source": [
        "'''from sklearn import svm, metrics\n",
        "# Create a classifier: a support vector classifier\n",
        "classifier = svm.SVC(gamma=0.091)\n",
        "\n",
        "# We learn the digits on on the traning data\n",
        "classifier.fit(X_train, y_train)\n",
        "predicted_train = classifier.predict(X_train)\n",
        "# Now predict the value of the digit on the test data\n",
        "y_pred = classifier.predict(X_test)\n",
        "\n",
        "print(\"Classification report for classifier on the tranining data %s:\\n%s\\n\"\n",
        "      % (classifier, metrics.classification_report(predicted_train, y_train)))\n",
        "print(\"Confusion matrix on training data:\\n%s\" % metrics.confusion_matrix(predicted_train, y_train))'''"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "519187d6-6f60-56e1-634b-2ad0c73c1e9f"
      },
      "outputs": [],
      "source": [
        "submission = pd.DataFrame({\n",
        "        \"ImageId\": np.arange(1,len(X_test)+1),\n",
        "        \"Label\": y_pred\n",
        "    })\n",
        "submission.to_csv('sample_submission.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
}