Kaggle competition: Jane Street Market Prediction
print('using models to generate meta...')
 train_meta = []
test_meta = []
for model_file in all_results.keys() :
model = get_model(adam,True)
 model.load_weights(model_file)
 train_meta.append(model.predict(x_train,batch_size=batch_size, verbose=1))
test_meta.append(model.predict(x_test,batch_size=batch_size, verbose=1))
train_meta = np.mean(train_meta,axis=0)
 test_meta = np.mean(test_meta,axis=0)
 print(y_train.shape)
 print(train_meta.shape)
 print(test_meta.shape)
<save_to_csv>
e_size = 64
fc_input = 130
h_dims = [512,512,256,128]
dropout_rate = 0.5
epochs = 2000
minibatch_size = 100000
class MarketPredictor(nn.Module):
def __init__(self):
super(MarketPredictor, self ).__init__()
self.deep = nn.Sequential(
 nn.Linear(fc_input,h_dims[0]),
nn.BatchNorm1d(h_dims[0]),
nn.LeakyReLU() ,
nn.Dropout(dropout_rate),
nn.Linear(h_dims[0],h_dims[1]),
nn.BatchNorm1d(h_dims[1]),
nn.LeakyReLU() ,
nn.Dropout(dropout_rate),
nn.Linear(h_dims[1],h_dims[2]),
nn.BatchNorm1d(h_dims[2]),
nn.LeakyReLU() ,
nn.Dropout(dropout_rate),
nn.Linear(h_dims[2],h_dims[3]),
nn.BatchNorm1d(h_dims[3]),
nn.LeakyReLU() ,
nn.Dropout(dropout_rate),
nn.Linear(h_dims[3],e_size),
nn.BatchNorm1d(e_size),
nn.LeakyReLU() ,
nn.Dropout(dropout_rate)
)
 self.reduce = nn.utils.weight_norm(nn.Linear(e_size,5))
def forward(self,xf):
f_out = self.deep(xf)
 ef_out = self.reduce(f_out)
 return ef_out
search_result = threshold_search(y_train, train_meta)
 print(search_result)
 test_meta = test_meta>search_result['threshold']
test_meta = test_meta.astype(int)
 df_subm = pd.DataFrame()
df_subm['qid'] = df_final.qid
df_subm['prediction'] = test_meta
print(df_subm.head())
 df_subm.to_csv('submission.csv', index=False )<feature_engineering>
path = '/kaggle/input/pytorch-nn-model-w-o-feature-reduction/marketpredictor_state_dict_'+str(epochs)+'epochs.pt'
model = MarketPredictor()
model.load_state_dict(torch.load(path,map_location=dev))
model.to(dev)
 model.eval()
print('Preproccesing texts.... ')
 print('lower...')
 df["question_text"] = df["question_text"].apply(lambda x: x.lower())
 df_final["question_text"] = df_final["question_text"].apply(lambda x: x.lower())
 puncts = [',', '.', '"', ':', ')', '(', '-', '!', '?', '|', ';', "'", '$', '&', '/', '[', ']', '>', '%', '=', '
'·', '_', '{', '}', '©', '^', '®', '`', '<', '→', '°', '€', '™', '›', '♥', '←', '×', '§', '″', '′', 'Â', '█', '½', 'à', '…',
'“', '★', '”', '–', '●', 'â', '►', '−', '¢', '²', '¬', '░', '¶', '↑', '±', '¿', '▾', '═', '¦', '║', '―', '¥', '▓', '—', '‹', '─',
'▒', '：', '¼', '⊕', '▼', '▪', '†', '■', '’', '▀', '¨', '▄', '♫', '☆', 'é', '¯', '♦', '¤', '▲', 'è', '¸', '¾', 'Ã', '⋅', '‘', '∞',
'∙', '）', '↓', '、', '│', '（', '»', '，', '♪', '╩', '╚', '³', '・', '╦', '╣', '╔', '╗', '▬', '❤', 'ï', 'Ø', '¹', '≤', '‡', '√', ]
def clean_text(x):
x = str(x)
 for punct in puncts:
x = x.replace(punct, f' {punct} ')
 return x
print('clean text...')
 df["question_text"] = df["question_text"].apply(lambda x: clean_text(x))
