<jupyter_start><jupyter_text>Using `sklearn` to predict if a passanger survived the titanic shipwreck Imports<jupyter_code>from hashlib import sha1

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import altair as alt
from IPython.display import HTML

pd.set_option("display.max_colwidth", 200)

from sklearn.compose import ColumnTransformer
from sklearn.dummy import DummyClassifier
from sklearn.impute import SimpleImputer
from sklearn.model_selection import cross_val_score, cross_validate, train_test_split, RandomizedSearchCV, GridSearchCV
from sklearn.neighbors import KNeighborsClassifier
from sklearn.pipeline import Pipeline, make_pipeline
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.preprocessing import (
    FunctionTransformer,
    Normalizer,
    OneHotEncoder,
    OrdinalEncoder,
    StandardScaler,
    normalize,
    scale
)
from sklearn.svm import SVC
from sklearn.tree import DecisionTreeClassifier<jupyter_output><empty_output><jupyter_text>Get the data Raw Training data<jupyter_code>raw_df = pd.read_csv('data/titanic/train.csv')
raw_df<jupyter_output><empty_output><jupyter_text>EDA Shape and null values<jupyter_code>raw_df.shape
raw_df.info()<jupyter_output><class 'pandas.core.frame.DataFrame'>
RangeIndex: 891 entries, 0 to 890
Data columns (total 12 columns):
PassengerId    891 non-null int64
Survived       891 non-null int64
Pclass         891 non-null int64
Name           891 non-null object
Sex            891 non-null object
Age            714 non-null float64
SibSp          891 non-null int64
Parch          891 non-null int64
Ticket         891 non-null object
Fare           891 non-null float64
Cabin          204 non-null object
Embarked       889 non-null object
dtypes: float64(2), int64(5), object(5)
memory usage: 83.7+ KB<jupyter_text>Dropping featuresI don't think I can effectively use `Ticket`. `Cabin` has a lot of nulls. I am dropping these (and `PassengerId`)<jupyter_code>relevant_df = raw_df.drop(columns=['Cabin', 'Ticket'])

relevant_df.shape
relevant_df.info()
X_train, X_test, y_train, y_test = train_test_split(relevant_df.drop(columns=['Survived']), relevant_df['Survived'], test_size=0.20, random_state=2020)
X_train<jupyter_output><empty_output><jupyter_text>Feature EngineeringI'm splitting the data<jupyter_code>num_features = ['Pclass', 'Age', 'SibSp', 'Parch', 'Fare', 'PassengerId']
unordered_features = ['Sex']
ordered_features = ['Embarked']

X_train.Embarked.unique()
ports = ['S', 'C', 'Q']
numeric_pipe = make_pipeline(
    SimpleImputer(strategy='median'),
    StandardScaler()
)

ohe_pipe = make_pipeline(
    SimpleImputer(strategy='constant'),
    OneHotEncoder(handle_unknown='ignore')
)

ord_pipe = make_pipeline(
    SimpleImputer(strategy='constant'),
    OrdinalEncoder(categories=[ports], dtype='int')
)

preprocessor = ColumnTransformer(
    transformers=[
        ('num', numeric_pipe, num_features),
        ('ohe', ohe_pipe, unordered_features),
        ('bow', CountVectorizer(), 'Name'),
        ('ord', ord_pipe, ordered_features)
    ]
)

iceberg  = Pipeline(
    steps=[
        ('pre', preprocessor),
        ('clf', KNeighborsClassifier())
    ]
)
param_grid = {
    "pre__bow__max_features": [10, 100, 1000],
    "clf__n_neighbors": [3, 5, 11, 19],
    "clf__weights": ['uniform', 'distance'],
    "pre__ord__simpleimputer__fill_value": ['S', 'Q', 'C']
}
grid = GridSearchCV(iceberg, param_grid=param_grid, n_jobs=-1)
grid.fit(X_train, y_train)
grid.best_estimator_
grid.best_score_
grid.best_estimator_.score(X_test, y_test)
predict_df = pd.read_csv('data/titanic/test.csv')
predict_df.drop(columns=['PassengerId', 'Cabin', 'Ticket'])
predictions = grid.best_estimator_.predict(predict_df.drop(columns=['Cabin', 'Ticket']))
predictions
submit_df = pd.merge(predict_df, pd.DataFrame(predictions), how='left', left_index=True, right_index=True).iloc[:, [0,-1]]
submit_df.rename(columns={0: 'Survived'}, inplace=True)
submit_df.to_csv('data/titanic/submission.csv', index=False)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1154700222057735, 'fraction_numerical': 0.02442635085122132, 'mean_word_length': 5.314641744548287, 'pattern_counts': {'":': 4, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '542466', 'license': 'MIT', 'n_tokens_mistral': 1467, 'n_tokens_neox': 1385, 'n_words': 303, 'path': 'titanic-survivor.ipynb', 'repo_name': 'vigneshRajakumar/kaggle-notebooks', 'size': '4053'}