text_plain_output_1.png
from sklearn.preprocessing import OneHotEncoder, StandardScaler

import matplotlib.pyplot as plt

import numpy as np

import numpy as np # linear algebra

import pandas as pd

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

import seaborn as sns

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
sns.set()
from sklearn.preprocessing import OneHotEncoder, StandardScaler
from sklearn.model_selection import train_test_split, cross_val_score, GridSearchCV
from sklearn.metrics import confusion_matrix, accuracy_score
from sklearn.svm import SVC
from sklearn.ensemble import RandomForestClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.naive_bayes import GaussianNB

data_train = pd.read_csv('/kaggle/input/titanic/train.csv')
data_test = pd.read_csv('/kaggle/input/titanic/test.csv')
data_total = data_train.append(data_test, ignore_index=True)
data_train_length = len(data_train.index)
for c in ['Age', 'Fare']:
    g = sns.FacetGrid(data_train, col='Survived')
    g.map(plt.hist, c)
corr = data_train.corr()

age_mean_by_pclass = data_total[['Pclass', 'Age']].groupby('Pclass').mean()
data_total['Age'] = data_total.apply(lambda row: age_mean_by_pclass.loc[row['Pclass'], 'Age'] if np.isnan(row['Age']) else row['Age'], axis=1)
fare_median_by_pclass = data_total[['Pclass', 'Fare']].groupby('Pclass').median()
data_total['Fare'] = data_total.apply(lambda row: fare_median_by_pclass.loc[row['Pclass'], 'Fare'] if np.isnan(row['Fare']) else row['Fare'], axis=1)
data_total['Embarked'].fillna(data_total['Embarked'].mode()[0], inplace=True)

data_total['isAlone'] = (data_total['SibSp'] + data_total['Parch'] == 0) * 1
data_total['bigGroup'] = (data_total['SibSp'] + data_total['Parch'] > 3) * 1
data_total['Title'] = data_total['Name'].str.extract(' ([A-Za-z]+)\\.', expand=False)
data_total['Title'] = data_total['Title'].replace(['Lady', 'Countess', 'Capt', 'Col', 'Don', 'Dr', 'Major', 'Rev', 'Sir', 'Jonkheer', 'Dona'], 'Other')
data_total['Title'] = data_total['Title'].replace(['Mlle', 'Ms', 'Mme'], 'Miss')
age_bin_count = 10
data_total['Age_Bin'] = pd.cut(data_total['Age'], age_bin_count, labels=list(range(1, age_bin_count + 1)))
fare_bin_count = 5
data_total['Fare_Bin'] = pd.qcut(data_total['Fare'], fare_bin_count, labels=list(range(1, fare_bin_count + 1)))
enc = OneHotEncoder().fit(data_total[['Sex', 'Embarked', 'Title']])
enc_features_arr = enc.transform(data_total[['Sex', 'Embarked', 'Title']]).toarray()
enc_features_labels = [label for cat in enc.categories_ for label in cat]
enc_features_df = pd.DataFrame(enc_features_arr, columns=enc_features_labels).drop(columns=['male', 'S', 'Other'])
data_total = data_total.join(enc_features_df)
data_total = data_total.drop(columns=['PassengerId', 'Name', 'Cabin', 'Ticket', 'SibSp', 'Parch', 'Age', 'Fare', 'Sex', 'Embarked', 'Title'])
data_total.head()