Kaggle competition: Jane Street Market Prediction
<SOS> metric: JaneStreetPnl Kaggle data source: jane-street-market-prediction<filter>
START_TIME = time.time()
N_train=len(training_set[training_set['Country_Region']==my_country])
 N_train<filter>
train = pd.read_csv('.. /input/jane-street-market-prediction/train.csv')
 train = train.astype({c: np.float32 for c in train.select_dtypes(include='float64' ).columns})
 train.fillna(train.median() , inplace=True)
 train = train.query('weight > 0' ).reset_index(drop = True)
 train['action'] =(train['resp'] > 0 ).astype('int')
 features = [c for c in train.columns if 'feature' in c]
resp_cols = ['resp_1', 'resp_2', 'resp_3', 'resp', 'resp_4']
X = train[features].values
y = np.stack([(train[c] > 0 ).astype('int')for c in resp_cols] ).T
f_median = np.median(train[features[1:]].values, axis=0)
N_test=len(test_set[test_set['Country_Region']==my_country])
 N_test<prepare_output>
y_resps = train[resp_cols].values
y_actions = np.stack([(train[c] > 0 ).astype('int')for c in resp_cols] ).T
df_test = test_set.copy()
df_test['DsType']='test'
df_test['ConfirmedCases']=0
df_test['Fatalities']=0
df_test.info()<concatenate>
def create_model(input_dim, output_dims, add_models=0):
input_layer_0 = Input(input_dim)
 bn_0 = BatchNormalization()(input_layer_0)
 outputs_layer_0 = []
for m in range(2+add_models):
x = Dropout(0.2 )(bn_0)
 for i in range(m+1):
x = Dense(64 )(x)
 x = BatchNormalization()(x)
 x = Lambda(tf.keras.activations.swish )(x)
 x = Dropout(0.1 )(x)
 output = Dense(output_dims[3], activation='linear', name=f'level_0_output_{m}' )(x)
 outputs_layer_0.append(output)
 output_layer_0_average = Average(name='output_layer_0_average' )(outputs_layer_0)
 bn_1 = BatchNormalization()(output_layer_0_average)
 input_layer_1 = Concatenate()([bn_0] + [bn_1])
 outputs_layer_1 = []
for m in range(2+add_models):
x = Dropout(0.2 )(input_layer_1)
 for i in range(m+1):
x = Dense(64 )(x)
 x = BatchNormalization()(x)
 x = Lambda(tf.keras.activations.swish )(x)
 x = Dropout(0.1 )(x)
 output = Dense(output_dims[2], activation='sigmoid', name=f'level_1_output_{m}' )(x)
 outputs_layer_1.append(output)
 output_layer_1_average = Average(name='output_layer_1_average' )(outputs_layer_1)
 bn_2 = BatchNormalization()(output_layer_1_average)
 input_layer_2 = Concatenate()([bn_1] + [bn_2])
 outputs_layer_2 = []
for m in range(2+add_models):
x = Dropout(0.2 )(input_layer_2)
 for i in range(m+1):
x = Dense(64 )(x)
 x = BatchNormalization()(x)
 x = Lambda(tf.keras.activations.swish )(x)
 x = Dropout(0.1 )(x)
 output = Dense(output_dims[1], activation='linear', name=f'level_2_output_{m}' )(x)
 outputs_layer_2.append(output)
 output_layer_2_average = Average(name='output_layer_2_average' )(outputs_layer_2)
 bn_3 = BatchNormalization()(output_layer_2_average)
 input_layer_3 = Concatenate()([bn_2] + [bn_3])
 outputs_layer_3 = []
for m in range(2+add_models):
x = Dropout(0.2 )(input_layer_3)
 for i in range(m+1):
x = Dense(64 )(x)
 x = BatchNormalization()(x)
 x = Lambda(tf.keras.activations.swish )(x)
 x = Dropout(0.1 )(x)
 output = Dense(output_dims[1], activation='sigmoid', name=f'level_3_output_{m}' )(x)
 outputs_layer_3.append(output)
 output_layer_3_average = Average(name='output_layer_3_average' )(outputs_layer_3)
 model = Model(inputs=input_layer_0, outputs=[output_layer_3_average, output_layer_2_average, output_layer_1_average, output_layer_0_average])
 loss = {}
loss['output_layer_3_average'] = BinaryCrossentropy(label_smoothing = 0.1)
 loss['output_layer_2_average'] = 'mse'
