Kaggle competition: Mechanisms of Action (MoA) Prediction
<SOS> metric: MeanColumnwiseLogLoss Kaggle data source: mechanisms-of-action-(moa)-prediction<merge>
!pip install --no-index --find-links /kaggle/input/pytorchtabnet/pytorch_tabnet-2.0.0-py3-none-any.whl pytorch-tabnet
!pip install /kaggle/input/iterative-stratification/iterative-stratification-master/
train_match = pd.merge(train_match, item, on=['item_id'], how = 'left')
 train_match = pd.merge(train_match, ic, on=['item_category_id'], how = 'left')
 train_match.head()<merge>
sns.set_style('white')
 sns.set(font_scale=1.2)
def create_lag_feature(df,lags,col):
tmp = df[['date_block_num','shop_id','item_id',col]]
for lag in lags:
shifted = tmp.copy()
shifted.columns = ['date_block_num','shop_id','item_id',col+'_lag_'+str(lag)]
shifted['date_block_num'] += lag
df = pd.merge(df, shifted, on=['date_block_num','shop_id','item_id'],how = 'left')
 return df<feature_engineering>
def seed_everything(seed_value):
random.seed(seed_value)
 np.random.seed(seed_value)
 torch.manual_seed(seed_value)
 os.environ['PYTHONHASHSEED'] = str(seed_value)
 if torch.cuda.is_available() :
torch.cuda.manual_seed(seed_value)
 torch.cuda.manual_seed_all(seed_value)
 torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
seed_everything(42 )
train_match = create_lag_feature(train_match,[1,2,3,6,12],'item_cnt_month' )<groupby>
df_train = pd.read_csv('/kaggle/input/lish-moa/train_features.csv')
 display(df_train.head(3))
print('train data size', df_train.shape)
 df_train.drop(columns=["sig_id"], inplace=True)
 df_target_ns = pd.read_csv('/kaggle/input/lish-moa/train_targets_nonscored.csv')
 display(df_target_ns.head(3))
print('train target nonscored size', df_target_ns.shape)
 df_target_s = pd.read_csv('/kaggle/input/lish-moa/train_targets_scored.csv')
 display(df_target_s.head(3))
print('train target scored size', df_target_s.shape)
 df_target_s.drop(columns=["sig_id"], inplace=True)
 df_test = pd.read_csv('/kaggle/input/lish-moa/test_features.csv')
 display(df_test.head(3))
print('test data size', df_test.shape)
 df_test.drop(columns=["sig_id"], inplace=True)
 df_sample = pd.read_csv('/kaggle/input/lish-moa/sample_submission.csv')
 display(df_sample.head(3))
print('sample submission size', df_sample.shape)
 remove_vehicle = False
if remove_vehicle:
kept_index = df_train['cp_type']=='trt_cp'
df_train = df_train.loc[kept_index].reset_index(drop=True)
 df_target_s = df_target_s.loc[kept_index].reset_index(drop=True)
 df_train["cp_type"] =(df_train["cp_type"]=="trt_cp")+ 0
df_train["cp_dose"] =(df_train["cp_dose"]=="D1")+ 0
df_test["cp_type"] =(df_test["cp_type"]=="trt_cp")+ 0
df_test["cp_dose"] =(df_test["cp_dose"]=="D1")+ 0
X_test = df_test.values
grouped2 = train_match.groupby(['date_block_num'] ).agg({'item_cnt_month':['mean']})
 grouped2.columns = ['avg_item_cnt_month']
grouped2.reset_index(inplace = True )<merge>
MAX_EPOCH=200
tabnet_params = dict(n_d=24, n_a=24, n_steps=1, gamma=1.3,
lambda_sparse=0, optimizer_fn=torch.optim.Adam,
optimizer_params=dict(lr=2e-2, weight_decay=1e-5),
mask_type='entmax',
scheduler_params=dict(mode="min",
patience=5,
min_lr=1e-5,
factor=0.9,),
scheduler_fn=torch.optim.lr_scheduler.ReduceLROnPlateau,
verbose=10,
)
train_match = pd.merge(train_match, grouped2, on=['date_block_num'], how='left')
 train_match['avg_item_cnt_month'] = train_match['avg_item_cnt_month'].astype('float16')
 train_match = create_lag_feature(train_match, [1],'avg_item_cnt_month')
 train_match.drop(['avg_item_cnt_month'],axis =1, inplace = True )<merge>
class LogitsLogLoss(Metric):

 def __init__(self):
