text_plain_output_1.png
from sklearn.metrics import accuracy_score

from sklearn.model_selection import GridSearchCV

from sklearn.model_selection import train_test_split

from sklearn.preprocessing import OneHotEncoder

import pandas as pd

import seaborn as sns

import xgboost as xgb

train = pd.read_csv('/kaggle/input/titanic/train.csv')
test = pd.read_csv('/kaggle/input/titanic/test.csv')

def wrangle(df):
    df['Male'] = df['Sex'].map(lambda x: x == 'male')
    df['Pronoun'] = df['Name'].map(lambda x: x.split(', ')[1].split('.')[0])
    df['Prefix'] = df['Ticket'].map(lambda x: x.split(' ')[0].split('/')[0].replace('.', '') if ' ' in x else '')
    df['Letter'] = df['Cabin'].fillna('00').map(lambda x: x[0])
    df['Family'] = df['SibSp'] + df['Parch']
    df = df.drop(['PassengerId', 'Name', 'Ticket', 'Sex', 'Cabin'], axis=1)
    return df

def encode(X_train, X_test, cat_cols):
    encoder = OneHotEncoder(categories='auto', sparse=False, handle_unknown='ignore')
    encoder.fit(X_train[cat_cols])
    X_train_encoded = encoder.transform(X_train[cat_cols])
    X_train_final = pd.concat([X_train.drop(cat_cols, axis=1), pd.DataFrame(X_train_encoded, index=X_train.index)], axis=1)
    X_test_encoded = encoder.transform(X_test[cat_cols])
    X_test_final = pd.concat([X_test.drop(cat_cols, axis=1), pd.DataFrame(X_test_encoded, index=X_test.index)], axis=1)
    return (X_train_final, X_test_final, encoder)

new_train = wrangle(train)

sns.set(style='whitegrid')
sns.despine(left=True)

new_train['AgeGroup'] = pd.cut(new_train['Age'], bins=[0, 10, 20, 30, 40, 50, 60, 70, 80])
age_survival = new_train.groupby(['AgeGroup'])['Survived'].mean().reset_index()

new_train['FareGroup'] = pd.cut(new_train['Fare'], bins=[0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 600])
age_survival = new_train.groupby(['FareGroup'])['Survived'].mean().reset_index()
new_train = new_train.drop(['AgeGroup', 'FareGroup'], axis=1)

X_train, X_test, y_train, y_test = train_test_split(new_train.drop('Survived', axis=1), new_train['Survived'], test_size=0.2, random_state=42)

cat_cols = ['Embarked', 'Pronoun', 'Prefix', 'Letter']
X_train_final, X_test_final, encoder = encode(X_train, X_test, cat_cols)

xgb_classifier = xgb.XGBClassifier()
xgb_classifier.fit(X_train_final, y_train)
y_pred = xgb_classifier.predict(X_test_final)
accuracy = accuracy_score(y_test, y_pred)

param_grid = {'max_depth': [3, 4, 5], 'learning_rate': [0.1, 0.01], 'n_estimators': [100, 200], 'gamma': [0, 0.1], 'colsample_bytree': [0.6, 0.8]}
xgb_classifier = xgb.XGBClassifier()
grid_search = GridSearchCV(estimator=xgb_classifier, param_grid=param_grid, cv=5, n_jobs=-1)
grid_search.fit(X_train_final, y_train)

best_xgb_classifier = grid_search.best_estimator_
y_pred = best_xgb_classifier.predict(X_test_final)
accuracy = accuracy_score(y_test, y_pred)

new_test = wrangle(test)

X_train_final, X_test_final, encoder = encode(new_train.drop('Survived', axis=1), new_test, cat_cols)
y_train = new_train['Survived']

xgb_classifier = xgb.XGBClassifier()
grid_search = GridSearchCV(estimator=xgb_classifier, param_grid=param_grid, cv=5, n_jobs=-1)
grid_search.fit(X_train_final, y_train)
final_xgb_classifier = grid_search.best_estimator_

importance = final_xgb_classifier.feature_importances_
features = X_train_final.columns
encodings = encoder.get_feature_names()
translated = []
for feature in features:
    if feature in range(len(encodings)):
        translated.append(encodings[feature])
    else:
        translated.append(feature)
df_importance = pd.DataFrame({'Feature': translated, 'Importance': importance}).sort_values('Importance', ascending=False)
df_importance.head(10)