Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<import_modules>
%matplotlib inline
titanic_data = pd.read_csv('.. /input/train.csv')
 test_data = pd.read_csv('.. /input/test.csv')
 warnings.filterwarnings(action='ignore', category=DeprecationWarning)
 warnings.filterwarnings(action='ignore', category=FutureWarning)
 titanic_data.info()
titanic_data.describe()
titanic_data.columns
titanic_data.shape
import numpy as np
import os
import cv2
import glob
import tensorflow as tf
import keras
from keras.models import load_model, save_model
from keras.layers import Input, GlobalAveragePooling2D, GlobalMaxPooling2D
import keras.backend as K
from keras.models import Model, load_model
from keras.applications import VGG16
from keras.applications.vgg16 import preprocess_input<train_on_grid>
total = titanic_data.isnull().sum().sort_values(ascending=False)
 percent =(titanic_data.isnull().sum() /titanic_data.isnull().count() ).sort_values(ascending=False)
 missing_data = pd.concat([total, percent], axis=1, keys=['Total', 'Percent'])
 missing_data.head()
files = glob.glob(".. /input/landmark-retrieval-2020/train/a/b/c/*.jpg")
 for i in range(10):
im = cv2.imread(files[i])
 print(im.shape )<choose_model_class>
total = test_data.isnull().sum().sort_values(ascending=False)
 percent =(test_data.isnull().sum() /test_data.isnull().count() ).sort_values(ascending=False)
 missing_data = pd.concat([total, percent], axis=1, keys=['Total', 'Percent'])
 missing_data.head()
vgg = VGG16(input_shape=(224,224,3), weights=None, include_top=False)
 vgg.load_weights(".. /input/keras-pretrained-models/vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5")
 input_image = Input(( 224,224,3))
x = vgg(input_image)
 output = GlobalMaxPooling2D()(x)
 model = Model(inputs=[input_image], outputs=[output])
 model.summary()<normalization>
data["FamilySize"] = data['Parch'] + data['SibSp'] + 1
sns.barplot(x="FamilySize", y="Survived", data=data)
 data["IsAlone"] = 0
data["BigFamily"] = 0
data.loc[data['FamilySize'] == 1, 'IsAlone'] = 1
data.loc[data['FamilySize'] >= 5, 'BigFamily'] = 1
class MyModel(tf.keras.Model):
def __init__(self):
super(MyModel, self ).__init__()
self.model = model
@tf.function(input_signature=[
tf.TensorSpec(shape=[None, None, 3], dtype=tf.uint8, name='input_image')
 ])
 def call(self, input_image):
output_tensors = {}
im = tf.image.resize(input_image,(224,224))
im = preprocess_input(im)
 extracted_features = self.model(tf.convert_to_tensor([im], dtype=tf.uint8)) [0]
output_tensors['global_descriptor'] = tf.identity(extracted_features, name='global_descriptor')
 return output_tensors<load_pretrained>
ticket = pd.DataFrame(data['Ticket'].sort_values())
 ticket['SharedTicket'] = 0
ticket['SharedTicket'] =(ticket['Ticket'].eq(ticket['Ticket'].shift(1)) | ticket['Ticket'].eq(ticket['Ticket'].shift(-1)))
 ticket['SharedTicket'] = ticket['SharedTicket'].astype(int)
 data['SharedTicket'] = 0
data['SharedTicket'] = ticket['SharedTicket']
data.loc[data['SharedTicket']==1, 'Ticket'].sort_values()
data.loc[data['SharedTicket']==1, ['Ticket', 'Name']].sort_values("Ticket" ).head(20 )
m = MyModel()
served_function = m.call
tf.saved_model.save(
 m, export_dir="./my_model", signatures={'serving_default': served_function} )<load_pretrained>
def cabin_embarked(titanic_data):
df = titanic_data
df[['Cabin']] = df[['Cabin']].fillna(value="Unknown")
 df[['Embarked']] = df[['Embarked']].fillna(value="Unknown")
 titanic_data = df
