Kaggle competition: Mechanisms of Action (MoA) Prediction
<SOS> metric: MeanColumnwiseLogLoss Kaggle data source: mechanisms-of-action-(moa)-prediction<data_type_conversions>
import numpy as np
import pandas as pd
import tensorflow as tf
import tensorflow.keras.backend as K
import tensorflow.keras.layers as L
import tensorflow.keras.models as M
from tensorflow.keras.callbacks import ReduceLROnPlateau
import tensorflow_addons as tfa
from sklearn.model_selection import KFold
from sklearn.metrics import log_loss
from tqdm.notebook import tqdm
matrix[['date_block_num','shop_id','city_code',
'item_category_id','type_code','subtype_code']]=matrix[['date_block_num','shop_id','city_code',
'item_category_id','type_code','subtype_code']].astype(np.int8)
 matrix[['item_id']]=matrix[['item_id']].astype(np.int16)
 matrix[['item_cnt_month']]=matrix[['item_cnt_month']].astype(np.float16 )<merge>
train_features = pd.read_csv('.. /input/lish-moa/train_features.csv')
 train_targets = pd.read_csv('.. /input/lish-moa/train_targets_scored.csv')
 test_features = pd.read_csv('.. /input/lish-moa/test_features.csv')
 submission = pd.read_csv('.. /input/lish-moa/sample_submission.csv' )
def lag_feature(df,lags,col):
tmp=df[['date_block_num','shop_id','item_id',col]]
for i in lags:
shifted=tmp.copy()
shifted.columns=['date_block_num','shop_id','item_id',col+'_lag'+str(i)]
shifted['date_block_num']+=i
df=pd.merge(df,shifted,on=['date_block_num','shop_id','item_id'],how='left')
 return df<concatenate>
def preprocess(df):
df = df.copy()
df.loc[:, 'cp_type'] = df.loc[:, 'cp_type'].map({'trt_cp': 0, 'ctl_vehicle': 1})
 df.loc[:, 'cp_dose'] = df.loc[:, 'cp_dose'].map({'D1': 0, 'D2': 1})
 del df['sig_id']
return df
train = preprocess(train_features)
 test = preprocess(test_features)
 del train_targets['sig_id']
train_targets = train_targets.loc[train['cp_type']==0].reset_index(drop=True)
 train = train.loc[train['cp_type']==0].reset_index(drop=True )
matrix=lag_feature(matrix,[1,2,3,6,12],'item_cnt_month' )<merge>
def create_model(num_columns):
model = tf.keras.Sequential([
tf.keras.layers.Input(num_columns),
tf.keras.layers.BatchNormalization() ,
tfa.layers.WeightNormalization(tf.keras.layers.Dense(2048, activation="relu")) ,
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Dropout(0.3),
tfa.layers.WeightNormalization(tf.keras.layers.Dense(2048, activation="relu")) ,
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Dropout(0.2),
tfa.layers.WeightNormalization(tf.keras.layers.Dense(206, activation="sigmoid"))
])
 model.compile(optimizer=tfa.optimizers.Lookahead(tf.optimizers.Adam() , sync_period=10),
loss='binary_crossentropy' 
)
 return model
def add_mean_feature(df,addname,grouplist,time):
group_tmp=df.groupby(grouplist ).agg({'item_cnt_month':'mean'})
 group_tmp.columns=addname
group_tmp.reset_index(inplace=True)
 df=pd.merge(df,group_tmp,on=grouplist,how='left')
 df=lag_feature(df,time,addname[0])
 df.drop(addname,axis=1,inplace=True)
 return df<feature_engineering>
def metric(y_true, y_pred):
metrics = []
for _target in train_targets.columns:
metrics.append(log_loss(y_true.loc[:, _target], y_pred.loc[:, _target], labels=[0,1]))
return np.mean(metrics )
matrix=add_mean_feature(matrix,['date_avg_item_cnt'],['date_block_num'],[1])
 matrix=add_mean_feature(matrix,['date_item_avg_item_cnt'],['date_block_num','item_id'],[1,2,3,6,12])
 matrix=add_mean_feature(matrix,['date_shop_avg_item_cnt'],['date_block_num','shop_id'],[1,2,3,6,12])
 matrix=add_mean_feature(matrix,['date_cat_avg_item_cnt'],['date_block_num','item_category_id'],[1])
 matrix=add_mean_feature(matrix,['date_type_avg_item_cnt'],['date_block_num','type_code'],[1])
 matrix=add_mean_feature(matrix,['date_subtype_avg_item_cnt'],['date_block_num','subtype_code'],[1])
 matrix=add_mean_feature(matrix,['date_city_avg_item_cnt'],['date_block_num','city_code'],[1])
 matrix=add_mean_feature(matrix,['date_shop_cat_avg_item_cnt'],['date_block_num','shop_id','item_category_id'],[1])
 matrix=add_mean_feature(matrix,['date_shop_type_avg_item_cnt'],['date_block_num','shop_id','type_code'],[1])
 matrix=add_mean_feature(matrix,['date_shop_subtype_avg_item_cnt'],['date_block_num','shop_id','subtype_code'],[1])
 matrix=add_mean_feature(matrix,['date_item_city_avg_item_cnt'],['date_block_num','item_id','city_code'],[1] )<data_type_conversions>
N_STARTS =3
tf.random.set_seed(42)
 res = train_targets.copy()
submission.loc[:, train_targets.columns] = 0
res.loc[:, train_targets.columns] = 0
for seed in range(N_STARTS):
for n,(tr, te)in enumerate(KFold(n_splits=7, random_state=seed, shuffle=True ).split(train_targets)) :
print(f'Fold Started - {n}')
 model = create_model(len(train.columns))
reduce_lr_loss = ReduceLROnPlateau(monitor='val_loss', factor=0.1, patience=5, verbose=1, epsilon=1e-3, mode='max')
 model.fit(train.values[tr],
train_targets.values[tr],
validation_data=(train.values[te], train_targets.values[te]),
epochs=25, batch_size=64,shuffle=True,
callbacks=[reduce_lr_loss], verbose=2 
)
 test_predict = model.predict(test.values)
 val_predict = model.predict(train.values[te])
 submission.loc[:, train_targets.columns] += test_predict
res.loc[te, train_targets.columns] += val_predict
print('Fold Completed - ',n)
 submission.loc[:, train_targets.columns] /=(( n+1)* N_STARTS)
 res.loc[:, train_targets.columns] /= N_STARTS
train_f=train.drop(['date','item_cnt_day'],axis=1)
 train_f[['date_block_num','shop_id']]=train_f[['date_block_num','shop_id']].astype(np.int8)
 train_f[['item_id']]=train_f[['item_id']].astype(np.int16)
 train_f[['item_price','revenue']]=train_f[['item_price','revenue']].astype(np.float16)
 train_f.info()<merge>
submission.loc[test['cp_type']==1, train_targets.columns] = 0
def add_price_feature(mat,df,addname,grouplist):
group_tmp=df.groupby(grouplist ).agg({'item_price':'mean'})
 group_tmp.columns=addname
group_tmp[addname]=group_tmp[addname].astype(np.float16)
 group_tmp.reset_index(inplace=True)
 mat=pd.merge(mat,group_tmp,on=grouplist,how='left')
 return mat<feature_engineering>
submission.to_csv('submission.csv', index=False )
<data_type_conversions><EOS> 
submission.to_csv('submission.csv', index=False )