Kaggle competition: Mechanisms of Action (MoA) Prediction
<SOS> metric: MeanColumnwiseLogLoss Kaggle data source: mechanisms-of-action-(moa)-prediction<create_dataframe>
!pip install --no-index --find-links /kaggle/input/pytorchtabnet/pytorch_tabnet-2.0.1-py3-none-any.whl pytorch-tabnet
cat_feat_to_encode = train.columns.tolist() ; smoothing=0.20
oof = pd.DataFrame([])
<categorify>
X, y, genes, cells, classnames, features, X_test, test_control, submission = load()
splits = sorted(glob.glob(".. /input/moa-pipelines/x*.pkl"))
folds, pipes = list(zip(*[joblib.load(split)for split in splits]))
print(splits )
for tr_idx, oof_idx in StratifiedKFold(n_splits=5, random_state=2600, shuffle=True ).split(train, train_y):
ce_target_encoder = ce.TargetEncoder(cols = cat_feat_to_encode, smoothing=smoothing)
 ce_target_encoder.fit(train.iloc[tr_idx, :], train_y.iloc[tr_idx])
 oof = oof.append(ce_target_encoder.transform(train.iloc[oof_idx, :]), ignore_index=False)
 ce_target_encoder = ce.TargetEncoder(cols = cat_feat_to_encode, smoothing=smoothing)
 ce_target_encoder.fit(train, train_y)
 train = oof.sort_index()
test = ce_target_encoder.transform(test)
 glm =LogisticRegression(random_state=2, solver='lbfgs', max_iter=20600, fit_intercept=True, penalty='l2', verbose=0)
 glm.fit(train, train_y)
<save_to_csv>
def tabnet_oof(splits, seeds, pretrained_pipes, tabnet_params, train_params, stop=-1):
S = len(seeds)
 oof_train = np.zeros(( len(X), y.shape[1]), dtype=np.float64)
 oof_test = np.zeros(( len(X_test[~test_control]), y.shape[1]), dtype=np.float64)
 for split,(folds, seed)in enumerate(zip(splits, seeds)) :
nfolds = len(set(folds))
folds_oof = np.zeros(( len(X), y.shape[1]), dtype=np.float64)
 for fold,(t, v)in enumerate(index2folds(folds)) :
pipe = pretrained_pipes[split][fold]
xtrain, xvalid, xtest = X[t].copy() , X[v].copy() , X_test[~test_control].copy()
xtrain = pipe.transform(xtrain)
 xvalid = pipe.transform(xvalid)
 xtest = pipe.transform(xtest)
 model = TabNetRegressor(seed=seed, **tabnet_params)
 model.fit(X_train = xtrain, y_train = y[t].copy() , eval_set = [(xvalid, y[v].copy())], **train_params)
 fold_preds = sigmoid(model.predict(xvalid))
test_preds = sigmoid(model.predict(xtest))
folds_oof[v] += fold_preds
oof_test +=(test_preds / nfolds)
 torch.save(model.network.state_dict() , f"split{split}_seed{seed}_fold{fold}.pth")
 if fold == stop: break
oof_train += folds_oof
oof_train /= S
oof_test /= S
return oof_train, oof_test
pd.DataFrame({'id': test_id, 'target': glm.predict_proba(test)[:,1]} ).to_csv('submission.csv', index=False )<set_options>
class LogitsLogLoss(Metric):
def __init__(self):
self._name = "logits_ll"
self._maximize = False
def __call__(self, y_true, y_pred):
return logloss(y_true, sigmoid(y_pred))
def sigmoid(z):
return 1 /(1 + np.exp(-z))
MAX_EPOCH = 200
MASK_TYPE = "entmax"
N = 36
N_STEPS = 1
BATCH_SIZE = 1024
BATCH_SIZE_V = 32
LR = 2e-2
LAMBDA_SPARSE = 0
N_IND = 2
N_SHARED = 2
MOMENTUM = 0.02
PL_FACTOR = 0.9
rdpt_params = dict(mode = "min", patience = 5, min_lr = 1e-5, factor = PL_FACTOR)
 tabnet_params = dict(
 n_d = N,
n_a = N,
n_steps = N_STEPS,
lambda_sparse = LAMBDA_SPARSE,
mask_type = MASK_TYPE,
n_independent = N_IND,
n_shared = N_SHARED,
momentum = MOMENTUM,
optimizer_fn = optim.Adam,
optimizer_params = dict(lr=LR, weight_decay=1e-5),
gamma = 1.3,
scheduler_params = rdpt_params,
scheduler_fn = optim.lr_scheduler.ReduceLROnPlateau,
verbose = 10 
)
 train_params = dict(
 eval_name = ["val"],
eval_metric = ["logits_ll"],
max_epochs = MAX_EPOCH,
batch_size = BATCH_SIZE,
virtual_batch_size = BATCH_SIZE_V,
patience = 20,
num_workers = 1,
drop_last = False,
loss_fn = F.binary_cross_entropy_with_logits
)
warnings.filterwarnings("ignore")
<load_from_csv>
data = [
folds,
list(range(len(folds))),
pipes
]
oof_train, oof_test = tabnet_oof(*data, tabnet_params, train_params )
raw_train = pd.read_csv('.. /input/cat-in-the-dat-ii/train.csv', index_col='id')
 raw_test = pd.read_csv('.. /input/cat-in-the-dat-ii/test.csv', index_col='id')
 print(raw_train.shape, raw_test.shape )<data_type_conversions>
print(oof_train.mean() , oof_test.mean() , y.mean())
 oof_result(y, oof_train, oof_test )
df_train = raw_train.dropna(subset=['month', 'day'])[['day', 'month', 'target']]
df_test = raw_test.dropna(subset=['month', 'day'])[['day', 'month']]
df0 = df_train[df_train.target == 0]
df1 = df_train[df_train.target == 1]
def number2datetime(df):
time_col = '2019/' + df.month.astype(int ).astype(str)+ '/' + df.day.astype(int ).astype(str)
 df['time'] = pd.to_datetime(time_col , format = '%Y/%m/%d')
 df = df.drop(columns=['day', 'month'])
 return df
df0 = number2datetime(df0)
 df1 = number2datetime(df1)
 df_test = number2datetime(df_test )<count_values>
prediction = join_control_group(oof_test, test_control, submission, classnames)
 submit_preds(prediction, submission, test_control, classnames)
 pd.read_csv("submission.csv" ).iloc[:5, :5]