Kaggle competition: Jane Street Market Prediction
<SOS> metric: JaneStreetPnl Kaggle data source: jane-street-market-prediction<find_best_model_class>
warnings.filterwarnings('ignore')
 tf.random.set_seed(42)
 tempo = time.time()
dados = pd.read_csv('/kaggle/input/jane-street-market-prediction/train.csv')
clf = SGDClassifier()
pipe = Pipeline([('vectorizer', TfidfVectorizer()),('predictor', clf)])
 pipe.fit(train_df["text"], train_df["target"])
 predictions = pipe.predict(dev_df["text"]);
print(classification_report(dev_df['target'], predictions)) 
<load_pretrained>
def verifica_nulos(dataset):
rows = []
for column in dataset.columns:
row = {'coluna': column, 'nans': dataset[column].isnull().sum() , 'frac_nans': dataset[column].isnull().sum() / dataset.shape[0]}
rows.append(row)
 res = pd.DataFrame(rows)
 res[res.nans>0].sort_values('nans', ascending=False)
 return res
features = [c for c in dados.columns if 'feature' in c]
for dependency in("brown", "names", "wordnet", "averaged_perceptron_tagger", "universal_tagset"):
nltk.download(dependency )<categorify>
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 = tf.keras.optimizers.Adam(learning_rate = learning_rate),
loss = tf.keras.losses.BinaryCrossentropy(label_smoothing = label_smoothing),
metrics = tf.keras.metrics.AUC(name = 'AUC'), 
)
 return model
nlp = spacy.load("en", disable=['parser', 'tagger', 'ner'])
 nltk.download('stopwords')
 stops = stopwords.words("english")
 def remove_accented_chars(text):

 text = unidecode.unidecode(text)
 return text
def expand_contractions(text):

 return contractions.fix(text);
def remove_URL(text):
url = re.compile(r'https?://\S+|www\.\S+')
 return url.sub(r'',str(text))
def remove_emoji(text):
emoji_pattern = re.compile("["
u"\U0001F600-\U0001F64F"
u"\U0001F300-\U0001F5FF"
u"\U0001F680-\U0001F6FF"
u"\U0001F1E0-\U0001F1FF"
u"\U00002702-\U000027B0"
u"\U000024C2-\U0001F251"
"]+", flags=re.UNICODE)
 return emoji_pattern.sub(r'', text)
 def remove_html(text):
html=re.compile(r'<.*?>')
 return html.sub(r'',text)
 def remove_punctuation(text):
table=str.maketrans('','',string.punctuation)
 return text.translate(table)
 def text_normalizer(comment, lemmatize, lowercase, remove_stopwords, remove_accents, normalize_contractions, normalize_URL, normalize_emoji, normalize_html, normalize_punctuation):
if lowercase:
comment = comment.lower()
if(remove_accents):
comment = remove_accented_chars(comment)
 if(normalize_contractions):
comment = expand_contractions(comment)
 if(normalize_URL):
comment = remove_URL(comment)
 if(normalize_emoji):
comment = remove_emoji(comment)
 if(normalize_html):
comment = remove_html(comment)
 if(normalize_punctuation):
comment = remove_punctuation(comment)
 if(remove_stopwords):
comment = nlp(comment)
 words = [];
for token in comment:
if not remove_stopwords or(remove_stopwords and token.text not in stops):
words.append(token.text)
 comment = " ".join(words)
 if(lemmatize):
comment = nlp(comment)
 comment = " ".join(word.lemma_.strip() for word in comment)
 return comment
class PrePropTextTransformer(BaseEstimator, TransformerMixin):
def __init__(self, lemmatize=False, lowercase=False, remove_stopwords=False, remove_accents=False, normalize_contractions=False, normalize_URL=False, normalize_emoji=False, normalize_html=False, normalize_punctuation=False):
self.lemmatize=lemmatize
self.lowercase=lowercase
self.remove_stopwords=remove_stopwords
self.remove_accents=remove_accents
self.normalize_contractions=normalize_contractions
self.normalize_URL=normalize_URL
self.normalize_emoji=normalize_emoji
self.normalize_html=normalize_html
self.normalize_punctuation=normalize_punctuation
def transform(self, text_list):
texts = text_list.tolist()
result = [];
for text in texts:
result.append(text_normalizer(text, self.lemmatize, self.lowercase, self.remove_stopwords, self.remove_accents, self.normalize_contractions, self.normalize_URL, self.normalize_emoji, self.normalize_html, self.normalize_punctuation))
return pd.Series(result)
 def fit(self, X, y=None):

 return self 
<find_best_model_class>
batch_size = 4096
hidden_units = [384, 896, 896, 394]
dropout_rates = [0.10143786981358652, 0.19720339053599725, 0.2703017847244654, 0.23148340929571917, 0.2357768967777311]
label_smoothing = 1e-2
learning_rate = 1e-3
%%time
clf = MultinomialNB(fit_prior=False)
 pipe = Pipeline([('vectorizer', TfidfVectorizer()),('predictor', clf)])
 pipe.fit(train_df["text"], train_df["target"])
 predictions = pipe.predict(dev_df["text"]);
print(classification_report(dev_df['target'], predictions)) 
<find_best_model_class>
num_models = 1
models = []
for i in range(num_models):
clf = create_mlp(len(features), 1, hidden_units, dropout_rates, label_smoothing, learning_rate)
 clf.load_weights(f'.. /input/testeupload/JSModel_{i}.hdf5')
 models.append(clf)
 f_mean = np.load('.. /input/testeupload/f_mean.npy')
%%time
os.write(1, b"Starting experimetns w text preprop
")
 clf = MultinomialNB()
pipe = Pipeline([('preprop', PrePropTextTransformer()),('vectorizer', TfidfVectorizer()),('predictor', clf)])
 pipe.fit(train_df["text"], train_df["target"])
 predictions = pipe.predict(dev_df["text"]);
print(classification_report(dev_df['target'], predictions)) 
<choose_model_class>
env = janestreet.make_env()
env_iter = env.iter_test()
<categorify><EOS> 
opt_th = 0.5
for(test_df, pred_df)in tqdm(env_iter):
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 = 0.
 for clf in models:
pred += clf(x_tt, training = False ).numpy().item() / num_models
pred_df.action = np.where(pred >= opt_th, 1, 0 ).astype(int)
 else:
pred_df.action = 0
env.predict(pred_df)