return titanic_data
titanic_data = cabin_embarked(titanic_data)
 test_data = cabin_embarked(test_data)
 def shared_ticket(titanic_data):
ticket = pd.DataFrame(titanic_data['Ticket'].sort_values())
 ticket['SharedTicket'] = 0
ticket['SharedTicket'] =(ticket['Ticket'].eq(ticket['Ticket'].shift(1)) | ticket['Ticket'].eq(ticket['Ticket'].shift(-1)))
 ticket['SharedTicket'] = ticket['SharedTicket'].astype(int)
 titanic_data['SharedTicket'] = 0
titanic_data['SharedTicket'] = ticket['SharedTicket']
return titanic_data
titanic_data = shared_ticket(titanic_data)
 test_data = shared_ticket(test_data )
with ZipFile('submission.zip','w')as zip:
zip.write('./my_model/saved_model.pb', arcname='saved_model.pb')
 zip.write('./my_model/variables/variables.data-00000-of-00002', arcname='variables/variables.data-00000-of-00002')
 zip.write('./my_model/variables/variables.data-00001-of-00002', arcname='variables/variables.data-00001-of-00002')
 zip.write('./my_model/variables/variables.index', arcname='variables/variables.index' )<install_modules>
def cabin_letters(titanic_data):
df = titanic_data
cabins = df.loc[df['Cabin'] != 'Unknown', 'Cabin'].str.split()
df['Cabin_numbers'] = cabins.transform(lambda x: len(x)).astype(int)
 df['Cabin_numbers'] = df['Cabin_numbers'].fillna(value=1)
 titanic_data['Cabin_letter'] = titanic_data['Cabin'].str[0]
titanic_data = df
return titanic_data
titanic_data = cabin_letters(titanic_data)
 test_data = cabin_letters(test_data )
!pip install -U vega_datasets notebook vega<set_options>
def fill_unknown_cabins(titanic_data):
df = titanic_data
df.loc[(df['Pclass']==1)&(df['Cabin_letter']=='U'), 'Cabin_letter'] = 'B'
df.loc[(df['Pclass']==2)&(df['Cabin_letter']=='U'), 'Cabin_letter'] = 'D'
df.loc[(df['Pclass']==3)&(df['Cabin_letter']=='U'), 'Cabin_letter'] = 'G'
df.Cabin_letter.value_counts()
df.loc[df['Cabin_letter']=='T']
df.loc[df['Cabin_letter']=='T', ['Cabin_letter','Cabin']] = 'B'
titanic_data = df
return titanic_data
titanic_data = fill_unknown_cabins(titanic_data)
 test_data = fill_unknown_cabins(test_data)
 def family_size_alone(titanic_data):
titanic_data['FamilySize'] = titanic_data['SibSp'] + titanic_data['Parch'] + 1
titanic_data['IsAlone'] = 0
titanic_data['BigFamily'] = 0
titanic_data['Couple'] = 0
titanic_data.loc[titanic_data['FamilySize'] == 2, 'Couple'] = 1
titanic_data.loc[titanic_data['FamilySize'] == 1, 'IsAlone'] = 1
titanic_data.loc[titanic_data['FamilySize'] >= 5, 'BigFamily'] = 1
titanic_data = titanic_data.drop(['Parch', 'SibSp', 'FamilySize'], axis=1)
 return titanic_data
titanic_data = family_size_alone(titanic_data)
 test_data = family_size_alone(test_data )
%matplotlib inline
pd.options.display.precision = 15
warnings.filterwarnings("ignore")
 %matplotlib inline
alt.renderers.enable('notebook')
 %env JOBLIB_TEMP_FOLDER=/tmp<import_modules>
