Kaggle competition: SIIM-ISIC Melanoma Classification
<SOS> metric: MCAUC Kaggle data source: siim-isic-melanoma-classification<categorify>
warnings.filterwarnings('ignore')
def get_dummies_and_drop(df,dummy_cols,drop_cols):

 df_ = pd.get_dummies(df,columns=dummy_cols)
 for i in to_drop:
try:
df_.drop(i,inplace=True,axis=1)
 except:
pass
return df_
to_dummy = ['Sex','Embarked','Ticket Extracted','Title']
to_drop = ['Sex','Embarked','Name','Ticket','Cabin','Title'] 
<categorify>
def seed_everything(seed):
random.seed(seed)
 np.random.seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 SEED = 22
seed_everything(SEED)
 def read_data() :
train = pd.read_csv('/kaggle/input/siim-isic-melanoma-classification/train.csv')
 test = pd.read_csv('/kaggle/input/siim-isic-melanoma-classification/test.csv')
 sub = pd.read_csv('/kaggle/input/siim-isic-melanoma-classification/sample_submission.csv')
 train1 = pd.read_csv('.. /input/melanoma-subs/EfficientNetB0_256.csv')
 train2 = pd.read_csv('.. /input/melanoma-subs/EfficientNetB0_512EX.csv')
 train3 = pd.read_csv('.. /input/melanoma-subs/EfficientNetB1_256.csv')
 train4 = pd.read_csv('.. /input/melanoma-subs/EfficientNetB1_384.csv')
 train5 = pd.read_csv('.. /input/melanoma-subs/EfficientNetB1_512.csv')
 train6 = pd.read_csv('.. /input/melanoma-subs/EfficientNetB1_512EX.csv')
 train7 = pd.read_csv('.. /input/melanoma-subs/EfficientNetB2_256.csv')
 train8 = pd.read_csv('.. /input/melanoma-subs/EfficientNetB2_384.csv')
 train9 = pd.read_csv('.. /input/melanoma-subs/EfficientNetB2_512.csv')
 train10 = pd.read_csv('.. /input/melanoma-subs/EfficientNetB2_512EX.csv')
 train11 = pd.read_csv('.. /input/melanoma-subs/EfficientNetB3_256.csv')
 train12 = pd.read_csv('.. /input/melanoma-subs/EfficientNetB3_384.csv')
 train13 = pd.read_csv('.. /input/melanoma-subs/EfficientNetB3_512.csv')
 train14 = pd.read_csv('.. /input/melanoma-subs/EfficientNetB4_256.csv')
 train15 = pd.read_csv('.. /input/melanoma-subs/EfficientNetB4_384.csv')
 train16 = pd.read_csv('.. /input/melanoma-subs/EfficientNetB4_512.csv')
 train17 = pd.read_csv('.. /input/melanoma-subs/EfficientNetB5_256.csv')
 train18 = pd.read_csv('.. /input/melanoma-subs/EfficientNetB5_384.csv')
 train19 = pd.read_csv('.. /input/melanoma-subs/EfficientNetB6_384.csv')
 test1 = pd.read_csv('.. /input/melanoma-subs/sub_EfficientNetB0_256.csv')
 test2 = pd.read_csv('.. /input/melanoma-subs/sub_EfficientNetB0_512EX.csv')
 test3 = pd.read_csv('.. /input/melanoma-subs/sub_EfficientNetB1_256.csv')
 test4 = pd.read_csv('.. /input/melanoma-subs/sub_EfficientNetB1_384.csv')
 test5 = pd.read_csv('.. /input/melanoma-subs/sub_EfficientNetB1_512.csv')
 test6 = pd.read_csv('.. /input/melanoma-subs/sub_EfficientNetB1_512EX.csv')
 test7 = pd.read_csv('.. /input/melanoma-subs/sub_EfficientNetB2_256.csv')
 test8 = pd.read_csv('.. /input/melanoma-subs/sub_EfficientNetB2_384.csv')
 test9 = pd.read_csv('.. /input/melanoma-subs/sub_EfficientNetB2_512.csv')
 test10 = pd.read_csv('.. /input/melanoma-subs/sub_EfficientNetB2_512EX.csv')
 test11 = pd.read_csv('.. /input/melanoma-subs/sub_EfficientNetB3_256.csv')
 test12 = pd.read_csv('.. /input/melanoma-subs/sub_EfficientNetB3_384.csv')
 test13 = pd.read_csv('.. /input/melanoma-subs/sub_EfficientNetB3_512.csv')
 test14 = pd.read_csv('.. /input/melanoma-subs/sub_EfficientNetB4_256.csv')
 test15 = pd.read_csv('.. /input/melanoma-subs/sub_EfficientNetB4_384.csv')
 test16 = pd.read_csv('.. /input/melanoma-subs/sub_EfficientNetB4_512.csv')
 test17 = pd.read_csv('.. /input/melanoma-subs/sub_EfficientNetB5_256.csv')
 test18 = pd.read_csv('.. /input/melanoma-subs/sub_EfficientNetB5_384.csv')
 test19 = pd.read_csv('.. /input/melanoma-subs/sub_EfficientNetB6_384.csv')
 def print_roc_auc(df, model):
