Kaggle competition: Santander Customer Transaction Prediction
exec(' 
<define_variables>
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv')
TRAIN_TEST_RATIO = 1 -(70 + 15)/(70 + 15 + 15)
 TRAIN_VAL_RATIO = 1 - 70 /(70 + 15 )<feature_engineering>
print(train.isnull().sum() )
print("Image IDs and Labels(TRAIN)")
 train_df = pd.read_csv(TRAIN_DF_PATH ).sample(frac=1, random_state=seed)
 train_df['id_code'] = train_df['id_code'] + ".png"
print(f"Training images: {train_df.shape[0]}")
 print("Image IDs(TEST)")
 test_df = pd.read_csv(TEST_DF_PATH)
 test_df['id_code'] = test_df['id_code'] + ".png"
test_df = test_df.rename(columns={'diagnosis': 'label'})
 print(f"Testing Images: {test_df.shape[0]}")
<define_search_space>
print(test.isnull().sum() )
effnet_to_img_size = dict(enumerate(
 [(224, 224),(240, 240),(260, 260),(300, 300),(380, 380),(456, 456),(528, 528),(600, 600)] 
))
IMG_WIDTH, IMG_HEIGHT = effnet_to_img_size[EFFNET]
CHANNELS = 3
effnet_to_nb_epochs = dict(enumerate(
 [30, 30, 30, 30, 30, 25, 20, 15] 
))
EPOCHS = effnet_to_nb_epochs[EFFNET]<predict_on_test>
train['max'] = train.max(axis=1)
 train['min'] = train.min(axis=1)
 train['mean'] = train.mean(axis=1)
 train['sd'] = train.std(axis=1)
 test['max'] = test.max(axis=1)
 test['min'] = test.min(axis=1)
 test['mean'] = test.mean(axis=1)
 test['sd'] = test.std(axis=1 )
def get_preds_and_labels(model, generator):

 preds = []
labels = []
for _ in range(int(np.ceil(generator.samples / BATCH_SIZE))):
x, y = next(generator)
 preds.append(model.predict(x))
labels.append(y)
 return np.concatenate(preds ).ravel() , np.concatenate(labels ).ravel().astype(int )<train_model>
@jit
def augment(x,y,t=2):
xs,xn = [],[]
for i in range(t):
mask = y>0
x1 = x[mask].copy()
ids = np.arange(x1.shape[0])
 for c in range(x1.shape[1]):
np.random.shuffle(ids)
 x1[:,c] = x1[ids][:,c]
xs.append(x1)
 for i in range(t//2):
mask = y==0
x1 = x[mask].copy()
ids = np.arange(x1.shape[0])
 for c in range(x1.shape[1]):
np.random.shuffle(ids)
 x1[:,c] = x1[ids][:,c]
xn.append(x1)
 xs = np.vstack(xs)
 xn = np.vstack(xn)
 ys = np.ones(xs.shape[0])
 yn = np.zeros(xn.shape[0])
 x = np.vstack([x,xs,xn])
 y = np.concatenate([y,ys,yn])
 return x,y
class Metrics(Callback):

 def on_train_begin(self, logs={}):

 self.val_kappas = []
def on_epoch_end(self, epoch, logs={}):

 y_pred, labels = get_preds_and_labels(model, val_generator)
 y_pred = np.rint(y_pred ).astype(np.uint8 ).clip(0, 4)
 _val_kappa = cohen_kappa_score(labels, y_pred, weights='quadratic')
 self.val_kappas.append(_val_kappa)
 print(f"val_kappa: {round(_val_kappa, 4)}")
 if _val_kappa == max(self.val_kappas):
print("Validation Kappa has improved.Saving model.")
 self.model.save(SAVED_MODEL_NAME)
 return<prepare_x_and_y>
param = {
'bagging_freq': 5,
'bagging_fraction': 0.335,
'boost_from_average':'false',
'boost': 'gbdt',
'feature_fraction': 0.041,
'learning_rate': 0.0083,
'max_depth': -1,
'metric':'auc',
'min_data_in_leaf': 80,
'min_sum_hessian_in_leaf': 10.0,
'num_leaves': 13,
'num_threads': 8,
'tree_learner': 'serial',
'objective': 'binary',
'verbosity': -1
}
y_labels = train_df['diagnosis'].values<define_variables>
num_folds = 11
features = [c for c in train.columns if c not in ['ID_code', 'target']]
folds = KFold(n_splits=num_folds, random_state=2319)
 oof = np.zeros(len(train))
getVal = np.zeros(len(train))
predictions = np.zeros(len(train.target))
feature_importance_df = pd.DataFrame()
BATCH_SIZE = 4
train_datagen = ImageDataGenerator(rotation_range=360,
horizontal_flip=True,
vertical_flip=True,
validation_split=TRAIN_VAL_RATIO,
preprocessing_function=preprocess_image,
rescale=1 / 255.)
train_generator = train_datagen.flow_from_dataframe(train_df,
x_col='id_code',
y_col='diagnosis',
directory = TRAIN_IMG_PATH,
target_size=(IMG_WIDTH, IMG_HEIGHT),
batch_size=BATCH_SIZE,
class_mode='other',
subset='training',
seed=seed)
 val_generator = train_datagen.flow_from_dataframe(train_df,
x_col='id_code',
y_col='diagnosis',
directory = TRAIN_IMG_PATH,
target_size=(IMG_WIDTH, IMG_HEIGHT),
batch_size=BATCH_SIZE,
class_mode='other',
subset='validation',
seed=seed )<load_pretrained>
for fold_,(trn_idx, val_idx)in enumerate(folds.split(train.values, train.target.values)) :
X_train, y_train = train.iloc[trn_idx][features], train.target.iloc[trn_idx]
X_valid, y_valid = train.iloc[val_idx][features], train.target.iloc[val_idx]
X_tr, y_tr = augment(X_train.values, y_train.values)
 X_tr = pd.DataFrame(X_tr)
 print("Fold idx:{}".format(fold_ + 1))
trn_data = lgb.Dataset(X_tr, label=y_tr)
 val_data = lgb.Dataset(X_valid, label=y_valid)
 clf = lgb.train(param, trn_data, 1000000, valid_sets = [trn_data, val_data], verbose_eval=5000, early_stopping_rounds = 4000)
 oof[val_idx] = clf.predict(train.iloc[val_idx][features], num_iteration=clf.best_iteration)
 getVal[val_idx]+= clf.predict(train.iloc[val_idx][features], num_iteration=clf.best_iteration)/ folds.n_splits
fold_importance_df = pd.DataFrame()
fold_importance_df["feature"] = features
fold_importance_df["importance"] = clf.feature_importance()
fold_importance_df["fold"] = fold_ + 1
feature_importance_df = pd.concat([feature_importance_df, fold_importance_df], axis=0)
 predictions += clf.predict(test[features], num_iteration=clf.best_iteration)/ folds.n_splits
effnet = EfficientNet(weights=None,
include_top=False,
input_shape=(IMG_WIDTH, IMG_HEIGHT, CHANNELS))
effnet.load_weights(
 '.. /input/efficientnet-weights-for-keras/advprop/notop/efficientnet-b{}_weights_imagenet_1000_advprop_notop.h5'.format(
 EFFNET 
)
 )<choose_model_class>
print("
>> CV score: {:<8.5f}".format(roc_auc_score(train.target, oof)) )
for i, layer in enumerate(effnet.layers):
if "batch_normalization" in layer.name:
effnet.layers[i] = GroupNormalization(groups=32, axis=-1, epsilon=0.00001 )<choose_model_class>
submission = pd.DataFrame({"ID_code": test.ID_code.values})
 submission["target"] = predictions
submission.to_csv("submission.csv", index=False )