Kaggle competition: Mechanisms of Action (MoA) Prediction
sub_2['target'] =(sub_2['target']+sub_4['target3']+sub_5['target4']+sub_6['target5']+
sub_7['target6']+sub_8['target7'] +sub_9['target8'] +sub_10['target9']
+sub_11['target10'] +sub_13['target12']+
sub_14['target13'] + +sub_15['target14'] +sub_16['target15'])/13.0
sub_2.to_csv('sol.csv',index=False)
 sub_2.head()<merge>
import pickle
import numpy as np
import pandas as pd
import tensorflow as tf
df_base = pd.merge(sub_4,sub_2,how='inner',on='ID_code')
 df_base = pd.merge(df_base,sub_5,how='inner',on='ID_code')
 df_base = pd.merge(df_base,sub_6,how='inner',on='ID_code')
 df_base = pd.merge(df_base,sub_7,how='inner',on='ID_code')
 df_base = pd.merge(df_base,sub_8,how='inner',on='ID_code')
 df_base = pd.merge(df_base,sub_9,how='inner',on='ID_code')
 df_base = pd.merge(df_base,sub_10,how='inner',on='ID_code')
 df_base = pd.merge(df_base,sub_11,how='inner',on='ID_code')
 df_base = pd.merge(df_base,sub_13,how='inner',on='ID_code')
 df_base = pd.merge(df_base,sub_14,how='inner',on='ID_code')
 df_base = pd.merge(df_base,sub_15,how='inner',on='ID_code')
 df_base = pd.merge(df_base,sub_16,how='inner',on='ID_code')
 df_base.head() 
<load_from_csv>
MIXED_PRECISION = False
XLA_ACCELERATE = True
seed = 1337
precision = 5
train_data = pd.read_csv('.. /input/train.csv')
 test_data = pd.read_csv('.. /input/test.csv' )<prepare_x_and_y>
if MIXED_PRECISION:
policy = tf.keras.mixed_precision.experimental.Policy("mixed_float16")
 tf.keras.mixed_precision.experimental.set_policy(policy)
 tf.config.optimizer.set_jit(XLA_ACCELERATE )
index_column = 'ID_code'
target_column = 'target'
raws_features = train_data.columns[2:]
train_X, train_y = train_data[raws_features], train_data[target_column]
test_X = test_data[raws_features]<train_on_grid>
def build_callbacks(
 checkpoint_path,
factor=0.1,
mode="auto",
monitor="val_loss",
patience_for_es=0,
patience_for_rlop=10,
save_best_only=False,
verbose=0, 
):
callbacks = []
if checkpoint_path is not None:
model_checkpoint = tf.keras.callbacks.ModelCheckpoint(
 checkpoint_path,
mode=mode,
monitor=monitor,
save_best_only=save_best_only,
verbose=verbose, 
)
 callbacks.append(model_checkpoint)
 if patience_for_es is not None:
early_stopping = tf.keras.callbacks.EarlyStopping(
 mode=mode, monitor=monitor, patience=patience_for_es, verbose=verbose 
)
 callbacks.append(early_stopping)
 if patience_for_rlop is not None:
reduce_lr_on_plateau = tf.keras.callbacks.ReduceLROnPlateau(
 factor=factor,
monitor=monitor,
mode=mode,
patience=patience_for_rlop,
verbose=verbose, 
)
 callbacks.append(reduce_lr_on_plateau)
 return callbacks
class CVClassifier() :
def __init__(self, estimator, n_splits=5, stratified=True, num_round=20000, early_stop=200, shuffle=False, **params):
self.n_splits_ = n_splits
self.scores_ = []
self.clf_list_ = []
self.estimator_ = estimator
self.stratified_ = stratified
self.num_round_ = num_round
self.early_stop = early_stop
self.shuffle = shuffle
if params:
self.params_ = params
def cv(self, train_X, train_y):
if self.stratified_:
folds = StratifiedKFold(self.n_splits_, shuffle=self.shuffle, random_state=seed)
 else:
