Kaggle competition: Jane Street Market Prediction
<SOS> metric: JaneStreetPnl Kaggle data source: jane-street-market-prediction<train_model>
tf.random.set_seed(42)
estimator = get_estimator()
y_train = train['cuisine'].values
classifier = OneVsRestClassifier(estimator, n_jobs=-1)
 classifier.fit(x_train, y_train )<save_to_csv>
def reduce_mem_usage(df, verbose=True):
numerics = ['int16', 'int32', 'int64', 'float16', 'float32', 'float64']
start_mem = df.memory_usage().sum() / 1024**2
for col in df.columns:
col_type = df[col].dtypes
if col_type in numerics:
c_min = df[col].min()
c_max = df[col].max()
if str(col_type)[:3] == 'int':
if c_min > np.iinfo(np.int8 ).min and c_max < np.iinfo(np.int8 ).max: df[col] = df[col].astype(np.int8)
 elif c_min > np.iinfo(np.int16 ).min and c_max < np.iinfo(np.int16 ).max: df[col] = df[col].astype(np.int16)
 elif c_min > np.iinfo(np.int32 ).min and c_max < np.iinfo(np.int32 ).max: df[col] = df[col].astype(np.int32)
 elif c_min > np.iinfo(np.int64 ).min and c_max < np.iinfo(np.int64 ).max: df[col] = df[col].astype(np.int64)
 else:
if c_min > np.finfo(np.float16 ).min and c_max < np.finfo(np.float16 ).max: df[col] = df[col].astype(np.float16)
 elif c_min > np.finfo(np.float32 ).min and c_max < np.finfo(np.float32 ).max: df[col] = df[col].astype(np.float32)
 else: df[col] = df[col].astype(np.float64)
 end_mem = df.memory_usage().sum() / 1024**2
return df
test['cuisine'] = classifier.predict(x_test)
 test[['id', 'cuisine']].to_csv('submission.csv', index=False )<set_options>
TRAINING = False
USE_FINETUNE = False
FOLDS = 4
SEED = 42
train = pd.read_csv('.. /input/jane-street-market-prediction/train.csv')
 train = train.query('date > 85' ).reset_index(drop = True)
 train = train.astype({c: np.float32 for c in train.select_dtypes(include='float64' ).columns})
 train.fillna(train.mean() ,inplace=True)
 train = reduce_mem_usage(train)
 train = train.query('weight > 0' ).reset_index(drop = True)
 train['action'] =(( train['resp_1'] > 0)&(train['resp_2'] > 0)&(train['resp_3'] > 0)&(train['resp_4'] > 0)&(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_mean = np.mean(train[features[1:]].values,axis=0 )
%matplotlib inline
%config InlineBackend.figure_format = 'retina'<load_from_disk>
def create_autoencoder(input_dim,output_dim,noise=0.05):
i = Input(input_dim)
 encoded = BatchNormalization()(i)
 encoded = GaussianNoise(noise )(encoded)
 encoded = Dense(64,activation='relu' )(encoded)
 decoded = Dropout(0.2 )(encoded)
 decoded = Dense(input_dim,name='decoded' )(decoded)
 x = Dense(32,activation='relu' )(decoded)
 x = BatchNormalization()(x)
 x = Dropout(0.2 )(x)
 x = Dense(32,activation='relu' )(x)
 x = BatchNormalization()(x)
 x = Dropout(0.2 )(x)
 x = Dense(output_dim,activation='sigmoid',name='label_output' )(x)
 encoder = Model(inputs=i,outputs=encoded)
 autoencoder = Model(inputs=i,outputs=[decoded,x])
 autoencoder.compile(optimizer=Adam(0.005),loss={'decoded':'mse','label_output':'binary_crossentropy'})
 return autoencoder, encoder
train = pd.read_json('.. /input/train.json')
 test = pd.read_json('.. /input/test.json')
 train.head()<count_unique_values>
def create_model(input_dim,output_dim,encoder):
inputs = Input(input_dim)
 x = encoder(inputs)
 x = Concatenate()([x,inputs])
 x = BatchNormalization()(x)
 x = Dropout(0.13 )(x)
 hidden_units = [384, 896, 896, 394]
for idx, hidden_unit in enumerate(hidden_units):
x = Dense(hidden_unit )(x)
 x = BatchNormalization()(x)
 x = Lambda(tf.keras.activations.relu )(x)
 x = Dropout(0.25 )(x)
 x = Dense(output_dim,activation='sigmoid' )(x)
 model = Model(inputs=inputs,outputs=x)
 model.compile(optimizer=Adam(0.0005),loss=BinaryCrossentropy(label_smoothing=0.05),metrics=[tf.keras.metrics.AUC(name = 'auc')])
 return model
print("Total number of cuisines are: {}".format(len(train['cuisine'].unique())))
 print(train['cuisine'].unique() )<create_dataframe>
autoencoder, encoder = create_autoencoder(X.shape[-1],y.shape[-1],noise=0.1)
 if TRAINING:
autoencoder.fit(X,(X,y),
epochs=1000,
batch_size=4096,
validation_split=0.1,
callbacks=[EarlyStopping('val_loss',patience=10,restore_best_weights=True)])
 encoder.save_weights('./encoder.hdf5')
 else:
encoder.load_weights('.. /input/janestreetmodels/encoder.hdf5')
 encoder.trainable = False
cuisine_df = pd.DataFrame({
'Count': train['cuisine'].value_counts() })
 cuisine_df<define_variables>
FOLDS = 4
SEED = 42
if TRAINING:
gkf = PurgedGroupTimeSeriesSplit(n_splits = FOLDS, group_gap=20)
 splits = list(gkf.split(y, groups=train['date'].values))
for fold,(train_indices, test_indices)in enumerate(splits):
model = create_model(130, 5, encoder)
 X_train, X_test = X[train_indices], X[test_indices]
y_train, y_test = y[train_indices], y[test_indices]
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(f'./model_{SEED}_{fold}.hdf5')
 model.compile(Adam(0.00001),loss='binary_crossentropy')
 model.fit(X_test,y_test,epochs=3,batch_size=4096)
 model.save_weights(f'./model_{SEED}_{fold}_finetune.hdf5')
 else:
models = []
for f in range(FOLDS):
model = create_model(130, 5, encoder)
 if USE_FINETUNE:
model.load_weights(f'.. /input/janestreetmodels/model_{SEED}_{f}_finetune.hdf5')
 else:
model.load_weights(f'.. /input/janestreetmodels/model_{SEED}_{f}.hdf5')
 models.append(model )
<count_values><EOS> 
if not TRAINING:
f_median = np.median
models = models[-2:]
env = janestreet.make_env()
th = 0.50195
for(test_df, pred_df)in tqdm(env.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_mean
pred = np.mean([model(x_tt, training = False ).numpy() for model in models],axis=1)
 pred = f(pred)
 pred_df.action = np.where(pred >= th, 1, 0 ).astype(int)
 else:
pred_df.action = 0
env.predict(pred_df )