loss['output_layer_1_average'] = BinaryCrossentropy(label_smoothing = 0.1)
 loss['output_layer_0_average'] = 'mse'
loss_weights={}
loss_weights['output_layer_3_average'] =.25
loss_weights['output_layer_2_average'] =.25
loss_weights['output_layer_1_average'] =.25
loss_weights['output_layer_0_average'] =.25
metrics = {}
metrics['output_layer_3_average'] = tf.keras.metrics.AUC(name = 'auc')
 metrics['output_layer_2_average'] = 'mse'
metrics['output_layer_1_average'] = tf.keras.metrics.AUC(name = 'auc')
 metrics['output_layer_0_average'] = 'mse'
model.compile(optimizer = Adam() , loss = loss, metrics = metrics, loss_weights=loss_weights)
 return model
df_union=pd.concat([df_train,df_test],sort=False ).copy()
df_union.fillna('ND', inplace = True )<feature_engineering>
epochs = 50
batch_size = 1024 * 4
verbose = True
objective = 'val_output_layer_3_average_auc'
objective = 'output_layer_3_average_auc'
direction = 'max'
tr =(0, 400)
 te =(420, 500)
 train_indices = train[(train.date >= tr[0])&(train.date < tr[1])].index
test_indices = train[(train.date >= te[0])&(train.date < te[1])].index
model = create_model(input_dim=130, output_dims=(1,1,5,5), add_models=3)
 X_train, X_test = X[train_indices], X[test_indices]
y_train =(y_actions[train_indices][:,3], y_resps[train_indices][:,3], y_actions[train_indices], y_resps[train_indices])
 y_test =(y_actions[test_indices][:,3], y_resps[test_indices][:,3], y_actions[test_indices], y_resps[test_indices])
 rlr = ReduceLROnPlateau(monitor = objective, factor = 0.5, patience = 4, verbose = 1, min_delta = 1e-4, mode = direction)
 es = EarlyStopping(objective, patience=21, restore_best_weights=True, mode=direction)
 h = model.fit(X_train, y_train, epochs=epochs, batch_size=batch_size, verbose=verbose, callbacks = [es, rlr])
 model.save_weights('./model.hdf5')
 metrics = model.evaluate(X_test, y_test, batch_size=batch_size)
 print(metrics )
df_union['Month']=df_union['Date'].apply(lambda s : int(s.replace('-','')[4:6]))
df_union['Day']=df_union['Date'].apply(lambda s : int(s.replace('-','')[6:9]))
df_union['Date']=df_union['Date'].apply(lambda s : datetime.datetime.strptime(s, '%Y-%m-%d'))
df_union['DateOrd']=df_union['Date'].apply(lambda s : s.toordinal())
 df_union['Province_Norm']=df_union['Country_Region']+'-'+df_union['Province_State']
df_union.tail()<categorify>
tf.keras.utils.plot_model(model, to_file=f'model.png', show_shapes=True )
le1 = preprocessing.LabelEncoder()
le1.fit(df_union['Country_Region'])
 df_union['Country'] = le1.transform(df_union['Country_Region'] )<categorify>
pred = model.predict(X_test, batch_size=batch_size, verbose=True )
le2 = preprocessing.LabelEncoder()
le2.fit(df_union['Province_Norm'])
 df_union['Province'] = le2.transform(df_union['Province_Norm'])
 df_union.drop('Province_Norm',axis=1,inplace=True )<concatenate>
env = janestreet.make_env()
iter_test = env.iter_test()
def cut_neg(y):
m = max(0,np.amin(y))
for i in range(0,len(y)) :
if y[i]<m : y[i]=m
return y<predict_on_test>
selected_models = [model]
<find_best_model_class><EOS> 
start = time.time()
th = 0.5
j = 0
for(test_df, pred_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_median
try:
pred = model(x_tt, training=False)[2].numpy().flatten()
pred = np.median(pred)
 pred_df.action = np.where(pred >= th, 1, 0 ).astype(int)
 except:
pred_df.action = 0
else:
pred_df.action = 0
env.predict(pred_df)
 j +=1
total = time.time() - start
print(f'Expected time for 1M: { total * 1000000 /(j*60*60+1):.2} hours')
 print(f'Iters per second: {j/total:.1f} iter/s')
 print(f'Global time: {(time.time() - START_TIME)/ 60:.1f} minutes' )