# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# + _execution_state="idle" _uuid="8de57e95ed8a9225c5cfcbb628f41a120018b225" _cell_guid="d30d4995-14dd-425a-a1c3-4fbeed01aaa5"
# This Python 3 environment comes with many helpful analytics libraries installed
# It is defined by the kaggle/python docker image: https://github.com/kaggle/docker-python
# For example, here's several helpful packages to load in 

import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

# Input data files are available in the "../input/" directory.
# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory

from subprocess import check_output
print(check_output(["ls", "../input"]).decode("utf8"))

# Any results you write to the current directory are saved as output.


# + _execution_state="idle" _uuid="75a6332e62aacf14ed0c9ffc85e32ed42b1fae4e" _cell_guid="7edee3d8-c4b6-412d-9de3-252c8f4399bc"
# data analysis and wrangling
import pandas as pd
import numpy as np
import random as rnd

# visualization
import seaborn as sns
import matplotlib.pyplot as plt
# %matplotlib inline

# machine learning
from sklearn.linear_model import LogisticRegression
from sklearn.svm import SVC, LinearSVC
from sklearn.ensemble import RandomForestClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.naive_bayes import GaussianNB
from sklearn.linear_model import Perceptron
from sklearn.linear_model import SGDClassifier
from sklearn.tree import DecisionTreeClassifier

# + _execution_state="idle" _uuid="ad6b552552fbbfda7b775e597fb9b1ae1bc5dabf" _cell_guid="b26e942d-a19e-4fa8-83fe-d8657c5a0566"
train_df = pd.read_csv('../input/train.csv')
test_df = pd.read_csv('../input/test.csv')
df = [train_df, test_df]

# + _execution_state="idle" _uuid="3e43c7d070f9eb1c93e863b6ec67bbca25ec0bbd" _cell_guid="60326f26-db8a-4670-b407-038835add8f5"
train_df.head()

# + _execution_state="idle" _uuid="36a4f8f4cb2ac4279766292a18d545f222e5cd9e" _cell_guid="53e134cf-25a0-4a21-9aba-885460f5598b"
train_df.info()
test_df.info()

# + _execution_state="idle" _uuid="7be13129e07adad74867829e1af7a9c33999cd97" _cell_guid="a02947b7-9d77-45d6-8484-142383dafd5a"
train_df.describe(include=['O'])

# + _execution_state="idle" _uuid="b3039028eb3bf8ad6aded9e7218e9fef3a18843b" _cell_guid="ef1002c6-fc49-4880-84c9-c3d72e685e3d"
train_df[['Pclass','Survived']].groupby('Pclass').mean()

# + _execution_state="idle" _uuid="c85c0d13dcea2bf2161ec3132e7876371004457e" _cell_guid="19bd2792-43fe-4c84-9443-c6b56f3cdc24"
grid = sns.FacetGrid(train_df,col='Survived')
grid.map(plt.hist,'Age',bins=20)

# + _execution_state="idle" _uuid="abbe0ad972ce1e9b711491cf2d316eab4a911bf7" _cell_guid="98cdda58-d5e5-4af2-a004-788956f76b13"
grid = sns.FacetGrid(train_df, row='Embarked', size=2.2, aspect=1.6)
grid.map(sns.pointplot, 'Pclass', 'Survived', 'Sex', palette='deep')
grid.add_legend()

# + _execution_state="idle" _uuid="356b3f3b04fa5a4b497a6fd24c932a0615cd2f19" _cell_guid="96f1b668-72d3-48f8-ae0e-e41fff4ce57f"
train_df = train_df.drop(['Ticket','Cabin'],axis=1)
test_df = test_df.drop(['Ticket','Cabin'],axis=1)
combine = [train_df,test_df]

# + _execution_state="idle" _uuid="cca3f05a1bda2ddcd64c16d7ee51c5cdeb5405e6" _cell_guid="65a019ce-d46e-4cb9-bd42-4a1b24c7ac9a"
for dataset in combine:
    dataset['Title'] = dataset.Name.str.extract(' ([A-Za-z]+)\.', expand=False)

pd.crosstab(train_df['Title'],train_df['Sex'])

