<jupyter_start><jupyter_text><jupyter_code>!pip install catboost
!pip install ipywidgets
!jupyter nbextension enable --py widgetsnbextension
#Import all the required libraries
import pandas as pd
import numpy as np
%matplotlib inline 
import math, time, random, datetime

# Visualization 
import matplotlib.pyplot as plt
import missingno
import seaborn as sns
plt.style.use('seaborn-whitegrid')

# Preprocessing
from sklearn.preprocessing import OneHotEncoder, LabelEncoder, label_binarize

# Machine learning
import catboost
from sklearn.model_selection import train_test_split
from sklearn import model_selection, tree, preprocessing, metrics, linear_model
from sklearn.svm import LinearSVC
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.naive_bayes import GaussianNB
from sklearn.linear_model import LinearRegression, LogisticRegression, SGDClassifier
from sklearn.tree import DecisionTreeClassifier
from catboost import CatBoostClassifier, Pool, cv

# Ignore the warnings if any
import warnings
warnings.filterwarnings('ignore')
# Import the train and test data 
train = pd.read_csv('https://raw.githubusercontent.com/VallabhK/TitanicKaggle/main/Data/train.csv')
test = pd.read_csv('https://raw.githubusercontent.com/VallabhK/TitanicKaggle/main/Data/test.csv')
gender_submission = pd.read_csv('https://raw.githubusercontent.com/VallabhK/TitanicKaggle/main/Data/gender_submission.csv') 
#Lets check the training data
train.head()
train.Age.plot.hist()
test.head()<jupyter_output><empty_output><jupyter_text>Data DescriptionsSurvival: 0 = No, 1 = Yespclass (Ticket class): 1 = 1st, 2 = 2nd, 3 = 3rdsex: SexAge: Age in yearssibsp: number of siblings/spouses aboard the Titanicparch: number of parents/children aboard the Titanicticket: Ticket numberfare: Passenger farecabin: Cabin numberembarked: Port of Embarkation, C = Cherbourg, Q = Queenstown, S = Southampton<jupyter_code>train.describe()
#Check the missing values
missingno.matrix(train, figsize = (30,10))<jupyter_output><empty_output><jupyter_text>From the above plot it is clear that Age and Cabin column has missing values. We need to fix these values as they might give us false results<jupyter_code>#Check how many null values are there in each column
train.isnull().sum()
df_bin = pd.DataFrame() # for discretised continuous variables
df_con = pd.DataFrame() # for continuous variables
train.dtypes
train.head()
#Let's look at how many people survived
fig = plt.figure(figsize=(20,1))
sns.countplot(y='Survived', data=train);
print(train.Survived.value_counts())
df_bin['Survived'] = train['Survived']
df_con['Survived'] = train['Survived']
df_bin.head()
sns.distplot(train.Pclass)
train.Pclass.isnull().sum()
df_bin['Pclass'] = train['Pclass']
df_con['Pclass'] = train['Pclass']
train.Name.value_counts()
plt.figure(figsize=(10, 5))
sns.countplot(y="Sex", data=train);
train.Sex.isnull().sum()
train.Sex.head()
df_bin['Sex'] = train['Sex']
df_bin['Sex'] = np.where(df_bin['Sex'] == 'female', 1, 0) # change sex to 0 for male and 1 for female

df_con['Sex'] = train['Sex']
fig = plt.figure(figsize=(10, 10))
sns.distplot(df_bin.loc[df_bin['Survived'] == 1]['Sex'], kde_kws={'label': 'Survived'});
sns.distplot(df_bin.loc[df_bin['Survived'] == 0]['Sex'], kde_kws={'label': 'Did not survive'});
# How many missing values does age have?
train.Age.isnull().sum()
def plot_count_dist(data, bin_df, label_column, target_column, figsize=(20, 5), use_bin_df=False):
    """
    Function to plot counts and distributions of a label variable and 
    target variable side by side.
    ::param_data:: = target dataframe
    ::param_bin_df:: = binned dataframe for countplot
    ::param_label_column:: = binary labelled column
    ::param_target_column:: = column you want to view counts and distributions
    ::param_figsize:: = size of figure (width, height)
    ::param_use_bin_df:: = whether or not to use the bin_df, default False
    """
    if use_bin_df: 
        fig = plt.figure(figsize=figsize)
        plt.subplot(1, 2, 1)
        sns.countplot(y=target_column, data=bin_df);
        plt.subplot(1, 2, 2)
        sns.distplot(data.loc[data[label_column] == 1][target_column], 
                     kde_kws={"label": "Survived"});
        sns.distplot(data.loc[data[label_column] == 0][target_column], 
                     kde_kws={"label": "Did not survive"});
    else:
        fig = plt.figure(figsize=figsize)
        plt.subplot(1, 2, 1)
        sns.countplot(y=target_column, data=data);
        plt.subplot(1, 2, 2)
        sns.distplot(data.loc[data[label_column] == 1][target_column], 
                     kde_kws={"label": "Survived"});
        sns.distplot(data.loc[data[label_column] == 0][target_column], 
                     kde_kws={"label": "Did not survive"});
