Kaggle competition: Jane Street Market Prediction
data_generator = create_datagen().flow(x_train, y_train, batch_size=BATCH_SIZE )<train_model>
import pandas as pd
import gc
from sklearn.preprocessing import MinMaxScaler
from sklearn.model_selection import StratifiedKFold
import tensorflow as tf
history = model.fit_generator(
 data_generator,
steps_per_epoch=x_train.shape[0] / BATCH_SIZE,
epochs=20,
validation_data=(x_val, y_val),
callbacks=[kappa_metrics,learn_control,checkpoint]
)<predict_on_test>
train = pd.read_csv('/kaggle/input/jane-street-market-prediction/train.csv')
 train = reduce_mem_usage(train)
 features = [c for c in train.columns if 'feature' in c]
NAN_VALUE = -999
model.load_weights('val_model.h5')
 pred_val = model.predict(x_val )<compute_train_metric>
train = train.astype({c: np.float32 for c in train.select_dtypes(include='float16' ).columns})
 train = train.fillna(train.mean())
 f_mean = np.mean(train[features[1:]].values,axis=0)
 train = train.query('date > 85' ).reset_index(drop = True)
 train = train[train.weight != 0]
n_folds = 5
seed = 2020
skf = StratifiedKFold(n_splits=n_folds, shuffle=False)
 resp_cols = ['resp', 'resp_1', 'resp_2', 'resp_3', 'resp_4']
X_train = train[train['date'] < 400][features]
X_test = train[train['date'] >= 400][features]
y_train = np.stack([(train[train['date'] < 400][c] > 0 ).astype('int')for c in resp_cols] ).T
y_test = np.stack([(train[train['date'] >= 400][c] > 0 ).astype('int')for c in resp_cols] ).T
X_train = train[features]
y_train = np.stack([(train[c] > 0 ).astype('int')for c in resp_cols] ).T
def compute_score_inv(threshold):
y1 = pred_val > threshold
y1 = y1.astype(int ).sum(axis=1)- 1
y2 = y_val.sum(axis=1)- 1
score = cohen_kappa_score(y1, y2, weights='quadratic')
 return 1 - score
simplex = scipy.optimize.minimize(compute_score_inv, 0.5, method='nelder-mead')
 best_threshold = simplex['x'][0]
print(best_threshold)
 gc.collect()<install_modules>
TUNNING = False
def create_model(hp,input_dim,output_dim):
inputs = tf.keras.layers.Input(input_dim)
 x = tf.keras.layers.BatchNormalization()(inputs)
 x = tf.keras.layers.GaussianNoise(hp.Choice('noise',[0.0,0.03,0.05]))(x)
 x = tf.keras.layers.Dropout(hp.Choice('init_dropout',[0.0,0.3,0.5]))(x)
 x = tf.keras.layers.Dense(hp.Int('num_units_1', 128, 2048, 64), activation=hp.Choice('activation_1', ['tanh','relu','swish']))(x)
 x = tf.keras.layers.Dropout(hp.Choice(f'dropout_1',[0.0,0.3,0.5]))(x)
 x = tf.keras.layers.Dense(hp.Int('num_units_2', 128, 1024, 32), activation=hp.Choice('activation_2', ['tanh','relu','swish']))(x)
 x = tf.keras.layers.Dropout(hp.Choice(f'dropout_2',[0.0,0.3,0.5]))(x)
 x = tf.keras.layers.Dense(output_dim, activation='sigmoid' )(x)
 model = tf.keras.models.Model(inputs=inputs,outputs=x)
 model.compile(optimizer=tf.keras.optimizers.Adam(hp.Choice('lr',[1e-2, 1e-3, 1e-5])) ,loss=tf.keras.losses.BinaryCrossentropy(label_smoothing=hp.Choice('label_smoothing',[0.0, 0.01, 0.1])) ,metrics=[tf.keras.metrics.AUC(name = 'auc')])
 return model
model = tf.keras.Sequential([
tf.keras.Input(shape = len(features)) ,
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.GaussianNoise(0.05),
tf.keras.layers.Dropout(0.3),
tf.keras.layers.Dense(256, activation='tanh'),
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Dropout(0.3),
tf.keras.layers.Dense(64, activation='tanh'),
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Dropout(0.3),
tf.keras.layers.Dense(256, activation='tanh'),
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Dropout(0.3),
tf.keras.layers.Dense(5, activation = 'sigmoid')
 ])
 EPOCHS = 500
BATCH_SIZE = 4096
if TUNNING:
EPOCHS = 50
MAX_TRIAL = 20
model_fn = lambda hp: create_model(hp, X_train.shape[-1], y_train.shape[-1])
 tuner = kt.tuners.BayesianOptimization(model_fn, kt.Objective('val_auc', direction='max'), MAX_TRIAL, seed = 2020)
 tuner.search(X_train, y_train, epochs=EPOCHS, validation_data=(X_test, y_test),callbacks=[tf.keras.callbacks.EarlyStopping(monitor='val_loss', patience = 10, restore_best_weights=True)])
 model = tuner.get_best_models() [0]
else:
callback = tf.keras.callbacks.EarlyStopping(monitor='val_loss', patience=30, restore_best_weights=True)
 optimizer = tf.keras.optimizers.Adam(learning_rate=1e-3)
 loss=tf.keras.losses.BinaryCrossentropy(label_smoothing=1e-2)
 model.compile(loss = loss, optimizer=optimizer, metrics=[tf.keras.metrics.AUC() ])
 history = model.fit(X_train, y_train, batch_size=BATCH_SIZE, epochs=EPOCHS)
!pip install -U '.. /input/install/efficientnet-0.0.3-py2.py3-none-any.whl'<define_variables>
env = janestreet.make_env()
iter_test = env.iter_test()
for(test_df, sample_prediction_df)in tqdm(iter_test):
if test_df['weight'].item() > 0:
x_tt = test_df.loc[:, features].values
if np.isnan(x_tt[:, 1:].sum()):
x_tt[:, 1:] = np.nan_to_num(x_tt[:, 1:])+ np.isnan(x_tt[:, 1:])* f_mean
action = np.mean(model(x_tt, training = False ).numpy() [0])
 if(action > 0.5):
sample_prediction_df.action = 1
else:
sample_prediction_df.action = 0
else:
sample_prediction_df.action = 0
env.predict(sample_prediction_df )