Kaggle competition: Mechanisms of Action (MoA) Prediction
<load_from_csv>
df_feature = pd.read_csv('/kaggle/input/lish-moa/train_features.csv')
 df_label = pd.read_csv('/kaggle/input/lish-moa/train_targets_scored.csv')
 features = df_feature.columns
features = [_ for _ in features if _ != 'sig_id']
labels = df_label.columns
labels = [_ for _ in labels if _ != 'sig_id']
train = pd.read_csv('.. /input/train.csv',
dtype={'is_booking':bool,'srch_destination_id':np.int32, 'hotel_cluster':np.int32},
usecols=['srch_destination_id','is_booking','hotel_cluster'],
chunksize=1000000)
 aggs = []
print('-'*38)
 for chunk in train:
agg = chunk.groupby(['srch_destination_id',
'hotel_cluster'])['is_booking'].agg(['sum','count'])
 agg.reset_index(inplace=True)
 aggs.append(agg)
 print('.',end='')
 print('')
 aggs = pd.concat(aggs, axis=0)
 aggs.head()<split>
def preprocess(df=df_feature):
df['cp_type'] = df['cp_type'].map({'trt_cp': 0, 'ctl_vehicle': 1})
 df['cp_dose'] = df['cp_dose'].map({'D1': 0, 'D2': 1})
 df['cp_time'] = df['cp_time'] / 24
return df
df_feature = preprocess()
def most_popular(group, n_max=5):
relevance = group['relevance'].values
hotel_cluster = group['hotel_cluster'].values
most_popular = hotel_cluster[np.argsort(relevance)[::-1]][:n_max]
return np.array_str(most_popular)[1:-1]<prepare_output>
def MoA_dirctionary_IDs() :

 df_moa = df_label[df_label[labels].sum(axis=1)>= 1].reset_index(drop=True)
 moa_dict = {}
for label in labels:
try:
ls = df_moa[df_moa[label] == 1]['sig_id'].values
index = df_moa[df_moa[label] == 1].index
moa_dict[label] = ls
df_moa = df_moa.drop(index, axis=0)
 except:
print(f'An error occurred at label ==> {label}')
 return moa_dict
most_pop = agg.groupby(['srch_destination_id'] ).apply(most_popular)
 most_pop = pd.DataFrame(most_pop ).rename(columns={0:'hotel_cluster'})
 most_pop.head()<load_from_csv>
multi_label = pd.DataFrame(df_label[labels].sum(axis=1))
multi_label['sig_id'] = df_label['sig_id']
ids_no_label = multi_label[multi_label[0] == 0]['sig_id'].values
train_dict = MoA_dirctionary_IDs()
train_dict['no MoA'] = ids_no_label
for i,(k, v)in enumerate(train_dict.items()):
if i <= 4:
print(f'{k}: {v[0]}')
 c = 0
for k, v in train_dict.items() :
c += len(v)
 print(c )
test = pd.read_csv('.. /input/test.csv',
dtype={'srch_destination_id':np.int32},
usecols=['srch_destination_id'], )<merge>
class TrainSchema:
def __init__(self, features, labels, train_dict, k=5):
self.features = features
self.labels = labels
self.train_dict = train_dict
self.k = k
self.models = {'model_'+str(i): self.create_model(i)for i in range(self.k)}
self.total_params = self.models['model_0'].count_params() * self.k
print(f'Total parameters: {self.total_params}')
 def create_model(self, i):
template = 'model_'+str(i)+'_'
inp_dim = len(self.features)
 out_dim = len(self.labels)
 inp = layers.Input(shape=(inp_dim,), name=template+'inp')
 x = layers.Reshape(( 1, 1, inp_dim))(inp)
 x = layers.BatchNormalization()(x)
 x = tfa.layers.WeightNormalization(layers.Conv2D(256, 1, 1))(x)
 x = layers.BatchNormalization()(x)
 x = layers.LeakyReLU()(x)
 x = layers.Dropout(0.3 )(x)
 x = tfa.layers.WeightNormalization(layers.Conv2D(256, 1, 1))(x)
 x = layers.BatchNormalization()(x)
 x = layers.LeakyReLU()(x)
 x = layers.Dropout(0.3 )(x)
 x = layers.Flatten()(x)
 x = tfa.layers.WeightNormalization(layers.Dense(200))(x)
 x = layers.BatchNormalization()(x)
 x = layers.LeakyReLU()(x)
 x = layers.Dropout(0.2 )(x)
 x = tfa.layers.WeightNormalization(layers.Dense(200))(x)
 x = layers.BatchNormalization()(x)
 x = layers.LeakyReLU()(x)
 x = layers.Dropout(0.2 )(x)
 out = tfa.layers.WeightNormalization(layers.Dense(206, activation='sigmoid', name=template+'out'))(x)
 model = tf.keras.models.Model(inputs=inp, outputs=out)
 model.compile(optimizer=tfa.optimizers.Lookahead(tf.optimizers.Adam() , sync_period=10), loss='binary_crossentropy')
 return model
def create_dataset(self, df_feature, df_target, i, batch_size=256):
def kfold_data_trim() :
train_ids = []
valid_ids = []
for moa, ids in self.train_dict.items() :
IDs = ids.copy()
IDs = IDs.tolist()
if len(ids)> self.k:
chunk = len(ids)// self.k
if i == self.k-1:
valid_ids.extend(IDs[::-1][chunk*i:chunk*(i+1)])
 for ID in IDs[::-1][chunk*i:chunk*(i+1)]:
IDs.remove(ID)
 else:
valid_ids.extend(IDs[chunk*i:chunk*(i+1)])
 for ID in IDs[chunk*i:chunk*(i+1)]:
IDs.remove(ID)
 train_ids.extend(IDs)
 else:
train_ids.extend(ids)
 return train_ids, valid_ids
train_ids, valid_ids = kfold_data_trim()
def train_valid_df() :
df = pd.merge(df_feature, df_target, on='sig_id')
 df = df.set_index('sig_id')
 df_train = df.loc[train_ids]
df_valid = df.loc[valid_ids]
return df_train, df_valid
df_train, df_valid = train_valid_df()
print(f'train samples {df_train.shape[0]}, valid samples {df_valid.shape[0]}')
 x_trn = df_train[self.features].to_numpy()
y_trn = df_train[self.labels].to_numpy()
x_val = df_valid[self.features].to_numpy()
y_val = df_valid[self.labels].to_numpy()
dataset_valid = tf.data.Dataset.from_tensor_slices(( x_val, y_val)).batch(batch_size)
 dataset_train = tf.data.Dataset.from_tensor_slices(( x_trn, y_trn)).shuffle(y_trn.shape[0] ).batch(batch_size)
 return dataset_train, dataset_valid
def train(self, df_feature, df_target, path_int=0, epochs=100, patience=10):
self.ckpt_path = '/tmp/checkpoint_'+str(path_int)+'/'
loss = []
val_loss = []
es = tf.keras.callbacks.EarlyStopping(patience=patience)
 rlr = tf.keras.callbacks.ReduceLROnPlateau(monitor='val_loss', factor=0.1, patience=8, min_delta=0.0001, cooldown=0, min_lr=0)
 for i in range(self.k):
start = time.time()
ckpt = tf.keras.callbacks\ 
.ModelCheckpoint(self.ckpt_path+'model_'+str(i), monitor='val_loss',
verbose=0, save_best_only=True, save_weights_only=True)
 dataset_train, dataset_valid = self.create_dataset(df_feature, df_target, i)
 print('
Model_'+str(i), ' Starts to Train...')
 h = self.models['model_'+str(i)].fit(dataset_train, epochs=epochs,
validation_data=dataset_valid,
verbose=0, callbacks=[es, rlr, ckpt])
 loss.append(h.history['loss'][-1])
 val_loss.append(h.history['val_loss'][-1])
 end = time.time() - start
print('train time: {:.2f} mins.'.format(end/60))
for i in range(len(loss)) :
print('Model_{}: loss {:.5f}, val_loss {:.5f}'.format(str(i), loss[i], val_loss[i]))
def load_weights(self):
for i in range(self.k):
self.models['model_'+str(i)].load_weights(self.ckpt_path+'model_'+str(i))
def evaluate_models(self, df_feature, df_target):
print('
Evaluating')
 loss = []
val_loss = []
for i in range(self.k):
dataset_train, dataset_valid = self.create_dataset(df_feature, df_target, i)
 rel1 = self.models['model_'+str(i)].evaluate(dataset_train, verbose=0)
 rel2 = self.models['model_'+str(i)].evaluate(dataset_valid, verbose=0)
 loss.append(rel1)
 val_loss.append(rel2)
 for i in range(len(loss)) :
print('Model_{}: loss {:.5f}, val_loss {:.5f}'.format(str(i), loss[i], val_loss[i]))
model_losses = {'model_'+str(i): _/sum(val_loss)for i, _ in enumerate(val_loss)}
model_names = [k for k, v in sorted(model_losses.items() , key=lambda item: item[1], reverse=False)]
model_desc = {k: v for k, v in sorted(model_losses.items() , key=lambda item: item[1], reverse=True)}
self.amount_to_say = {}
for i,(k, v)in enumerate(model_desc.items()):
self.amount_to_say[model_names[i]] = v
test = test.merge(most_pop, how='left',left_on='srch_destination_id',right_index=True)
 test.head()<count_missing_values>
train_schema = TrainSchema(features, labels, train_dict, k=5)
 train_schema.train(df_feature, df_label)
 train_schema.load_weights()
train_schema.evaluate_models(df_feature, df_label )
test.hotel_cluster.isnull().sum()<groupby>
df_merge = pd.merge(df_feature, df_label, on='sig_id')
 df_pos = df_merge[df_merge[labels].sum(axis=1)>= 1]
for k, model in train_schema.models.items() :
model.evaluate(df_pos[features].to_numpy() , df_pos[labels].to_numpy() )
most_pop_all = agg.groupby('hotel_cluster')['relevance'].sum().nlargest(5 ).index
most_pop_all = np.array_str(most_pop_all)[1:-1]
most_pop_all<categorify>
df_test = pd.read_csv('/kaggle/input/lish-moa/test_features.csv')
 df_test = preprocess(df_test)
 submission = pd.read_csv('/kaggle/input/lish-moa/sample_submission.csv')
 label_cols = submission.columns.tolist() [1:]
test.hotel_cluster.fillna(most_pop_all,inplace=True )<save_to_csv>
pred = np.zeros(( df_test.shape[0], len(labels)))
 for i in range(len(train_schema.models)) :
pred += train_schema.models['model_'+str(i)].predict([df_test[features].to_numpy() ])
 pred = pred / len(train_schema.models )
<set_options><EOS> 
for i, c in enumerate(labels):
submission[c] = pred[:, i]
submission.loc[df_test['cp_type']==1, labels] = 0
submission.to_csv('submission.csv', index=False )