# + _execution_state="idle" _uuid="719875980e838be4ed330824886ead3b18e5ce9e" _cell_guid="b2993fa0-19eb-40e4-ba8e-0fe42ced1f05"
for dataset in combine:
    dataset['Title'] = dataset['Title'].replace(['Lady', 'Countess','Capt', 'Col',\
 	'Don', 'Dr', 'Major', 'Rev', 'Sir', 'Jonkheer', 'Dona'], 'Other')

    dataset['Title'] = dataset['Title'].replace('Mlle', 'Miss')
    dataset['Title'] = dataset['Title'].replace('Ms', 'Miss')
    dataset['Title'] = dataset['Title'].replace('Mme', 'Mrs')
    
train_df[['Title', 'Survived']].groupby(['Title']).mean()

# + _execution_state="idle" _uuid="13363bb4a12f8aa49ec220cec37ff8eba1a9e214" _cell_guid="9c90d504-2ff8-42a2-8d0c-893e415d13bf"
title_mapping = {"Mr": 1, "Miss": 2, "Mrs": 3, "Master": 4, "Other": 5}
for dataset in combine:
    dataset['Title'] = dataset['Title'].map(title_mapping)
    dataset['Title'] = dataset['Title'].fillna(0)

train_df.head()

# + _execution_state="idle" _uuid="d696eba01d5c9524264d8fb2ba5409c6b70650b4" _cell_guid="58c53053-503b-4213-89c8-1e6311ea925a"
train_df = train_df.drop(['Name', 'PassengerId'], axis=1)
test_df = test_df.drop(['Name'], axis=1)
combine = [train_df, test_df]

# + _execution_state="idle" _uuid="d59490ab50f988015fc393421febefea8c0cb2cb" _cell_guid="425bf4e6-1e96-405c-87dc-ce19844ae287"
for dataset in combine:
    dataset['Sex'] = dataset['Sex'].map( {'female': 1, 'male': 0} ).astype(int)

train_df.head()

# + _execution_state="idle" _uuid="7930c403734540992ce6a66a7e7ae1ebea5c2c57" _cell_guid="f8cf39a0-055d-48cf-b264-22de1af59626"
age_median = np.zeros((2,3))
for dataset in combine:
    for sex in range(0,2):
        for pclass in range (0,3):
            age_df = dataset[(dataset['Sex']==sex) & (dataset['Pclass']==pclass+1)]['Age'].dropna()
            age_median[sex,pclass] = int(age_df.median())
    for sex in range(0,2):
        for pclass in range (0,3):
            dataset.loc[(dataset.Age.isnull()) & (dataset['Sex']==sex) & (dataset['Pclass']==pclass+1),'Age'] = age_median[sex,pclass]


# + _execution_state="idle" _uuid="7abd4ef03ddfe8ecb8b3613c6b8f5306b023eaed" _cell_guid="3d6394ed-804f-48cc-adf9-9fa239cce1dd"
bins = (0,6,12,18,25,35,60,100)
groups = [0,1,2,3,4,5,6]
for dataset in combine:
    dataset.Age = pd.cut(dataset['Age'],bins,labels=groups).astype(int)

# + _execution_state="idle" _uuid="741bd7ba2ca52fd481026848655dd9a2832f512f" _cell_guid="5b149ec5-9e8c-4743-9083-14cc8c32b4de"
for dataset in combine:
    dataset['isAlone'] = 0
    dataset.loc[(dataset['SibSp']==0) & (dataset['Parch']==0), 'isAlone'] = 1

train_df = train_df.drop(['SibSp','Parch'],axis=1)
test_df = test_df.drop(['SibSp','Parch'],axis=1)
combine = [train_df, test_df]

# + _execution_state="idle" _uuid="6ad306fa0cb376d0c9e51efbf680f3de4f436d79" _cell_guid="56433977-6dd5-471f-bcdc-cb27b3f143a8"
port = train_df.Embarked.dropna().mode()[0]
for dataset in combine:
    dataset['Embarked'] = dataset['Embarked'].fillna(port)

# + _execution_state="idle" _uuid="eeca8e4c673b361df51ce8430559a068d06a93fd" _cell_guid="a9d3337f-c13a-4c25-ad5c-19669ba09b64"
for dataset in combine:
    dataset['Embarked'] = dataset['Embarked'].map( {'S': 0, 'C': 1, 'Q': 2} ).astype(int)

