Kaggle competition: Mechanisms of Action (MoA) Prediction
kfold = StratifiedKFold(n_splits=n_splits, random_state=1337)
 oof_train_lgb = np.zeros(( X_train.shape[0]))
oof_test_lgb = np.zeros(( X_test.shape[0], n_splits))
qwk_scores = []
i = 0
for train_index, valid_index in kfold.split(X_train, X_train['AdoptionSpeed'].values):
X_tr = X_train.iloc[train_index, :]
X_val = X_train.iloc[valid_index, :]
y_tr = X_tr['AdoptionSpeed'].values
X_tr = X_tr.drop(['AdoptionSpeed'], axis=1)
 y_val = X_val['AdoptionSpeed'].values
X_val = X_val.drop(['AdoptionSpeed'], axis=1)
 print('
y_tr distribution: {}'.format(Counter(y_tr)))
 d_train = lgb.Dataset(X_tr, label=y_tr)
 d_valid = lgb.Dataset(X_val, label=y_val)
 watchlist = [d_train, d_valid]
print('training LGB:')
 model = lgb.train(params,
train_set=d_train,
num_boost_round=num_rounds,
valid_sets=watchlist,
verbose_eval=verbose_eval,
early_stopping_rounds=early_stop)
 val_pred = model.predict(X_val, num_iteration=model.best_iteration)
 optR = OptimizedRounder()
optR.fit(val_pred, y_val)
 coefficients = optR.coefficients()
pred_val_y_k = optR.predict(val_pred, coefficients)
 print("Valid Counts = ", Counter(y_val))
print("Predicted Counts = ", Counter(pred_val_y_k))
qwk = quadratic_weighted_kappa(y_val, pred_val_y_k)
 qwk_scores.append(qwk)
 print("QWK = ", qwk)
 test_pred = model.predict(X_test, num_iteration=model.best_iteration)
 oof_train_lgb[valid_index] = val_pred
oof_test_lgb[:, i] = test_pred
i += 1
print('{} cv mean QWK score : {}'.format('LGBM', np.mean(qwk_scores)) )<features_selection>
!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/
!mkdir -p /root/.cache/torch/hub/checkpoints/
!cp.. /input/gen-efficientnet-pretrained/tf_efficientnet_*.pth /root/.cache/torch/hub/checkpoints/
!ls -la /root/.cache/torch/hub/checkpoints/
!pip install.. /input/gen-efficientnet-pytorch
importance_type= "split"
idx_sort = np.argsort(model.feature_importance(importance_type=importance_type)) [::-1]
names_sorted = np.array(model.feature_name())[idx_sort]
imports_sorted = model.feature_importance(importance_type=importance_type)[idx_sort]
for n, im in zip(names_sorted, imports_sorted):
print(n, im )<compute_train_metric>
def create_submission() :
test = pd.read_csv('.. /input/lish-moa/test_features.csv')
 train_target_columns = pd.read_csv('.. /input/lish-moa/train_targets_scored.csv' ).columns
submission = pd.DataFrame(columns=train_target_columns)
 submission.sig_id = test.sig_id
submission.iloc[:, 1:] = 0.0
return submission
def load_df_list(file_paths):
df_list = []
for p in file_paths:
if type(p)is pd.DataFrame:
df_list.append(p)
 else:
df_list.append(pd.read_csv(p))
df1 = df_list[0]
for(i, df2)in enumerate(df_list[1:]):
assert len(df1)== len(df2)
 df_list[i + 1] = df2.set_index('sig_id' ).loc[df1.sig_id].reset_index()
return df_list
def apply_emsamble(df_list, weights=None):
return np.average([df.iloc[:, 1:].values for df in df_list], axis=0, weights=weights)
 def create_ensamble_submission(paths, weights=None):
submission = create_submission()
df_list = load_df_list(paths)
 ensamble = apply_emsamble(df_list, weights=weights)
 submission.iloc[:, 1:] = ensamble
print('Check NA: ', submission.isna().any().any())
 return submission
<predict_on_test><EOS> 
submission = create_ensamble_submission([
'blending-02-tabnet-submission.csv',
'blending-01-nn-0-submission.csv',
'blending-01-nn-1-submission.csv',
'blending-01-nn-2-submission.csv',
'blending-01-nn-3-submission.csv',
'blending-01-nn-4-submission.csv',
'blending-01-nn-5-submission.csv',
'deepinsignt-submission_0.csv',
'deepinsignt-submission_1.csv',
], weights=(0.5, 0.7, 0.7, 0.5, 1.5, 1.5, 0.5, 0.4, 0.5))
submission.to_csv('submission.csv', index=False)
 pd.read_csv('submission.csv' ).head()