Kaggle competition: Jane Street Market Prediction
avg_tta_score('bd-efficientnet-b03-2015val-psu3-sz-256' )<choose_model_class>
train = pd.read_csv('/kaggle/input/jane-street-market-prediction/train.csv', nrows=2000000)
 train.info()
seed_everything(420)
 data = get_data(seed=420, size=300, bs=64)
 learn = Learner(data, model, model_dir=".", callback_fns=[BnFreeze])
 make_or_preds(
 model_name='bd-efficientnet-b03-2015val-psu3-sz-300',
learner=learn,
model_path='.. /input/bd-efficientnet-b03-2015val-psu3-sz-300/best_model',
expected_val=0.7433966054032339
)<choose_model_class>
train = train[train['weight']!=0]
train['action'] = train['resp'].apply(lambda x:x>0 ).astype(int )
seed_everything(420)
 data = get_data(seed=420, size=300, bs=64, tfms=((),(flip_lr(p=1))))
learn = Learner(data, model, model_dir=".", callback_fns=[BnFreeze])
 make_or_preds(
 model_name='bd-efficientnet-b03-2015val-psu3-sz-300-flip',
learner=learn,
model_path='.. /input/bd-efficientnet-b03-2015val-psu3-sz-300/best_model',
expected_val=0.7433966054032339
)<compute_test_metric>
features = [col for col in list(train.columns)if 'feature' in col]
avg_tta_score('bd-efficientnet-b03-2015val-psu3-sz-300' )<load_pretrained>
X = train[features]
y = train['action']
train_x, valid_x, train_y, valid_y = train_test_split(X, y, test_size=0.2 )
data = get_data(seed=0, size=IMG_SIZE, bs=BS)
 learn = cnn_learner(data, models.densenet201, model_dir=".", lin_ftrs=[2048], callback_fns=[BnFreeze], pretrained=False)
 make_or_preds(
 model_name='bd-densenet201-2015val-psu3-2019-val',
learner=learn,
model_path='.. /input/bd-densenet201-2015val-psu3-2019-val/best_model',
expected_val=0.690059677041891
)<choose_model_class>
missing_values = pd.DataFrame()
missing_values['feature'] = features
missing_values['num_missing'] = [train_x[i].isna().sum() for i in features]
missing_values.T
data = get_data(seed=0, size=IMG_SIZE, bs=BS, tfms=((),(flip_lr(p=1))))
learn = cnn_learner(data, models.densenet201, model_dir=".", lin_ftrs=[2048], callback_fns=[BnFreeze], pretrained=False)
 make_or_preds(
 model_name='bd-densenet201-2015val-psu3-2019-val-flip',
learner=learn,
model_path='.. /input/bd-densenet201-2015val-psu3-2019-val/best_model',
expected_val=0.690059677041891
)<compute_test_metric>
train_median = train_x.median()
train_x = train_x.fillna(train_median)
 valid_x = valid_x.fillna(train_median )
avg_tta_score('bd-densenet201-2015val-psu3-2019-val' )<load_from_csv>
scaler = StandardScaler()
scaler.fit(train_x)
 train_x_norm = scaler.transform(train_x)
 pca = PCA()
comp = pca.fit(train_x_norm)
 plt.plot(np.cumsum(comp.explained_variance_ratio_))
plt.grid()
plt.xlabel('Number of Principal Components')
 plt.ylabel('Explained Variance')
 sns.despine() ;
columns = ['x_0', 'x_1', 'x_2', 'x_3', 'x_4']
eff_b3_300_val_preds_no_flip = pd.read_csv('bd-efficientnet-b03-2015val-psu3-sz-300_val_preds.csv' ).sort_values('id_code')
 eff_b3_300_val_preds_flip = pd.read_csv('bd-efficientnet-b03-2015val-psu3-sz-300-flip_val_preds.csv' ).sort_values('id_code')
 eff_b3_300_val_preds =(eff_b3_300_val_preds_flip[columns] + eff_b3_300_val_preds_no_flip[columns])/ 2
eff_b3_256_val_preds_no_flip = pd.read_csv('bd-efficientnet-b03-2015val-psu3-sz-256_val_preds.csv' ).sort_values('id_code')
 eff_b3_256_val_preds_flip = pd.read_csv('bd-efficientnet-b03-2015val-psu3-sz-256_val_preds.csv' ).sort_values('id_code')
 eff_b3_256_val_preds =(eff_b3_256_val_preds_flip[columns] + eff_b3_256_val_preds_no_flip[columns])/ 2
densenet_201_val_preds_no_flip = pd.read_csv('bd-densenet201-2015val-psu3-2019-val_val_preds.csv' ).sort_values('id_code')
 densenet_201_val_preds_flip = pd.read_csv('bd-densenet201-2015val-psu3-2019-val-flip_val_preds.csv' ).sort_values('id_code')
 densenet_201_val_preds =(densenet_201_val_preds_flip[columns] + densenet_201_val_preds_no_flip[columns])/ 2<define_variables>
