Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<compute_test_metric>
import pandas as pd
import numpy as np
score = auc_score(lasso)
 print("
Lasso score: {:.4f}({:.4f})
 ".format(score.mean() , score.std()))
score = auc_score(ENet)
 print("ElasticNet score: {:.4f}({:.4f})
 ".format(score.mean() , score.std()))
score = auc_score(model_lgb)
 print("LGBM score: {:.4f}({:.4f})
 ".format(score.mean() , score.std()))
score = auc_score(glm)
 print("Logistic Regression: {:.4f}({:.4f})
 ".format(score.mean() , score.std()))
".format(score.mean() , score.std()))
".format(score.mean() , score.std()))
".format(score.mean() , score.std()))<train_model>
train=pd.read_csv(".. /input/train.csv")
 test=pd.read_csv(".. /input/test.csv" )
class average_stacking(BaseEstimator, RegressorMixin, TransformerMixin):
def __init__(self,models):
self.models = models
def fit(self, x,y):
self.model_clones = [clone(x)for x in self.models]
for model in self.model_clones:
model.fit(x,y)
 return self
def predict(self, x):
preds = np.column_stack([
model.predict(x)for model in self.model_clones
])
 return np.mean(preds, axis = 1 )<compute_train_metric>
train.drop(['Name'],axis=1,inplace=True )
averaged_models = average_stacking(models =(ENet, glm,model_lgb, lasso))
score = auc_score(averaged_models)
 print(" Averaged base models score: {:.4f}({:.4f})
 ".format(score.mean() , score.std()))<predict_on_test>
sample_sub=pd.read_csv(".. /input/gender_submission.csv" )
averaged_models.fit(train.values, train_y)
 avg_pred = averaged_models.predict(test )<save_to_csv>
test.drop(['Name'],axis=1,inplace=True )
pd.DataFrame({'id': test_id, 'target': avg_pred} ).to_csv('submission.csv', index=False )<set_options>
train.isnull().sum()
%matplotlib inline 
<load_from_csv>
train.isnull().sum()
def read_data(file_path):
print('Loading datasets...')
 train = pd.read_csv(file_path + 'train.csv', sep=',')
 test = pd.read_csv(file_path + 'test.csv', sep=',')
 print('Datasets loaded')
 return train, test<split>
test.isnull().sum()
PATH = '.. /input/cat-in-the-dat-ii/'
train, test = read_data(PATH )<count_unique_values>
test.isnull().sum()
def zoom_dataset(data):
Count_missing_val = data.isnull().sum()
Percent_missing =(data.isnull().sum() /data.isnull().count() *100)
 Percent_no_missing = 100 - Percent_missing
Count_unique = data.nunique()
Percent_unique_val = Count_unique / len(data)*100
Type = data.dtypes
data=[[i, Counter(data[i][data[i].notna() ] ).most_common(3)] for i in list(data)]
top = pd.DataFrame(data, columns=['Name', 'Top_3'] ).set_index(['Name'])
 tt = pd.concat([Percent_no_missing, Count_missing_val, Percent_missing, Count_unique, Percent_unique_val, Type], axis=1,
keys=['Percent_no_missing','Count_missing_val', 'Percent_missing', ' Count_unique', 'Percent_unique_val', 'Type'])
 tt = pd.concat([tt, top[['Top_3']]], axis=1, sort=False ).reset_index()
return(( tt))<categorify>
train.drop(['Cabin'],axis=1,inplace=True)
 test.drop(['Cabin'],axis=1,inplace=True)
 test.drop(['Ticket'],axis=1,inplace=True)
 train.drop(['Ticket'],axis=1,inplace=True)
 train.drop(['PassengerId'],axis=1,inplace=True)
 test.drop(['PassengerId'],axis=1,inplace=True )
def encoding(train, test, smooth):
print('Target encoding...')
 train.sort_index(inplace=True)
 target = train['target']
test_id = test['id']
train.drop(['target', 'id'], axis=1, inplace=True)
 test.drop('id', axis=1, inplace=True)
 cat_feat_to_encode = train.columns.tolist()
smoothing=smooth
oof = pd.DataFrame([])
 for tr_idx, oof_idx in StratifiedKFold(n_splits=5, random_state=2020, shuffle=True ).split(train, target):
ce_target_encoder = ce.TargetEncoder(cols = cat_feat_to_encode, smoothing=smoothing)
 ce_target_encoder.fit(train.iloc[tr_idx, :], target.iloc[tr_idx])
 oof = oof.append(ce_target_encoder.transform(train.iloc[oof_idx, :]), ignore_index=False)
 ce_target_encoder = ce.TargetEncoder(cols = cat_feat_to_encode, smoothing=smoothing)
 ce_target_encoder.fit(train, target)
 train = oof.sort_index()
test = ce_target_encoder.transform(test)
 features = list(train)
 print('Target encoding done!')
 return train, test, test_id, features, target<feature_engineering>
train.isnull().sum()
def timer(start_time=None):
if not start_time:
start_time = datetime.now()
return start_time
elif start_time:
thour, temp_sec = divmod(( datetime.now() - start_time ).total_seconds() , 3600)
 tmin, tsec = divmod(temp_sec, 60)
 print('Time taken : %i hours %i minutes and %s seconds.' %(thour, tmin, round(tsec, 2)) )<train_model>
train.isnull().sum()
def run_model(splits, features, target, train):
model = lgb.LGBMClassifier(**{
'learning_rate': 0.05,
'feature_fraction': 0.1,
'min_data_in_leaf' : 12,
'max_depth': 3,
'reg_alpha': 1,
'reg_lambda': 1,
'objective': 'binary',
'metric': 'auc',
'n_jobs': -1,
'n_estimators' : 5000,
'feature_fraction_seed': 42,
'bagging_seed': 42,
'boosting_type': 'gbdt',
'verbose': 1,
'is_unbalance': True,
'boost_from_average': False})
 plt.rcParams['figure.figsize']=(6,4)
 start_time = timer(None)
 print('LGBM modeling...')
 cms= []
tprs = []
aucs = []
y_real = []
y_proba = []
recalls = []
roc_aucs = []
mean_tpr = []
mean_fpr = []
f1_scores = []
accuracies = []
precisions = []
oof = np.zeros(len(train))
mean_fpr = np.linspace(0,1,100)
 feature_importance_df = pd.DataFrame()
i = 1
folds = StratifiedKFold(n_splits=splits, shuffle=True, random_state=42)
 for fold_,(trn_idx, val_idx)in enumerate(folds.split(train.values, target.values)) :
print('Fold:', fold_)
 model = model.fit(train.iloc[trn_idx][features], target.iloc[trn_idx],
eval_set = [(train.iloc[trn_idx][features], target.iloc[trn_idx]), 
(train.iloc[val_idx][features], target.iloc[val_idx])],
verbose = 1000,
eval_metric = 'auc',
early_stopping_rounds = 1000)
 oof[val_idx] = model.predict_proba(train.iloc[val_idx][features])[:,1]
f = plt.figure(1)
 fpr, tpr, t = roc_curve(target[val_idx], oof[val_idx])
 tprs.append(interp(mean_fpr, fpr, tpr))
roc_auc = auc(fpr, tpr)
 aucs.append(roc_auc)
 plt.plot(fpr, tpr, lw=2, alpha=0.5, label='ROC fold %d(AUC = %0.4f)' %(i,roc_auc))
g = plt.figure(2)
 precision, recall, _ = precision_recall_curve(target[val_idx], oof[val_idx])
 y_real.append(target[val_idx])
 y_proba.append(oof[val_idx])
 plt.plot(recall, precision, lw=2, alpha=0.3, label='P|R fold %d' %(i))
i= i+1
roc_aucs.append(roc_auc_score(target[val_idx], oof[val_idx]))
accuracies.append(accuracy_score(target[val_idx], oof[val_idx].round()))
recalls.append(recall_score(target[val_idx], oof[val_idx].round()))
precisions.append(precision_score(target[val_idx], oof[val_idx].round()))
f1_scores.append(f1_score(target[val_idx], oof[val_idx].round()))
cms.append(confusion_matrix(target[val_idx], oof[val_idx].round()))
print(
 '
CV roc score : {0:.4f}, std: {1:.4f}.'.format(np.mean(roc_aucs), np.std(roc_aucs)) ,
'
CV accuracy score : {0:.4f}, std: {1:.4f}.'.format(np.mean(accuracies), np.std(accuracies)) ,
'
CV recall score : {0:.4f}, std: {1:.4f}.'.format(np.mean(recalls), np.std(recalls)) ,
'
CV precision score : {0:.4f}, std: {1:.4f}.'.format(np.mean(precisions), np.std(precisions)) ,
'
CV f1 score : {0:.4f}, std: {1:.4f}.'.format(np.mean(f1_scores), np.std(f1_scores)))
 print('Modeling done!
')
 timer(start_time)
 return model, cms, tprs, aucs, y_real, y_proba, mean_fpr, mean_tpr, train
def graph_metrics(cms, tprs, aucs, y_real, y_proba, mean_fpr, mean_tpr):
f = plt.figure(1)
 plt.plot([0,1],[0,1],linestyle = '--',lw = 2,color = 'grey')
 mean_tpr = np.mean(tprs, axis=0)
 mean_auc = auc(mean_fpr, mean_tpr)
 plt.plot(mean_fpr, mean_tpr, color='blue',
label=r'Mean ROC(AUC = %0.4f)' %(( mean_auc)) ,lw=2, alpha=1)
 plt.grid(b=True, which='major', color='grey', linewidth=0.4)
 plt.xlabel('False Positive Rate', fontsize=12)
 plt.ylabel('True Positive Rate', fontsize=12)
 plt.title('LGBM - ROC by folds', fontsize=18)
 plt.legend(loc="lower right")
 g = plt.figure(2)
 plt.plot([0,1],[1,0],linestyle = '--',lw = 2,color = 'grey')
 y_real = np.concatenate(y_real)
 y_proba = np.concatenate(y_proba)
 precision, recall, _ = precision_recall_curve(y_real, y_proba)
 plt.plot(recall, precision, color='blue',
label=r'Mean P|R')
 plt.grid(b=True, which='major', color='grey', linewidth=0.4)
 plt.xlabel('Recall')
 plt.ylabel('Precision')
 plt.title('LGBM P|R curve by folds', fontsize=18)
 plt.legend(loc="lower left")
 plt.rcParams["axes.grid"] = False
cm = np.average(cms, axis=0)
 class_names = [0,1]
plt.figure()
plot_confusion_matrix(cm,
classes=class_names,
title='LGBM Confusion matrix [averaged/folds]')
 plt.show()<compute_train_metric>
test.isnull().sum()
train, test, test_id, features, target = encoding(train, test, 0.3)
 model, cms, tprs, aucs, y_real, y_proba, mean_fpr, mean_tpr, df_ml = run_model(5, features, target, train)
 graph_metrics(cms, tprs, aucs, y_real, y_proba, mean_fpr, mean_tpr )<save_to_csv>
test.isnull().sum()
pd.DataFrame({'id': test_id, 'target': model.predict_proba(test)[:,1]} ).to_csv('submission.csv', index=False )<import_modules>
train['Age'].fillna(( train['Age'].mean()), inplace=True )
import numpy as np
import pandas as pd
from time import time
import pprint
import joblib
from catboost import CatBoostClassifier, Pool
from sklearn.model_selection import StratifiedKFold
from sklearn.metrics import roc_auc_score, average_precision_score
from sklearn.metrics import make_scorer<choose_model_class>
test['Age'].fillna(( test['Age'].mean()), inplace=True )
SEED = 42
FOLDS = 10
skf = StratifiedKFold(n_splits=FOLDS, shuffle=True, random_state=SEED )<load_from_csv>
test['Fare'].fillna(( test['Fare'].mean()), inplace=True )
X = pd.read_csv("/kaggle/input/cat-in-the-dat-ii/train.csv")
 Xt = pd.read_csv("/kaggle/input/cat-in-the-dat-ii/test.csv")
 y = X.target.values
id_train = X.id
id_test = Xt.id
X.drop(['id', 'target'], axis=1, inplace=True)
 Xt.drop(['id'], axis=1, inplace=True)
 binary_vars = [c for c in X.columns if 'bin_' in c]
nominal_vars = [c for c in X.columns if 'nom_' in c]
high_cardinality = [c for c in nominal_vars if len(X[c].unique())> 16]
low_cardinality = [c for c in nominal_vars if len(X[c].unique())<= 16]
ordinal_vars = [c for c in X.columns if 'ord_' in c]
time_vars = ['day', 'month']<feature_engineering>
train.dropna()
X['ord_5_1'] = X['ord_5'].apply(lambda x: x[0] if type(x)== str else np.nan)
 X['ord_5_2'] = X['ord_5'].apply(lambda x: x[1] if type(x)== str else np.nan)
 Xt['ord_5_1'] = Xt['ord_5'].apply(lambda x: x[0] if type(x)== str else np.nan)
 Xt['ord_5_2'] = Xt['ord_5'].apply(lambda x: x[1] if type(x)== str else np.nan)
 ordinal_vars += ['ord_5_1', 'ord_5_2']<normalization>
test.isnull().sum()
ordinals = {
'ord_1' : {
'Novice' : 0,
'Contributor' : 1,
'Expert' : 2,
'Master' : 3,
'Grandmaster' : 4
},
'ord_2' : {
'Freezing' : 0,
'Cold' : 1,
'Warm' : 2,
'Hot' : 3,
'Boiling Hot' : 4,
'Lava Hot' : 5
}
}
def return_order(X, Xt, var_name):
mode = X[var_name].mode() [0]
el = sorted(set(X[var_name].fillna(mode ).unique())|set(Xt[var_name].fillna(mode ).unique()))
return {v:e for e, v in enumerate(el)}
for mapped_var in ordinal_vars:
if mapped_var not in ordinals:
mapped_values = return_order(X, Xt, mapped_var)
 X[mapped_var + '_num'] = X[mapped_var].replace(mapped_values)
 Xt[mapped_var + '_num'] = Xt[mapped_var].replace(mapped_values)
 else:
X[mapped_var + '_num'] = X[mapped_var].replace(ordinals[mapped_var])
 Xt[mapped_var + '_num'] = Xt[mapped_var].replace(ordinals[mapped_var] )<categorify>
test.isnull().sum()
label_encoders = [LabelEncoder() for _ in range(X.shape[1])]
for col, column in enumerate(X.columns):
unique_values = pd.Series(X[column].append(Xt[column] ).unique())
 unique_values = unique_values[unique_values.notnull() ]
label_encoders[col].fit(unique_values)
 X.loc[X[column].notnull() , column] = label_encoders[col].transform(X.loc[X[column].notnull() , column])
 Xt.loc[Xt[column].notnull() , column] = label_encoders[col].transform(Xt.loc[Xt[column].notnull() , column] )<categorify>
Pclass=pd.get_dummies(train['Pclass'],drop_first=True)
 Pclass1=pd.get_dummies(test['Pclass'],drop_first=True)
 Sex=pd.get_dummies(train['Sex'],drop_first=True)
 Sex1=pd.get_dummies(test['Sex'],drop_first=True)
 Embarked=pd.get_dummies(train['Embarked'],drop_first=True)
 Embarked1=pd.get_dummies(test['Embarked'],drop_first=True )
X = X.fillna(-1)
 Xt = Xt.fillna(-1 )<categorify>
train=pd.concat([train,Pclass,Sex,Embarked],axis=1)
 test=pd.concat([test,Pclass1,Sex1,Embarked1],axis=1 )
def frequency_encoding(column, df, df_test=None):
frequencies = df[column].value_counts().reset_index()
df_values = df[[column]].merge(frequencies, how='left',
left_on=column, right_on='index' ).iloc[:,-1].values
if df_test is not None:
df_test_values = df_test[[column]].merge(frequencies, how='left',
left_on=column, right_on='index' ).fillna(1 ).iloc[:,-1].values
else:
df_test_values = None
return df_values, df_test_values
for column in X.columns:
train_values, test_values = frequency_encoding(column, X, Xt)
 X[column+'_counts'] = train_values
Xt[column+'_counts'] = test_values<categorify>
train.drop(['Sex','Embarked','Pclass'],axis=1,inplace=True)
 test.drop(['Sex','Embarked','Pclass'],axis=1,inplace=True )
cat_feat_to_encode = binary_vars + ordinal_vars + nominal_vars + time_vars
smoothing = 0.3
enc_x = np.zeros(X[cat_feat_to_encode].shape)
 for tr_idx, oof_idx in skf.split(X, y):
encoder = cat_encs.TargetEncoder(cols=cat_feat_to_encode, smoothing=smoothing)
 encoder.fit(X[cat_feat_to_encode].iloc[tr_idx], y[tr_idx])
 enc_x[oof_idx, :] = encoder.transform(X[cat_feat_to_encode].iloc[oof_idx], y[oof_idx])
 encoder.fit(X[cat_feat_to_encode], y)
 enc_xt = encoder.transform(Xt[cat_feat_to_encode] ).values
for idx, new_var in enumerate(cat_feat_to_encode):
new_var = new_var + '_enc'
X[new_var] = enc_x[:,idx]
Xt[new_var] = enc_xt[:, idx]<data_type_conversions>
from sklearn.model_selection import train_test_split
X = X.astype(np.float32)
 Xt = Xt.astype(np.float32)
 cat_features = nominal_vars + ordinal_vars
X[cat_features] = X[cat_features].astype(np.int64)
 Xt[cat_features] = Xt[cat_features].astype(np.int64 )<choose_model_class>
from sklearn.model_selection import train_test_split
best_params = {'bagging_temperature': 0.8,
'depth': 5,
'iterations': 1000,
'l2_leaf_reg': 30,
'learning_rate': 0.05,
'random_strength': 0.8}<split>
y=train['Survived']
roc_auc = list()
average_precision = list()
oof = np.zeros(len(X))
cv_test_preds = np.zeros(len(Xt))
best_iteration = list()
for train_idx, test_idx in skf.split(X, y):
X_train, y_train = X.iloc[train_idx, :], y[train_idx]
X_test, y_test = X.iloc[test_idx, :], y[test_idx]
train = Pool(data=X_train,
label=y_train,
feature_names=list(X_train.columns),
cat_features=cat_features)
 val = Pool(data=X_test,
label=y_test,
feature_names=list(X_test.columns),
cat_features=cat_features)
 catb = CatBoostClassifier(**best_params,
loss_function='Logloss',
eval_metric = 'AUC',
nan_mode='Min',
thread_count=2,
verbose = False)
 catb.fit(train,
verbose_eval=100,
early_stopping_rounds=50,
eval_set=val,
use_best_model=True,
plot=False)
 best_iteration.append(catb.best_iteration_)
 preds = catb.predict_proba(X_test)
 oof[test_idx] = preds[:,1]
Xt_pool = Pool(data=Xt[list(X_train.columns)],
feature_names=list(X_train.columns),
cat_features=cat_features)
 cv_test_preds += catb.predict_proba(Xt_pool)[:,1] / FOLDS
roc_auc.append(roc_auc_score(y_true=y_test, y_score=preds[:,1]))
average_precision.append(average_precision_score(y_true=y_test, y_score=preds[:,1]))<save_to_csv>
X=train.drop('Survived',axis=1 )
oof = pd.DataFrame({'id':id_train, 'catboost_oof': oof})
 oof.to_csv("oof.csv", index=False)
 cv_submission = pd.read_csv("/kaggle/input/cat-in-the-dat-ii/sample_submission.csv")
 cv_submission.target = cv_test_preds
cv_submission.to_csv("./catboost_cv_submission.csv", index=False )<compute_test_metric>
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=2 )
print("Average cv roc auc score %0.3f ± %0.3f" %(np.mean(roc_auc), np.std(roc_auc)))
 print("Average cv roc average precision %0.3f ± %0.3f" %(np.mean(average_precision), np.std(average_precision)))
 print("Roc auc score OOF %0.3f" % roc_auc_score(y_true=y, y_score=oof.catboost_oof))
print("Average precision OOF %0.3f" % average_precision_score(y_true=y, y_score=oof.catboost_oof))<choose_model_class>
logmodel = LogisticRegression()
logmodel.fit(X_train,y_train )
catb = CatBoostClassifier(**best_params,
loss_function='Logloss',
eval_metric = 'AUC',
nan_mode='Min',
thread_count=2,
verbose = False)
 train = Pool(data=X,
label=y,
feature_names=list(X_train.columns),
cat_features=cat_features)
 catb.fit(train,
verbose_eval=100,
plot=False)
 Xt_pool = Pool(data=Xt[list(X_train.columns)],
feature_names=list(X_train.columns),
cat_features=cat_features)
 submission = pd.read_csv("/kaggle/input/cat-in-the-dat-ii/sample_submission.csv")
 submission.target = catb.predict_proba(Xt_pool)[:,1]
submission.to_csv("./submission.csv", index=False )<import_modules>
predictions = logmodel.predict(X_test )
import numpy as np
import pandas as pd
from time import time
import pprint
import joblib
from sklearn.model_selection import StratifiedKFold
from sklearn.metrics import roc_auc_score, average_precision_score
from sklearn.metrics import make_scorer
from sklearn.preprocessing import LabelEncoder
from sklearn.preprocessing import OneHotEncoder
from sklearn.preprocessing import RobustScaler, StandardScaler
import matplotlib.pyplot as plt
import seaborn as sns<import_modules>
from sklearn.metrics import classification_report
import tensorflow as tf
from keras import backend as K
from keras.models import Sequential
from keras.layers import Dense
from keras.optimizers import Adam, Nadam
from keras.layers import Input, Embedding, Reshape, GlobalAveragePooling1D
from keras.layers import Flatten, concatenate, Concatenate, Lambda, Dropout, SpatialDropout1D
from keras.layers import Reshape, MaxPooling1D,BatchNormalization, AveragePooling1D, Conv1D
from keras.layers import Activation, LeakyReLU
from keras.optimizers import SGD, Adam, Nadam
from keras.models import Model, load_model
from keras.callbacks import EarlyStopping, ModelCheckpoint, ReduceLROnPlateau
from keras.regularizers import l2, l1_l2
from keras.losses import binary_crossentropy
from keras.utils import get_custom_objects
from keras.layers import Activation, LeakyReLU
from keras.models import load_model<load_from_csv>
classification_report(y_test,predictions )
X = pd.read_csv("/kaggle/input/cat-in-the-dat-ii/train.csv")
 Xt = pd.read_csv("/kaggle/input/cat-in-the-dat-ii/test.csv" )<prepare_x_and_y>
from sklearn.metrics import confusion_matrix
y = X.target.values
id_train = X.id
id_test = Xt.id
X.drop(['id', 'target'], axis=1, inplace=True)
 Xt.drop(['id'], axis=1, inplace=True )<define_variables>
confusion_matrix(y_test,predictions )
binary_vars = [c for c in X.columns if 'bin_' in c]
nominal_vars = [c for c in X.columns if 'nom_' in c]
high_cardinality = [c for c in nominal_vars if len(X[c].unique())> 16]
low_cardinality = [c for c in nominal_vars if len(X[c].unique())<= 16]
ordinal_vars = [c for c in X.columns if 'ord_' in c]
time_vars = ['day', 'month']<feature_engineering>
( 145+67)/(41+67+15+145 )
X['ord_5_1'] = X['ord_5'].apply(lambda x: x[0] if type(x)== str else np.nan)
 X['ord_5_2'] = X['ord_5'].apply(lambda x: x[1] if type(x)== str else np.nan)
 Xt['ord_5_1'] = Xt['ord_5'].apply(lambda x: x[0] if type(x)== str else np.nan)
 Xt['ord_5_2'] = Xt['ord_5'].apply(lambda x: x[1] if type(x)== str else np.nan)
 ordinal_vars += ['ord_5_1', 'ord_5_2']<normalization>
gbk = GradientBoostingClassifier()
gbk.fit(X_train, y_train)
 y_pred = gbk.predict(X_test)
 acc_gbk = round(accuracy_score(y_pred, y_test)* 100, 2)
 print(acc_gbk )
ordinals = {
'ord_1' : {
'Novice' : 0,
'Contributor' : 1,
'Expert' : 2,
'Master' : 3,
'Grandmaster' : 4
},
'ord_2' : {
'Freezing' : 0,
'Cold' : 1,
'Warm' : 2,
'Hot' : 3,
'Boiling Hot' : 4,
'Lava Hot' : 5
}
}
def return_order(X, Xt, var_name):
mode = X[var_name].mode() [0]
el = sorted(set(X[var_name].fillna(mode ).unique())|set(Xt[var_name].fillna(mode ).unique()))
return {v:e for e, v in enumerate(el)}
for mapped_var in ordinal_vars:
if mapped_var not in ordinals:
mapped_values = return_order(X, Xt, mapped_var)
 X[mapped_var].replace(mapped_values, inplace=True)
 Xt[mapped_var].replace(mapped_values, inplace=True)
 else:
X[mapped_var].replace(ordinals[mapped_var], inplace=True)
 Xt[mapped_var].replace(ordinals[mapped_var], inplace=True )<categorify>
test.isnull().sum()
X_cat, Xt_cat = list() , list()
categorical_counts = dict()
for hc in binary_vars+nominal_vars+ordinal_vars+time_vars:
levels = set(X[hc].astype(str ).fillna("NAN" ).unique())|set(Xt[hc].astype(str ).fillna("NAN" ).unique())
 levels = np.array(list(levels))
categorical_counts[hc] = len(levels)
 le = LabelEncoder()
le.fit(np.ravel(levels.reshape(-1,1)))
 X_cat.append(le.transform(X[hc].astype(str ).fillna("NAN" ).values))
Xt_cat.append(le.transform(Xt[hc].astype(str ).fillna("NAN" ).values))
print("Countings for high cardinality variables:")
 print(categorical_counts )<categorify>
test.isnull().sum()
<categorify><EOS> 
predictions1 = gbk.predict(test)
 sample_sub['Survived']= predictions1
sample_sub.to_csv("submit.csv", index=False)
 sample_sub.head()