{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "11b3ee24-503e-80f4-c3d1-c565cd27f00d"
      },
      "source": [
        "This notebook explains a way of completing this competition using Linear Regression Using Multiple Variable. I am using sklearn library of python to call linear regression function."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "cffdee0f-6cb7-3467-6cf3-c42e0ad15d00"
      },
      "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": "e6029751-f75c-0b73-8e45-2874e470f7bf"
      },
      "outputs": [],
      "source": [
        "from sklearn import linear_model\n",
        "import pandas as pd\n",
        "import numpy as np\n",
        "\n",
        "data = pd.read_csv('../input/train.csv')\n",
        "data2 = pd.read_csv('../input/test.csv')\n",
        "\n",
        "data = np.array(data)\n",
        "\n",
        "y = data[:,1]\n",
        "pclass = data[:,2]\n",
        "sex = data[:,4]\n",
        "age = data[:,5]\n",
        "sibsp = data[:,6]\n",
        "parch = data[:,7]\n",
        "fare = data[:,9]"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "718a1cc2-7af5-6901-a0c0-f8f7c8806028"
      },
      "source": [
        "Now we have got the multiple variables we have to clean the data and organise it.\n",
        "The age array is not complete so we have to fill the data. I am using forward propagation for this.\n",
        "Also the sex array has 'male' and 'female' values, i.e, it has character string and the required is int or float type. So I have taken male as 1 and female as 0."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "01d318ad-0df5-82a4-96ad-346a08f33c41"
      },
      "outputs": [],
      "source": [
        "age_new = pd.DataFrame(age).fillna(method='pad')\n",
        "age_new = np.mat(age_new)\n",
        "\n",
        "for i in range(np.size(sex)):\n",
        "\tif(sex[i] == 'male'):\n",
        "\t\tsex[i] = 1\n",
        "\telse:\n",
        "\t\tsex[i]= 0\n",
        "\n",
        "X = [list(pclass), list(sex), list(sibsp), list(parch), list(age_new)]"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "b55ee63f-d6c2-75f9-8e20-cfbca8eda7ef"
      },
      "source": [
        "Now comes the code to train our model."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "63a6b7dd-7b74-da6a-89aa-45e89321e9ae"
      },
      "outputs": [],
      "source": [
        "regr = linear_model.LinearRegression()\n",
        "regr.fit(np.array(X).transpose(), y)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "5aafba98-62ca-4daf-761e-70b3036f4330"
      },
      "source": [
        "The model training is complete, now we can move onto predicting the survival of the passengers."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "8b272982-270a-2b75-dcb5-53c5468195d7"
      },
      "outputs": [],
      "source": [
        "data2 = np.array(data2)\n",
        "\n",
        "p_id = data2[:,0]\n",
        "pclass = data2[:,1]\n",
        "sex = data2[:,3]\n",
        "age = data2[:,4]\n",
        "sibsp = data2[:,5]\n",
        "parch = data2[:,6]\n",
        "fare = data2[:,8]\n",
        "\n",
        "age_new = pd.DataFrame(age).fillna(method='pad')\n",
        "age_new = np.mat(age_new)\n",
        "\n",
        "for i in range(np.size(sex)):\n",
        "\tif(sex[i] == 'male'):\n",
        "\t\tsex[i] = 1\n",
        "\telse:\n",
        "\t\tsex[i]= 0\n",
        "\n",
        "X_new = [list(pclass), list(sex), list(sibsp), list(parch), list(age_new)]\n",
        "\n",
        "Y_new = regr.predict(np.array(X_new).transpose())"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "de345a57-6ff8-85c6-158c-59db747ee322"
      },
      "source": [
        "Y_new is the predicted values of the survival of given passengers in 'test.csv'. Now we have to save our prediction in a .csv file.\n",
        "I have named my file as 'my_solution.csv'"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "51d9cf32-5d55-ac5a-b30f-44b6e983c6b7"
      },
      "outputs": [],
      "source": [
        "a = np.array(p_id)\n",
        "b = np.array(np.round(Y_new))\n",
        "p = [a,b]\n",
        "pd.DataFrame(p).transpose().to_csv(\"my_solution.csv\", index = 0)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "d404e216-b3f3-36bf-6336-119655cc0820"
      },
      "source": [
        "I can't seem to name the columns using python code so I did so manually. The accuracy on submission came out to be 0.77512. \n",
        "If you have any inputs to give or see any fault in this code, please do comment. I am a noob and would love to learn and rectify the mistake. \n",
        "Thank You :D "
      ]
    }
  ],
  "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
}