{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "3e07f3a3-b0dd-8e7c-da33-9e43a68e7a90"
      },
      "source": [
        "*This is my first solution, please send me if there is an error*"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "d5960f3e-f779-3bee-8c55-00f230dab592"
      },
      "outputs": [],
      "source": [
        "import numpy as np\n",
        "import pandas as pd\n",
        "import matplotlib.pyplot as plt\n",
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "4ad663cb-0d63-5ce7-be40-224c7d47223e"
      },
      "outputs": [],
      "source": [
        "train_data = pd.read_csv('../input/train.csv')\n",
        "test_data = pd.read_csv('../input/test.csv')\n",
        "# gender_submission = pd.read_csv('../input/gender_submission.csv')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "510a4cd8-e77b-6d51-3622-8d9d94cf9091"
      },
      "outputs": [],
      "source": [
        "#drop unnecessary columns\n",
        "train_data.drop(['PassengerId', 'Ticket', 'Cabin', 'Name'], inplace=True, axis=1)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "53d2fe93-6d0c-ee0d-455a-38919c1c013b"
      },
      "outputs": [],
      "source": [
        "train_data.head()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "836237f1-9b14-628a-9a92-ca0120a10001"
      },
      "outputs": [],
      "source": [
        "train_data.describe()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "9aa69de5-421d-c0d0-11fa-e4c1dafe4f48"
      },
      "outputs": [],
      "source": [
        "plt.hist(train_data['Pclass'], color='lightblue')\n",
        "plt.tick_params(top='off', bottom='on', left='off', right='off', labelleft='on', labelbottom='on')\n",
        "plt.xlim([0, 4])\n",
        "ax = plt.gca()\n",
        "ax.spines['right'].set_visible(False)\n",
        "ax.spines['top'].set_visible(False)\n",
        "ax.spines['left'].set_visible(True)\n",
        "ax.spines['bottom'].set_visible(True)\n",
        "ax.set_xticks([1, 2, 3])\n",
        "plt.xlabel('Pclass')\n",
        "plt.ylabel('Count')\n",
        "plt.grid(True)\n",
        "plt.tight_layout()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "c4bf44ef-eaed-dd08-b548-652b86ab1988"
      },
      "outputs": [],
      "source": [
        "plt.hist(train_data['Survived'], color='lightblue')\n",
        "plt.tick_params(top='off', bottom='off', left='off', right='off', labelleft='on', labelbottom='on')\n",
        "ax = plt.gca()\n",
        "ax.spines['right'].set_visible(False)\n",
        "ax.spines['top'].set_visible(False)\n",
        "ax.spines['left'].set_visible(True)\n",
        "ax.spines['bottom'].set_visible(True)\n",
        "plt.grid(True)\n",
        "plt.xlim([-1, 2])\n",
        "ax.set_xticks([0, 1])\n",
        "ax.set_xticklabels(['Not survived', 'Survived'], rotation='vertical')\n",
        "plt.ylabel('Count')\n",
        "plt.tight_layout()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "8780012a-6902-d1bd-416d-fadbb4c64046"
      },
      "outputs": [],
      "source": [
        "train_data['Pclass'].unique()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "e749c77b-20fa-e711-72ae-ab1993638bf7"
      },
      "outputs": [],
      "source": [
        "y_surv = [len(train_data[((train_data['Survived'] == 1) & (train_data['Pclass'] == 1))]['Pclass'].tolist()), len(train_data[((train_data['Survived'] == 1) & (train_data['Pclass'] == 2))]['Pclass'].tolist()), len(train_data[((train_data['Survived'] == 1) & (train_data['Pclass'] == 3))]['Pclass'].tolist())]\n",
        "y_not_surv = [len(train_data[((train_data['Survived'] == 0) & (train_data['Pclass'] == 1))]['Pclass'].tolist()), len(train_data[((train_data['Survived'] == 0) & (train_data['Pclass'] == 2))]['Pclass'].tolist()), len(train_data[((train_data['Survived'] == 0) & (train_data['Pclass'] == 3))]['Pclass'].tolist())]\n",
        "y_surv , y_not_surv"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "b31d0ceb-2455-6def-6c4a-5b490c0d3f6f"
      },
      "outputs": [],
      "source": [
        "x = np.array([1, 2, 3])\n",
        "width=0.3\n",
        "fig, ax = plt.subplots()\n",
        "bar1 = ax.bar(x - width, y_surv, width, color='lightblue', label='Survived')\n",
        "bar2 = ax.bar(x, y_not_surv, width, color='pink', label='Not survived')\n",
        "plt.tick_params(top='off', bottom='off', left='off', right='off', labelleft='on', labelbottom='on')\n",
        "ax.spines['right'].set_visible(False)\n",
        "ax.spines['top'].set_visible(False)\n",
        "ax.spines['left'].set_visible(True)\n",
        "ax.spines['bottom'].set_visible(True)\n",
        "ax.set_xticks([1, 2, 3])\n",
        "plt.xlim([0, 4])\n",
        "plt.ylabel('Count')\n",
        "plt.grid(True)\n",
        "plt.legend(loc='upper left')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "6692bd54-0441-5b5e-51e7-9df5b7b831ce"
      },
      "outputs": [],
      "source": [
        "sum(train_data['Age'].isnull()) / len(train_data)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "e00bfbd9-d832-8f60-51de-f4d7ab826212"
      },
      "outputs": [],
      "source": [
        "sum(train_data[train_data['Survived']==1]['Age'].isnull()) / len(train_data)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "d0782494-61d3-70a9-e95d-63484ab83d14"
      },
      "outputs": [],
      "source": [
        "sum(train_data[train_data['Survived']==0]['Age'].isnull()) / len(train_data)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "397bc6ba-b1cf-d788-9cae-fd004a6a1585"
      },
      "outputs": [],
      "source": [
        "mean_age = np.mean(train_data['Age'])\n",
        "train_data['Age'] = train_data['Age'].fillna(mean_age)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "39629c55-2f51-c1d0-5030-a84b93c4e31d"
      },
      "outputs": [],
      "source": [
        "train_data['Age_group'] = pd.cut(train_data['Age'], 10)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "5bb49691-c360-3478-d33d-b2a2efdef83b"
      },
      "outputs": [],
      "source": [
        "counts = train_data.groupby(['Age_group', 'Survived']).Age_group.count().unstack()\n",
        "# plt.bar(counts['', stacked=True, color=['lightblue', 'pink'])\n",
        "counts.plot(kind='bar', stacked=True, color=['lightblue', 'pink'])\n",
        "plt.tick_params(top='off', bottom='off', left='off', right='off', labelleft='on', labelbottom='on')\n",
        "ax = plt.gca()\n",
        "ax.spines['right'].set_visible(False)\n",
        "ax.spines['top'].set_visible(False)\n",
        "ax.spines['left'].set_visible(True)\n",
        "ax.spines['bottom'].set_visible(True)\n",
        "plt.grid(True)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "aa6caaf0-10cd-4054-0403-6cc42c3c2bc9"
      },
      "outputs": [],
      "source": [
        "sum(train_data['Embarked'].isnull())"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "de20f366-1ce3-fff6-a0b5-93c84366d8d9"
      },
      "outputs": [],
      "source": [
        "train_data['Embarked'].value_counts()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "bdf5ad21-9017-fac1-2e0d-806861e0aaa4"
      },
      "outputs": [],
      "source": [
        "train_data['Embarked'] = train_data['Embarked'].fillna('S')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "97e81813-8b58-93c7-704f-0f5211e88b05"
      },
      "outputs": [],
      "source": [
        "y_surv_2 = [len(train_data[((train_data['Survived'] == 1) & (train_data['Embarked'] == 'S'))]['Embarked'].tolist()), len(train_data[((train_data['Survived'] == 1) & (train_data['Embarked'] == 'C'))]['Embarked'].tolist()), len(train_data[((train_data['Survived'] == 1) & (train_data['Embarked'] == 'Q'))]['Embarked'].tolist())]\n",
        "y_not_surv_2 = [len(train_data[((train_data['Survived'] == 0) & (train_data['Embarked'] == 'S'))]['Embarked'].tolist()), len(train_data[((train_data['Survived'] == 0) & (train_data['Embarked'] == 'C'))]['Embarked'].tolist()), len(train_data[((train_data['Survived'] == 0) & (train_data['Embarked'] == 'Q'))]['Embarked'].tolist())]\n",
        "y_surv_2 , y_not_surv_2"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "15b13644-7ed0-3a56-153e-1fb14dac4bbd"
      },
      "outputs": [],
      "source": [
        "x = np.array([1, 2, 3])\n",
        "width=0.3\n",
        "fig, ax = plt.subplots()\n",
        "bar1 = ax.bar(x - width, y_surv_2, width, color='lightblue', label='Survived')\n",
        "bar2 = ax.bar(x, y_not_surv_2, width, color='pink', label='Not survived')\n",
        "plt.tick_params(top='off', bottom='off', left='off', right='off', labelleft='on', labelbottom='on')\n",
        "ax.spines['right'].set_visible(False)\n",
        "ax.spines['top'].set_visible(False)\n",
        "ax.spines['left'].set_visible(True)\n",
        "ax.spines['bottom'].set_visible(True)\n",
        "ax.set_xticks([1, 2, 3])\n",
        "ax.set_xticklabels(['S', 'C', 'Q'])\n",
        "plt.xlim([0, 4])\n",
        "plt.ylabel('Count')\n",
        "plt.grid(True)\n",
        "plt.legend(loc='upper right')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "d6aa4672-7312-6f72-80e5-38a755085aa5"
      },
      "outputs": [],
      "source": [
        "# This makes the model worse\n",
        "# from sklearn.preprocessing import StandardScaler\n",
        "# sc = StandardScaler()\n",
        "# train_data[['Fare', 'Age']] = sc.fit_transform(train_data[['Fare', 'Age']])"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "babdd76d-e7ea-c5a7-05a2-f2e3062f23dc"
      },
      "outputs": [],
      "source": [
        "from sklearn.preprocessing import OneHotEncoder, LabelEncoder"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "6f169dcd-4cd0-0c87-6513-6d8f04b9a42f"
      },
      "outputs": [],
      "source": [
        "labelEncoder_sex = LabelEncoder()\n",
        "train_data['Sex'] = labelEncoder_sex.fit_transform(train_data['Sex'])"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "7a49b4d0-2a0c-e288-91a2-cc7617d0fead"
      },
      "outputs": [],
      "source": [
        "labelEncoder_embarked = LabelEncoder()\n",
        "train_data['Embarked'] = labelEncoder_embarked.fit_transform(train_data['Embarked'])"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "5cbbe72e-e347-3241-40ef-c9f43cd7a2ba"
      },
      "outputs": [],
      "source": [
        "train_data.drop(['Age_group'], inplace=True, axis=1)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "3b31b172-92cb-9d3e-6d86-846dc1b93a1e"
      },
      "outputs": [],
      "source": [
        "train_data.head(10)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "f561894b-13b5-e72f-c6c9-d9af3377ba0c"
      },
      "outputs": [],
      "source": [
        "X = train_data.iloc[:, 1:8].values\n",
        "y = train_data['Survived'].values"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "d380439f-4ebb-3263-b56d-7d69f159a8cb"
      },
      "outputs": [],
      "source": [
        "oneHotEncoder = OneHotEncoder(categorical_features=[0, 6])\n",
        "X = oneHotEncoder.fit_transform(X).toarray()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "6b0090e8-a0ea-9d2e-52a0-07eda176863b"
      },
      "outputs": [],
      "source": [
        "# avoiding the dummy variable trap\n",
        "X = X[:, [1, 2, 3, 4, 6, 7, 8, 9, 10]]"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "004832a2-e46f-0913-a033-f5074e6ab658"
      },
      "outputs": [],
      "source": [
        "# Splitting the dataset into the Training set and Test set\n",
        "from sklearn.model_selection import train_test_split\n",
        "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.25, random_state = 0)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "5275b4bf-2b50-d1dc-d98b-66f26bd2e28f"
      },
      "outputs": [],
      "source": [
        "from sklearn.linear_model import LogisticRegression\n",
        "classifier = LogisticRegression(random_state=0)\n",
        "classifier.fit(X_train, y_train)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "6581799a-406d-6a6f-453a-93a9fdfabf0d"
      },
      "outputs": [],
      "source": [
        "#prepare the test data\n",
        "# test_data_cp = test_data.copy()\n",
        "# test_data_cp.drop(['PassengerId', 'Ticket', 'Cabin', 'Name'], inplace=True, axis=1)\n",
        "# test_data_cp['Age'] = test_data_cp['Age'].fillna(mean_age)\n",
        "# test_data_cp['Embarked'] = test_data_cp['Embarked'].fillna('S')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "ef2a38f5-4db1-103f-7eeb-d415ba79d41d"
      },
      "outputs": [],
      "source": [
        "# test_data_cp.describe()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "4742a1c4-ba63-e929-e126-dc6aee8cd555"
      },
      "outputs": [],
      "source": [
        "# mean_fare = np.mean(train_data['Fare'])\n",
        "# test_data_cp['Fare'] = test_data_cp['Fare'].fillna(mean_fare)\n",
        "# train_data[['Fare', 'Age']] = sc.fit_transform(train_data[['Fare', 'Age']])"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "5f5e8a74-fba0-57f4-0521-911720219ca0"
      },
      "outputs": [],
      "source": [
        "# test_data_cp['Sex'] = labelEncoder_sex.transform(test_data_cp['Sex'])\n",
        "# test_data_cp['Embarked'] = labelEncoder_embarked.transform(test_data_cp['Embarked'])\n",
        "\n",
        "# X_test = test_data_cp.iloc[:, :].values\n",
        "# y_test = gender_submission['Survived'].values\n",
        "\n",
        "# X_test = oneHotEncoder.transform(X_test).toarray()\n",
        "\n",
        "# X_test = X_test[:, [1, 2, 3, 4, 6, 7, 8, 9, 10]]"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "275642db-ad47-7e44-ff26-d60a2141bec5"
      },
      "outputs": [],
      "source": [
        "y_pred = classifier.predict(X_test)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "e4820dc9-008c-13ca-9d3a-33915ee41700"
      },
      "outputs": [],
      "source": [
        "from sklearn.metrics import confusion_matrix\n",
        "cm = confusion_matrix(y_test, y_pred)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "f21b5a53-e6e4-1eba-d893-8755fb71ba6c"
      },
      "outputs": [],
      "source": [
        "cm"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "9f338d6f-1dbe-1892-8928-7cc9db2f0dd3"
      },
      "outputs": [],
      "source": [
        "from sklearn.tree import DecisionTreeClassifier\n",
        "classifier = DecisionTreeClassifier(criterion='entropy', random_state=0)\n",
        "classifier.fit(X_train, y_train)\n",
        "\n",
        "y_pred = classifier.predict(X_test)\n",
        "\n",
        "from sklearn.metrics import confusion_matrix\n",
        "cm = confusion_matrix(y_test, y_pred)\n",
        "\n",
        "cm"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "a64d8689-8aa9-b08f-c9e1-5777d83c102b"
      },
      "outputs": [],
      "source": [
        "from sklearn.naive_bayes import GaussianNB\n",
        "classifier = GaussianNB()\n",
        "classifier.fit(X_train, y_train)\n",
        "\n",
        "y_pred = classifier.predict(X_test)\n",
        "\n",
        "from sklearn.metrics import confusion_matrix\n",
        "cm = confusion_matrix(y_test, y_pred)\n",
        "\n",
        "cm"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "42bc4d7c-7566-cf91-a662-a2008b838705"
      },
      "outputs": [],
      "source": [
        "#logistic regression seems the best"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "b781c1ab-9ebc-db62-7662-1a1cc9c0d873"
      },
      "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
}