# + _execution_state="idle" _uuid="20dc1a4c9b5d824981b6376b0a884e417eb70aad" _cell_guid="681cca0f-c230-475c-9b60-1bb2fd89fb5d"
test_df['Fare'].fillna(test_df['Fare'].dropna().median(),inplace=True)
test_df.info()

# + _execution_state="idle" _uuid="1f844be055086c9908c851fcd3c7120bee566212" _cell_guid="1716a637-101c-475f-bef1-bd03b813861b"
fare_band = pd.qcut(train_df['Fare'],5)
fare_band
bins = (-0.05,7.854,10.5,21.679,39.688,512.33)
groups = (0,1,2,3,4)
for dataset in combine:
    dataset.Fare = pd.cut(dataset['Fare'],bins,labels=groups).astype(int)

# + _execution_state="idle" _uuid="75146f6ec6f62543271a5dd39802d7868fc63426" _cell_guid="f5302161-ca53-4f82-9a5c-acc2e9887e24"
X_train = train_df.drop("Survived", axis=1)
Y_train = train_df["Survived"]
X_test  = test_df.drop("PassengerId", axis=1)

# + _execution_state="idle" _uuid="0a6c06486afed5257d785e3a27858e88f188017e" _cell_guid="4fbcf8da-b9f7-46f8-b1ff-70c91947d9a1"
logreg = LogisticRegression()
logreg.fit(X_train, Y_train)
Y_test = logreg.predict(X_test)
acc_log = round(logreg.score(X_train, Y_train) * 100, 2)
acc_log

# + _execution_state="idle" _uuid="9c9c16c7941a82001bf606208b4993ea3d32e899" _cell_guid="889acf94-c199-47b9-a850-d46135400fde"
coeff_df = pd.DataFrame()
coeff_df['Feature'] = train_df.columns.delete('0')
coeff_df["Correlation"] = pd.Series(logreg.coef_[0])
coeff_df.sort_values(by='Correlation', ascending=False)

# + _execution_state="idle" _uuid="c843af991337058e54c978e69569095e03bd0853" _cell_guid="a3110038-6eb1-48ca-9127-34d3ec0f0154"
svc = SVC()
svc.fit(X_train, Y_train)
Y_pred = svc.predict(X_test)
acc_svc = round(svc.score(X_train, Y_train) * 100, 2)
acc_svc

# + _execution_state="idle" _uuid="2c052440c67631204eed434d7e89ad0f816ee247" _cell_guid="9367f9cb-b8ef-459a-8de3-287d9fe1681a"
knn = KNeighborsClassifier(n_neighbors = 3)
knn.fit(X_train, Y_train)
Y_pred = knn.predict(X_test)
acc_knn = round(knn.score(X_train, Y_train) * 100, 2)
acc_knn

# + _execution_state="idle" _uuid="c5473b377f970ea073b677908ca157f0e9cfd2ae" _cell_guid="4e60189d-eeac-4b52-bb10-9a64ab844b34"
# Decision Tree

decision_tree = DecisionTreeClassifier()
decision_tree.fit(X_train, Y_train)
Y_pred = decision_tree.predict(X_test)
acc_decision_tree = round(decision_tree.score(X_train, Y_train) * 100, 2)
acc_decision_tree

# + _execution_state="idle" _uuid="11a692ba9f9a772a8ff12af5a5c8a30cb6cff4a6" _cell_guid="88ae01fb-5f03-44e2-895f-cae15bba244d"
# Random Forest

random_forest = RandomForestClassifier(n_estimators=100)
random_forest.fit(X_train, Y_train)
Y_pred = random_forest.predict(X_test)
random_forest.score(X_train, Y_train)
acc_random_forest = round(random_forest.score(X_train, Y_train) * 100, 2)
acc_random_forest

# + _execution_state="idle" _uuid="fc4f0dd6c50fcf8900e79ab3c326e2361dafbf95" _cell_guid="aebbe9b2-02b5-4538-b0c7-4f27700ba3ca"
submission = pd.DataFrame({
        "PassengerId": test_df["PassengerId"],
        "Survived": Y_pred
    })
submission.to_csv('submission.csv', index=False)

StarcoderdataJupyterScriptsDedupFiltered