text_plain_output_1.png image_output_1.png
from sklearn.impute import KNNImputer

from sklearn.model_selection import train_test_split

from sklearn.preprocessing import MinMaxScaler

from sklearn.preprocessing import StandardScaler

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

import seaborn as sns

from sklearn import preprocessing
from sklearn.model_selection import train_test_split
test = pd.read_csv('/kaggle/input/titanic/test.csv')
train = pd.read_csv('/kaggle/input/titanic/train.csv')
def countna(df, column):
    col = pd.Series(list(df.loc[:, column]))
    num = col.isna().sum()
for column in train.columns:
    countna(train, column)
train = train.drop(['Cabin'], axis=1)
train = train.dropna(subset=['Embarked'])
for column in train.columns:
    countna(train, column)
irrelevant_columns = ['PassengerId']
categorical_columns = ['Pclass', 'Embarked']
to_binary_columns = ['Sex']
numerical_columns = ['Age', 'SibSp', 'Parch', 'Fare']
other_columns = ['Name', 'Ticket']
label_columns = ['Survived']
train = train.drop(['PassengerId'], axis=1)
trainbeforedummies = train.copy()
trainbeforedummies['Survived'] = trainbeforedummies['Survived'].replace(['0'], 'Did Not Survive')
trainbeforedummies['Survived'] = trainbeforedummies['Survived'].replace(['1'], 'Survived')
train = pd.get_dummies(train, columns=categorical_columns)
gender = {'male': 1, 'female': 0}
train.Sex = [gender[item] for item in train.Sex]
train = train.drop(['Name', 'Ticket'], axis=1)
y = train.pop('Survived')
x = train
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.33)
from sklearn.impute import KNNImputer
imputer = KNNImputer(n_neighbors=2, weights='uniform')
imputer = imputer.fit(x)
x_train = imputer.transform(x_train)
x_test = imputer.transform(x_test)
from sklearn.preprocessing import MinMaxScaler
norm = MinMaxScaler().fit(x_train)
x_train_norm = norm.transform(x_train)
x_test_norm = norm.transform(x_test)
from sklearn.preprocessing import StandardScaler
x_train_stand = x_train.copy()
x_test_stand = x_test.copy()
for i in [1, 2, 3, 4]:
    scale = StandardScaler().fit(x_train_stand[[i]])
    x_train_stand[i] = scale.transform(x_train_stand[[i]])
    x_test_stand[i] = scale.transform(x_test_stand[[i]])

x_train_stand_df = pd.DataFrame(x_train_stand, columns=list(x.columns))
x_train_norm_df = pd.DataFrame(x_train_norm, columns=list(x.columns))
x_train_reg_df = pd.DataFrame(x_train, columns=list(x.columns))
x_test_stand_df = pd.DataFrame(x_test_stand, columns=list(x.columns))
x_test_norm_df = pd.DataFrame(x_test_norm, columns=list(x.columns))
x_test_reg_df = pd.DataFrame(x_test, columns=list(x.columns))
x_stand_df = pd.concat([x_train_stand_df, x_test_stand_df])
x_norm_df = pd.concat([x_train_norm_df, x_test_norm_df])
x_reg_df = pd.concat([x_train_reg_df, x_test_reg_df])
x_stand_df_with_y = x_stand_df.copy()
x_stand_df_with_y['Survived'] = y
x_norm_df_with_y = x_norm_df.copy()
x_norm_df_with_y['Survived'] = y
x_reg_df_with_y = x_norm_df.copy()
x_reg_df_with_y['Survived'] = y

import seaborn as sns

sns.countplot(data=trainbeforedummies, x='Parch', hue='Survived')