Kaggle competition: Mechanisms of Action (MoA) Prediction
<SOS> metric: MeanColumnwiseLogLoss Kaggle data source: mechanisms-of-action-(moa)-prediction<drop_column>
!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/
data.drop(columns=['PassengerId','Name','Ticket','Cabin'],inplace=True )<load_from_csv>
os.environ["CUDA_LAUNCH_BLOCKING"] = "1"
train=pd.read_csv('.. /input/titanic/train.csv')
 test=pd.read_csv('.. /input/titanic/test.csv' )<correct_missing_values>
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 )
test['Fare'].fillna(test['Fare'].mean() ,inplace=True )<feature_engineering>
data_path = ".. /input/lish-moa/"
train = pd.read_csv(data_path+'train_features.csv')
 train.drop(columns=["sig_id"], inplace=True)
 train_targets = pd.read_csv(data_path+'train_targets_scored.csv')
 train_targets.drop(columns=["sig_id"], inplace=True)
 test = pd.read_csv(data_path+'test_features.csv')
 test.drop(columns=["sig_id"], inplace=True)
 submission = pd.read_csv(data_path+'sample_submission.csv')
 remove_vehicle = True
if remove_vehicle:
kept_index = train['cp_type']=='trt_cp'
train = train.loc[kept_index].reset_index(drop=True)
 train_targets = train_targets.loc[kept_index].reset_index(drop=True)
 train["cp_type"] =(train["cp_type"]=="trt_cp")+ 0
train["cp_dose"] =(train["cp_dose"]=="D1")+ 0
test["cp_type"] =(test["cp_type"]=="trt_cp")+ 0
test["cp_dose"] =(test["cp_dose"]=="D1")+ 0
X_test = test.values
datas=[train,test]
for data in datas:
data['Ticket_type']=data['Ticket'].apply(lambda x: x[0:3])
 data['Ticket_type']=data['Ticket_type'].astype('category' ).cat.codes
data['Words_counts']=data['Name'].apply(lambda x: len(x.split()))
data['cabin_or_not']=data["Cabin"].apply(lambda x: 0 if type(x)== float else 1)
 data['Family_size']=data['SibSp'] + data['Parch'] + 1
data['IsAlone'] = 0
data.loc[data['Family_size'] == 1, 'IsAlone'] = 1
data['Embarked'] = data['Embarked'].fillna('S')
 data['fare_cat']=pd.qcut(data['Fare'], 4)
 data['Age'].fillna(data['Age'].mean() ,inplace=True)
 data['cat_age']=pd.cut(data['Age'],5)
 title=[]
for i in range(0,len(data)) :
title.append(re.search('([A-Za-z]+)\.', data['Name'][i] ).group(1))
title=pd.Series(title)
 title=title.replace(['Lady', 'Countess','Capt', 'Col','Don', 'Dr', 'Major', 'Rev', 'Sir', 'Jonkheer', 'Dona'],'Rare')
 title=title.replace('Mlle','Miss')
 title=title.replace('Ms','Miss')
 title=title.replace('Mme','Mrs')
 data['Title']=title
data['Sex'] = data['Sex'].map({'female': 0, 'male': 1} ).astype(int)
 dic1= {"Mr": 1, "Miss": 2, "Mrs": 3, "Master": 4, "Rare": 5}
data['Title'] = data['Title'].map(dic1)
 data['Title'] =data['Title'].fillna(0)
 data['Embarked'] = data['Embarked'].map({'S': 0, 'C': 1, 'Q': 2} ).astype('int')
 data['fare_cat']=data['fare_cat'].astype('category' ).cat.codes.astype('int')
 data['cat_age']=data['cat_age'].astype('category' ).cat.codes.astype('int')
 data.drop(columns=['PassengerId','Name','Ticket','Cabin'],inplace=True)
<prepare_x_and_y>
from iterstrat.ml_stratifiers import MultilabelStratifiedKFold, MultilabelStratifiedShuffleSplit
from sklearn.metrics import roc_auc_score
x=train.drop(columns=['Survived'])
 y=train['Survived']<train_model>
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,
)
log_model = LogisticRegression(C=22 ).fit(x, y )<predict_on_test>
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))
ll =(1-y_true)*np.log(1-logits + 1e-15)+ y_true*np.log(logits + 1e-15)
 return np.mean(-ll)
 class PartialLogitsLogLoss(Metric):
def __init__(self):
self._name = "logits_ll(partial)"
self._maximize = False
def __call__(self, y_true, y_pred):
y_true = y_true
y_pred = y_pred
logits = 1 /(1 + np.exp(-y_pred))
logloss =(1-y_true)*np.log(1-logits + 1e-15)+ y_true*np.log(logits + 1e-15)
 return np.mean(-logloss )
submitssion_dis={'PassengerId':[a for a in range(892,1310)]
,'Survived':log_model.predict(test)}<save_to_csv>
use_cols = [col for col in train_targets.columns if train_targets[col].mean() > 0.01]
submission=pd.DataFrame(submitssion_dis)
 submission.to_csv('Submission_out.csv',index=False )<load_from_csv>
scores_auc_all= []
test_cv_preds = []
NB_SPLITS = 10
mskf = MultilabelStratifiedKFold(n_splits=NB_SPLITS, random_state=0, shuffle=True)
 oof_preds = []
oof_targets = []
scores = []
scores_auc = []
for fold_nb,(train_idx, val_idx)in enumerate(mskf.split(train, train_targets)) :
print("FOLDS : ", fold_nb)
 X_train, y_train = train.values[train_idx, :], train_targets[use_cols].values[train_idx, :]
X_val, y_val = train.values[val_idx, :], train_targets[use_cols].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', 'logits_ll(partial)'],
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(partial)"])
 oof_preds.append(preds_val)
 oof_targets.append(y_val)
 scores.append(score)
 preds_test = model.predict(X_test)
 test_cv_preds.append(1 /(1 + np.exp(-preds_test)))
 oof_preds_all = np.concatenate(oof_preds)
 oof_targets_all = np.concatenate(oof_targets)
 test_preds_all = np.stack(test_cv_preds )
train=pd.read_csv('/kaggle/input/titanic/train.csv')
 test=pd.read_csv('/kaggle/input/titanic/test.csv')
 train.head()<create_dataframe>
aucs = []
for task_id in range(oof_preds_all.shape[1]):
aucs.append(roc_auc_score(y_true=oof_targets_all[:, task_id],
y_score=oof_preds_all[:, task_id]))
print(f"Overall AUC : {np.mean(aucs)}")
 print(f"Average CV : {np.mean(scores)}" )
df = train.copy()<create_dataframe>
all_feat = [col for col in submission.columns if col not in ["sig_id"]]
other = [col for col in all_feat if col not in use_cols]
submission[use_cols] = test_preds_all.mean(axis=0)
 submission[other] = train_targets[other].mean().values
submission.loc[test['cp_type']==0, submission.columns[1:]] = 0
submission.to_csv('submission.csv', index=None )