Kaggle competition: Jane Street Market Prediction
best_alpha=100
best_iter=3500
final_reg_fatal = ElasticNet(random_state=42,alpha=best_alpha,l1_ratio=0.1,max_iter=best_iter)
 final_reg_fatal.fit(X_train_fatal, y_train_fatal )<predict_on_test>
if tuning or training:
train_data = pd.read_csv('/kaggle/input/jane-street-market-prediction/train.csv')
 train_data.fillna(train_data.mean() ,inplace=True)
 metadata = pd.read_csv('/kaggle/input/jane-street-market-prediction/features.csv')
 metadata.drop(['feature'],axis=1,inplace=True)
 def replace_bool(tf):
if tf:
return 1
else:
return 0
metadata = metadata.applymap(replace_bool)
 metadata_norm = metadata/metadata.sum()
metadata_norm = metadata_norm.applymap(np.sqrt)
 features_transform = metadata_norm.values
start_date=86
feature_columns = [col for col in train_data.columns.values if 'feature' in col]
X_m = np.matmul(train_data[train_data['date']>=start_date][feature_columns].values,features_transform)
 corr=abs(train_data[feature_columns].corr())
 ordered_feature_columns=[feature_columns.pop(0)]
for col in feature_columns:
corr_max = corr[col][ordered_feature_columns].idxmax()
corr_max_idx = ordered_feature_columns.index(corr_max)
 ordered_feature_columns.insert(corr_max_idx+1,col)
 f_mean = train_data[ordered_feature_columns].mean()
f_mean.to_csv('f_mean.csv',index=False)
 with open("ordered_columns.txt", 'wb')as f:
pickle.dump(( ordered_feature_columns), f)
 resp_cols = ['resp_1', 'resp_2', 'resp_3', 'resp', 'resp_4']
train_data = train_data[train_data['date']>=start_date]
X = np.concatenate(( train_data[ordered_feature_columns].values,X_m),axis=1)
 y = np.stack([(train_data[c] > 0 ).astype('int')for c in resp_cols] ).T
max_date = train_data['date'].max()
split_date = int(0.7*(max_date-start_date))
train_test_split = train_data['date'][train_data['date']==split_date].index[0]
del train_data
X_train=X[:train_test_split,:]
X_test=X[train_test_split:,:]
y_train=y[:train_test_split,:]
y_test=y[train_test_split:,:]
pred=final_reg_fatal.predict(X_test_fatal)
 print("The RMSE value",(mean_squared_error(y_test_fatal,pred)) **0.5 )<split>
tf.random.set_seed(42)
 SEED=42
def create_model(hp, num_columns, num_labels):
inp = tf.keras.layers.Input(shape =(num_columns, 1))
x = tf.keras.layers.BatchNormalization()(inp)
 x = tf.keras.layers.Conv1D(filters=8,
kernel_size=hp.Int('kernel_size',5,10,step=5),
strides=1,
activation='relu' )(x)
 x = tf.keras.layers.MaxPooling1D(pool_size=2 )(x)
 x = tf.keras.layers.Flatten()(x)
 for i in range(hp.Int('num_layers',12,16)) :
x = tf.keras.layers.Dense(hp.Int(f'num_units_{i}',32,64))(x)
 x = tf.keras.layers.BatchNormalization()(x)
 x = tf.keras.layers.Activation(tf.keras.activations.swish )(x)
 x = tf.keras.layers.Dropout(hp.Float(f'dropout_{i}',0,0.5))(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 = hp.Float('lr',0.00001,0.1,default=0.001)) ,
loss = tf.keras.losses.BinaryCrossentropy(label_smoothing = 0.01),
metrics = tf.keras.metrics.AUC(name = 'AUC'), 
)
 return model
featured=pd.DataFrame(data)
 X_y_f=shuffle(featured)
 y_cases_f=X_y_f['target_infection']
