{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "b9d5306b-9d75-8e61-1833-26239b1dff7d"
      },
      "source": [
        "# LogisticRegression, SVC, GradientBoostingClassifier"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "b91fb19b-1e45-e81e-59e7-c1ace0f38672"
      },
      "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",
        "# 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": "1aa65103-c6af-6c4e-7383-d22a4fa87183"
      },
      "outputs": [],
      "source": [
        "train = pd.read_csv('../input/train.csv')\n",
        "test = pd.read_csv('../input/test.csv')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "8dc8b99d-35d6-506d-4c05-3188ec01d6ca"
      },
      "outputs": [],
      "source": [
        "from sklearn.model_selection import train_test_split\n",
        "from sklearn.linear_model.logistic import LogisticRegression\n",
        "from sklearn.svm import SVC"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "33e9aa00-7c06-d618-273a-4212f843cad4"
      },
      "outputs": [],
      "source": [
        "images = train.iloc[0:5000,1:]\n",
        "labels = train.iloc[0:5000,:1]\n",
        "X_train, X_test,y_train, y_test = train_test_split(images, labels, train_size=0.8, random_state=0)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "d7b6d537-7f64-5018-55b1-5c8b18b41fbd"
      },
      "outputs": [],
      "source": [
        "#splitting data in 75-25 ration\n",
        "#X_train, X_test, y_train, y_test = train_test_split(X,y, train_size=0.3)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "c496597b-6ce3-e141-0fd4-919061453a77"
      },
      "outputs": [],
      "source": [
        "#X_test.head()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "82ba257a-543a-7b2a-d2fa-1f3ac906f1b9"
      },
      "outputs": [],
      "source": [
        "#fitting logistic regression model\n",
        "logreg = LogisticRegression()\n",
        "logreg.fit(X_train,y_train.values.ravel())\n",
        "logreg.score(X_test, y_test)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "0c306141-cdcc-afa8-ca5c-a7073a664e90"
      },
      "outputs": [],
      "source": [
        "X_train[X_train>0] = 1\n",
        "X_test[X_test>0] = 1"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "07374b9e-e502-95ee-d620-06be295b6f49"
      },
      "outputs": [],
      "source": [
        "# svm\n",
        "sv = SVC()\n",
        "sv.fit(X_train, y_train.values.ravel())\n",
        "sv.score(X_test, y_test)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "53d143cb-9204-0672-9824-a69f38663919"
      },
      "outputs": [],
      "source": [
        "from sklearn.ensemble import GradientBoostingClassifier"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "90acda3f-aa42-3965-f4e7-b6c2631cd0e3"
      },
      "outputs": [],
      "source": [
        "gbc = GradientBoostingClassifier(n_estimators=500,learning_rate=0.1, max_depth=4)\n",
        "gbc.fit(X_train, y_train.values.ravel())\n",
        "gbc.score(X_test, y_test)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "70325c92-3294-4c0a-f2af-efeece492d50"
      },
      "outputs": [],
      "source": [
        "Y_pred = gbc.predict(test)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "e9c16fbc-f5c6-9b36-2cf1-ef239e8d373a"
      },
      "outputs": [],
      "source": [
        "df = pd.DataFrame(Y_pred)\n",
        "df.index.name='ImageId'\n",
        "df.index+=1\n",
        "df.columns=['Label']\n",
        "df.to_csv('submission.csv',header=True)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "04ecb732-fb07-e513-20da-709c8aaf6571"
      },
      "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
}