Kaggle competition: Jane Street Market Prediction
<SOS> metric: JaneStreetPnl Kaggle data source: jane-street-market-prediction<import_modules>
from tensorflow.keras.layers import Input, Dense, BatchNormalization, Dropout, Concatenate, Lambda, GaussianNoise, Activation
from tensorflow.keras.models import Model, Sequential
from tensorflow.keras.losses import BinaryCrossentropy
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.callbacks import EarlyStopping
from tensorflow.keras.layers.experimental.preprocessing import Normalization
from keras.callbacks import ModelCheckpoint
import tensorflow as tf
import numpy as np
import pandas as pd
from tqdm import tqdm
from random import choices
import dask.dataframe as dd
from glob import glob
import os
class Metrics(Callback):
def on_train_begin(self, logs={}):
self.val_f1s = []
def _threshold_search(self,y_true, y_proba):
best_threshold = 0
best_score = 0
for threshold in [i * 0.01 for i in range(100)]:
score = f1_score(y_true=y_true, y_pred=y_proba > threshold)
 if score > best_score:
best_threshold = threshold
best_score = score
search_result = {'threshold': best_threshold, 'f1': best_score}
return search_result
def on_epoch_end(self, epoch, logs={}):
y_val = self.validation_data[1]
y_pred = self.model.predict(self.validation_data[0])
 with warnings.catch_warnings() :
warnings.simplefilter("ignore")
 search_result = self._threshold_search(y_val, y_pred)
 _val_f1 = search_result['f1']
self.val_f1s.append(_val_f1)
 print('— val_f1_on_epoch_end: %f '%(_val_f1))
return
def get_model(trainable=False):
inp1 = Input(shape=(max_len,))
emb = Embedding(num_words, dim, weights=[matrix],trainable = trainable, )(inp1)
 filters = []
for f in [3,4,5]:
conv = Conv1D(64,f, activation='elu' )(emb)
 pool = GlobalMaxPooling1D()(conv)
 filters.append(pool)
 x = concatenate(filters)
 x = Dense(max_len, activation="relu" )(x)
 x = Dense(1, activation='sigmoid' )(x)
 model = Model(inputs=inp1, outputs=x)
 model.compile(loss='binary_crossentropy', optimizer=optimizers.Adam(lr=1e-3), metrics=['accuracy'])
 return model
batch_size = 512
print('Batch size = ',batch_size)
 patience = 2
best_model=None
all_results = {}
train_meta = np.zeros(y_train.shape)
 test_meta = np.zeros(x_test.shape[0])
 splits = list(StratifiedKFold(n_splits=5, shuffle=True, random_state=14 ).split(x_train, y_train))
for idx,(train_idx, valid_idx)in enumerate(splits):
print('----'+str(idx)+'-----')
 X_train1 = x_train[train_idx]
y_train1 = y_train[train_idx]
X_val = x_train[valid_idx]
y_val = y_train[valid_idx]
model_file = 'model_'+str(idx)+'.h5'
modelcheck = ModelCheckpoint(model_file,save_best_only=True)
 stop = EarlyStopping(patience=patience)
 metrics = Metrics()
model = get_model()
history = model.fit(X_train1,y_train1,
batch_size=batch_size,
validation_data=(X_val,y_val),
epochs=100,
callbacks=[modelcheck,stop,metrics],
verbose=2)
 print('Pretraining finished, unfreezing embeddings layer...')
 model = get_model(trainable=True)
 model.load_weights(model_file)
 modelcheck = ModelCheckpoint(model_file,save_best_only=True)
 stop = EarlyStopping(patience=patience)
 metrics = Metrics()
history = model.fit(X_train1,y_train1,
batch_size=batch_size,
validation_data=(X_val,y_val),
epochs=1,
callbacks=[modelcheck,stop,metrics],
verbose=2)
 print('training finished...')
 model.load_weights(model_file)
 y_pred = model.predict(X_val,batch_size=batch_size, verbose=1)
 train_meta[valid_idx] = y_pred.reshape(-1)
 search_result = threshold_search(y_val, y_pred)
 print(search_result)
 y_pred = y_pred>search_result['threshold']
