Kaggle competition: Mechanisms of Action (MoA) Prediction
def record_answer(idx, df_sub, data):
for i in range(len(data['test'])) :
t_in = data['test'][i]['input']
t_out = data['test'][i]['output']
t_pred = data['test'][i]['predict']
ans = flattener(t_pred)
 df_sub['output'][df_sub['output_id']=="{}_{}".format(idx, i)] = ans
return df_sub<save_to_csv>
X_train = pd.read_csv('/kaggle/input/lish-moa/train_features.csv')
 X_test = pd.read_csv('/kaggle/input/lish-moa/test_features.csv')
 y_train = pd.read_csv('/kaggle/input/lish-moa/train_targets_scored.csv')
 submission = pd.read_csv('/kaggle/input/lish-moa/sample_submission.csv' )
df_sub.to_csv("submission.csv", index=False )<feature_engineering>
X_train.set_index('sig_id', inplace=True)
 X_test.set_index('sig_id', inplace=True)
 y_train.set_index('sig_id', inplace=True )
t0 = time()<define_variables>
X_train.cp_time = X_train.cp_time // 24
X_train.cp_dose = X_train.cp_dose.map({'D1': 0, 'D2': 1})
 X_test.cp_time = X_test.cp_time // 24
X_test.cp_dose = X_test.cp_dose.map({'D1': 0, 'D2': 1} )
!cp /kaggle/input/abstraction-and-reasoning-challenge/sample_submission.csv./submission.csv<load_pretrained>
X_train_moa = X_train[X_train.cp_type != 'ctl_vehicle'].drop(columns=['cp_type'])
 X_test_moa = X_test[X_test.cp_type != 'ctl_vehicle'].drop(columns=['cp_type'])
 y_train_moa = y_train.loc[X_train_moa.index]
test_path = Path('/kaggle/input/abstraction-and-reasoning-challenge/test')
 test_tasks = sorted(os.listdir(test_path))
task_file = str(test_path / test_tasks[0])
 with open(task_file, 'r')as f:
task = json.load(f)
 color = task["train"][0]["input"][1][1]
print(color )<set_options>
def mean_predictions(models_dense, X):
y_pred_dense = [model.predict(X)for model in models_dense]
return np.mean(y_pred_dense, axis=0)
 def macro_log_loss(y_true, y_pred):
if len(y_true.shape)== 1:
return log_loss(y_true, y_pred, labels=[0, 1]), [log_loss(y_true, y_pred, labels=[0, 1])]
y_pred = np.maximum(np.minimum(y_pred, [[1 - 1e-15] * y_pred.shape[1]] * y_pred.shape[0]), [[1e-15] * y_pred.shape[1]] * y_pred.shape[0])
 losses = [log_loss(y_true[:, i], y_pred[:, i], labels=[0, 1])for i in range(y_true.shape[1])]
return np.mean(losses), losses
warnings.filterwarnings("ignore" )<define_variables>
def create_dense_model(input_shape, output_shape):
inputs = keras.Input(shape=(input_shape,), name='drug')
 x = layers.BatchNormalization()(inputs)
 x = layers.Dropout(0.2 )(x)
 x = tfa.layers.WeightNormalization(layers.Dense(units=256, activation='relu', name='dense_1'))(x)
 x = layers.BatchNormalization()(x)
 x = layers.Dropout(0.2 )(x)
 x = tfa.layers.WeightNormalization(layers.Dense(units=256, activation='relu', name='dense_2'))(x)
 x = layers.BatchNormalization()(x)
 x = layers.Dropout(0.2 )(x)
 output = tfa.layers.WeightNormalization(layers.Dense(output_shape, activation='sigmoid', kernel_regularizer=keras.regularizers.l2(l2=1e-5),
name='predictions'))(x)
 model = keras.Model(inputs=inputs, outputs=output)
 opt = tfa.optimizers.AdamW(weight_decay=1e-5, learning_rate=1e-2)
 model.compile(optimizer=opt, loss='binary_crossentropy')
 return model
data_path = Path('/kaggle/input/abstraction-and-reasoning-challenge/')
 training_path = data_path / 'training'