folds = KFold(self.n_splits_, shuffle=self.shuffle, random_state=seed)
 oof = np.zeros(len(train_y))
for fold,(train_idx, val_idx)in enumerate(folds.split(train_X, train_y)) :
print('fold %d' %(fold + 1))
trn_data, trn_y = train_X.iloc[train_idx], train_y[train_idx]
val_data, val_y = train_X.iloc[val_idx], train_y[val_idx]
if self.estimator_ == 'lgbm':
train_set = lgb.Dataset(data=trn_data.values, label=trn_y.values)
 val_set = lgb.Dataset(data=val_data.values, label=val_y.values)
 clf = lgb.train(params=params, train_set=train_set, num_boost_round=self.num_round_, valid_sets=[train_set, val_set],
verbose_eval=500, early_stopping_rounds=self.early_stop)
 oof[val_idx] = clf.predict(train_X.iloc[val_idx], num_iteration=clf.best_iteration)
 elif self.estimator_ == 'xgb':
train_set = xgb.DMatrix(data=trn_data, label=trn_y)
 val_set = xgb.DMatrix(data=val_data, label=val_y)
 watchlist = [(train_set, 'train'),(val_set, 'valid')]
clf = xgb.train(self.params_, train_set, self.num_round_, watchlist, early_stopping_rounds=self.early_stop, verbose_eval=500)
 oof[val_idx] = clf.predict(val_set, ntree_limit=clf.best_ntree_limit)
 elif self.estimator_ == 'cat':
clf = CatBoostClassifier(self.num_round_, task_type='GPU', early_stopping_rounds=self.early_stop, **self.params_)
 clf.fit(trn_data, trn_y, eval_set=(val_data, val_y), cat_features=[], use_best_model=True, verbose=500)
 oof[val_idx] = clf.predict_proba(val_data)[:, 1]
else:
clf = self.estimator_.fit(trn_data, trn_y)
 try:
oof[val_idx] = clf.predict_proba(val_data)[:, 1]
except AttributeError:
oof[val_idx] = clf.decision_function(val_data)
 self.clf_list_.append(clf)
 fold_score = roc_auc_score(train_y[val_idx], oof[val_idx])
 self.scores_.append(fold_score)
 print('Fold score: {:<8.5f}'.format(fold_score))
