{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "615e8caa-b1b4-6fb5-efcd-e916ea825081"
      },
      "outputs": [],
      "source": [
        "import numpy as np # linear algebra\n",
        "import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n",
        "from sklearn.model_selection import train_test_split\n",
        "import matplotlib.pyplot as plt, matplotlib.image as mpimg\n",
        "from sklearn import svm\n",
        "%matplotlib inline\n",
        "labeled_images = pd.read_csv(\"../input/train.csv\")\n",
        "images = labeled_images.iloc[0:,1:]\n",
        "labels = labeled_images.iloc[0:,: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": "be02f15d-46a1-e8fd-626e-fc54f0c47105"
      },
      "outputs": [],
      "source": [
        "i = 1\n",
        "img = train_images.iloc[i].as_matrix()\n",
        "img = img.reshape((28, 28))\n",
        "plt.imshow(img)\n",
        "plt.title(train_labels.iloc[i,0])"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "1ad6bcfc-feb0-c933-2bdc-be4c6ce744f6"
      },
      "outputs": [],
      "source": [
        "test_images[test_images>0] = 1\n",
        "train_images[train_images>0] = 1\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": "6e5ee95d-1d9f-8a8d-8f9a-655fa04184f1"
      },
      "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": "0454d17f-d6dd-de45-5951-c180052cb6a3"
      },
      "outputs": [],
      "source": [
        "test_data=pd.read_csv('../input/test.csv')\n",
        "test_data[test_data>0]=1\n",
        "results=clf.predict(test_data)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "b3c42386-649a-e573-2f46-bdfd6a1484b4"
      },
      "outputs": [],
      "source": [
        "df = pd.DataFrame(results)\n",
        "df.index.name='ImageId'\n",
        "df.index+=1\n",
        "df.columns=['Label']\n",
        "df.to_csv('results.csv', header=True)"
      ]
    }
  ],
  "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
}