{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "bc544d79-f288-7865-2d6d-6ff30bce92a1"
      },
      "source": [
        "First steps in DataScience with Titanic.csv"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "67d499da-9c06-6e44-fa52-02234c4873bb"
      },
      "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",
        "from sklearn.tree import DecisionTreeClassifier\n",
        "import matplotlib.pyplot as plt\n",
        "from sklearn.ensemble import RandomForestClassifier\n",
        "from sklearn.model_selection import train_test_split, KFold, cross_val_score, GridSearchCV, StratifiedKFold\n",
        "from sklearn.metrics import classification_report, f1_score\n",
        "\n",
        "#matplotlib magic\n",
        "%matplotlib inline\n",
        "\n",
        "#importing data\n",
        "df = pd.read_csv('../input/train.csv', index_col='PassengerId')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "a198876b-7d5c-a447-7ec0-d3011aca7fd8"
      },
      "outputs": [],
      "source": [
        "from statistics import median\n",
        "\n",
        "x_labels = ['Pclass', 'Fare', 'Age', 'Sex','Embarked', 'Parch', 'SibSp', 'Survived']\n",
        "X = df[x_labels]\n",
        "\n",
        "print(len(X[pd.isnull(X.Age)]))\n",
        "\n",
        "hlp = X[X.Age.notnull()]\n",
        "\n",
        "X.fillna(median(hlp['Age'].tolist()), inplace=True)\n",
        "print(None in X)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "9035c971-1ead-e5e1-0626-235b31c10450"
      },
      "outputs": [],
      "source": [
        "from sklearn.preprocessing import LabelEncoder\n",
        "lb = LabelEncoder()\n",
        "X['Sex'] = lb.fit_transform(X['Sex'])\n",
        "\n",
        "#all possible features\n",
        "colds = ['Pclass', 'Fare', 'Age', 'Sex', 'Embarked', 'Parch', 'SibSp']\n",
        "#features we use ('Parch', 'SibSp' dropp the accuracy)\n",
        "cols = ['Pclass', 'Fare', 'Age', 'Sex']\n",
        "\n",
        "#parameters of the model, that we will fit; Tried different ones, theese gives best fitting\n",
        "parameters = [\n",
        "    {'min_samples_split': range(2,5), 'max_features': [4], 'min_samples_leaf': [4]}\n",
        "]\n",
        "\n",
        "#divide features from answers\n",
        "labels = X[\"Survived\"].values\n",
        "features = X[list(cols)].values\n",
        "\n",
        "#classifier we use\n",
        "dtc = DecisionTreeClassifier()\n",
        "\n",
        "#grid search model to scroll parameters\n",
        "clf = GridSearchCV(dtc, parameters, n_jobs=-1)\n",
        "\n",
        "#arays to stock answers\n",
        "y_pred = []\n",
        "y_true = []\n",
        "\n",
        "#cross-validation model\n",
        "skf = StratifiedKFold(n_splits=3)\n",
        "\n",
        "#loop that fits the grid search model using clross-validation\n",
        "for train, test in skf.split(features, labels):\n",
        "    clf.fit(features[train], labels[train])\n",
        "    #colect answers\n",
        "    y_pred = np.append(y_pred, clf.predict(features[test]))\n",
        "    y_true = np.append(y_true, labels[test])\n",
        "\n",
        "#print firs estimation of accuracy of the model\n",
        "classif_rate = np.mean(y_pred == y_true) * 100\n",
        "print(\"Classification rate : %f\" % classif_rate)\n",
        "\n",
        "#print report\n",
        "target_names = ('Dead', 'Survived')\n",
        "print(classification_report(y_true, y_pred, target_names=target_names))\n",
        "\n",
        "print(f1_score(y_true, y_pred, average='weighted'))\n",
        "\n",
        "print(\"Grid scores on development set:\")\n",
        "print()\n",
        "means = clf.cv_results_['mean_test_score']\n",
        "stds = clf.cv_results_['std_test_score']\n",
        "for mean, std, params in zip(means, stds, clf.cv_results_['params']):\n",
        "    print(\"%0.3f (+/-%0.03f) for %r\"\n",
        "          % (mean, std * 2, params))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "1918f651-fefb-01d8-ad3b-5925f67cb9e0"
      },
      "outputs": [],
      "source": [
        "from sklearn.ensemble import RandomForestClassifier\n",
        "\n",
        "rf = RandomForestClassifier()\n",
        "rf.fit(features[train], labels[train])\n",
        "target_names = ('Dead', 'Survived')\n",
        "\n",
        "y_pred = np.append(y_pred, rf.predict(features[test]))\n",
        "y_true = np.append(y_true, labels[test])\n",
        "print(classification_report(y_true, y_pred, target_names=target_names))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "77de2ae7-b99e-95bc-b687-4a52c4899c1e"
      },
      "outputs": [],
      "source": [
        "from sklearn.linear_model import LogisticRegression\n",
        "\n",
        "lr = LogisticRegression()\n",
        "lr.fit(features[train], labels[train])\n",
        "target_names = ('Dead', 'Survived')\n",
        "\n",
        "y_pred = np.append(y_pred, lr.predict(features[test]))\n",
        "y_true = np.append(y_true, labels[test])\n",
        "print(classification_report(y_true, y_pred, target_names=target_names))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "8cf6b376-cf99-0222-f2bc-c2b63e3ad67f"
      },
      "outputs": [],
      "source": [
        "test_df = pd.read_csv('../input/test.csv')\n",
        "X_test = test_df[cols]\n",
        "#print(X_test)\n",
        "#print(len(X_test[pd.isnull(X_test.Age)]))\n",
        "\n",
        "hlp = X_test[X_test.Age.notnull()]\n",
        "X_test.fillna(median(hlp['Age'].tolist()), inplace=True)\n",
        "#print(X_test[pd.isnull(X_test.Age)])\n",
        "\n",
        "from sklearn.preprocessing import LabelEncoder\n",
        "lb = LabelEncoder()\n",
        "X_test['Sex'] = lb.fit_transform(X_test['Sex'])\n",
        "#print(X_test)\n",
        "y_pred_on_test = rf.predict(X_test)\n",
        "print(y_pred_on_test)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "cd272bcf-bf9b-b939-6b66-f3c54ab6c368"
      },
      "outputs": [],
      "source": [
        "submission = pd.DataFrame({\n",
        "        \"PassengerId\": test_df[\"PassengerId\"],\n",
        "        \"Survived\": y_pred_on_test\n",
        "    })\n",
        "submission.to_csv('submission.csv', index=False)"
      ]
    }
  ],
  "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
}