pca = PCA(n_components=50 ).fit(train_x_norm)
 train_x_transform = pca.transform(train_x_norm )
val_preds_avg =(
 eff_b3_300_val_preds.values * 0.4 +
eff_b3_256_val_preds.values * 0.4 +
densenet_201_val_preds.values * 0.2
)<find_best_params>
valid_x_transform = pca.transform(scaler.transform(valid_x))
preds = []
thres = []
for i in np.arange(0, 1, 0.005):
thres.append(i)
 preds.append(cohen_kappa_score(
 eff_b3_300_val_preds_flip.sort_values('id_code' ).diagnosis,
get_output_preds(( val_preds_avg > i)) , weights='quadratic'))<sort_values>
dtrain = xgb.DMatrix(train_x_transform, label=train_y)
 dvalid = xgb.DMatrix(valid_x_transform, label=valid_y )
val_preds_and_threshold = sorted(list(zip(preds, thres)) , key=lambda x: x[0], reverse=True )<define_variables>
def objective(trial):
params = {
'n_estimators': trial.suggest_int('n_estimators', 200, 600),
'max_depth': trial.suggest_int('max_depth', 10, 25),
'learning_rate': trial.suggest_uniform('learning_rate', 0.01, 0.1),
'subsample': trial.suggest_uniform('subsample', 0.50, 1),
'colsample_bytree': trial.suggest_uniform('colsample_bytree', 0.50, 1),
'gamma': trial.suggest_int('gamma', 0, 10),
'tree_method': 'gpu_hist',
'objective': 'binary:logistic'
}
bst = xgb.train(params, dtrain)
 preds = bst.predict(dvalid)
 pred_labels = np.rint(preds)
 accuracy = sklearn.metrics.accuracy_score(valid_y, pred_labels)
 return accuracy
best_thresh = 0.45<load_from_csv>
if __name__ == "__main__":
study = optuna.create_study(direction="maximize")
 study.optimize(objective, n_trials=25, timeout=600)
 print("Number of finished trials: ", len(study.trials))
print("Best trial:")
 trial = study.best_trial
print(" Value: {}".format(trial.value))
print(" Params: ")
 for key, value in trial.params.items() :
print(" {}: {}".format(key, value))
columns = ['x_0', 'x_1', 'x_2', 'x_3', 'x_4']
eff_b3_300_test_preds_no_flip = pd.read_csv('bd-efficientnet-b03-2015val-psu3-sz-300_test_preds.csv')
 eff_b3_300_test_preds_flip = pd.read_csv('bd-efficientnet-b03-2015val-psu3-sz-300-flip_test_preds.csv')
 eff_b3_300_test_preds =(eff_b3_300_test_preds_flip[columns] + eff_b3_300_test_preds_no_flip[columns])/ 2
eff_b3_256_test_preds_no_flip = pd.read_csv('bd-efficientnet-b03-2015val-psu3-sz-256_test_preds.csv')
 eff_b3_256_test_preds_flip = pd.read_csv('bd-efficientnet-b03-2015val-psu3-sz-256-flip_test_preds.csv')
 eff_b3_256_test_preds =(eff_b3_256_test_preds_flip[columns] + eff_b3_256_test_preds_no_flip[columns])/ 2
densenet_201_test_preds_no_flip = pd.read_csv('bd-densenet201-2015val-psu3-2019-val_test_preds.csv' ).sort_values('id_code')
 densenet_201_test_preds_flip = pd.read_csv('bd-densenet201-2015val-psu3-2019-val-flip_test_preds.csv' ).sort_values('id_code')
 densenet_201_test_preds =(densenet_201_test_preds_flip[columns] + densenet_201_test_preds_no_flip[columns])/ 2<define_variables>
best_params = trial.params
best_params['tree_method'] = 'gpu_hist'
best_params['objective'] = 'binary:logistic'
test_preds_avg =(
 eff_b3_300_test_preds.values * 0.4 +
eff_b3_256_test_preds.values * 0.4 +
densenet_201_test_preds.values * 0.2
)<predict_on_test>
optimal_clf = xgb.XGBClassifier(**best_params )
sample_df.diagnosis = get_output_preds(( test_preds_avg > best_thresh))<save_to_csv>
optimal_clf.fit(train_x_transform, train_y )
sample_df.to_csv('submission.csv', index=False )<count_values>
def fillna_npwhere(array, values):
if np.isnan(array.sum()):
array = np.where(np.isnan(array), values, array)
 return array
pd.read_csv('submission.csv' ).diagnosis.value_counts()<define_variables>
env = janestreet.make_env()
iter_test = env.iter_test()
<install_modules><EOS> 
for(test_df, sample_prediction_df)in iter_test:
wt = test_df.iloc[0].weight
if(wt == 0):
sample_prediction_df.action = 0
else:
sample_prediction_df.action = optimal_clf.predict(pca.transform(scaler.transform(fillna_npwhere(test_df[features].values,train_median[features].values))))
env.predict(sample_prediction_df )