self.oof_ = oof
self.score_ = roc_auc_score(train_y, oof)
 print('
KFold CV: %s' % list(map(lambda x: np.round(x, precision), list(self.scores_))))
print('Mean Kfold CV: {0} +/- {1}'.format(np.round(np.mean(self.scores_), precision), np.round(np.std(list(self.scores_)) , precision)))
 print("Estimated CV: {0:0.5f}".format(self.score_))
def predict(self, test_X):
self.predictions_ = np.zeros(len(test_X))
if self.estimator_ == 'lgbm':
self.feature_importance_df_ = pd.DataFrame()
for fold, clf in enumerate(self.clf_list_):
fold_importance_df = pd.DataFrame()
fold_importance_df["feature"] = clf.feature_name()
fold_importance_df["importance"] = clf.feature_importance(importance_type='gain')
 fold_importance_df["fold"] = fold + 1
self.feature_importance_df_ = pd.concat([self.feature_importance_df_, fold_importance_df], axis=0)
 self.predictions_ += clf.predict(test_X, num_iteration=clf.best_iteration)/ self.n_splits_
elif self.estimator_ == 'xgb':
for fold, clf in enumerate(self.clf_list_):
self.predictions_ += clf.predict(xgb.DMatrix(test_X), ntree_limit=clf.best_ntree_limit)/ self.n_splits_
elif self.estimator_ == 'cat':
for fold, clf in enumerate(self.clf_list_):
self.predictions_ += clf.predict_proba(test_X)[:, 1] / self.n_splits_
else:
for fold, clf in enumerate(self.clf_list_):
self.predictions_ += clf.predict_proba(test_X)[:, 1] / self.n_splits_<train_on_grid>
def build_model(
 input_dim,
output_dim,
activation="relu",
kernel_initializer="glorot_uniform",
label_smoothing=0.0,
momentum=0.99,
n_layers=3,
n_units="auto",
optimizer_params=None,
rate=0.0, 
):
if n_units == "auto":
n_units = 0.5 *(input_dim + output_dim)
 inputs = tf.keras.layers.Input(shape=input_dim)
 x = inputs
for i in range(n_layers - 2):
x = tf.keras.layers.Dense(n_units, kernel_initializer=kernel_initializer )(x)
 x = tf.keras.layers.BatchNormalization(momentum=momentum )(x)
 x = tf.keras.layers.Activation(activation )(x)
 x = tf.keras.layers.Dropout(rate )(x)
 x = tf.keras.layers.Dense(output_dim )(x)
 outputs = tf.keras.layers.Activation("sigmoid" )(x)
 model = ModelWithCutmix(inputs=inputs, outputs=outputs)
 if optimizer_params is None:
optimizer_params = {}
loss = tf.keras.losses.BinaryCrossentropy(label_smoothing=label_smoothing)
 optimizer = tf.keras.optimizers.Adam(**optimizer_params)
 model.compile(loss=loss, optimizer=optimizer)
 return model
class CVForBO() :
def __init__(self, model, train_X, train_y, test_X, base_params, int_params=[], n_splits=5, num_round=20000, early_stop=200, shuffle=False):
self.oofs_ = []
self.params_ = []
self.predictions_ = []
self.cv_scores_ = []
self.model_ = model
self.train_X_ = train_X
self.train_y_ = train_y
self.test_X_ = test_X
self.base_params_ = base_params
self.int_params_ = int_params
self.n_splits_ = n_splits
self.num_round_ = num_round
self.early_stop = early_stop
self.shuffle = shuffle
def cv(self, **opt_params):
for p in self.int_params_:
if p in opt_params:
opt_params[p] = int(np.round(opt_params[p]))
self.base_params_.update(opt_params)
 cv_model = CVClassifier(self.model_, n_splits=self.n_splits_, num_round=self.num_round_, early_stop=self.early_stop, shuffle=self.shuffle, **self.base_params_)
 cv_model.cv(self.train_X_, self.train_y_)
 cv_model.predict(self.test_X_)
 self.oofs_.append(cv_model.oof_)
 self.predictions_.append(cv_model.predictions_)
 self.params_.append(self.base_params_)
 self.cv_scores_.append(cv_model.score_)
 return cv_model.score_
def post_process(self, model_type=None, oof_path='inter_oofs.csv', pred_path='inter_preds.csv', params_path='inter_params.csv'):
if not model_type:
model_type=self.model_
cols = ['{}_{}_{}'.format(model_type, str(self.cv_scores_[k] ).split('.')[-1][:5], k)for k in range(len(self.cv_scores_)) ]
self.oof_df = pd.DataFrame(np.array(self.oofs_ ).T, columns=cols)
 self.pred_df = pd.DataFrame(np.array(self.predictions_ ).T, columns=cols)
 self.params_df = pd.DataFrame(self.params_ ).T.rename(columns={c_old: c_new for c_old, c_new in enumerate(cols)})
 self.oof_df.to_csv(oof_path)
 self.pred_df.to_csv(pred_path)
 self.params_df.to_csv(params_path )<normalization>
def score(Y, Y_pred, eps=1e-15, label_smoothing=0.0):
Y = np.asarray(Y)
 Y = np.ravel(Y)
 if label_smoothing > 0.0:
Y = Y *(1.0 - label_smoothing)+ 0.5 * label_smoothing
Y_pred = np.asarray(Y_pred)
 Y_pred = np.ravel(Y_pred)
 Y_pred = np.clip(Y_pred, eps, 1.0 - eps)
 return -np.mean(Y * np.log(Y_pred)+(1.0 - Y)* np.log(1.0 - Y_pred))
def standard_scaler(train_X, test_X):
train_X_df = train_X.copy()
test_X_df = test_X.copy()
scaler = StandardScaler()
train_X_df = pd.DataFrame(scaler.fit_transform(train_X_df), columns=train_X_df.columns)
 test_X_df = pd.DataFrame(scaler.transform(test_X_df), columns=test_X_df.columns)
 return train_X_df, test_X_df
def quantile_transformer(train_X, test_X):
train_X_df = train_X.copy()
test_X_df = test_X.copy()
transformer = QuantileTransformer(output_distribution='normal', random_state=seed)
 train_X_df = pd.DataFrame(transformer.fit_transform(train_X_df), columns=train_X_df.columns)
 test_X_df = pd.DataFrame(transformer.transform(test_X_df), columns=test_X_df.columns)
 return train_X_df, test_X_df
def min_max_scaler(train_X, test_X):
train_X_df = train_X.copy()
test_X_df = test_X.copy()
scaler = MinMaxScaler()
train_X_df = pd.DataFrame(scaler.fit_transform(train_X_df), columns=train_X_df.columns)
 test_X_df = pd.DataFrame(scaler.transform(test_X_df), columns=test_X_df.columns)
 return train_X_df, test_X_df
def power_transformer(train_X, test_X):
train_X_df = train_X.copy()
test_X_df = test_X.copy()
scaler = PowerTransformer()
train_X_df = pd.DataFrame(scaler.fit_transform(train_X_df), columns=train_X_df.columns)
 test_X_df = pd.DataFrame(scaler.transform(test_X_df), columns=test_X_df.columns)
 return train_X_df, test_X_df
def robust_scaler(train_X, test_X):
train_X_df = train_X.copy()
test_X_df = test_X.copy()
scaler = RobustScaler(with_centering=True, with_scaling=True, quantile_range=(5.0, 95.0))
train_X_df = pd.DataFrame(scaler.fit_transform(train_X_df), columns=train_X_df.columns)
 test_X_df = pd.DataFrame(scaler.transform(test_X_df), columns=test_X_df.columns)
 return train_X_df, test_X_df
def features_generation(train_X, test_X):
train_X_df = train_X.copy()
test_X_df = test_X.copy()
train_test_df = train_X_df.append(test_X_df)
 scaler = StandardScaler()
train_test_df = pd.DataFrame(scaler.fit_transform(train_test_df), columns=train_test_df.columns)
 print("Number of null/inf in dataframe: %d" % train_test_df.isin([np.inf, -np.inf, np.nan] ).sum().sum())
 new_feats = []
for col in train_test_df.columns:
train_test_df[col+'^2'] = train_test_df[col].pow(2)
 train_test_df[col+'^3'] = train_test_df[col].pow(3)
 train_test_df[col+'^4'] = train_test_df[col].pow(4)
 train_test_df[col+'_cp'] = rankdata(train_test_df[col])
 train_test_df[col+'_cnp'] = norm.cdf(train_test_df[col])
 new_feats.extend([col+'^2', col+'^3', col+'^4', col+'_cp', col+'_cnp'])
 for col in new_feats:
train_test_df[col] =(( train_test_df[col] - train_test_df[col].mean())/ train_test_df[col].std())
 train_X_df = train_test_df.iloc[0:200000]
test_X_df = train_test_df.iloc[200000:]
del train_test_df; gc.collect()
return train_X_df, test_X_df<init_hyperparams>
def set_seed(seed=0):
os.environ["PYTHONHASHSEED"] = str(seed)
 rn.seed(seed)
 np.random.seed(seed)
 tf.random.set_seed(seed)
 graph = tf.compat.v1.get_default_graph()
session_conf = tf.compat.v1.ConfigProto(
 inter_op_parallelism_threads=1, intra_op_parallelism_threads=1 
)
 sess = tf.compat.v1.Session(graph=graph, config=session_conf)
 tf.compat.v1.keras.backend.set_session(sess )
%%time
model_seeds_list = [2018, 516, 986]
model_name = 'cat'
scaler = False
generate_features = False
num_round = 100000
n_splits = 12
early_stop = 2000
shuffle = True
cat_params = {
'eval_metric': 'AUC',
'bootstrap_type': 'Bernoulli',
'objective': 'Logloss',
'od_type': 'Iter',
'depth': 2,
'eta': 0.0095,
'l2_leaf_reg': 30,
'random_strength': 5.41,
'subsample': 0.4,
'random_seed': seed,
'allow_writing_files': False,
}
if generate_features:
train_X_df, test_X_df = features_generation(train_X, test_X)
 else:
train_X_df, test_X_df = train_X, test_X
if scaler == 'ss':
train_X_df, test_X_df = standard_scaler(train_X, test_X)
 elif scaler == 'qt':
train_X_df, test_X_df = quantile_transformer(train_X, test_X)
 elif scaler == 'mm':
train_X_df, test_X_df = min_max_scaler(train_X, test_X)
 elif scaler == 'pt':
train_X_df, test_X_df = power_transformer(train_X, test_X)
 elif scaler == 'rs':
train_X_df, test_X_df = robust_scaler(train_X, test_X)
 clf_name = model_name if isinstance(model_name, str)else model_name.__class__.__name__.lower()
clf_name = clf_name + '_' + scaler if scaler else clf_name
oofs = []
predictions = []
kfold_scores = {}
final_cv_score = {}
for i, seed_ in enumerate(model_seeds_list):
i += 1
print("
if model_name == 'cat':
params = cat_params.copy()
params['random_seed'] = seed_
elif model_name == 'lgbm':
params = lgbm_params.copy()
params['random_state'] = seed_
elif model_name == GaussianNB() :
params = {}
elif model_name == LogisticRegression() :
params = logreg_params.copy()
params['random_state'] = seed_
model_name.set_params(**params)
 else:
params = {}
model = CVClassifier(model_name, n_splits=n_splits, stratified=True, num_round=num_round, early_stop=early_stop, shuffle=shuffle, **params)
 model.cv(train_X_df, train_y)
 model.predict(test_X_df)
 kfold_scores[seed_] = model.scores_
final_cv_score[seed_] = model.score_
oofs.append(pd.DataFrame(np.array(model.oof_ ).T, columns=['seed_' + str(i)]))
predictions.append(pd.DataFrame(np.array(model.predictions_ ).T, columns=['seed_' + str(i)]))
oof_df = pd.concat(oofs, axis=1)
 oof_df = pd.concat([oof_df, train_y], axis=1)
 oof_df.insert(loc=0, column=index_column, value=train_data[index_column].values)
 predictions_df = pd.concat(predictions, axis=1)
 predictions_df.insert(loc=0, column=index_column, value=test_data[index_column].values)
 kfold_scores_df = pd.DataFrame([kfold_scores] ).T.reset_index()
kfold_scores_df.rename(columns={'index': 'seed', 0: 'cv_score_per_each_fold'}, inplace=True)
 kfold_scores_df.insert(loc=1, column='cv_std', value=kfold_scores_df['cv_score_per_each_fold'].map(
 lambda x: np.round(np.std(x), precision)))
 final_cv_score_df = pd.DataFrame(index=list(final_cv_score.keys()), data=list(final_cv_score.values()),
columns=['cv_mean_score'] ).reset_index()
final_cv_score_df.rename(columns={'index': 'seed'}, inplace=True)
 final_cv_score_df['cv_mean_score'] = final_cv_score_df['cv_mean_score'].map(lambda x: np.round(x, precision))
final_cv_score_df = final_cv_score_df.merge(kfold_scores_df, how='left', on='seed')
 oof_df.to_csv(clf_name + '_train_OOF_bagged.csv', index=False)
 predictions_df.to_csv(clf_name + '_test_bagged.csv', index=False)
 final_cv_score_df.to_csv(clf_name + '_cv_results.csv', index=False)
 print('
Bagged CV scores: %s' % list(map(lambda x: np.round(x, precision), list(final_cv_score.values()))))
print('Bagged mean CV: {0} +/- {1}
'.format(np.round(np.mean(list(final_cv_score.values())) , precision),
np.round(np.std(list(final_cv_score.values())) , precision)) )<save_to_csv>
def cutmix_for_tabular(x, y=None, alpha=1.0, p=1.0, random_state=None):
n, d = x.shape
if n is not None and rn.random() < p:
random_state = check_random_state(random_state)
 l = random_state.beta(alpha, alpha)
 mask = random_state.choice([False, True], size=d, p=[l, 1.0 - l])
 mask = np.where(mask)[0]
shuffle = random_state.choice(n, n, replace=False)
 x[:, mask] = x[shuffle, mask]
if y is not None:
y = l * y +(1.0 - l)* y[shuffle]
return x, y
class ModelWithCutmix(tf.keras.models.Model):
def __init__(self, *args, alpha=1.0, p=1.0, **kwargs):
super().__init__(*args, **kwargs)
 self.alpha = alpha
self.p = p
def train_step(self, data):
data = cutmix_for_tabular(*data, alpha=self.alpha, p=self.p)
 return super().train_step(data )
predictions_df_mean = predictions_df.copy()
pred_cols = predictions_df_mean.columns
predictions_df_mean[target_column] = predictions_df_mean.loc[:, ~predictions_df_mean.columns.isin([index_column, target_column])].apply(np.mean, axis=1)
 predictions_df_mean = predictions_df_mean[[index_column, target_column]]
predictions_df_mean.to_csv(clf_name + '_test.csv', index=False )<set_options>
class MultilabelStratifiedGroupKFold(_BaseKFold):
def __init__(self, n_splits=5, random_state=None, shuffle=False):
super().__init__(n_splits=n_splits, random_state=random_state, shuffle=shuffle)
 def _iter_test_indices(self, X=None, y=None, groups=None):
cv = MultilabelStratifiedKFold(
 n_splits=self.n_splits,
random_state=self.random_state,
shuffle=self.shuffle, 
)
 value_counts = groups.value_counts()
regluar_indices = value_counts.loc[value_counts <= 18].index.sort_values()
irregluar_indices = value_counts.loc[value_counts > 18].index.sort_values()
group_to_fold = {}
tmp = y.groupby(groups ).mean().loc[regluar_indices]
for fold,(_, test)in enumerate(cv.split(tmp, tmp)) :
group_to_fold.update({group: fold for group in tmp.index[test]})
 sample_to_fold = {}
tmp = y.loc[groups.isin(irregluar_indices)]
for fold,(_, test)in enumerate(cv.split(tmp, tmp)) :
sample_to_fold.update({sample: fold for sample in tmp.index[test]})
 folds = groups.map(group_to_fold)
 is_na = folds.isna()
folds[is_na] = folds[is_na].index.map(sample_to_fold ).values
for i in range(self.n_splits):
yield np.where(folds == i)[0]
warnings.simplefilter(action='ignore', category=FutureWarning)
 warnings.simplefilter(action='ignore', category=DeprecationWarning)
 %matplotlib inline
print(os.listdir(".. /input"))<load_from_csv>
dtype = {"cp_type": "category", "cp_dose": "category"}
index_col = "sig_id"
train_features = pd.read_csv(
 ".. /input/lish-moa/train_features.csv", dtype=dtype, index_col=index_col 
)
 test_features = pd.read_csv(
 ".. /input/lish-moa/test_features.csv", dtype=dtype, index_col=index_col 
)
 X = train_features.select_dtypes("number")
 X_test = test_features.select_dtypes("number")
 Y = pd.read_csv(".. /input/lish-moa/train_targets_scored.csv", index_col=index_col)
 groups = pd.read_csv(
 ".. /input/lish-moa/train_drug.csv", index_col=index_col, squeeze=True 
)
 train_size, n_features = X.shape
test_size, _ = X_test.shape
_, n_classes = Y.shape
train_df = pd.read_csv(".. /input/train.csv")
 train_df.head()<sort_values>
n_seeds = 5
n_splits = 5
shuffle = True
params = {"n_layers": 4, "rate": 0.25}
callbacks_params = {"patience_for_es": 20, "save_best_only": True}
fit_params = {"epochs": 1_000}
def null_values(df):
mis_val = df.isnull().sum()
mis_val_percent = 100 * df.isnull().sum() / len(df)
 mis_val_table = pd.concat([mis_val, mis_val_percent], axis=1)
 mis_val_table_ren_columns = mis_val_table.rename(
 columns = {0 : 'Missing Values', 1 : '% of Total Values'})
 mis_val_table_ren_columns = mis_val_table_ren_columns[
mis_val_table_ren_columns.iloc[:,1] != 0].sort_values(
 '% of Total Values', ascending=False ).round(1)
 print("Dataframe has " + str(df.shape[1])+ " columns.
 "
"There are " + str(mis_val_table_ren_columns.shape[0])+
" columns that have missing values.")
 return mis_val_table_ren_columns<load_from_csv>
%%time
Y_pred = np.zeros(( train_size, n_classes))
Y_pred = pd.DataFrame(Y_pred, columns=Y.columns, index=Y.index)
 for i in range(n_seeds):
set_seed(seed=i)
 cv = MultilabelStratifiedGroupKFold(
 n_splits=n_splits, random_state=i, shuffle=shuffle 
)
 for j,(train, valid)in enumerate(cv.split(X, Y, groups)) :
model_path = f"model_seed_{i}_fold_{j}.h5"
model = build_model(n_features, n_classes, **params)
 callbacks = build_callbacks(model_path, **callbacks_params)
 model.fit(
 X.iloc[train],
Y.iloc[train],
callbacks=callbacks,
validation_data=(X.iloc[valid], Y.iloc[valid]),
**fit_params, 
)
 model.load_weights(model_path)
 Y_pred.iloc[valid] += model.predict(X.iloc[valid])/ n_seeds
Y_pred[train_features["cp_type"] == "ctl_vehicle"] = 0.0
test_df = pd.read_csv(".. /input/test.csv")
 test_df.head()<define_variables>
score(Y, Y_pred )
var_features = ["var_0", "var_15", "var_22", "var_25", "var_39", "var_45", "var_52", "var_68",
"var_85", "var_99", "var_112", "var_135", "var_147", "var_158", "var_160",
"var_169", "var_173", "var_180", "var_192", "var_199"]<train_model>
%%time
Y_pred = np.zeros(( test_size, n_classes))
Y_pred = pd.DataFrame(Y_pred, columns=Y.columns, index=X_test.index)
 for i in range(n_seeds):
for j in range(n_splits):
model = tf.keras.models.load_model(
 f"model_seed_{i}_fold_{j}.h5",
compile=False,
custom_objects={"ModelWithCutmix": ModelWithCutmix}, 
)
 Y_pred += model.predict(X_test)/ n_seeds / n_splits
Y_pred[test_features["cp_type"] == "ctl_vehicle"] = 0.0
X = np.array(train_df.loc[:, train_df.columns != 'target'])
 y = np.array(train_df.loc[:, train_df.columns == 'target'])
 print('Shape of X: {}'.format(X.shape))
print('Shape of y: {}'.format(y.shape))<split>
Y_pred.to_csv("submission.csv" )