y_pred = y_pred.astype(int)
 print('RESULTS ON VALIDATION SET:
',classification_report(y_val,y_pred))
all_results[model_file] = search_result['f1']
y_pred = model.predict(x_test,batch_size=batch_size, verbose=1)
 test_meta += y_pred.reshape(-1)/ len(splits)
 if best_model is None or best_model['f1'] < search_result['f1']:
best_model={'model':model_file,'f1':search_result['f1']}
print('-'*80)
 print(all_results)
 print('-'*80)
 print(best_model)
 print('-'*80 )<save_to_csv>
def create_mlp(
 num_columns, num_labels, hidden_units, dropout_rates, label_smoothing, learning_rate 
):
inp = tf.keras.layers.Input(shape=(num_columns,))
x = tf.keras.layers.BatchNormalization()(inp)
 x = tf.keras.layers.Dropout(dropout_rates[0] )(x)
 for i in range(len(hidden_units)) :
x = tf.keras.layers.Dense(hidden_units[i] )(x)
 x = tf.keras.layers.BatchNormalization()(x)
 x = tf.keras.layers.Activation(tf.keras.activations.swish )(x)
 x = tf.keras.layers.Dropout(dropout_rates[i + 1] )(x)
 x = tf.keras.layers.Dense(num_labels )(x)
 out = tf.keras.layers.Activation("sigmoid" )(x)
 model = tf.keras.models.Model(inputs=inp, outputs=out)
 model.compile(
 optimizer=tf.keras.optimizers.Adam(learning_rate=learning_rate),
loss=tf.keras.losses.BinaryCrossentropy(label_smoothing=label_smoothing),
metrics=tf.keras.metrics.AUC(name="AUC"), 
)
 return model
search_result = threshold_search(y_train, train_meta)
 print(search_result)
 df_subm = pd.DataFrame()
df_subm['qid'] = df_final.qid
df_subm['prediction'] = test_meta > search_result['threshold']
print(df_subm.head())
 df_subm.to_csv('submission.csv', index=False )<import_modules>
data = dd.read_parquet('.. /input/janestreetparquetdata/date*.parquet')
 features = ['feature_{}'.format(i)for i in range(130)]
resp_cols = ['resp_1', 'resp_2', 'resp_3', 'resp', 'resp_4']
train = data.compute()
train = train.query('date > 85' ).reset_index(drop = True)
 train = train[train['weight'] != 0]
f_mean = train[features].mean().values
train = train.dropna()
X_train = train[features].values
y_train =(train[resp_cols]> 0 ).astype(int ).values
tqdm.pandas()<load_from_csv>
SEED = 1111
tf.random.set_seed(SEED)
 np.random.seed(SEED)
 hidden_units = [150, 150, 150]
dropout_rates = [0.2, 0.2, 0.2, 0.2]
label_smoothing = 1e-2
learning_rate = 1e-3
epochs = 250
batch_size = 5000
save_every_n_epochs = 10
save_freq =(len(X_train)//batch_size)*save_every_n_epochs
clf = create_mlp(
 len(features), len(resp_cols), hidden_units, dropout_rates, label_smoothing, learning_rate 
)
 checkpoint_path = "./cp-{epoch:04d}.ckpt"
checkpoint = ModelCheckpoint(checkpoint_path,save_weights_only=True,save_freq=save_freq)
 clf.fit(X_train, y_train, epochs=epochs, batch_size=batch_size,callbacks=[checkpoint])
 clf.save_weights('./SimpleMLP.ckpt' )
train_df = pd.read_csv(".. /input/train.csv")
 test_df = pd.read_csv(".. /input/test.csv")
 print('Train data dimension: ', train_df.shape)
 display(train_df.head())
 print('Test data dimension: ', test_df.shape)
 display(test_df.head() )<set_options>
class TrainData(Dataset):
def __init__(self,file_name,root_dir,predict=False):
self.file_name = file_name
self.root_dir = root_dir
self.feature = ['feature_{}'.format(i)for i in range(130)]
self.resp = ['resp_{}'.format(i)for i in range(1,5)]+['resp']
self.prediction = predict
def __len__(self):
return len(glob(os.path.join(self.root_dir,'*.parquet')))
 def __getitem__(self, idx):
data = pd.read_parquet(os.path.join(self.root_dir,self.file_name+'_{}.parquet'.format(idx)))
 X = data[self.feature].values
W = data['weight'].values
R = data['resp'].values
return X,W,R
def seed_everything(seed=1234):
random.seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 np.random.seed(seed)
 torch.manual_seed(seed)
 torch.cuda.manual_seed(seed)
 torch.backends.cudnn.deterministic = True
seed_everything()<compute_test_metric>
dataset = TrainData('date','.. /input/janestreetparquetdata/',predict = True)
 weight_path = glob('./*.ckpt.index')
 weight_path = [os.path.basename(each ).split('.')[0] for each in weight_path]
weight_path.sort()
for path in weight_path:
clf.load_weights('./{}.ckpt'.format(path))
p = []
for i in range(len(dataset)) :
net_input,weight,resp= dataset[i]
net_input = np.nan_to_num(net_input)+f_mean*(np.isnan(net_input ).astype(int))
net_prediction = clf.predict(net_input)
 pre =(np.median(net_prediction,axis=1)>0.5 ).astype(int)
 p.append(( weight*resp*pre ).sum())
 result = pd.DataFrame(data={'p':p})
 result ['p2'] = result['p']**2
t =(result['p'].sum() /np.sqrt(result['p2'].sum())) *(np.sqrt(250/len(dataset)))
 print(path,min(max(t,0),6)*result['p'].sum() )
def threshold_search(y_true, y_proba):
best_threshold = 0
best_score = 0
for threshold in tqdm([i * 0.01 for i in range(100)]):
score = f1_score(y_true=y_true, y_pred=y_proba > threshold)
 if score > best_score:
best_threshold = threshold
best_score = score
search_result = {'threshold': best_threshold, 'f1': best_score}
return search_result<compute_test_metric>
selection = 'cp-0200'
clf.load_weights('./{}.ckpt'.format(selection))
<define_variables><EOS> 
env = janestreet.make_env()
th = 0.5
for(test_df, pred_df)in tqdm(env.iter_test()):
if test_df['weight'].item() > 0:
x_tt = test_df.loc[:, features].values
x_tt = np.nan_to_num(x_tt)+f_mean*(np.isnan(x_tt ).astype(int))
pred = np.median(clf(x_tt, training=False))
pred_df.action = np.where(pred >= th, 1, 0 ).astype(int)
 else:
pred_df.action = 0
env.predict(pred_df )