Kaggle competition: Titanic - Machine Learning from Disaster
from cuml.linear_model import Ridge 
<load_from_csv>
warnings.filterwarnings('ignore' )
train = cudf.read_csv('.. /input/multi-cat-encodings/X_train_te.csv')
 test = cudf.read_csv('.. /input/multi-cat-encodings/X_test_te.csv')
 sample_submission = cudf.read_csv('.. /input/cat-in-the-dat-ii/sample_submission.csv' )<define_variables>
sns.set_style('whitegrid' )
train_oof = cp.zeros(( train.shape[0],))
test_preds = 0
train_oof.shape<compute_train_metric>
train_data = pd.read_csv('/kaggle/input/titanic/train.csv')
 test_data = pd.read_csv('/kaggle/input/titanic/test.csv' )
def auc_cp(y_true,y_pred):
y_true = y_true.astype('float32')
 ids = np.argsort(-y_pred)
 y_true = y_true[ids.values]
y_pred = y_pred[ids.values]
zero = 1 - y_true
acc_one = cp.cumsum(y_true)
 acc_zero = cp.cumsum(zero)
 sum_one = cp.sum(y_true)
 sum_zero = cp.sum(zero)
 tpr = acc_one/sum_one
fpr = acc_zero/sum_zero
return calculate_area(fpr,tpr)
 def calculate_area(fpr,tpr):
return cp.sum(( fpr[1:]-fpr[:-1])*(tpr[1:]+tpr[:-1])) /2<find_best_model_class>
train_data['Embarked'] = train_data['Embarked'].fillna('S')
 test_data['Embarked'] = test_data['Embarked'].fillna('S' )
%%time
n_splits = 5
kf = KFold(n_splits=n_splits, random_state=137)
 scores = []
for jj,(train_index, val_index)in enumerate(kf.split(train)) :
print("Fitting fold", jj+1)
 train_features = train.loc[train['fold_column'] != jj][features]
train_target = train.loc[train['fold_column'] != jj]['target'].values.astype(float)
 val_features = train.loc[train['fold_column'] == jj][features]
val_target = train.loc[train['fold_column'] == jj]['target'].values.astype(float)
 model = Ridge(alpha = 5)
 model.fit(train_features, train_target)
 val_pred = model.predict(val_features)
 train_oof[val_index] = val_pred
val_target = cp.asarray(val_target)
 score = auc_cp(val_target, val_pred)
 print("Fold AUC:", score)
 scores.append(cp.asnumpy(score))
test_preds += model.predict(test ).values/n_splits
del train_features, train_target, val_features, val_target
gc.collect()
print("Mean AUC:", np.mean(scores))<save_to_csv>
def fix_age(blob):
Age = blob[0]
Pclass = blob[1]
Sex = blob[2]
if pd.isnull(Age):
if Sex == 'male':
if Pclass == 1:
return 40
elif Pclass == 2:
return 30
else:
return 25
else:
if Pclass == 1:
return 35
elif Pclass == 2:
return 27
else:
return 22
else:
return Age
sample_submission['target'] = test_preds
sample_submission.to_csv('submission.csv', index=False )<save_model>
train_data['Age'] = train_data[['Age','Pclass','Sex']].apply(fix_age,axis=1)
 test_data['Age'] = test_data[['Age','Pclass','Sex']].apply(fix_age,axis=1 )
cp.save('test_preds', test_preds)
 cp.save('train_oof', train_oof )<import_modules>
train_data["Fare"] = train_data["Fare"].fillna(train_data["Fare"].median())
 test_data["Fare"] = test_data["Fare"].fillna(test_data["Fare"].median() )
import scipy
import pandas as pd
from sklearn.linear_model import LogisticRegression<load_from_csv>
train_data['Sex'] = train_data['Sex'].fillna('male')
 test_data['Sex'] = test_data['Sex'].fillna('male' )
D0 = pd.read_csv("/kaggle/input/cat-in-the-dat-ii/train.csv", index_col="id")
 D_test = pd.read_csv("/kaggle/input/cat-in-the-dat-ii/test.csv", index_col="id")
 y_train = D0["target"]
