{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "8e5a1c01-9f38-204f-d9cd-f652fa14bcd8"
      },
      "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": "d54931d0-8145-3296-b78b-098d037cfd74"
      },
      "outputs": [],
      "source": [
        "import pandas as pd\n",
        "import matplotlib.pyplot as plt, matplotlib.image as mpimg\n",
        "from sklearn.model_selection import train_test_split\n",
        "from sklearn import svm\n",
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "fcdbdb3c-d4aa-a3e0-3513-c10859b1e390"
      },
      "outputs": [],
      "source": [
        "labeled_images=pd.read_csv('../input/train.csv')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "37924b16-b78e-0443-cd71-d722ff15bad5"
      },
      "outputs": [],
      "source": [
        "images = labeled_images.iloc[0:5000,1:]\n",
        "labels = labeled_images.iloc[0:5000,:1]"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "cfb1cb56-9612-6bf2-ff8f-ffc552ed512e"
      },
      "outputs": [],
      "source": [
        "train_images, test_images,train_labels, test_labels = train_test_split(images, labels, train_size=0.8, random_state=0)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "cdc3068c-55d8-92d3-25cd-6479ad241f15"
      },
      "outputs": [],
      "source": [
        "i=2\n",
        "img=train_images.iloc[i].as_matrix()\n",
        "img=img.reshape((28,28))\n",
        "plt.imshow(img,cmap='gray')\n",
        "plt.title(train_labels.iloc[i,0])"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "b007ba56-123c-1beb-089f-6a34c7783313"
      },
      "outputs": [],
      "source": [
        "plt.hist(train_images.iloc[i])"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "751c2a5d-2b22-0dfd-287c-a90ce34ed450"
      },
      "outputs": [],
      "source": [
        "clf = svm.SVC()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "e81656d4-35ae-f47e-4a5c-e36d596a2959"
      },
      "outputs": [],
      "source": [
        "clf.fit(train_images, train_labels.values.ravel())"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "37bbae08-87fb-a030-f886-3ab1d8077499"
      },
      "outputs": [],
      "source": [
        "clf.score(test_images,test_labels)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "4b793fe3-ca3d-a8d5-3c17-2d74ccd41143"
      },
      "outputs": [],
      "source": [
        "test_images[test_images>0]=1\n",
        "train_images[train_images>0]=1\n",
        "\n",
        "img=train_images.iloc[i].as_matrix().reshape((28,28))\n",
        "plt.imshow(img,cmap='binary')\n",
        "plt.title(train_labels.iloc[i])"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "4fa8ff66-22e2-6458-1929-fa23609c2fa1"
      },
      "outputs": [],
      "source": [
        "clf = svm.SVC()\n",
        "clf.fit(train_images, train_labels.values.ravel())\n",
        "clf.score(test_images,test_labels)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "92052ba3-ead5-ceaf-027a-bc71f4819fa8"
      },
      "outputs": [],
      "source": [
        "test_data=pd.read_csv('../input/test.csv')\n",
        "test_data[test_data>0]=1\n",
        "results=clf.predict(test_data[0:5000])"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "feecab4a-0afb-390d-33ea-3f625a809ec7"
      },
      "outputs": [],
      "source": [
        "results"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "f692fe15-9bd4-2e86-5388-b1dbe333a155"
      },
      "outputs": [],
      "source": [
        "df = pd.DataFrame(results)\n",
        "df.index.name='ImageId'\n",
        "df.index+=1\n",
        "df.columns=['Label']\n",
        "df\n",
        "df.to_csv('results.csv', header=True)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "e6a8db4e-7040-077f-32f0-b7dcb0a71579"
      },
      "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
}