train.SibSp.isnull().sum()
train.SibSp.value_counts()
#Add SibSp to subset dataframes
df_bin['SibSp'] = train['SibSp']
df_con['SibSp'] = train['SibSp']
plot_count_dist(train, 
                bin_df=df_bin, 
                label_column='Survived', 
                target_column='SibSp', 
                figsize=(20, 10))
#Let's Analyse Parch feature from the dataset
#Let's look for any missing values
train.Parch.isnull().sum()
train.Parch.value_counts()
# Add Parch to subset dataframes
df_bin['Parch'] = train['Parch']
df_con['Parch'] = train['Parch']
# Visualise the counts of Parch and the distribution of the values
# against Survived
plot_count_dist(train, 
                bin_df=df_bin,
                label_column='Survived', 
                target_column='Parch', 
                figsize=(20, 10))
train.head()
df_con.head()
#Let's Analyse Ticket feature and how it is related to survival variable

train.Ticket.isnull().sum()
# How many kinds of ticket are there?
sns.countplot(y="Ticket", data=train);
train.Ticket.value_counts()
# How many unique kinds of Ticket are there?
print("There are {} unique Ticket values.".format(len(train.Ticket.unique())))
#Fare: Price of the ticket and relation with survival variable
train.Fare.isnull().sum()
sns.countplot(y="Fare", data=train);
# How many unique kinds of Fare are there?
print("There are {} unique Fare values.".format(len(train.Fare.unique())))
#Let's add fare to the continuous sub data frame
# Add Fare to sub dataframes
df_con['Fare'] = train['Fare'] 
df_bin['Fare'] = pd.cut(train['Fare'], bins=5) # discretised
df_bin.Fare.value_counts()
plot_count_dist(data=train,
                bin_df=df_bin,
                label_column='Survived', 
                target_column='Fare', 
                figsize=(20,10), 
                use_bin_df=True)
#Let's Analyse Cabin Feature
train.Cabin.isnull().sum()
train.head()
train.Cabin.value_counts() #Ignored Cabin feature right now due to lot of missing values
#Let's analyse Embarked feature
train.Embarked.isnull().sum()
train.Embarked.value_counts() #This is a categorical variable
sns.countplot(y='Embarked', data=train)
# Add Embarked to sub dataframes
df_bin['Embarked'] = train['Embarked']
df_con['Embarked'] = train['Embarked']
# Remove Embarked rows which are missing values
print(len(df_con))
df_con = df_con.dropna(subset=['Embarked'])
df_bin = df_bin.dropna(subset=['Embarked'])
print(len(df_con))
#Now that we have analysed all the features, its time to encode them using one hot
#encoding using sklearn library
df_bin.head()
# One-hot encode binned variables
one_hot_cols = df_bin.columns.tolist()
one_hot_cols.remove('Survived')
df_bin_enc = pd.get_dummies(df_bin, columns=one_hot_cols)

df_bin_enc.head()

df_con.head(10)
# One hot encode the categorical columns
df_embarked_one_hot = pd.get_dummies(df_con['Embarked'], 
                                     prefix='embarked')

df_sex_one_hot = pd.get_dummies(df_con['Sex'], 
                                prefix='sex')

df_plcass_one_hot = pd.get_dummies(df_con['Pclass'], 
                                   prefix='pclass')
# Combine the one hot encoded columns with df_con_enc
df_con_enc = pd.concat([df_con, 
                        df_embarked_one_hot, 
                        df_sex_one_hot, 
                        df_plcass_one_hot], axis=1)

# Drop the original categorical columns (because now they've been one hot encoded)
df_con_enc = df_con_enc.drop(['Pclass', 'Sex', 'Embarked'], axis=1)
# Let's look at df_con_enc
df_con_enc.head(20)
#Let's build machine learning model now
selected_df = df_con_enc
#Copied the dataframe
selected_df.head()
# Split the dataframe into data and labels
X_train = selected_df.drop('Survived', axis=1) # data
y_train = selected_df.Survived # labels
X_train.shape
X_train.head()
y_train.shape
#Let's define a function to fit machine leaning algorithms
#This function will run the specified algorithm and return corresponding accuracy metric
def fit_ml_algo(algo, X_train, y_train, cv):
    
    # One Pass
    model = algo.fit(X_train, y_train)
    acc = round(model.score(X_train, y_train) * 100, 2)
    
    # Cross Validation 
    train_pred = model_selection.cross_val_predict(algo, 
                                                  X_train, 
                                                  y_train, 
                                                  cv=cv, 
                                                  n_jobs = -1)
    # Cross-validation accuracy metric
    acc_cv = round(metrics.accuracy_score(y_train, train_pred) * 100, 2)
    
    return train_pred, acc, acc_cv<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11573829404925484, 'fraction_numerical': 0.008032977486523624, 'mean_word_length': 3.832482124616956, 'pattern_counts': {'":': 4, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 3, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 11, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '266525', 'license': 'MIT', 'n_tokens_mistral': 3232, 'n_tokens_neox': 3066, 'n_words': 775, 'path': 'Titanic_MLfromDisaster.ipynb', 'repo_name': 'VallabhK/TitanicKaggle', 'size': '9461'}