def Age_Fare_Band(titanic_data):
titanic_data['AgeBand'] = pd.cut(titanic_data['Age'], 5)
 titanic_data.loc[ titanic_data['Age'] <= 16, 'Age'] = 0
titanic_data.loc[(titanic_data['Age'] > 16)&(titanic_data['Age'] <= 32), 'Age'] = 1
titanic_data.loc[(titanic_data['Age'] > 32)&(titanic_data['Age'] <= 48), 'Age'] = 2
titanic_data.loc[(titanic_data['Age'] > 48)&(titanic_data['Age'] <= 64), 'Age'] = 3
titanic_data.loc[ titanic_data['Age'] > 64, 'Age'] = 4
titanic_data['Fare'].fillna(titanic_data['Fare'].dropna().median() , inplace=True)
 titanic_data['FareBand'] = pd.qcut(titanic_data['Fare'], 4)
 titanic_data.loc[ titanic_data['Fare'] <= 7.91, 'Fare'] = 0
titanic_data.loc[(titanic_data['Fare'] > 7.91)&(titanic_data['Fare'] <= 14.454), 'Fare'] = 1
titanic_data.loc[(titanic_data['Fare'] > 14.454)&(titanic_data['Fare'] <= 31), 'Fare'] = 2
titanic_data.loc[ titanic_data['Fare'] > 31, 'Fare'] = 3
titanic_data['Fare'] = titanic_data['Fare'].astype(int)
 titanic_data = titanic_data.drop(['AgeBand'], axis=1)
 titanic_data = titanic_data.drop(['FareBand'], axis=1)
 return titanic_data
titanic_data = Age_Fare_Band(titanic_data)
 test_data = Age_Fare_Band(test_data )
def prepare_altair() :

 vega_url = 'https://cdn.jsdelivr.net/npm/vega@' + v5.SCHEMA_VERSION
vega_lib_url = 'https://cdn.jsdelivr.net/npm/vega-lib'
vega_lite_url = 'https://cdn.jsdelivr.net/npm/vega-lite@' + alt.SCHEMA_VERSION
vega_embed_url = 'https://cdn.jsdelivr.net/npm/vega-embed@3'
noext = "?noext"
paths = {
'vega': vega_url + noext,
'vega-lib': vega_lib_url + noext,
'vega-lite': vega_lite_url + noext,
'vega-embed': vega_embed_url + noext
}
workaround = f
return workaround
def add_autoincrement(render_func):
cache = {}
def wrapped(chart, id="vega-chart", autoincrement=True):
if autoincrement:
if id in cache:
counter = 1 + cache[id]
cache[id] = counter
else:
cache[id] = 0
actual_id = id if cache[id] == 0 else id + '-' + str(cache[id])
 else:
if id not in cache:
cache[id] = 0
actual_id = id
return render_func(chart, id=actual_id)
 return wrapped
@add_autoincrement
def render(chart, id="vega-chart"):

 chart_str =
return HTML(
 chart_str.format(
 id=id,
chart=json.dumps(chart)if isinstance(chart, dict)else chart.to_json(indent=None)
)
)
 def reduce_mem_usage(df, verbose=True):
numerics = ['int16', 'int32', 'int64', 'float16', 'float32', 'float64']
start_mem = df.memory_usage(deep=True ).sum() / 1024**2
for col in df.columns:
col_type = df[col].dtypes
if col_type in numerics:
c_min = df[col].min()
c_max = df[col].max()
if str(col_type)[:3] == 'int':
if c_min > np.iinfo(np.int8 ).min and c_max < np.iinfo(np.int8 ).max:
df[col] = df[col].astype(np.int8)
 elif c_min > np.iinfo(np.int16 ).min and c_max < np.iinfo(np.int16 ).max:
df[col] = df[col].astype(np.int16)
 elif c_min > np.iinfo(np.int32 ).min and c_max < np.iinfo(np.int32 ).max:
df[col] = df[col].astype(np.int32)
 elif c_min > np.iinfo(np.int64 ).min and c_max < np.iinfo(np.int64 ).max:
df[col] = df[col].astype(np.int64)
 else:
c_prec = df[col].apply(lambda x: np.finfo(x ).precision ).max()
if c_min > np.finfo(np.float32 ).min and c_max < np.finfo(np.float32 ).max and c_prec == np.finfo(np.float32 ).precision:
df[col] = df[col].astype(np.float32)
 else:
df[col] = df[col].astype(np.float64)
 end_mem = df.memory_usage(deep=True ).sum() / 1024**2
if verbose: print('Mem.usage decreased to {:5.2f} Mb({:.1f}% reduction)'.format(end_mem, 100 *(start_mem - end_mem)/ start_mem))
return df
@jit
def fast_auc(y_true, y_prob):

 y_true = np.asarray(y_true)
 y_true = y_true[np.argsort(y_prob)]
nfalse = 0
auc = 0
n = len(y_true)
 for i in range(n):
y_i = y_true[i]
nfalse +=(1 - y_i)
 auc += y_i * nfalse
auc /=(nfalse *(n - nfalse))
return auc
def eval_auc(y_true, y_pred):

 return 'auc', fast_auc(y_true, y_pred), True
def group_mean_log_mae(y_true, y_pred, types, floor=1e-9):

 maes =(y_true-y_pred ).abs().groupby(types ).mean()
return np.log(maes.map(lambda x: max(x, floor)) ).mean()
def train_model_regression(X, X_test, y, params, folds=None, model_type='lgb', eval_metric='mae', columns=None, plot_feature_importance=False, model=None,
verbose=10000, early_stopping_rounds=200, n_estimators=50000, splits=None, n_folds=3):

 columns = X.columns if columns is None else columns
X_test = X_test[columns]
splits = folds.split(X)if splits is None else splits
n_splits = folds.n_splits if splits is None else n_folds
metrics_dict = {'mae': {'lgb_metric_name': 'mae',
'catboost_metric_name': 'MAE',
'sklearn_scoring_function': metrics.mean_absolute_error},
'group_mae': {'lgb_metric_name': 'mae',
'catboost_metric_name': 'MAE',
'scoring_function': group_mean_log_mae},
'mse': {'lgb_metric_name': 'mse',
'catboost_metric_name': 'MSE',
'sklearn_scoring_function': metrics.mean_squared_error}
}
result_dict = {}
oof = np.zeros(len(X))
prediction = np.zeros(len(X_test))
scores = []
feature_importance = pd.DataFrame()
for fold_n,(train_index, valid_index)in enumerate(splits):
if verbose:
print(f'Fold {fold_n + 1} started at {time.ctime() }')
 if type(X)== np.ndarray:
X_train, X_valid = X[columns][train_index], X[columns][valid_index]
y_train, y_valid = y[train_index], y[valid_index]
else:
X_train, X_valid = X[columns].iloc[train_index], X[columns].iloc[valid_index]
y_train, y_valid = y.iloc[train_index], y.iloc[valid_index]
if model_type == 'lgb':
model = lgb.LGBMRegressor(**params, n_estimators = n_estimators, n_jobs = -1)
 model.fit(X_train, y_train,
eval_set=[(X_train, y_train),(X_valid, y_valid)], eval_metric=metrics_dict[eval_metric]['lgb_metric_name'],
verbose=verbose, early_stopping_rounds=early_stopping_rounds)
 y_pred_valid = model.predict(X_valid)
 y_pred = model.predict(X_test, num_iteration=model.best_iteration_)
 if model_type == 'xgb':
train_data = xgb.DMatrix(data=X_train, label=y_train, feature_names=X.columns)
 valid_data = xgb.DMatrix(data=X_valid, label=y_valid, feature_names=X.columns)
 watchlist = [(train_data, 'train'),(valid_data, 'valid_data')]
