Kaggle competition: Jane Street Market Prediction
n_head = 5
d_lim1 = 0
d_lim2 = 10
sel_var = 'Fatalities'
sel_cols = ['Country_Region','Province_State',sel_var+'_x','Delta'+sel_var,sel_var+'Pos_x']
condition =(df_last['Delta'+sel_var]>d_lim1)&(df_last['Delta'+sel_var]<d_lim2)
 df_last[condition][sel_cols].sort_values(by=['Delta'+sel_var], ascending=True ).head(n_head )<prepare_x_and_y>
tf.__version__
def plotExample() :
train_cond =(( df_train['Country_Region']==my_country)&(df_train['Province_State']==my_province))
test_cond =(( df_test['Country_Region']==my_country)&(df_test['Province_State']==my_province))
x_train_plt = df_train[train_cond]['Date']
y_train_plt = df_train[train_cond][my_variable]
y_valid_plt = df_train[train_cond][my_variable+'Valid']
y_trend_plt = df_train[train_cond][my_variable+'Trend']
y_resid_plt = df_train[train_cond][my_variable+'Resid']
x_test_plt = df_test[test_cond]['Date']
y_test_plt = df_test[test_cond][my_variable]
plt.rcParams["figure.figsize"] =(12,6)
 fig, ax = plt.subplots()
ax.plot(x_train_plt,y_train_plt,'o',color='orange', label='y_true')
 ax.plot(x_train_plt,y_valid_plt,'x',color='gray' , label ='y_valid')
 ax.plot(x_train_plt,y_trend_plt,'.',color='lightblue', label='y_trend')
 ax.plot(x_train_plt,y_resid_plt,'-',color='green', label='y_resid')
 ax.plot(x_test_plt,y_test_plt,'*',color='red', label='y_test')
 ax.set_xticks([])
 ax.legend()<filter>
SEED = 1111
tf.random.set_seed(SEED)
 np.random.seed(SEED)
sel_cols = ['Country_Region','Province_State','ConfirmedCases_x','ConfirmedCases_y','DeltaConfirmedCases','ConfirmedCasesPos_x']
df_last[sel_cols][(( df_last['Country_Region']==my_country)&(df_last['Province_State']==my_province)) ]<filter>
%%time
train=pd.read_parquet('.. /input/step01-csv-parquet/dtrain.parquet')
 train = train.query('date > 85' ).reset_index(drop = True)
 train = train[train['weight'] != 0]
train['action'] =(( train['resp'].values)> 0 ).astype(int)
 train.fillna(train.mean() ,inplace=True)
 raw_features = [c for c in train.columns if "feature" in c]
f_mean = np.mean(train[raw_features[1:]].values,axis=0)
 np.save("f_mean_online.npy",f_mean)
 train.shape
sel_cols = ['Country_Region','Province_State','ConfirmedCases_x','ConfirmedCases_y','DeltaConfirmedCases','ConfirmedCasesPos_x']
df_last[sel_cols][(( df_last['Country_Region']==my_country)&(df_last['Province_State']==my_province)) ]<filter>
def stats_features(tmp_df):
tmp_df['feature_cross_41_42_43']=tmp_df['feature_41']+tmp_df['feature_42']+tmp_df['feature_43']
tmp_df['feature_cross_1_2']=tmp_df['feature_1']/(tmp_df['feature_2']+1e-5)
 tmp_df.head()
return tmp_df
sel_cols = ['Country_Region','Province_State','ConfirmedCases_x','ConfirmedCases_y','DeltaConfirmedCases','ConfirmedCasesPos_x']
df_last[sel_cols][(( df_last['Country_Region']==my_country)&(df_last['Province_State']==my_province)) ]<filter>
train=stats_features(train)
 train.head()
sel_cols = ['Country_Region','Province_State','ConfirmedCases_x','ConfirmedCases_y','DeltaConfirmedCases','ConfirmedCasesPos_x']
df_last[sel_cols][(( df_last['Country_Region']==my_country)&(df_last['Province_State']==my_province)) ]<prepare_x_and_y>
features = [c for c in train.columns if "feature" in c]
y1_valid = cut_neg(df_train['ConfirmedCasesValid'].copy())
 y2_valid = cut_neg(df_train['FatalitiesValid'].copy())
 y1_pred = cut_neg(df_test['ConfirmedCases'].copy())
 y2_pred = cut_neg(df_test['Fatalities'].copy() )<compute_test_metric>
