Kaggle competition: Jane Street Market Prediction
<SOS> metric: JaneStreetPnl Kaggle data source: jane-street-market-prediction<load_from_disk>
epochs = 40
batch_size = 4096
hidden_units = [160, 160, 160]
dropout_rates = [0.2, 0.2, 0.2, 0.2]
label_smoothing = 1e-2
learning_rate = 1e-3
th = 0.502
train = pd.read_json('.. /input/train.json')
 test = pd.read_json('.. /input/test.json')
 class_names = list(train.cuisine.unique())
 class_names<feature_engineering>
@njit(fastmath = True)
 def utility_score_numba(date, weight, resp, action):
Pi = np.bincount(date, weight * resp * action)
 t = np.sum(Pi)/ np.sqrt(np.sum(Pi ** 2)) * np.sqrt(250 / len(Pi))
u = min(max(t, 0), 6)* np.sum(Pi)
 return u
train['num_ings'] = train['ingredients'].apply(lambda x : len(x))
test['num_ings'] = test['ingredients'].apply(lambda x : len(x))
train.head()<filter>
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
import tensorflow as tf
import tensorflow_addons as tfa
import numpy as np
import pandas as pd
from tqdm import tqdm
from random import choices
train = train[train['num_ings'] > 2]
len(train )<feature_engineering>
SEED = 1111
np.random.seed(SEED )
train['ingredients'] = train['ingredients'].apply(lambda x: list(map(lambda y: y.lower() , x)))
 test['ingredients'] = test['ingredients'].apply(lambda x: list(map(lambda y: y.lower() , x)))
 train.head()<categorify>
train = pd.read_csv('.. /input/jane-street-market-prediction/train.csv')
 train = train.query('date > 85' ).reset_index(drop = True)
 train = train[train['weight'] != 0]
train.fillna(train.mean() ,inplace=True)
 train['action'] =(( train['resp'].values)> 0 ).astype(int)
 val = train.loc[train["date"]>=450].copy()
train = train.loc[train["date"]<450]
features = [c for c in train.columns if "feature" in c]
f_mean = np.mean(train[features[1:]].values,axis=0)
 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_val = val.loc[:, val.columns.str.contains('feature')]
y_val = np.stack([(val[c] > 0 ).astype('int')for c in resp_cols] ).T
def get_replacements() :
return {'wasabe': 'wasabi', '-': '', 'sauc': 'sauce',
'baby spinach': 'babyspinach', 'coconut cream': 'coconutcream',
'coriander seeds': 'corianderseeds', 'corn tortillas': 'corntortillas',
'cream cheese': 'creamcheese', 'fish sauce': 'fishsauce',
'purple onion': 'purpleonion','refried beans': 'refriedbeans',
'rice cakes': 'ricecakes', 'rice syrup': 'ricesyrup',
'sour cream': 'sourcream', 'toasted sesame seeds': 'toastedsesameseeds',
'toasted sesame oil': 'toastedsesameoil', 'yellow onion': 'yellowonion'}<choose_model_class>
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=tfa.optimizers.RectifiedAdam(learning_rate=learning_rate),
loss=tf.keras.losses.BinaryCrossentropy(label_smoothing=label_smoothing),
metrics=tf.keras.metrics.AUC(name="AUC"), 
)
 return model
lemmatizer = WordNetLemmatizer()
replacements = get_replacements()
stop_pattern = re.compile('[\d’%]' )<string_transform>
tf.keras.backend.clear_session()
tf.random.set_seed(SEED)
 clf = create_mlp(
 len(features), 5, hidden_units, dropout_rates, label_smoothing, learning_rate 
)
 clf.fit(X_train, y_train, validation_data=(X_val, y_val), epochs=epochs, batch_size=batch_size, verbose=2)
 clf.save(f'model.h5')
def tranform_to_single_string(ingredients, lemmatizer, replacements, stop_pattern):
ingredients_text = ' '.join(iter(ingredients))
for key, value in replacements.items() :
ingredients_text = ingredients_text.replace(key, value)
 words = []
for word in ingredients_text.split() :
if not stop_pattern.match(word)and len(word)> 2:
word = lemmatizer.lemmatize(word)
 words.append(word)
 return ' '.join(words )<categorify>
preds = clf(X_train.values, training = False ).numpy()
preds = np.median(preds, axis=1)
 u_score = utility_score_numba(train.date.values, train.weight.values, train.resp.values,(preds>th ).astype(int))
u_score
transform = lambda ingredients: tranform_to_single_string(ingredients, lemmatizer, replacements, stop_pattern)
 train['x'] = train['ingredients'].apply(transform)
 test['x'] = test['ingredients'].apply(transform)
 train.head()<data_type_conversions>
preds = clf(X_val.values, training = False ).numpy()
preds = np.median(preds, axis=1)
 u_score = utility_score_numba(val.date.values, val.weight.values, val.resp.values,(preds>th ).astype(int))
u_score
vectorizer = make_pipeline(
 TfidfVectorizer(sublinear_tf=True),
FunctionTransformer(lambda x: x.astype('float'), validate=False)
 )<feature_engineering>
del train, val
gc.collect()
<choose_model_class><EOS> 
models = [clf]
f = np.median
env = janestreet.make_env()
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=0)
 pred = f(pred)
 pred_df.action = np.where(pred >= th, 1, 0 ).astype(int)
 else:
pred_df.action = 0
env.predict(pred_df )