evaluation_path = data_path / 'evaluation'
test_path = data_path / 'test'
training_tasks = sorted(os.listdir(training_path))
evaluation_tasks = sorted(os.listdir(evaluation_path))
test_tasks = sorted(os.listdir(test_path))<categorify>
callbacks = [
keras.callbacks.EarlyStopping(
 monitor='val_loss',
min_delta=1e-5,
patience=3,
verbose=0),
keras.callbacks.ReduceLROnPlateau(
 monitor="val_loss",
factor=0.1,
patience=2)
 ]
def train(task, pred_func):
try:
ok = 0
for i, t in enumerate(task["train"]):
t_in, t_out = np.array(t["input"] ).astype('uint8'), np.array(t["output"] ).astype('uint8')
 sz_out = t_out.shape
if len(inspect.getargspec(pred_func ).args)==1:
t_pred = pred_func(t_in)
 else:
t_pred = pred_func(t_in,sz_out)
 if len(t_out)==len(t_pred):
if len(t_out)==1:
if t_pred==t_out:
ok += 1
elif(t_pred==t_out ).all() :
ok += 1
t_pred = []
if ok==len(task["train"]):
for i, t in enumerate(task["test"]):
t_in = np.array(t["input"] ).astype('uint8')
 if len(inspect.getargspec(pred_func ).args)==1:
t_pred.append(pred_func(t_in))
else:
t_pred.append(pred_func(t_in, sz_out))
return t_pred
else:
return None
except:
return None<categorify>
x_train_dense = X_train_moa.to_numpy()
y_train_dense = y_train_moa.to_numpy()
kf = MultilabelStratifiedKFold(n_splits=5, shuffle=True)
 mlls = []
models = []
i = 1
for train, test in kf.split(x_train_dense, y_train_dense):
print(f'FOLD {i}.', end=' ')
 i = i + 1
seed(train[0])
 tf.random.set_seed(train[0])
 dense = create_dense_model(x_train_dense.shape[1], y_train_dense.shape[1])
 history_dense = dense.fit(x_train_dense[train], y_train_dense[train],
batch_size=64,
epochs=100,
callbacks=callbacks,
validation_data=(x_train_dense[test], y_train_dense[test]),
shuffle=True, verbose=0)
 mll, _ = macro_log_loss(y_train_dense[test], dense.predict(x_train_dense[test]))
models.append(dense)
 mlls.append(mll)
 print('Dense macro log loss:', mll )
def flattener(pred):
str_pred = str([row for row in pred])
 return str_pred.replace(', ', '' ).replace('[[', '|' ).replace('][', '|' ).replace(']]', '|' )<categorify>
print(np.mean(mlls))
def crop_min(a):
try:
b = np.bincount(a.flatten() ,minlength=10)
 c = int(np.where(b==np.min(b[np.nonzero(b)])) [0])
 coords = np.argwhere(a==c)
 x_min, y_min = coords.min(axis=0)
 x_max, y_max = coords.max(axis=0)
 return a[x_min:x_max+1, y_min:y_max+1]
except:
return a<categorify>
preds = mean_predictions(models, X_test_moa )
def crop_max(a):
try:
b = np.bincount(a.flatten() ,minlength=10)
 b[0] = 255
c = np.argsort(b)[-2]
coords = np.argwhere(a==c)
 x_min, y_min = coords.min(axis=0)
 x_max, y_max = coords.max(axis=0)
 return a[x_min:x_max+1, y_min:y_max+1]
except:
return a<train_model>
indexes = dict(zip(X_test_moa.index, range(X_test_moa.shape[0])) )
def resize_o(a,s):
try:
nx,ny = s[1]//a.shape[1],s[0]//a.shape[0]
return np.repeat(np.repeat(a, nx, axis=0), ny, axis=1)
 except:
return a<normalization>
for idx, row in submission.iterrows() :
sig_id = row.sig_id
if sig_id in indexes.keys() :
submission.loc[idx] = [sig_id] + [np.maximum(np.minimum(pred, 1 - 1e-3), 1e-3)for pred in preds[indexes[sig_id]]]
else:
submission.loc[idx] = [sig_id] + [0] * preds.shape[1]
def resize_c(a):
c = np.count_nonzero(np.bincount(a.flatten() ,minlength=10)[1:])
 return np.repeat(np.repeat(a, c, axis=0), c, axis=1 )<normalization>
submission.to_csv('submission.csv', index=False )
def resize_2(a):
return np.repeat(np.repeat(a, 2, axis=0), 2, axis=1 )<train_model>
submission.to_csv('submission.csv', index=False )