Kaggle competition: Mechanisms of Action (MoA) Prediction
<SOS> metric: MeanColumnwiseLogLoss Kaggle data source: mechanisms-of-action-(moa)-prediction<split>
import numpy as np
import pandas as pd
X_train, X_test, y_train, y_test = train_test_split(train_data, train_targets,
test_size = 0.1,
shuffle=False,
random_state = 42 )<categorify>
submission = pd.read_csv('/kaggle/input/lish-moa/sample_submission.csv')
 public_ids = list(submission['sig_id'].values)
 submission.shape
test = pd.read_csv('/kaggle/input/lish-moa/test_features.csv')
 test_ids = list(test['sig_id'].values)
 private_ids = list(set(test_ids)-set(public_ids))
len(private_ids )
f = open(os.path.join('.. /input/embeddings/glove.840B.300d', 'glove.840B.300d.txt'))
embeddings_index = {}
for line in tqdm(f):
values = line.split(' ')
 word = values[0]
embedding = np.asarray(values[1:], dtype='float32')
 embeddings_index[word] = embedding
f.close()<categorify>
kernels = pd.read_csv('/kaggle/input/moa-public-kernels/kernels.csv', index_col='id')
 kernels.head(10 )
all_embs = np.stack(embeddings_index.values())
 emb_mean = all_embs.mean()
emb_std = all_embs.std()
print(all_embs.shape)
 embedding_dim = all_embs.shape[1]<compute_test_metric>
def make_filename(idx):
return glob.glob('/kaggle/input/moa-public-kernels/' + str(idx)+ '__submission.csv')[0]
def read_predictions(idx):
temp = pd.read_csv(make_filename(idx))
return temp
predictions = [read_predictions(idx)for idx in kernels.index]
len(predictions )
embedding_matrix = np.random.normal(emb_mean,
emb_std, 
(number_of_words, embedding_dim))<feature_engineering>
sub = pd.read_csv('.. /input/pytorch-new/submission.csv')
 sub = sub.drop(['sig_id'],axis = 1)
 sub.head(5)
 predictions.append(sub )
for word, i in word_dict.items() :
if i >= max_words:
continue
embedding_vector = embeddings_index.get(word)
 if embedding_vector is not None:
embedding_matrix[i] = embedding_vector<choose_model_class>
columns = list(submission.columns)
 columns.remove('sig_id')
 y_pred = pd.DataFrame()
y_pred['sig_id'] = predictions[0]['sig_id']
for column in columns:
column_data = []
for i in range(len(predictions)) :
column_data.append(predictions[i][column])
 y_pred[column] = np.mean(column_data, axis=0)
 y_pred.shape
input_class = Input(shape=(maxlen,))
x = Embedding(max_words, embedding_dim, weights=[embedding_matrix], trainable=False )(input_class)
 x = Bidirectional(LSTM(64, return_sequences=True))(x)
 x = Bidirectional(LSTM(32, return_sequences=True))(x)
 x = GlobalMaxPool1D()(x)
 x = Dense(16, activation="relu" )(x)
 x = Dropout(0.1 )(x)
 x = Dense(1, activation="sigmoid" )(x)
 model = Model(inputs=input_class, outputs=x)
 print(model.summary() )<train_model>
columns = list(submission.columns)
 columns.remove('sig_id')
 submission = pd.DataFrame(index = public_ids + private_ids, columns=columns)
 submission.index.name = 'sig_id'
submission[:] = 0
submission.loc[y_pred.sig_id,:] = y_pred[columns].values
submission.loc[test[test.cp_type == 'ctl_vehicle'].sig_id] = 0
submission.to_csv('submission.csv', index=True)
 submission.head().T