{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "2a832106-a69d-7974-3267-0fe0ee894468"
      },
      "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": "2680889e-4af6-c3a0-f5bd-779d73c4dbdf"
      },
      "outputs": [],
      "source": [
        "import pandas as pd\n",
        "import matplotlib.pyplot as plt\n",
        "from sklearn.model_selection import train_test_split\n",
        "from sklearn import svm\n",
        "import numpy as np\n",
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "e0b40bca-8a4a-a1f6-10ad-7485b84e89c6"
      },
      "outputs": [],
      "source": [
        "img = pd.read_csv('../input/train.csv')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "d9187a29-f787-ee9e-7eaa-78224e53a5a8"
      },
      "outputs": [],
      "source": [
        "images = img.iloc[:5000,1:]\n",
        "labels = img.iloc[:5000,:1]\n",
        "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": "b72b4c2c-56cc-7b20-5b83-c5355044d62e"
      },
      "outputs": [],
      "source": [
        "current_img = train_images.iloc[1].as_matrix()\n",
        "current_img = current_img.reshape(28,28)\n",
        "plt.imshow(current_img,cmap='gray')\n",
        "plt.title(train_labels.iloc[1,0])\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "6f3bfe50-0815-9ef1-15e9-f224029fcaa2"
      },
      "outputs": [],
      "source": [
        "train_images[train_images>0] = 1\n",
        "test_images[test_images>0] = 1"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "bf7fa75a-ac4d-4882-4395-b14d5f2d8fc3"
      },
      "outputs": [],
      "source": [
        "train_images.iloc[1].as_matrix()\n",
        "current_img = train_images.iloc[1].as_matrix()\n",
        "current_img = current_img.reshape(28,28)\n",
        "plt.imshow(current_img,cmap='binary')\n",
        "plt.title(train_labels.iloc[1,0])"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "697606f6-9b7a-dddb-6594-ac052f516225"
      },
      "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": "7ce108b8-276e-ff43-1a5d-1fd576514adc"
      },
      "outputs": [],
      "source": [
        "test_data = pd.read_csv('../input/test.csv')\n",
        "test_data[test_data>0]=1\n",
        "output_result = clf.predict(test_data)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "cb094b08-dd30-9ccf-bb17-e1c4ef333cc2"
      },
      "outputs": [],
      "source": [
        "output_result"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "001f48f3-5445-83f5-819d-33778bf98f33"
      },
      "outputs": [],
      "source": [
        "of = pd.DataFrame(output_result)\n",
        "of.index.name='ImageId'\n",
        "of.index+=1\n",
        "of.columns=['Label']\n",
        "of.to_csv('results.csv', header=True)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "bcf7e984-afa5-ed02-9dfc-d6709807a0da"
      },
      "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
}