{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "eda4294e-e971-625e-d4a6-8817fd8a5a7f"
      },
      "outputs": [],
      "source": [
        "import pandas as pd\n",
        "import numpy as np"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "97cad32d-23e5-ece7-7707-f70f6b4a7010"
      },
      "outputs": [],
      "source": [
        "d = pd.read_csv(\"../input/train.csv\")"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "cb603f6a-9c85-cf9e-8dac-5a055078cf45"
      },
      "outputs": [],
      "source": [
        "d.head()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "0ab9ef0b-398b-14ca-9840-cc656be2afa1"
      },
      "outputs": [],
      "source": [
        "from sklearn.tree import DecisionTreeClassifier"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "041bed5b-829e-3974-7fba-f67a6d7ac02a"
      },
      "outputs": [],
      "source": [
        "d['Male']=(d['Sex']=='male')\n",
        "n = d['Age'].mean()\n",
        "d['Class1']=(d['Pclass']==1)\n",
        "d['Class2']=(d['Pclass']==2)\n",
        "\n",
        "d['Age'].fillna(n, inplace=True)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "c59d7e15-a48a-e190-3358-1ac3de8d4a2b"
      },
      "outputs": [],
      "source": [
        "X = d.loc[:, ['Class1', 'Class2','Male', 'Age', 'SibSp', 'Parch', 'Fare']]\n",
        "y = d['Survived']"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "9c100236-6dc6-1669-81c6-273608d3eddb"
      },
      "outputs": [],
      "source": [
        "thisclf = DecisionTreeClassifier()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "6a6ca978-7113-0c5e-1482-d309ee79cec7"
      },
      "outputs": [],
      "source": [
        "thisclf.fit(X,y)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "dd68ca33-b86b-a544-0528-d31f47d88996"
      },
      "outputs": [],
      "source": [
        "d['predicted'] = thisclf.predict(X)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "a2e88555-a9be-0c3b-d049-3d668a5350fc"
      },
      "outputs": [],
      "source": [
        "from sklearn.metrics import accuracy_score"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "bda02daa-ed18-9b7b-6a93-c7dc465a9107"
      },
      "outputs": [],
      "source": [
        "accuracy_score(y, d['predicted'])"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "bd7d8551-192f-40e3-ee94-8bc0663e9143"
      },
      "outputs": [],
      "source": [
        "t = pd.read_csv(\"../input/test.csv\")"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "8cac6268-d7ee-2b0c-d666-dfe40b36b3ba"
      },
      "outputs": [],
      "source": [
        "t['Male']=(t['Sex']=='male')\n",
        "nn = t['Age'].mean()\n",
        "t['Class1']=(t['Pclass']==1)\n",
        "t['Class2']=(t['Pclass']==2)\n",
        "\n",
        "t['Age'].fillna(nn, inplace=True)\n",
        "\n",
        "f = t['Fare'].mean()\n",
        "t['Fare'].fillna(f, inplace=True)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "66beb77a-8282-b630-6431-d01b054bc8cd"
      },
      "outputs": [],
      "source": [
        "X_t = t.loc[:, ['Class1', 'Class2','Male', 'Age', 'SibSp', 'Parch', 'Fare']]"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "2b590523-ec43-ef03-0d0c-a45e48620f8e"
      },
      "outputs": [],
      "source": [
        "t['Survived'] = thisclf.predict(X_t)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "2c904caa-b776-60c0-2502-0af58026f0fb"
      },
      "outputs": [],
      "source": [
        "t_out = t.loc[:,['PassengerId','Survived']]"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "25218a40-0162-3fea-bbeb-1797829ee143"
      },
      "outputs": [],
      "source": [
        "t_out.to_csv(\"out.csv\")"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "4b7d00ed-edf6-b8f4-b272-3478c8566b4b"
      },
      "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
}