df_final["question_text"] = df_final["question_text"].apply(lambda x: clean_text(x))
mispell_dict = {"aren't" : "are not",
"can't" : "cannot",
"couldn't" : "could not",
"didn't" : "did not",
"doesn't" : "does not",
"don't" : "do not",
"hadn't" : "had not",
"hasn't" : "has not",
"haven't" : "have not",
"he'd" : "he would",
"he'll" : "he will",
"he's" : "he is",
"i'd" : "I would",
"i'd" : "I had",
"i'll" : "I will",
"i'm" : "I am",
"isn't" : "is not",
"it's" : "it is",
"it'll":"it will",
"i've" : "I have",
"let's" : "let us",
"mightn't" : "might not",
"mustn't" : "must not",
"shan't" : "shall not",
"she'd" : "she would",
"she'll" : "she will",
"she's" : "she is",
"shouldn't" : "should not",
"that's" : "that is",
"there's" : "there is",
"they'd" : "they would",
"they'll" : "they will",
"they're" : "they are",
"they've" : "they have",
"we'd" : "we would",
"we're" : "we are",
"weren't" : "were not",
"we've" : "we have",
"what'll" : "what will",
"what're" : "what are",
"what's" : "what is",
"what've" : "what have",
"where's" : "where is",
"who'd" : "who would",
"who'll" : "who will",
"who're" : "who are",
"who's" : "who is",
"who've" : "who have",
"won't" : "will not",
"wouldn't" : "would not",
"you'd" : "you would",
"you'll" : "you will",
"you're" : "you are",
"you've" : "you have",
"'re": " are",
"wasn't": "was not",
"we'll":" will",
"didn't": "did not",
"tryin'":"trying"}
def _get_mispell(mispell_dict):
mispell_re = re.compile('(%s)' % '|'.join(mispell_dict.keys()))
return mispell_dict, mispell_re
mispellings, mispellings_re = _get_mispell(mispell_dict)
 def replace_typical_misspell(text):
def replace(match):
return mispellings[match.group(0)]
return mispellings_re.sub(replace, text)
 print('clean misspellings...')
 df["question_text"] = df["question_text"].apply(lambda x: replace_typical_misspell(x))
df_final["question_text"] = df_final["question_text"].apply(lambda x: replace_typical_misspell(x)) 
<string_transform>
with open('/kaggle/input/pytorch-nn-model-w-o-feature-reduction/feature_processing.pkl', 'rb')as f:
sc, maxindex, fill_val = pickle.load(f )
dim = 300
num_words = 75966
max_len = 100
print('Fiting tokenizer')
 tokenizer = Tokenizer(num_words=num_words)
 tokenizer.fit_on_texts(list(df['question_text'])+list(df_final['question_text']))
print('text to sequence')
 x_train = tokenizer.texts_to_sequences(df['question_text'])
 print('pad sequence')
 x_train = pad_sequences(x_train,maxlen=max_len)
 y_train = df['target'].values
print(x_train.shape)
 print(y_train.shape)
 x_test=tokenizer.texts_to_sequences(df_final['question_text'])
 x_test = pad_sequences(x_test,maxlen=max_len)
 print('Test data loaded:',x_test.shape )<compute_train_metric>
feature_names = ['feature_'+str(i)for i in range(130)]
exclude = np.where([maxindex[i,1] > 100 and maxindex [i,2] > 1 for i in range(129)])[0]
print('Glove...')
 def get_coefs(word,*arr): return word, np.asarray(arr, dtype='float32')
 embeddings_index = dict(get_coefs(*o.split(" ")) for o in open('.. /input/embeddings/glove.840B.300d/glove.840B.300d.txt'))
all_embs = np.stack(embeddings_index.values())
 emb_mean,emb_std = all_embs.mean() , all_embs.std()
print(len(all_embs))
word_index = tokenizer.word_index
embedding_matrix_glov = np.random.normal(emb_mean, emb_std,(num_words, dim))
count=0
for word, i in word_index.items() :
if i >= num_words:
break
embedding_vector = embeddings_index.get(word)
 if embedding_vector is not None:
embedding_matrix_glov[i] = embedding_vector
else:
count += 1
print('embedding matrix size:',embedding_matrix_glov.shape)
 print('Number of words not in vocab:',count)
 del embeddings_index,all_embs
gc.collect()<statistical_test>
env = janestreet.make_env()
iter_test = env.iter_test()
<feature_engineering><EOS> 
for(test_df, sample_prediction_df)in iter_test:
if test_df['weight'].item() == 0:
sample_prediction_df.action = 0
else:
test_df_features = test_df[feature_names].to_numpy()
for i in exclude:
if test_df_features[0,i+1] == maxindex[i,0]:
test_df_features[0,i+1] = fill_val[i]
nans = np.isnan(test_df_features)
 for i in range(130):
if nans[0,i]:
test_df_features[0,i] = fill_val[i]
test_df_features = sc.transform(test_df_features)
 ftensor = torch.tensor(test_df_features,dtype=torch.float,device=dev)
 s = model(ftensor)[0,0].item()
sample_prediction_df.action = int(np.round(1/(1+np.exp(-s))))
env.predict(sample_prediction_df )