self._name = "logits_ll"
self._maximize = False
def __call__(self, y_true, y_pred):

 logits = 1 /(1 + np.exp(-y_pred))
aux =(1-y_true)*np.log(1-logits+1e-15)+ y_true*np.log(logits+1e-15)
 return np.mean(-aux )
grouped3 = train_match.groupby(['date_block_num','item_id'] ).agg({'item_cnt_month':['mean']})
 grouped3.columns = ['item_avg_item_cnt_month']
grouped3.reset_index(inplace = True)
 train_match = pd.merge(train_match, grouped3, on=['date_block_num','item_id'], how ='left')
 train_match['item_avg_item_cnt_month'] = train_match['item_avg_item_cnt_month'].astype('float16')
 train_match = create_lag_feature(train_match, [1,2,3,6,12], 'item_avg_item_cnt_month')
 train_match.drop(['item_avg_item_cnt_month'], axis =1, inplace =True )<merge>
scores_auc_all= []
test_cv_preds = []
NB_SPLITS = 5
mskf = MultilabelStratifiedKFold(n_splits=NB_SPLITS, random_state=0, shuffle=True)
 y_preds = []
y_targets = []
scores = []
scores_auc = []
for fold_nb,(train_idx, val_idx)in enumerate(mskf.split(df_train, df_target_s)) :
print("FOLDS : ", fold_nb)
 X_train, y_train = df_train.values[train_idx, :], df_target_s.values[train_idx, :]
X_val, y_val = df_train.values[val_idx, :], df_target_s.values[val_idx, :]
model = TabNetRegressor(**tabnet_params)
 model.fit(X_train=X_train,
y_train=y_train,
eval_set=[(X_val, y_val)],
eval_name = ["val"],
eval_metric = ["logits_ll"],
max_epochs=MAX_EPOCH,
patience=20, batch_size=1024, virtual_batch_size=128,
num_workers=1, drop_last=False,
loss_fn=torch.nn.functional.binary_cross_entropy_with_logits)
 preds_val = model.predict(X_val)
 preds = 1 /(1 + np.exp(-preds_val))
score = np.min(model.history["val_logits_ll"])
 y_preds.append(preds_val)
 y_targets.append(y_val)
 scores.append(score)
 preds_test = model.predict(X_test)
 test_cv_preds.append(1 /(1 + np.exp(-preds_test)))
 y_preds_all = np.concatenate(y_preds)
 y_targets_all = np.concatenate(y_targets)
 test_preds_all = np.stack(test_cv_preds )
grouped4 = train_match.groupby(['date_block_num','shop_id'] ).agg({'item_cnt_month':['mean']})
 grouped4.columns = ['shop_avg_item_cnt_month']
grouped4.reset_index(inplace = True)
 train_match = pd.merge(train_match, grouped4, on=['date_block_num','shop_id'], how ='left')
 train_match['shop_avg_item_cnt_month'] = train_match['shop_avg_item_cnt_month'].astype('float16')
 train_match = create_lag_feature(train_match, [1,2,3,6,12], 'shop_avg_item_cnt_month')
 train_match.drop(['shop_avg_item_cnt_month'], axis =1, inplace =True )<merge>
aucs = []
for task_id in range(y_preds_all.shape[1]):
aucs.append(roc_auc_score(y_true=y_targets_all[:, task_id],
y_score=y_preds_all[:, task_id]))
print(f"Overall AUC : {np.mean(aucs)}")
 print(f"Average CV : {np.mean(scores)}" )
grouped5 = train_match.groupby(['date_block_num','item_category_id'] ).agg({'item_cnt_month':['mean']})
 grouped5.columns = ['ic_avg_item_cnt_month']
grouped5.reset_index(inplace = True)
 train_match = pd.merge(train_match, grouped5, on=['date_block_num','item_category_id'], how ='left')
 train_match['ic_avg_item_cnt_month'] = train_match['ic_avg_item_cnt_month'].astype('float16')
 train_match = create_lag_feature(train_match, [1], 'ic_avg_item_cnt_month')
 train_match.drop(['ic_avg_item_cnt_month'], axis =1, inplace =True )<merge>
all_feat = [col for col in df_sample.columns if col not in ["sig_id"]]
df_sample[all_feat] = test_preds_all.mean(axis=0)
 df_sample.loc[df_test['cp_type']==0, df_sample.columns[1:]] = 0
df_sample.to_csv('submission.csv', index=None)
 df_submit = pd.read_csv('./submission.csv')
 df_submit.head()