model = xgb.train(dtrain=train_data, num_boost_round=20000, evals=watchlist, early_stopping_rounds=200, verbose_eval=verbose, params=params)
 y_pred_valid = model.predict(xgb.DMatrix(X_valid, feature_names=X.columns), ntree_limit=model.best_ntree_limit)
 y_pred = model.predict(xgb.DMatrix(X_test, feature_names=X.columns), ntree_limit=model.best_ntree_limit)
 if model_type == 'sklearn':
model = model
model.fit(X_train, y_train)
 y_pred_valid = model.predict(X_valid ).reshape(-1,)
 score = metrics_dict[eval_metric]['sklearn_scoring_function'](y_valid, y_pred_valid)
 print(f'Fold {fold_n}.{eval_metric}: {score:.4f}.')
 print('')
 y_pred = model.predict(X_test ).reshape(-1,)
 if model_type == 'cat':
model = CatBoostRegressor(iterations=20000, eval_metric=metrics_dict[eval_metric]['catboost_metric_name'], **params,
loss_function=metrics_dict[eval_metric]['catboost_metric_name'])
 model.fit(X_train, y_train, eval_set=(X_valid, y_valid), cat_features=[], use_best_model=True, verbose=False)
 y_pred_valid = model.predict(X_valid)
 y_pred = model.predict(X_test)
 oof[valid_index] = y_pred_valid.reshape(-1,)
 if eval_metric != 'group_mae':
scores.append(metrics_dict[eval_metric]['sklearn_scoring_function'](y_valid, y_pred_valid))
else:
scores.append(metrics_dict[eval_metric]['scoring_function'](y_valid, y_pred_valid, X_valid['type']))
prediction += y_pred
if model_type == 'lgb' and plot_feature_importance:
fold_importance = pd.DataFrame()
fold_importance["feature"] = columns
fold_importance["importance"] = model.feature_importances_
fold_importance["fold"] = fold_n + 1
feature_importance = pd.concat([feature_importance, fold_importance], axis=0)
 prediction /= n_splits
print('CV mean score: {0:.4f}, std: {1:.4f}.'.format(np.mean(scores), np.std(scores)))
 result_dict['oof'] = oof
result_dict['prediction'] = prediction
result_dict['scores'] = scores
if model_type == 'lgb':
if plot_feature_importance:
feature_importance["importance"] /= n_splits
cols = feature_importance[["feature", "importance"]].groupby("feature" ).mean().sort_values(
 by="importance", ascending=False)[:50].index
best_features = feature_importance.loc[feature_importance.feature.isin(cols)]
plt.figure(figsize=(16, 12)) ;
sns.barplot(x="importance", y="feature", data=best_features.sort_values(by="importance", ascending=False)) ;
plt.title('LGB Features(avg over folds)');
result_dict['feature_importance'] = feature_importance
return result_dict
def train_model_classification(X, X_test, y, params, folds, model_type='lgb', eval_metric='auc', columns=None, plot_feature_importance=False, model=None,
verbose=10000, early_stopping_rounds=200, n_estimators=50000, splits=None, n_folds=3, averaging='usual', n_jobs=-1):

 columns = X.columns if columns is None else columns
n_splits = folds.n_splits if splits is None else n_folds
X_test = X_test[columns]
metrics_dict = {'auc': {'lgb_metric_name': eval_auc,
'catboost_metric_name': 'AUC',
'sklearn_scoring_function': metrics.roc_auc_score},
}
result_dict = {}
if averaging == 'usual':
oof = np.zeros(( len(X), 1))
prediction = np.zeros(( len(X_test), 1))
elif averaging == 'rank':
oof = np.zeros(( len(X), 1))
prediction = np.zeros(( len(X_test), 1))
scores = []
feature_importance = pd.DataFrame()
for fold_n,(train_index, valid_index)in enumerate(folds.split(X)) :
print(f'Fold {fold_n + 1} started at {time.ctime() }')
 if type(X)== np.ndarray:
X_train, X_valid = X[columns][train_index], X[columns][valid_index]
y_train, y_valid = y[train_index], y[valid_index]
else:
X_train, X_valid = X[columns].iloc[train_index], X[columns].iloc[valid_index]
y_train, y_valid = y.iloc[train_index], y.iloc[valid_index]
if model_type == 'lgb':
model = lgb.LGBMClassifier(**params, n_estimators=n_estimators, n_jobs = n_jobs)
 model.fit(X_train, y_train,
eval_set=[(X_train, y_train),(X_valid, y_valid)], eval_metric=metrics_dict[eval_metric]['lgb_metric_name'],
verbose=verbose, early_stopping_rounds=early_stopping_rounds)
 y_pred_valid = model.predict_proba(X_valid)[:, 1]
y_pred = model.predict_proba(X_test, num_iteration=model.best_iteration_)[:, 1]
if model_type == 'xgb':
train_data = xgb.DMatrix(data=X_train, label=y_train, feature_names=X.columns)
 valid_data = xgb.DMatrix(data=X_valid, label=y_valid, feature_names=X.columns)
 watchlist = [(train_data, 'train'),(valid_data, 'valid_data')]
model = xgb.train(dtrain=train_data, num_boost_round=n_estimators, evals=watchlist, early_stopping_rounds=early_stopping_rounds, verbose_eval=verbose, params=params)
 y_pred_valid = model.predict(xgb.DMatrix(X_valid, feature_names=X.columns), ntree_limit=model.best_ntree_limit)
 y_pred = model.predict(xgb.DMatrix(X_test, feature_names=X.columns), ntree_limit=model.best_ntree_limit)
 if model_type == 'sklearn':
model = model
model.fit(X_train, y_train)
 y_pred_valid = model.predict(X_valid ).reshape(-1,)
 score = metrics_dict[eval_metric]['sklearn_scoring_function'](y_valid, y_pred_valid)
 print(f'Fold {fold_n}.{eval_metric}: {score:.4f}.')
 print('')
 y_pred = model.predict_proba(X_test)
 if model_type == 'cat':
model = CatBoostClassifier(iterations=n_estimators, eval_metric=metrics_dict[eval_metric]['catboost_metric_name'], **params,
loss_function=Logloss)
 model.fit(X_train, y_train, eval_set=(X_valid, y_valid), cat_features=[], use_best_model=True, verbose=False)
 y_pred_valid = model.predict(X_valid)
 y_pred = model.predict(X_test)
 if averaging == 'usual':
oof[valid_index] = y_pred_valid.reshape(-1, 1)
 scores.append(metrics_dict[eval_metric]['sklearn_scoring_function'](y_valid, y_pred_valid))
prediction += y_pred.reshape(-1, 1)
 elif averaging == 'rank':
oof[valid_index] = y_pred_valid.reshape(-1, 1)
 scores.append(metrics_dict[eval_metric]['sklearn_scoring_function'](y_valid, y_pred_valid))
prediction += pd.Series(y_pred ).rank().values.reshape(-1, 1)
 if model_type == 'lgb' and plot_feature_importance:
fold_importance = pd.DataFrame()
fold_importance["feature"] = columns
fold_importance["importance"] = model.feature_importances_
fold_importance["fold"] = fold_n + 1
feature_importance = pd.concat([feature_importance, fold_importance], axis=0)
 prediction /= n_splits
print('CV mean score: {0:.4f}, std: {1:.4f}.'.format(np.mean(scores), np.std(scores)))
 result_dict['oof'] = oof
result_dict['prediction'] = prediction
result_dict['scores'] = scores
if model_type == 'lgb':
if plot_feature_importance:
feature_importance["importance"] /= n_splits
cols = feature_importance[["feature", "importance"]].groupby("feature" ).mean().sort_values(
 by="importance", ascending=False)[:50].index