D = D0.drop(columns="target")
 test_ids = D_test.index
D_all = pd.concat([D, D_test])
 num_train = len(D)
 print(f"D_all.shape = {D_all.shape}" )<count_unique_values>
def add_family(blob):
temp = blob.split(' ')[0]
temp = temp[:len(temp)-1]
return temp
ord_maps = {
"ord_0": {val: i for i, val in enumerate([1, 2, 3])},
"ord_1": {
val: i
for i, val in enumerate(
 ["Novice", "Contributor", "Expert", "Master", "Grandmaster"] 
)
 },
"ord_2": {
val: i
for i, val in enumerate(
 ["Freezing", "Cold", "Warm", "Hot", "Boiling Hot", "Lava Hot"] 
)
 },
**{col: {val: i for i, val in enumerate(sorted(D_all[col].dropna().unique())) } for col in ["ord_3", "ord_4", "ord_5", "day", "month"]},
}<categorify>
train_data['FamilyName'] = train_data['Name'].apply(add_family )
oh_cols = D_all.columns.difference(ord_maps.keys() - {"day", "month"})
 print(f"OneHot encoding {len(oh_cols)} columns")
 one_hot = pd.get_dummies(
 D_all[oh_cols],
columns=oh_cols,
drop_first=True,
dummy_na=True,
sparse=True,
dtype="int8",
).sparse.to_coo()<categorify>
test_data['FamilyName'] = test_data['Name'].apply(add_family )
ord_cols = pd.concat([D_all[col].map(ord_map ).fillna(max(ord_map.values())//2 ).astype("float32")for col, ord_map in ord_maps.items() ], axis=1)
 ord_cols /= ord_cols.max()
ord_cols_sqr = 4*(ord_cols - 0.5)**2<concatenate>
train_data['FamilyName'].value_counts()
X = scipy.sparse.hstack([one_hot, ord_cols, ord_cols_sqr] ).tocsr()
print(f"X.shape = {X.shape}")
 X_train = X[:num_train]
X_test = X[num_train:]<save_to_csv>
def apply_name_suffix(blob):
temp = blob.split(' ')[1]
return temp
clf=LogisticRegression(C=0.05, solver="lbfgs", max_iter=5000)
 clf.fit(X_train, y_train)
 pred = clf.predict_proba(X_test)[:, 1]
pd.DataFrame({"id": test_ids, "target": pred} ).to_csv("submission.csv", index=False )<install_modules>
train_data['NameSuffix'] = train_data['Name'].apply(apply_name_suffix )
!pip install --no-warn-conflicts -q deepctr<import_modules>
test_data['NameSuffix'] = test_data['Name'].apply(apply_name_suffix )
warnings.simplefilter('ignore' )<load_from_csv>
def fix_name_suffix(blob):
temp = ['Mr.','Miss.','Mrs.','Master.','Dr.','Rev.']
if blob in temp:
return blob[0:len(blob)-1]
else:
return 'no_suffix'
train = pd.read_csv('.. /input/cat-in-the-dat-ii/train.csv')
 test = pd.read_csv('.. /input/cat-in-the-dat-ii/test.csv' )<feature_engineering>
train_data['NameSuffix'] = train_data['NameSuffix'].apply(fix_name_suffix)
 test_data['NameSuffix'] = test_data['NameSuffix'].apply(fix_name_suffix )
test['target'] = -1<concatenate>
del train_data['Name']
del test_data['Name']
data = pd.concat([train, test] ).reset_index(drop=True )<feature_engineering>
train_data.Ticket = [i[0] for i in train_data.Ticket.astype("str")]
test_data.Ticket = [i[0] for i in test_data.Ticket.astype("str")]
data['null'] = data.isna().sum(axis=1 )<feature_engineering>
train_data['Ticket'] = train_data['Ticket'].apply(lambda x: x if x in ['S','P','C','A','W','F','L'] else 'G' )
sparse_features = [feat for feat in train.columns if feat not in ['id','target']]
data[sparse_features] = data[sparse_features].fillna('-1', )<categorify>
test_data['Ticket'] = test_data['Ticket'].apply(lambda x: x if x in ['S','P','C','A','W','F','L'] else 'G' )
for feat in sparse_features:
lbe = LabelEncoder()
data[feat] = lbe.fit_transform(data[feat].fillna('-1' ).astype(str ).values )<prepare_x_and_y>
train_data.Cabin = [i[0] for i in train_data.Cabin.astype("str")]
test_data.Cabin = [i[0] for i in test_data.Cabin.astype("str")]
train = data[data.target != -1].reset_index(drop=True)
 test = data[data.target == -1].reset_index(drop=True )<count_unique_values>
def most_common(lst):
return max(set(lst), key=lst.count)
 def fix_cabin(blob):
temp = blob.tolist()
temp1 = []
for i in temp :
if i == 'n':
continue
else:
temp1.append(i)
 if temp1 == [] :
return 'Z'
else:
temp1 = most_common(temp1)
 return temp1
fixlen_feature_columns = [SparseFeat(feat, data[feat].nunique())for feat in sparse_features]
dnn_feature_columns = fixlen_feature_columns
linear_feature_columns = fixlen_feature_columns
feature_names = get_feature_names(linear_feature_columns + dnn_feature_columns )<compute_test_metric>
train_data['Cabin'] = train_data['Cabin'].groupby(train_data['FamilyName'] ).transform(fix_cabin)
 test_data['Cabin'] = test_data['Cabin'].groupby(test_data['FamilyName'] ).transform(fix_cabin )
def auc(y_true, y_pred):
def fallback_auc(y_true, y_pred):
try:
return roc_auc_score(y_true, y_pred)
 except:
return 0.5
return tf.py_function(fallback_auc,(y_true, y_pred), tf.double )<compute_train_metric>
del train_data['PassengerId']
def focal_loss(gamma=2., alpha=.25):
def focal_loss_fixed(y_true, y_pred):
pt_1 = tf.where(tf.equal(y_true, 1), y_pred, tf.ones_like(y_pred))
pt_0 = tf.where(tf.equal(y_true, 0), y_pred, tf.zeros_like(y_pred))
return -K.mean(alpha * K.pow(1.- pt_1, gamma)* K.log(K.epsilon() +pt_1)) -K.mean(( 1-alpha)* K.pow(pt_0, gamma)* K.log(1.- pt_0 + K.epsilon()))
return focal_loss_fixed
get_custom_objects().update({'focal_loss_fn': focal_loss() } )<feature_engineering>
from sklearn.pipeline import Pipeline , FeatureUnion
from sklearn.preprocessing import FunctionTransformer
from sklearn.feature_extraction import DictVectorizer
def custom_gelu(x):
return 0.5 * x *(1 + tf.tanh(tf.sqrt(2 / np.pi)*(x + 0.044715 * tf.pow(x, 3))))
get_custom_objects().update({'custom_gelu': Activation(custom_gelu)} )<choose_model_class>
list_of_obj = []
list_of_num = []
for i in train_data.columns :
if train_data[i].dtypes == 'object':
list_of_obj.append(i)
 else:
if i == 'Survived':
continue
else:
list_of_num.append(i )
class WarmUpLearningRateScheduler(tf.keras.callbacks.Callback):

 def __init__(self, warmup_batches, init_lr, verbose=0):

 super(WarmUpLearningRateScheduler, self ).__init__()
self.warmup_batches = warmup_batches
self.init_lr = init_lr
self.verbose = verbose
self.batch_count = 0
self.learning_rates = []
def on_batch_end(self, batch, logs=None):
self.batch_count = self.batch_count + 1
lr = K.get_value(self.model.optimizer.lr)
 self.learning_rates.append(lr)
 def on_batch_begin(self, batch, logs=None):
if self.batch_count <= self.warmup_batches:
lr = self.batch_count*self.init_lr/self.warmup_batches
K.set_value(self.model.optimizer.lr, lr)
 if self.verbose > 0:
print('
Batch %05d: WarmUpLearningRateScheduler setting learning '
'rate to %s.' %(self.batch_count + 1, lr))<choose_model_class>
def return_text(df):
return df[list_of_obj]
class CyclicLR(keras.callbacks.Callback):
def __init__(self, base_lr=0.001, max_lr=0.006, step_size=2000., mode='triangular',
gamma=1., scale_fn=None, scale_mode='cycle'):
super(CyclicLR, self ).__init__()
self.base_lr = base_lr
self.max_lr = max_lr
self.step_size = step_size
self.mode = mode
self.gamma = gamma
if scale_fn == None:
if self.mode == 'triangular':
self.scale_fn = lambda x: 1.
 self.scale_mode = 'cycle'
elif self.mode == 'triangular2':
self.scale_fn = lambda x: 1 /(2.**(x - 1))
self.scale_mode = 'cycle'
elif self.mode == 'exp_range':
self.scale_fn = lambda x: gamma **(x)
 self.scale_mode = 'iterations'
else:
self.scale_fn = scale_fn
self.scale_mode = scale_mode
self.clr_iterations = 0.
 self.trn_iterations = 0.
 self.history = {}
self._reset()
def _reset(self, new_base_lr=None, new_max_lr=None,
new_step_size=None):

 if new_base_lr != None:
self.base_lr = new_base_lr
if new_max_lr != None:
self.max_lr = new_max_lr
if new_step_size != None:
self.step_size = new_step_size
self.clr_iterations = 0.
 def clr(self):
cycle = np.floor(1 + self.clr_iterations /(2 * self.step_size))
x = np.abs(self.clr_iterations / self.step_size - 2 * cycle + 1)
 if self.scale_mode == 'cycle':
return self.base_lr +(self.max_lr - self.base_lr)* np.maximum(0,(1 - x)) * self.scale_fn(cycle)
 else:
return self.base_lr +(self.max_lr - self.base_lr)* np.maximum(0,(1 - x)) * self.scale_fn(
 self.clr_iterations)
 def on_train_begin(self, logs={}):
logs = logs or {}
if self.clr_iterations == 0:
K.set_value(self.model.optimizer.lr, self.base_lr)
 else:
K.set_value(self.model.optimizer.lr, self.clr())
 def on_batch_end(self, epoch, logs=None):
logs = logs or {}
self.trn_iterations += 1
self.clr_iterations += 1
K.set_value(self.model.optimizer.lr, self.clr())
<define_variables>
get_text = FunctionTransformer(func=return_text,validate=False )
target = ['target']
N_Splits = 50
Verbose = 0
Epochs = 10
SEED = 2020
Batch_S_T = 128
Batch_S_P = 512<prepare_x_and_y>
def return_num(df):
return df[list_of_num]
oof_pred_deepfm = np.zeros(( len(train),))
y_pred_deepfm = np.zeros(( len(test),))
skf = StratifiedKFold(n_splits=N_Splits, shuffle=True, random_state=SEED)
 for fold,(tr_ind, val_ind)in enumerate(skf.split(train, train[target])) :
X_train, X_val = train[sparse_features].iloc[tr_ind], train[sparse_features].iloc[val_ind]
y_train, y_val = train[target].iloc[tr_ind], train[target].iloc[val_ind]
train_model_input = {name:X_train[name] for name in feature_names}
val_model_input = {name:X_val[name] for name in feature_names}
test_model_input = {name:test[name] for name in feature_names}
model = DeepFM(linear_feature_columns, dnn_feature_columns, dnn_hidden_units=(256, 256), dnn_dropout=0.0, dnn_activation='Mish', dnn_use_bn=False, task='binary')
 model.compile('adam', loss = 'binary_crossentropy', metrics=[auc],)
 es = callbacks.EarlyStopping(monitor='val_auc', min_delta=0.001, patience=3, verbose=Verbose, mode='max', baseline=None, restore_best_weights=True)
 sb = callbacks.ModelCheckpoint('./nn_model.w8', save_weights_only=True, save_best_only=True, verbose=Verbose)
 clr = CyclicLR(base_lr=1e-7, max_lr = 1e-4, step_size= int(1.0*(test.shape[0])/(Batch_S_T*4)) , mode='exp_range', gamma=1.0, scale_fn=None, scale_mode='cycle')
 history = model.fit(train_model_input, y_train,
validation_data=(val_model_input, y_val),
batch_size=Batch_S_T, epochs=Epochs, verbose=Verbose,
callbacks=[es, sb, clr],)
 model.load_weights('./nn_model.w8')
 val_pred = model.predict(val_model_input, batch_size=Batch_S_P)
 print(f'validation AUC fold {fold+1} : {round(roc_auc_score(y_val, val_pred), 5)}')
 oof_pred_deepfm[val_ind] = val_pred.ravel()
y_pred_deepfm += model.predict(test_model_input, batch_size=Batch_S_P ).ravel() /(N_Splits)
 K.clear_session() 
<compute_test_metric>
get_numerical = FunctionTransformer(func=return_num,validate=False )
print(f'OOF AUC : {round(roc_auc_score(train.target.values, oof_pred_deepfm), 5)}' )<save_to_csv>
num_pipeline = Pipeline([ 
('numerical',get_numerical),
] )
test_idx = test.id.values
submission = pd.DataFrame.from_dict({
'id': test_idx,
'target': y_pred_deepfm
})
 submission.to_csv('submission.csv', index=False)
 print('Submission file saved!' )<save_model>
def return_dict(blob):
return blob.to_dict("records" )
np.save('oof_pred_deepfm.npy',oof_pred_deepfm)
 np.save('y_pred_deepfm.npy', y_pred_deepfm )<set_options>
text_pipeline = Pipeline([ 
('textual',get_text), 
('dictifier',FunctionTransformer(func=return_dict,validate=False)) , 
('vectorizer',DictVectorizer(sort=False)) ,
] )
print(h2o.__version__)
 h2o.init(max_mem_size='16G' )<load_from_csv>
from sklearn.model_selection import train_test_split
%%time
train = h2o.import_file(".. /input/multi-cat-encodings/X_train_te.csv")
 test = h2o.import_file(".. /input/multi-cat-encodings/X_test_te.csv" )<feature_engineering>
from sklearn.model_selection import train_test_split
x = test.columns
y = 'target'
train[y] = train[y].asfactor()<train_model>
X_train, X_test, y_train, y_test = train_test_split(
...train_data.drop(['Survived'],axis=1), train_data['Survived'], test_size=0.2, random_state=42 )
aml = H2OAutoML(max_models=50, seed=47, max_runtime_secs=30000)
 aml.train(x=x, y=y, training_frame=train, fold_column='fold_column' )<find_best_params>
from sklearn.metrics import confusion_matrix , classification_report, roc_auc_score
aml.leader<predict_on_test>
import xgboost as xgb
preds = aml.predict(test)
 preds['p1'].as_data_frame().values.flatten().shape<load_from_csv>
xgb_model = xgb.XGBClassifier(silent=False,
scale_pos_weight=1,
learning_rate=0.01,
colsample_bytree = 0.4,
subsample = 0.8,
objective='binary:logistic',
n_estimators=500,
reg_alpha = 0.3,
gamma=10,
eval_metric = 'auc' )
sample_submission = pd.read_csv('.. /input/cat-in-the-dat-ii/sample_submission.csv')
 sample_submission.shape<save_to_csv>
pipeline = Pipeline([ 
('union',FeatureUnion(
 transformer_list = [ 
('num',num_pipeline), 
('text',text_pipeline)
 ])) , 
('clf',xgb_model)
 ] )
sample_submission['target'] = preds['p1'].as_data_frame().values
sample_submission.to_csv('h2o_automl_submission_4.csv', index=False )<load_from_csv>
pipeline.fit(X_train,y_train )
for dirname, _, filenames in os.walk('/kaggle/input'):
for filename in filenames:
print(os.path.join(dirname, filename))
test = pd.read_csv('/kaggle/input/cat-in-the-dat-ii/test.csv')
 train = pd.read_csv('/kaggle/input/cat-in-the-dat-ii/train.csv' )<categorify>
preds_xgb = pipeline.predict(X_test )
%%time
def random_permutation(x):
perm = np.random.permutation(len(x))
x = x.iloc[perm].reset_index(drop=True)
 return x
train = random_permutation(train)
 test = random_permutation(test)
 train_ids = train.id
test_ids = test.id
train.drop('id', 1, inplace=True)
 test.drop('id', 1, inplace=True)
 train_targets = train.target
train.drop('target', 1, inplace=True )<categorify>
print(confusion_matrix(y_test,preds_xgb))
print(roc_auc_score(y_test,preds_xgb))
print(classification_report(y_test,preds_xgb))
%%time
bin_recode = {0: 0, 1: 1, 'F':0, 'T':1, 'N':0, 'Y':1}
for i in range(5):
train[f'bin_{i}'] = train[f'bin_{i}'].map(bin_recode)
 test[f'bin_{i}'] = test[f'bin_{i}'].map(bin_recode)
 levels = { 'Novice':0, 'Contributor':1,
'Expert':2, 'Master':3, 'Grandmaster':4 }
train['ord_1'] = train['ord_1'].map(levels)
 test['ord_1'] = test['ord_1'].map(levels)
 temps = { 'Freezing':0, 'Cold':1, 'Warm':2, 'Hot':3,
'Boiling Hot':4, 'Lava Hot':5 }
train['ord_2'] = train['ord_2'].map(temps)
 test['ord_2'] = test['ord_2'].map(temps)
 lowercase_letters = {'a':1,'b':2,'c':3,'d':4,'e':5,'f':6,
'g':7,'h':8,'i':9,'j':10,'k':11,
'l':12,'m':13,'n':14,'o':15,'p':16,
'q':17,'r':18,'s':19,'t':20,'u':21,
'v':22,'w':23,'x':24,'y':25,'z':26}
train['ord_3'] = train['ord_3'].map(lowercase_letters)
 test['ord_3'] = test['ord_3'].map(lowercase_letters)
 uppercase_letters = {'A':1,'B':2,'C':3,'D':4,'E':5,'F':6,
'G':7,'H':8,'I':9,'J':10,'K':11,
'L':12,'M':13,'N':14,'O':15,'P':16,
'Q':17,'R':18,'S':19,'T':20,'U':21,
'V':22,'W':23,'X':24,'Y':25,'Z':26}
train['ord_4'] = train['ord_4'].map(uppercase_letters)
 test['ord_4'] = test['ord_4'].map(uppercase_letters )<categorify>
from sklearn.model_selection import RandomizedSearchCV
%%time
noms_0_4 = ['nom_0', 'nom_1', 'nom_2', 'nom_3', 'nom_4']
train = pd.get_dummies(train,
columns = noms_0_4,
prefix = noms_0_4,
drop_first=True,
sparse=True,
dtype=np.int8)
 test = pd.get_dummies(test,
columns = noms_0_4,
prefix = noms_0_4,
drop_first=True,
sparse=True,
dtype=np.int8 )<categorify>
params = {
"clf__learning_rate" : list(np.arange(0.05,0.6,0.05)) ,
"clf__max_depth" : list(np.arange(1,20,2)) ,
"clf__min_child_weight" : list(np.arange(1,9,1)) ,
"clf__gamma" : list(np.arange(1,20,1)) ,
"clf__colsample_bytree" : [ 0.3, 0.4, 0.5 , 0.7 ],
"clf__subsample" : list(np.arange(0.1,0.9,0.1)) ,
"clf__n_estimators" : [200,500,800,1000,1500,2000],
"clf__reg_alpha" : list(np.arange(0.1,0.9,0.1))
}
%%time
for i in [5,6,7,8,9]:
cbe = CatBoostEncoder()
train[f'nom_{i}'] = cbe.fit_transform(train[f'nom_{i}'], train_targets)
 test[f'nom_{i}'] = cbe.transform(test[f'nom_{i}'])
 cbe = CatBoostEncoder()
train['ord_5'] = cbe.fit_transform(train['ord_5'], train_targets)
 test['ord_5'] = cbe.transform(test['ord_5'] )<train_model>
grid = RandomizedSearchCV(pipeline,
param_distributions=params,
scoring='roc_auc',cv=5,verbose=5 )
%%time
cb = CatBoostClassifier(eval_metric='AUC',
learning_rate=0.1,
depth=3,
l2_leaf_reg=5)
 cb.fit(train, train_targets, verbose=False )<save_to_csv>
grid.fit(X_train,y_train )
preds = cb.predict_proba(test)[:, 1]
preds_df = pd.DataFrame(list(zip(test_ids, preds)) ,
columns = ['id', 'target'])
 preds_df.sort_values(by=['id'], inplace = True)
 preds_df.to_csv("./submission.csv", index=False )<load_from_csv>
grid.best_params_, grid.best_score_
train = pd.read_csv('.. /input/cat-in-the-dat-ii/train.csv')
 test = pd.read_csv('.. /input/cat-in-the-dat-ii/test.csv')
 train.sort_index(inplace=True)
 train_y = 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=0.20
oof = pd.DataFrame([])
 for tr_idx, oof_idx in StratifiedKFold(n_splits=5, random_state=2020, shuffle=True ).split(train, train_y):
ce_target_encoder = ce.TargetEncoder(cols = cat_feat_to_encode, smoothing=smoothing)
 ce_target_encoder.fit(train.iloc[tr_idx, :], train_y.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, train_y)
 train = oof.sort_index()
test = ce_target_encoder.transform(test )<train_model>
best_matrix = {'subsample': 0.30000000000000004,
'reg_alpha': 0.2,
'n_estimators': 500,
'min_child_weight': 3,
'max_depth': 9,
'learning_rate': 0.15000000000000002,
'gamma': 4,
'colsample_bytree': 0.3}
glm = linear_model.LogisticRegression(random_state=1, solver='lbfgs', max_iter=2020, fit_intercept=True, penalty='none', verbose=0); glm.fit(train, train_y )<import_modules>
xgb_model = xgb.XGBClassifier(**best_matrix,silent=False,
eval_metric = 'auc' )
from sklearn.linear_model import ElasticNet, Lasso, BayesianRidge, LassoLarsIC, LogisticRegression
from sklearn.ensemble import RandomForestRegressor, GradientBoostingRegressor
from sklearn.kernel_ridge import KernelRidge
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import RobustScaler
from sklearn.base import BaseEstimator, TransformerMixin, RegressorMixin, clone
from sklearn.model_selection import KFold, cross_val_score, train_test_split
from sklearn.metrics import mean_squared_error
import xgboost as xgb
import lightgbm as lgb<compute_train_metric>
finalized_model = pipeline = Pipeline([ 
('union',FeatureUnion(
 transformer_list = [ 
('num',num_pipeline), 
('text',text_pipeline)
 ])) , 
('clf',xgb_model)
 ] )
n_folds = 5
def auc_score(model):
kf = KFold(n_folds, shuffle= True ).get_n_splits(train.values)
 auc_score = cross_val_score(model, train.values, train_y, scoring = "roc_auc", cv = kf)
 return auc_score<choose_model_class>
X = train_data.drop(['Survived'],axis=1)
 y = train_data['Survived']
lasso = make_pipeline(RobustScaler() , Lasso(alpha =0.0005, random_state=1))<choose_model_class>
finalized_model.fit(X,y )
ENet = make_pipeline(RobustScaler() , ElasticNet(alpha=0.0005, l1_ratio=.9, random_state=3)) 
<choose_model_class>
final_test = test_data.drop(['PassengerId'],axis=1 )
KRR = KernelRidge(alpha=0.6, kernel='polynomial', degree=2, coef0=2.5 )<choose_model_class>
PassengerId = test_data['PassengerId']
GBoost = GradientBoostingRegressor(n_estimators=3000, learning_rate=0.05,
max_depth=4, max_features='sqrt',
min_samples_leaf=15, min_samples_split=10,
loss='huber', random_state =5 )<choose_model_class>
final_prediction = finalized_model.predict(final_test )
<choose_model_class><EOS> 
submission = pd.DataFrame({ 'PassengerId': PassengerId,
'Survived': final_prediction })
 submission.to_csv(path_or_buf ="Titanic_Submission.csv", index=False )