valid = train.loc[(train.date >= 450)&(train.date < 500)].reset_index(drop=True)
 resp_cols = ['resp_1', 'resp_2', 'resp_3', 'resp', 'resp_4']
X_train = train.loc[:, train.columns.str.contains('feature')]
y_train = np.stack([(train[c] > 0 ).astype('int')for c in resp_cols] ).T
X_valid = valid.loc[:, valid.columns.str.contains('feature')]
y_valid = np.stack([(valid[c] > 0 ).astype('int')for c in resp_cols] ).T
del train
mse =(mean_squared_log_error(y1_train, y1_valid)+mean_squared_log_error(y2_train, y2_valid)) /2
mse<prepare_output>
def create_resnet(
 num_columns, num_labels, hidden_size, dropout_rate, label_smoothing, learning_rate 
):
inp = tf.keras.layers.Input(shape=(num_columns,))
x=tf.keras.layers.BatchNormalization()(inp)
 x=tf.keras.layers.Dropout(dropout_rate )(x)
 x1=tf.keras.layers.Dense(hidden_size )(x)
 x1=tf.keras.layers.BatchNormalization()(x1)
 x1=tf.keras.layers.LeakyReLU()(x1)
 x1=tf.keras.layers.Dropout(dropout_rate )(x1)
 x = tf.keras.layers.concatenate([x, x1], axis=1)
 x2=tf.keras.layers.Dense(hidden_size )(x)
 x2=tf.keras.layers.BatchNormalization(axis=1 )(x2)
 x2=tf.keras.layers.LeakyReLU()(x2)
 x2=tf.keras.layers.Dropout(dropout_rate )(x2)
 x = tf.keras.layers.concatenate([x1, x2], axis=1)
 x3=tf.keras.layers.Dense(hidden_size )(x)
 x3=tf.keras.layers.BatchNormalization(axis=1 )(x3)
 x3=tf.keras.layers.LeakyReLU()(x3)
 x3=tf.keras.layers.Dropout(dropout_rate )(x3)
 x = tf.keras.layers.concatenate([x2, x3], axis=1)
 x4=tf.keras.layers.Dense(hidden_size )(x)
 x4=tf.keras.layers.BatchNormalization(axis=1 )(x4)
 x4=tf.keras.layers.LeakyReLU()(x4)
 x4=tf.keras.layers.Dropout(dropout_rate )(x4)
 x = tf.keras.layers.concatenate([x3, x4], axis=1)
 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=tfa.optimizers.RectifiedAdam(lr=learning_rate,weight_decay=1e-5),
loss=tf.keras.losses.BinaryCrossentropy(label_smoothing=label_smoothing),
metrics=tf.keras.metrics.AUC(name="AUC"), 
)
 return model
NUM_FOLDS=5
TRAINING=True
if TRAINING:
for i in range(NUM_FOLDS):
tf.keras.backend.clear_session()
SEED = 1111
tf.random.set_seed(SEED+i)
 np.random.seed(SEED+i)
 reduce_lr = tf.keras.callbacks.ReduceLROnPlateau(monitor='val_loss',
verbose=1,
factor=0.2,
patience=8, mode='min')
 earlystop_callback = tf.keras.callbacks.EarlyStopping(monitor='val_loss', patience=10)
 model_checkpoint_callback = tf.keras.callbacks.ModelCheckpoint(
 filepath='online_model_{}.weights'.format(i),
save_weights_only=True,
monitor='val_AUC',
mode='max',
verbose=1,
save_best_only=True)
 dropout_rate=0.2
hidden_size=256
label_smoothing = 0.005
learning_rate = 1e-3
clf = create_resnet(
 len(features), 5, hidden_size, dropout_rate, label_smoothing, learning_rate 
)
 clf.summary()
clf.fit(X_train, y_train, epochs=200, batch_size=4096,validation_data=(X_valid,y_valid),
callbacks=[reduce_lr,
earlystop_callback,
model_checkpoint_callback])
 gc.collect()
<save_to_csv><EOS>