best_features = feature_importance.loc[feature_importance.feature.isin(cols)]
plt.figure(figsize=(16, 12)) ;
sns.barplot(x="importance", y="feature", data=best_features.sort_values(by="importance", ascending=False)) ;
plt.title('LGB Features(avg over folds)');
result_dict['feature_importance'] = feature_importance
result_dict['top_columns'] = cols
return result_dict
workaround = prepare_altair()
HTML("".join((
"<script>",
workaround,
"</script>", 
)) )<load_from_csv>
titanic_data.loc[titanic_data['Embarked'] == 'Unknown']
titanic_data['Embarked'] = titanic_data['Embarked'].replace('Unknown', 'U')
folder_path = '.. /input/'
train_identity = pd.read_csv(f'{folder_path}train_identity.csv')
 train_transaction = pd.read_csv(f'{folder_path}train_transaction.csv')
 test_identity = pd.read_csv(f'{folder_path}test_identity.csv')
 test_transaction = pd.read_csv(f'{folder_path}test_transaction.csv')
 sub = pd.read_csv(f'{folder_path}sample_submission.csv')
 train = pd.merge(train_transaction, train_identity, on='TransactionID', how='left')
 test = pd.merge(test_transaction, test_identity, on='TransactionID', how='left' )<drop_column>
def mapping(titanic_data):
titanic_data['Sex'] = titanic_data['Sex'].map({"female": 1, "male": 0} ).astype(int)
 return titanic_data
titanic_data = mapping(titanic_data)
 test_data = mapping(test_data)
 def encode_features(titanic_data, test_data):
features = ['Cabin_letter','Embarked']
df_combined = pd.concat([titanic_data, test_data])
 for feature in features:
le = preprocessing.LabelEncoder()
le = le.fit(df_combined[feature])
 titanic_data[feature] = le.transform(titanic_data[feature])
 test_data[feature] = le.transform(test_data[feature])
 return titanic_data, test_data
titanic_data, test_data = encode_features(titanic_data, test_data )
del train_identity, train_transaction, test_identity, test_transaction<count_missing_values>
titanic_data.head()
test_data.head()
features = ['Pclass', 'Sex', 'Age', 'Fare', 'Embarked', 'Title', 'Cabin_letter', 'IsAlone']
X = titanic_data [features]
y = titanic_data['Survived']
print(f'There are {train.isnull().any().sum() } columns in train dataset with missing values.' )<count_unique_values>
train_X, test_X, train_y, test_y = train_test_split(X, y, test_size=0.50, random_state=1)
 n_est = 1000
learn = 0.10
max_dp = 3
xgb_model = XGBClassifier(n_estimators=n_est, learning_rate=learn, max_depth=max_dp)
 xgb_model.fit(train_X, train_y)
 xgb_predictions = xgb_model.predict(test_X)
 print("Accuracy: {0}".format(accuracy_score(test_y, xgb_predictions)) )
one_value_cols = [col for col in train.columns if train[col].nunique() <= 1]
one_value_cols_test = [col for col in test.columns if test[col].nunique() <= 1]
one_value_cols == one_value_cols_test<count_unique_values>
xgb_final = make_pipeline(XGBClassifier(n_estimators=n_est,
learning_rate=learn,
xgbclassifier__early_stopping_rounds=5,
xgbclassifier__eval_set=[(X, y)]))
scores = cross_val_score(xgb_final, X, y, scoring='accuracy', cv=3)
 print('XGB Pipeline Cross-Validation Accuracy: %2f' %scores.mean())
 print(scores)
 xgb_final.fit(X, y);
<count_values><EOS> 
ids = test_data['PassengerId']
final_data = test_data [features]
predictions = xgb_final.predict(final_data);
output = pd.DataFrame({ 'PassengerId' : ids, 'Survived': predictions })
 output.to_csv('submission.csv', index=False)
 output.head()