Kaggle competition: Santander Customer Transaction Prediction
def build_model() :

 model = Sequential()
model.add(effnet)
 model.add(GlobalAveragePooling2D())
 model.add(Dropout(0.5))
model.add(Dense(5, activation=elu))
model.add(Dense(1, activation="linear"))
model.compile(loss='mse',
optimizer=RAdam(learning_rate=0.00005),
metrics=['mse', 'acc'])
 print(model.summary())
 return model
model = build_model()<train_on_grid>
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv' )
kappa_metrics = Metrics()
es = EarlyStopping(monitor='val_loss', mode='auto', verbose=1, patience=EPOCHS // 2)
 rlr = ReduceLROnPlateau(monitor='val_loss',
factor=0.5,
patience=EPOCHS // 4,
verbose=1,
mode='auto',
epsilon=0.0001)
 model.fit_generator(train_generator,
steps_per_epoch=train_generator.samples // BATCH_SIZE,
epochs=EPOCHS,
validation_data=val_generator,
validation_steps = val_generator.samples // BATCH_SIZE,
callbacks=[kappa_metrics, es, rlr] )<load_pretrained>
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv' )
model.load_weights(SAVED_MODEL_NAME )<compute_test_metric>
def shuffle_col_vals(x1):
rand_x = np.array([np.random.choice(x1.shape[0], size=x1.shape[0], replace=False)for i in range(x1.shape[1])] ).T
grid = np.indices(x1.shape)
 rand_y = grid[1]
return x1[(rand_x, rand_y)]
def augment_fast1(x,y,t=2):
xs,xn = [],[]
for i in range(t):
mask = y>0
x1 = x[mask].copy()
x1 = shuffle_col_vals(x1)
 xs.append(x1)
 for i in range(t//2):
mask = y==0
x1 = x[mask].copy()
x1 = shuffle_col_vals(x1)
 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
def disarrange(a, axis=-1):

 b = a.swapaxes(axis, -1)
 shp = b.shape[:-1]
for ndx in np.ndindex(shp):
np.random.shuffle(b[ndx])
 return
def augment_fast2(x,y,t=2):
xs,xn = [],[]
for i in range(t):
mask = y>0
x1 = x[mask].copy()
disarrange(x1,axis=0)
 xs.append(x1)
 for i in range(t//2):
mask = y==0
x1 = x[mask].copy()
disarrange(x1,axis=0)
 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 OptimizedRounder(object):

 def __init__(self):
self.coef_ = 0
def _kappa_loss(self, coef, X, y):

 X_p = np.copy(X)
 for i, pred in enumerate(X_p):
if pred < coef[0]:
X_p[i] = 0
elif pred >= coef[0] and pred < coef[1]:
X_p[i] = 1
elif pred >= coef[1] and pred < coef[2]:
X_p[i] = 2
elif pred >= coef[2] and pred < coef[3]:
X_p[i] = 3
else:
X_p[i] = 4
ll = cohen_kappa_score(y, X_p, weights='quadratic')
 return -ll
def fit(self, X, y):

 loss_partial = partial(self._kappa_loss, X=X, y=y)
 initial_coef = [0.5, 1.5, 2.5, 3.5]
self.coef_ = sp.optimize.minimize(loss_partial, initial_coef, method='nelder-mead')
 def predict(self, X, coef):

 X_p = np.copy(X)
 for i, pred in enumerate(X_p):
if pred < coef[0]:
X_p[i] = 0
elif pred >= coef[0] and pred < coef[1]:
X_p[i] = 1
elif pred >= coef[1] and pred < coef[2]:
X_p[i] = 2
elif pred >= coef[2] and pred < coef[3]:
X_p[i] = 3
else:
X_p[i] = 4
return X_p.astype(int)
 def coefficients(self):

 return self.coef_['x']<compute_train_metric>
%%time
x = train[train.columns[2:]].values
y = train['target'].values
x_ts = test[test.columns[1:]].values
y_val_preds, val_labels = get_preds_and_labels(model, val_generator)
 optR = OptimizedRounder()
optR.fit(y_val_preds, val_labels)
 coefficients = optR.coefficients()
opt_val_predictions = optR.predict(y_val_preds, coefficients)
 new_val_score = cohen_kappa_score(val_labels, opt_val_predictions, weights="quadratic")
 new_acc_val_score = accuracy_score(val_labels, opt_val_predictions )<compute_test_metric>
%%time
x1,y1 = augment_fast2(x,y,t=10)
 print(x.shape,y.shape,x1.shape,y1.shape )
print(f"Optimized Thresholds:
{coefficients}
")
 print(f"The Validation Quadratic Weighted Kappa(QWK)
 \
with optimized rounding thresholds is: {round(new_val_score, 5)}
")
 print(f"The Validation Accuracy
\
with optimized rounding thresholds is: {round(new_acc_val_score, 5)}
" )<feature_engineering>
import lightgbm as lgb
from sklearn.metrics import roc_auc_score
from sklearn.model_selection import StratifiedKFold, KFold, train_test_split
test_df['diagnosis'] = np.zeros(test_df.shape[0])
 test_generator = ImageDataGenerator(preprocessing_function=preprocess_image,
rescale=1 / 255.).flow_from_dataframe(test_df,
x_col='id_code',
y_col='diagnosis',
directory=TEST_IMG_PATH,
target_size=(IMG_WIDTH, IMG_HEIGHT),
batch_size=BATCH_SIZE,
class_mode='other',
shuffle=False,
seed=seed )<save_to_csv>
x_train,x_test,y_train,y_test = train_test_split(x1,y1, test_size = 1/9, random_state=1999111000 )
y_test, _ = get_preds_and_labels(model, test_generator)
 y_test = optR.predict(y_test, coefficients ).astype(np.uint8)
 test_df['diagnosis'] = y_test
test_df['id_code'] = test_df['id_code'].str.replace(r'.png$', '')
 test_df.to_csv('submission.csv', index=False )<import_modules>
param = {
'bagging_freq': 5,
'bagging_fraction': 0.331,
'boost_from_average':'false',
'boost': 'gbdt',
'feature_fraction': 0.0405,
'learning_rate': 0.01,
'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
}
import numpy as np
import pandas as pd
import os
import albumentations as ab
import matplotlib.pyplot as plt
import cv2
from sklearn.metrics import cohen_kappa_score
from sklearn.model_selection import train_test_split
from tqdm import tqdm
import seaborn as sns
from sklearn.utils import class_weight,shuffle
import keras as K
from keras.utils import to_categorical
from keras.models import Model
from keras.regularizers import l1
from keras.optimizers import Adam
from keras.preprocessing.image import ImageDataGenerator
from keras.callbacks import Callback,ModelCheckpoint,ReduceLROnPlateau
import imgaug.augmenters as iaa
import imgaug as ia<define_variables>
trn_data = lgb.Dataset(x_train, label=y_train)
 val_data = lgb.Dataset(x_test, label=y_test )
train_df_path='.. /input/aptos2019-blindness-detection/train.csv'
train_img_path='.. /input/aptos2019-blindness-detection/train_images/'
test_df_path='.. /input/aptos2019-blindness-detection/test.csv'
test_images_path='.. /input/aptos2019-blindness-detection/test_images/'<define_variables>
clf = lgb.train(param, trn_data, num_boost_round=30000, valid_sets = [trn_data, val_data], verbose_eval=2000, early_stopping_rounds = 3500 )
SIZE=224
batch_size=35<load_from_csv>
predictions = clf.predict(x_ts, num_iteration=clf.best_iteration )
train_df=pd.read_csv(train_df_path)
 test_df=pd.read_csv(test_df_path )<define_variables>
<feature_engineering><EOS> 
sub = pd.read_csv('.. /input/sample_submission.csv')
 sub["target"] = predictions
sub.to_csv("submission.csv", index=False)
 sub.head()