text_plain_output_1.png
from sklearn.metrics import log_loss

from sklearn.model_selection import KFold

from xgboost import XGBRegressor

import matplotlib.pyplot as plt

import numpy as np # linear algebra

import pandas as pd

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

import seaborn as sns

sample = pd.read_csv('../input/lish-moa/sample_submission.csv')
test_f = pd.read_csv('../input/lish-moa/test_features.csv')
train_f = pd.read_csv('../input/lish-moa/train_features.csv')
drug = pd.read_csv('../input/lish-moa/train_drug.csv')
targ_nscore = pd.read_csv('../input/lish-moa/train_targets_nonscored.csv')
targ_score = pd.read_csv('../input/lish-moa/train_targets_scored.csv')

train = train_f.merge(targ_score, on='sig_id', how='left')
train = train.merge(targ_nscore, on='sig_id', how='left')

stargs_name = list(targ_score.columns[1:])
scored_targets = train[list(targ_score.columns[1:])].sum(axis = 1)
nscored_targets = train[list(targ_nscore.columns[1:])].sum(axis = 1)

fig,axes = plt.subplots(figsize = (32,8),ncols = 2)
sns.countplot(scored_targets,ax = axes[0])
sns.countplot(nscored_targets,ax = axes[1])
# scored_targets

for i in range(2):
    axes[i].tick_params(axis = 'x',labelsize =20)
    axes[i].tick_params(axis = 'y', labelsize = 20)

axes[0].set_title(f'Training set unique scored per sample',size = 22 , pad = 22)  
axes[1].set_title(f'Training set unique not scored per sample',size = 22 , pad = 22)   
plt.show()

test_f.shape

train_f.shape

train_f.columns

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_f)
test = preprocess(test_f)
del targ_score['sig_id']

targ_score.shape

def metric(y_true, y_pred):
    metrics = []
    metrics.append(log_loss(y_true, y_pred.astype(float), labels=[0, 1]))
    return np.mean(metrics)

cols = targ_score.columns
submission = sample.copy()
submission.loc[:, cols] = 0
submission
N_splits = 5
off_loss = 0
for c, columns in enumerate(cols, 1):
    y = targ_score[columns]
    total_loss = 0
    for fn, (trn_idx, val_idx) in enumerate(KFold(n_splits=N_splits, shuffle=True).split(train)):
        X_train, X_val = (train.iloc[trn_idx], train.iloc[val_idx])
        y_train, y_val = (y.iloc[trn_idx], y.iloc[val_idx])
        model = XGBRegressor(tree_method='gpu_hist', min_child_weight=1, learning_rate=0.015, colsample_bytree=0.65, gamma=3.69, max_delta_step=2.07, max_depth=10, n_estimators=207, subsample=1)
        model.fit(X_train, y_train)
        pred = model.predict(X_val)
        loss = metric(y_val, pred)
        total_loss += loss
        predictions = model.predict(test)
        submission[columns] += predictions / N_splits
    off_loss += total_loss / N_splits

submission