Kaggle competition: Jane Street Market Prediction
<SOS> metric: JaneStreetPnl Kaggle data source: jane-street-market-prediction<choose_model_class>
SEED = 1111
np.random.seed(SEED)
 tf.random.set_seed(SEED)
 print('Loading data...')
 train_datatable = dt.fread('/kaggle/input/jane-street-market-prediction/train.csv')
 train = train_datatable.to_pandas()
del train_datatable
display(train )
inp = Input(shape=(maxlen,))
x = Embedding(max_features, embed_size )(inp)
 x = Bidirectional(CuDNNGRU(64, return_sequences=True))(x)
 x = GlobalMaxPool1D()(x)
 x = Dense(16, activation="relu" )(x)
 x = Dropout(0.1 )(x)
 x = Dense(1, activation="sigmoid" )(x)
 model = Model(inputs=inp, outputs=x)
 model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
 print(model.summary() )<train_model>
print('Data preprocessing...')
 train = train.query('date > 85' ).reset_index(drop = True)
 train = train[train['weight'] > 0]
features = []
for item in train.columns:
if 'feature' in item:
features.append(item)
 features_mean = train.loc[:, features].mean()
train.fillna(train.mean() ,inplace=True)
 print('Done!' )
model.fit(train_X, train_y, batch_size=512, epochs=4, validation_data=(val_X, val_y))<predict_on_test>
VALID_DAYS = 0
resp_cols = ['resp_1', 'resp_2', 'resp_3', 'resp_4', 'resp']
print('Make NN sample...')
 X_train = train[train['date'] <= 499-VALID_DAYS]
y_train = np.stack([(X_train[c]>0 ).astype('int')for c in resp_cols] ).T
X_train = X_train.loc[:, features].values
print(X_train.shape, y_train.shape)
 X_valid = train[train['date'] > 499-VALID_DAYS]
y_valid = np.stack([(X_valid[c]>0 ).astype('int')for c in resp_cols] ).T
X_valid = X_valid.loc[:, features].values
print(X_valid.shape, y_valid.shape)
 Make RNN sample...')
 print('Done!' )
pred_noemb_val_y = model.predict([val_X], batch_size=1024, verbose=1)
 for thresh in np.arange(0.1, 0.501, 0.01):
thresh = np.round(thresh, 2)
 print("F1 score at threshold {0} is {1}".format(thresh, metrics.f1_score(val_y,(pred_noemb_val_y>thresh ).astype(int))))<predict_on_test>
print('Create NN models...')
 HIDDEN_LAYER_1 = [256, 256]
HIDDEN_LAYER_2 = [160, 160, 160]
HIDDEN_LAYER_3 = [128, 128, 128, 128]
TARGET_NUM = 5
input = tf.keras.layers.Input(shape=(X_train.shape[1],))
x1 = tf.keras.layers.BatchNormalization()(input)
 x1 = tf.keras.layers.Dropout(0.25 )(x1)
 for units in HIDDEN_LAYER_1:
x1 = tf.keras.layers.Dense(units )(x1)
 x1 = tf.keras.layers.BatchNormalization()(x1)
 x1 = tf.keras.layers.Activation(tf.keras.activations.swish )(x1)
 x1 = tf.keras.layers.Dropout(0.25 )(x1)
 x2 = tf.keras.layers.BatchNormalization()(input)
 x2 = tf.keras.layers.Dropout(0.25 )(x2)
 for units in HIDDEN_LAYER_2:
x2 = tf.keras.layers.Dense(units )(x2)
 x2 = tf.keras.layers.BatchNormalization()(x2)
 x2 = tf.keras.layers.Activation(tf.keras.activations.swish )(x2)
 x2 = tf.keras.layers.Dropout(0.25 )(x2)
 x3 = tf.keras.layers.BatchNormalization()(input)
 x3 = tf.keras.layers.Dropout(0.25 )(x3)
 for units in HIDDEN_LAYER_3:
x3 = tf.keras.layers.Dense(units )(x3)
 x3 = tf.keras.layers.BatchNormalization()(x3)
 x3 = tf.keras.layers.Activation(tf.keras.activations.swish )(x3)
 x3 = tf.keras.layers.Dropout(0.25 )(x3)
 x = tf.keras.layers.concatenate([x1, x2, x3])
 x = tf.keras.layers.Dense(TARGET_NUM )(x)
 output = tf.keras.layers.Activation("sigmoid" )(x)
 model = tf.keras.models.Model(inputs=input, outputs=output)
 model.compile(
 optimizer = tfa.optimizers.RectifiedAdam(learning_rate=1e-3),
metrics = tf.keras.metrics.AUC(name="AUC"),
loss = tf.keras.losses.BinaryCrossentropy(label_smoothing=1e-2), 
)
 display(plot_model(model))
print('Done!' )
pred_noemb_test_y = model.predict([test_X], batch_size=1024, verbose=1 )<set_options>
print('Train NN...')
 history = model.fit(
 x = X_train,
y = y_train,
epochs=25,
batch_size=4096,
validation_data=(X_valid, y_valid), 
)
 models = []
models.append(model)
 print('Done!' )
del model, inp, x
time.sleep(10 )<train_model>
THRESHOLD = 0.502
janestreet.make_env.__called__ = False
env = janestreet.make_env()
print('predicting...')
 for(test_df, pred_df)in tqdm(env.iter_test()):
if test_df['weight'].item() > 0:
X_test = test_df.loc[:, features].values
if np.isnan(X_test.sum()):
X_test = np.nan_to_num(X_test)+ np.isnan(X_test)* features_mean.values
pred = model(X_test, training = False ).numpy()
pred = np.mean(pred)
 pred_df.action = np.where(pred >= THRESHOLD, 1, 0 ).astype(int)
 else:
pred_df.action = 0
env.predict(pred_df )