y_fatal_f=X_y_f['target_fatal']
X_f=X_y_f.drop(['target_infection','target_fatal'],axis=1)
 X_train_cases_f, X_test_cases_f, y_train_cases_f, y_test_cases_f = train_test_split(X_f, y_cases_f, test_size=0.33)
 X_train_fatal_f, X_test_fatal_f, y_train_fatal_f, y_test_fatal_f = train_test_split(X_f, y_fatal_f, test_size=0.33)
 print("Shape of featurized infection train dataset:",(X_train_cases_f.shape,y_train_cases_f.shape))
print("Shape of featurized infection test dataset:",(X_test_cases_f.shape,y_test_cases_f.shape))
print("Shape of featurized fatal train dataset:",(X_train_fatal_f.shape,y_train_fatal_f.shape))
print("Shape of featurized fatal test dataset:",(X_test_fatal_f.shape,y_test_fatal_f.shape))<train_on_grid>
if tuning:
model_fn = lambda hp: create_model(hp,X_train.shape[-1],y_train.shape[-1])
 tuner = kt.tuners.bayesian.BayesianOptimization(
 hypermodel=model_fn,
objective= kt.Objective('val_AUC', direction='max'),
num_initial_points=4,
max_trials=20)
 tuner.search(X_train,y_train,batch_size=4096,epochs=20, validation_data =(X_test, y_test), callbacks=[EarlyStopping('val_AUC', mode='max',patience=5)])
 hp = tuner.get_best_hyperparameters(1)[0]
pd.to_pickle(hp,'best_hp_cnn_day_86_metadata_deep.pkl' )
reg_case_f=ElasticNet(random_state=42,l1_ratio=0.1,max_iter=2200)
 params = [{'alpha': [10**-4,10**-3, 10**-2,10**-1, 10**0,10**1, 10**2,10**3,10**4]}]
clf_f= RandomizedSearchCV(reg_case_f, params, cv=4, scoring='neg_root_mean_squared_error',return_train_score=True)
 search_f=clf_f.fit(X_train_cases_f, y_train_cases_f)
 results_f = pd.DataFrame.from_dict(clf_f.cv_results_ )<train_model>
if training:
hp = pd.read_pickle('best_hp_cnn_day_86_metadata_deep.pkl')
 model_fn = lambda hp: create_model(hp,X_train.shape[-1],y_train.shape[-1])
 model = model_fn(hp)
 model.fit(X_train,y_train,validation_data=(X_test,y_test),epochs=100,batch_size=4096,
callbacks=[EarlyStopping('val_AUC',mode='max',patience=10,restore_best_weights=True)])
 model.save_weights('JS_CNN_day_86_metadata_deep.hdf5' )
<predict_on_test><EOS> 
if not training or tuning:
model_fn = lambda hp: create_model(hp,159,5)
 hp = pd.read_pickle('/kaggle/input/jscnn/best_hp_cnn_day_86.pkl')
 model = model_fn(hp)
 model.load_weights('/kaggle/input/jscnn/JS_CNN_day_86.hdf5')
 samples_mean = pd.read_csv('/kaggle/input/jscnn/f_mean.csv')
 features_transform = np.load('/kaggle/input/jscnn/features_transform _130.npy')
 env = janestreet.make_env()
iter_test = env.iter_test()
for(test_df, sample_prediction_df)in iter_test:
weight = test_df.weight.iloc[0]
with open("/kaggle/input/jscnn/ordered_columns.txt", 'rb')as f:
ordered_cols = pickle.load(f)
 test_df = test_df[ordered_cols]
X_test = test_df.values[0]
if weight==0:
sample_prediction_df.action = 0
else:
for index, x in np.ndenumerate(X_test):
idx=index[0]
if np.isnan(x):
X_test[idx] = samples_mean.iloc[idx]
X_test=X_test.reshape(( 1,-1))
X_metadata = np.matmul(X_test,features_transform)
 X = np.concatenate(( X_test,X_metadata),axis=1)
 prediction = np.median(model(X, training=False ).numpy() [0])
 sample_prediction_df.action = int(round(prediction))
env.predict(sample_prediction_df)