roc_auc = metrics.roc_auc_score(df['target'], df['predictions'])
 print(f'Our model {model} out of folds roc auc score is {roc_auc}')
 print('-'*50)
 print('
')
 print_roc_auc(train1, 1)
 print_roc_auc(train2, 2)
 print_roc_auc(train3, 3)
 print_roc_auc(train4, 4)
 print_roc_auc(train5, 5)
 print_roc_auc(train6, 6)
 print_roc_auc(train7, 7)
 print_roc_auc(train8, 8)
 print_roc_auc(train9, 9)
 print_roc_auc(train10, 10)
 print_roc_auc(train11, 11)
 print_roc_auc(train12, 12)
 print_roc_auc(train13, 13)
 print_roc_auc(train14, 14)
 print_roc_auc(train15, 15)
 print_roc_auc(train16, 16)
 print_roc_auc(train17, 17)
 print_roc_auc(train18, 18)
 print_roc_auc(train19, 19)
 def fix_predictions(train, test, model):
test.columns = ['image_name', 'predictions_{}'.format(model)]
train = train[['image_name', 'predictions']]
train.columns = ['image_name', 'predictions_{}'.format(model)]
return train, test
train1, test1 = fix_predictions(train1, test1, 1)
 train2, test2 = fix_predictions(train2, test2, 2)
 train3, test3 = fix_predictions(train3, test3, 3)
 train4, test4 = fix_predictions(train4, test4, 4)
 train5, test5 = fix_predictions(train5, test5, 5)
 train6, test6 = fix_predictions(train6, test6, 6)
 train7, test7 = fix_predictions(train7, test7, 7)
 train8, test8 = fix_predictions(train8, test8, 8)
 train9, test9 = fix_predictions(train9, test9, 9)
 train10, test10 = fix_predictions(train10, test10, 10)
 train11, test11 = fix_predictions(train11, test11, 11)
 train12, test12 = fix_predictions(train12, test12, 12)
 train13, test13 = fix_predictions(train13, test13, 13)
 train14, test14 = fix_predictions(train14, test14, 14)
 train15, test15 = fix_predictions(train15, test15, 15)
 train16, test16 = fix_predictions(train16, test16, 16)
 train17, test17 = fix_predictions(train17, test17, 17)
 train18, test18 = fix_predictions(train18, test18, 18)
 train19, test19 = fix_predictions(train19, test19, 19)
 train = train.merge(train1, on = 'image_name' ).merge(train2, on = 'image_name' ).merge(train3, on = 'image_name' ).merge(train4, on = 'image_name' ).merge(train5, on = 'image_name' ).merge(train6, on = 'image_name' ).merge(train7, on = 'image_name' ).merge(train8, on = 'image_name' ).merge(train9, on = 'image_name' ).merge(train10, on = 'image_name' ).merge(train11, on = 'image_name' ).merge(train12, on = 'image_name' ).merge(train13, on = 'image_name' ).merge(train14, on = 'image_name' ).merge(train15, on = 'image_name' ).merge(train16, on = 'image_name' ).merge(train17, on = 'image_name' ).merge(train18, on = 'image_name' ).merge(train19, on = 'image_name')
 test = test.merge(test1, on = 'image_name' ).merge(test2, on = 'image_name' ).merge(test3, on = 'image_name' ).merge(test4, on = 'image_name' ).merge(test5, on = 'image_name' ).merge(test6, on = 'image_name' ).merge(test7, on = 'image_name' ).merge(test8, on = 'image_name' ).merge(test9, on = 'image_name' ).merge(test10, on = 'image_name' ).merge(test11, on = 'image_name' ).merge(test12, on = 'image_name' ).merge(test13, on = 'image_name' ).merge(test14, on = 'image_name' ).merge(test15, on = 'image_name' ).merge(test16, on = 'image_name' ).merge(test17, on = 'image_name' ).merge(test18, on = 'image_name' ).merge(test19, on = 'image_name')
 return train, test, sub
def feature_engineering(train, test):
trn_images = train['image_name'].values
trn_sizes = np.zeros(( trn_images.shape[0], 2))
for i, img_path in enumerate(tqdm(trn_images)) :
img = Image.open(os.path.join('/kaggle/input/siim-isic-melanoma-classification/jpeg/train/', f'{img_path}.jpg'))
trn_sizes[i] = np.array([img.size[0], img.size[1]])
 test_images = test['image_name'].values
test_sizes = np.zeros(( test_images.shape[0],2))
for i, img_path in enumerate(tqdm(test_images)) :
img = Image.open(os.path.join('/kaggle/input/siim-isic-melanoma-classification/jpeg/test/', f'{img_path}.jpg'))
test_sizes[i] = np.array([img.size[0],img.size[1]])
 train['w'] = trn_sizes[:,0]
train['h'] = trn_sizes[:,1]
test['w'] = test_sizes[:,0]
test['h'] = test_sizes[:,1]
return train, test
def encode_categorical(train, test):
for col in ['sex', 'anatom_site_general_challenge']:
encoder = preprocessing.LabelEncoder()
train[col].fillna('unknown', inplace = True)
 test[col].fillna('unknown', inplace = True)
 train[col] = encoder.fit_transform(train[col])
 test[col] = encoder.transform(test[col])
 train['patient_id'].fillna('unknown', inplace = True)
 return train, test
def train_and_evaluate_lgbm(train, test, params, verbose_eval, folds = 5):
features = [col for col in train.columns if col not in ['image_name', 'patient_id', 'diagnosis', 'benign_malignant', 'target', 'source']]
if verbose_eval != False:
print('Training with features: ', features)
 kf = GroupKFold(n_splits = folds)
 target = 'target'
oof_pred = np.zeros(len(train))
y_pred = np.zeros(len(test))
for fold,(tr_ind, val_ind)in enumerate(kf.split(train, groups = train['patient_id'])) :
if verbose_eval != False:
print('
')
 print('-'*50)
 print(f'Training fold {fold + 1}"')
 x_train, x_val = train[features].iloc[tr_ind], train[features].iloc[val_ind]
y_train, y_val = train[target][tr_ind], train[target][val_ind]
train_set = lgb.Dataset(x_train, y_train)
 val_set = lgb.Dataset(x_val, y_val)
 model = lgb.train(params, train_set, num_boost_round = 10000, early_stopping_rounds = 50,
valid_sets = [train_set, val_set], verbose_eval = verbose_eval)
 oof_pred[val_ind] = model.predict(x_val)
 y_pred += model.predict(test[features])/ kf.n_splits
rauc = metrics.roc_auc_score(train['target'], oof_pred)
 if verbose_eval != False:
print(f'Our oof roc auc score for our lgbm model is {rauc}')
 gc.collect()
return rauc, y_pred
train, test, sub = read_data()
train, test = feature_engineering(train, test)
 train, test = encode_categorical(train, test )
<normalization><EOS> 
def run_lgb_bayesian(num_leaves, learning_rate, max_depth, lambda_l1, lambda_l2, bagging_fraction, bagging_freq, colsample_bytree, colsample_bynode, min_data_per_leaf, min_sum_hessian_per_leaf):
params = {
'boosting_type': 'gbdt',
'metric': 'auc',
'objective': 'binary',
'n_jobs': -1,
'seed': SEED,
'num_leaves': int(num_leaves),
'learning_rate': learning_rate,
'max_depth': int(max_depth),
'lambda_l1': lambda_l1,
'lambda_l2': lambda_l2,
'bagging_fraction': bagging_fraction,
'bagging_freq': int(bagging_freq),
'colsample_bytree': colsample_bytree,
'colsample_bynode': colsample_bynode,
'min_data_per_leaf': int(min_data_per_leaf),
'min_sum_hessian_per_leaf': min_sum_hessian_per_leaf,
'verbose': 0
}
rauc, y_pred = train_and_evaluate_lgbm(train, test, params, False)
 return rauc
bounds_lgb = {
'num_leaves':(20, 500),
'learning_rate':(0.01, 0.2),
'max_depth':(8, 250),
'lambda_l1':(0, 3),
'lambda_l2':(0, 3),
'bagging_fraction':(0.4, 1),
'bagging_freq':(1, 10),
'colsample_bytree':(0.4, 1),
'colsample_bynode':(0.4, 1),
'min_data_per_leaf':(10, 100),
'min_sum_hessian_per_leaf':(0.0001, 0.01)
 }
lgb_bo = BayesianOptimization(run_lgb_bayesian, bounds_lgb, random_state = SEED)
 lgb_bo.maximize(init_points = 300, n_iter = 300, acq = 'ucb', xi = 0.0, alpha = 1e-6)
 params = {
'boosting_type': 'gbdt',
'metric': 'auc',
'objective': 'binary',
'n_jobs': -1,
'seed': SEED,
'num_leaves': int(lgb_bo.max['params']['num_leaves']),
'learning_rate': lgb_bo.max['params']['learning_rate'],
'max_depth': int(lgb_bo.max['params']['max_depth']),
'lambda_l1': lgb_bo.max['params']['lambda_l1'],
'lambda_l2': lgb_bo.max['params']['lambda_l2'],
'bagging_fraction': lgb_bo.max['params']['bagging_fraction'],
'bagging_freq': int(lgb_bo.max['params']['bagging_freq']),
'colsample_bytree': lgb_bo.max['params']['colsample_bytree'],
'colsample_bynode': lgb_bo.max['params']['colsample_bynode'],
'min_data_per_leaf': int(lgb_bo.max['params']['min_data_per_leaf']),
'min_sum_hessian_per_leaf': lgb_bo.max['params']['min_sum_hessian_per_leaf']
}
roc_auc, y_pred = train_and_evaluate_lgbm(train, test, params, 50)
 test['target'] = y_pred
sub = test[['image_name', 'target']]
sub.to_csv('lgbm_baseline_sub.csv', index = False )