text_plain_output_1.png
import numpy as np

import pandas as pd

import numpy as np
import pandas as pd
import seaborn as sb
from xgboost import XGBClassifier
train_df = pd.read_csv('../input/spaceship-titanic/train.csv')
test_df = pd.read_csv('../input/spaceship-titanic/test.csv')
X_train = train_df.iloc[:, :-1]
Y_train = train_df.iloc[:, -1]
X_test = test_df

X_train.drop(['Name'], axis=1, inplace=True)
X_test.drop(['Name'], axis=1, inplace=True)

max_age = X_train['Age'].max()
X_train['Age'] = X_train['Age'] / max_age
X_test['Age'] = X_test['Age'] / max_age
max_money = np.log(X_train['Summary'].max() + 1)
X_train['Summary'] = np.log(X_train['Summary'] + 1) / max_money
X_test['Summary'] = np.log(X_test['Summary'] + 1) / max_money
mean_age = X_train['Age'].mean(axis=0)
X_train['Age'].fillna(mean_age, axis=0, inplace=True)
X_test['Age'].fillna(mean_age, axis=0, inplace=True)

def id_parser(s):
    if s is np.nan:
        return np.nan
    group, _ = s.split('_')
    return group

X_train['GroupId'] = X_train['PassengerId'].apply(id_parser)
X_train.drop(['PassengerId'], axis=1, inplace=True)
X_test['GroupId'] = X_test['PassengerId'].apply(id_parser)
X_test.drop(['PassengerId'], axis=1, inplace=True)

def cabin_parser(row):
    s = row['Cabin']
    if s is np.nan:
        return [np.nan] * 3
    deck, cabin, side = s.split('/')
    return [deck, side, cabin]

X_train[['Deck', 'Side', 'GroupCabin']] = X_train.apply(cabin_parser, axis=1, result_type='expand')
X_test[['Deck', 'Side', 'GroupCabin']] = X_test.apply(cabin_parser, axis=1, result_type='expand')
X_train.drop(['Cabin'], axis=1, inplace=True)
X_test.drop(['Cabin'], axis=1, inplace=True)

def get_groups(group_column):
    groups = group_column.value_counts()
    groups = dict(groups)
    return groups

groups = ['GroupId', 'GroupCabin']
united_df = pd.concat([X_train, X_test], ignore_index=True)
for group in groups:
    group_count = get_groups(united_df[group])
    X_train.replace(group_count, inplace=True)
    X_test.replace(group_count, inplace=True)
m = (X_train[groups] - 1).max()
X_train[groups] = (X_train[groups] - 1) / m
X_test[groups] = (X_test[groups] - 1) / m
X_train[groups] = X_train[groups].fillna(0, axis=0)
X_test[groups] = X_test[groups].fillna(0, axis=0)

cols = ['CryoSleep', 'VIP', 'Side', 'Destination', 'HomePlanet', 'Deck']
X_train = pd.get_dummies(X_train, columns=cols, dtype='float64')
X_test = pd.get_dummies(X_test, columns=cols, dtype='float64')

X_train.describe()