application_vnd.jupyter.stderr_output_1.png
from sklearn.metrics import log_loss

import numpy as np

import pandas as pd

intercept = 0.9668835542286371
coef = [-0.964522806, 1.17172755, 0.179847765, 0.715775158, 0.943909901, 0.771809223, 0.166641048, 3.36556037, -3.04227717, -1.27799877, 3.24426534, 0.0839780267, -2.01609571, -0.204835338, -0.915522223, -2.85557145, 0.193031923, -0.610949126, 2.36655937, -0.688545522, -0.413897388, 2.36123594, -5.32875178, -0.257607263, -1.59041078, 0.56651361, -4.30048221, 0.583728152, -0.755718528, -4.85939641, -4.77435586, 1.20158973, 1.52684705, 1.42038088, -0.470860607, -1.02184815, -0.186602382, 0.350855131, -2.86119402, 0.181263079, -3.98183995, 3.85179998, 0.595682016, 0.976153845, 0.456293927, -1.33132314, 0.363681261, 0.794664912, -3.9104109, -0.120555465, -3.9104109, -2.9114099, 1.36755837, 2.53799497, -4.6606329, -0.630443626, 0.166641048, -7.08818234, -1.03160754, -0.214803976, 4.86850322, 1.36755837, -2.9660072, -1.35837391, -5.08890666, 1.20158973, -3.9104109, -6.2872943, -0.297537878, -2.18535836, -2.59118068, 3.86950222, -0.633065361, -1.89416541, -0.448762478, 0.40582232, 1.36755837, 1.14356994, -1.51346861, -1.77735286, 0.684120905, 1.21749418, -2.01609571, -1.33132314, 0.42970794, -2.50444579, -0.413897388, 1.46527571, 0.475974538, 1.20782761, -1.79853836, 7.02807914, -5.51602225, 0.0839780267, 4.33789914, -1.96700816, -4.87378955, -0.385577422, 1.38106353, -4.9640092, 3.8952826, -2.9189663, 1.36755837, 0.684120905, -4.47665162, -1.27799877, 1.36755837, 3.36556037, -0.204835338, -0.599912328, -0.204314343, -4.45619265, -1.26221209, -1.55571983, -1.93596293, -0.0675983678, -2.55472083, -0.610949126, 2.36655937, -1.85313989, -0.962103086, 4.36456137, 4.86850322, -2.92095622, -1.42047927, 4.36456137, 2.36655937, 0.179847765, 2.61138626, -1.2224172, 2.15956162, 1.36755837, -1.33363593, 2.31341538, -2.49972847, -2.05475149, 0.0309957974, -0.0693110501, 2.36655937, 0.581666839, 4.36456137, -4.30048221, -2.8726154, -3.08550042, -1.08649626, -0.755718528, 1.08199981, -1.42047927, 0.806595523, 4.86850322, 0.409200801, -1.9670062, -0.0693110501, -3.18329286, -2.27241201, -0.00208025864, 0.778295227, -1.16768184, 3.36556037, 2.9219428, 0.121032855, -1.13677382, 4.36456137, -5.05666468, 2.54763868, -2.05653977, -0.836236669, -1.37407599, -1.52765698, -0.55544063, -4.77435586, 0.392717805, 2.54763868, -0.245071712, 0.806595523, 0.42970794, 1.61238526, 5.11333011, 4.11432911, 1.20782761, -1.13677382, -1.39447556, -2.2753443, -1.00108126, 2.61138626, 1.61238526, -2.1169698, -2.04970922, -3.055864, -2.06237301, -5.30942364, -3.060378, 4.56786669, 0.724590988, -1.19599384, -1.69171272, -0.599912328, 0.409200801, 4.36456137, -0.232589722, 2.05197573, -0.304659185, -2.4147726, -0.665305184, 0.794664912, -0.523026461, 1.48758438, -2.1169698, -0.599912328, -1.06918017, -2.92509555, -2.24778342, -2.05998755, -1.91946605, -1.27341101, 3.42621179, -0.945321798, -5.05666468, -1.16768184, -0.385577422, 2.36655937, 1.54600888, -1.96908321, 1.48758438, 1.36755837, -2.93685912, -0.863282842, -1.77796891, 1.36755837, 5.1035469, 3.07753467, 3.36556037, -1.70669288, -3.055864, -5.71550981, 1.35386262, 2.36655937, -0.945456361, -0.945321798, 0.456293927, -0.001963082, -2.52981213, -0.633065361, 4.36456137, -3.22981906, -2.46960559, -2.46960559, 1.26188506, 1.27495232, 1.27947673, -2.71404316, -1.71703518, -1.71742525]
def layers_num(x):
    return int(np.round(np.dot(np.array(x), coef) + intercept))
col = pd.read_csv('../input/lish-moa/train_targets_scored.csv').columns[1:]
startword = ['-', 'erase', 'acetylcholine', 'receptor_agonist', 'atp', 'anta', 'abl', 'peptide', 'tgf', 'calcicycl', 'hsp', 'im', 'proteas', 'secret', 'synth']
for subword in startword:
    col = [drtitle.replace(subword, '_' + subword + '_') for drtitle in col]
all = [drtitle.split('_') for drtitle in col]
all = sorted([word for item in all for word in item])
all = [word for word in all if len(word) > 0]
allcounted = {word: all.count(word) for word in all}
allcounted = {key: value for key, value in allcounted.items()}
allcounted = list(allcounted.keys()) + ['receptor_antagonist', 'tlr_antagonist', 'tlr_antagonist', 'acetylcholine_receptor_antagonist', 'cannabinoid_receptor_antagonist', 'opioid_receptor_antagonist', 'ppar_receptor_antagonist', 'progesterone_receptor_antagonist', 'serotonin_receptor_antagonist']

eps = 10 ** (-7)
def cube(x):
    x /= 10.0
    return 10.0 * x * x * x
def tgfunc(x):
    return np.tan(np.pi * x / 4.0)
def application_transform(x):
    """  x = 2*x-1.0
    x1 = weight_tg*tgfunc(x) + (weight_x3 + weight_x5*x**2)*x**3 + weight*x
    x1 = x1/2 + 0.5"""
    x1 = x
    if x1 < eps:
        x1 = eps
    elif x1 >= 1 - eps:
        x1 = 1 - eps
    return x1
def transform(x):
    x = x[x['cp_type'] == 'trt_cp']
    x.pop('cp_type')
    x['cp_dose'] = x['cp_dose'].replace({'D1': -1, 'D2': 1})
    x['cp_time'] = x['cp_time'] // 24
    genes = [feature for feature in x.columns if feature.startswith('g-')]
    cells = [feature for feature in x.columns if feature.startswith('c-')]
    x['g-mean'] = x[genes].mean(axis=1)
    x['g-std'] = x[genes].std(axis=1)
    x['g-kur'] = x[genes].kurtosis(axis=1)
    x['g-skew'] = x[genes].skew(axis=1)
    x['c-mean'] = x[cells].mean(axis=1)
    x['c-std'] = x[cells].std(axis=1)
    x['c-kur'] = x[cells].kurtosis(axis=1)
    x['c-skew'] = x[cells].skew(axis=1)
    x['mean'] = x[genes + cells].mean(axis=1)
    x['std'] = x[genes + cells].std(axis=1)
    x['kur'] = x[genes + cells].kurtosis(axis=1)
    x['skew'] = x[genes + cells].skew(axis=1)
    x.join(pd.get_dummies(x['cp_time']))
    x.pop('cp_time')
    return x
def transform2(x):
    x.pop('cp_type')
    x['cp_dose'] = x['cp_dose'].replace({'D1': -1, 'D2': 1})
    x['cp_time'] = x['cp_time'] // 24
    genes = [feature for feature in x.columns if feature.startswith('g-')]
    cells = [feature for feature in x.columns if feature.startswith('c-')]
    x['g-mean'] = x[genes].mean(axis=1)
    x['g-std'] = x[genes].std(axis=1)
    x['g-kur'] = x[genes].kurtosis(axis=1)
    x['g-skew'] = x[genes].skew(axis=1)
    x['c-mean'] = x[cells].mean(axis=1)
    x['c-std'] = x[cells].std(axis=1)
    x['c-kur'] = x[cells].kurtosis(axis=1)
    x['c-skew'] = x[cells].skew(axis=1)
    x['mean'] = x[genes + cells].mean(axis=1)
    x['std'] = x[genes + cells].std(axis=1)
    x['kur'] = x[genes + cells].kurtosis(axis=1)
    x['skew'] = x[genes + cells].skew(axis=1)
    x.join(pd.get_dummies(x['cp_time']))
    x.pop('cp_time')
    return x
X_all = pd.read_csv('/kaggle/input/lish-moa/train_features.csv')
y_all = pd.read_csv('/kaggle/input/lish-moa/train_targets_scored.csv')
X = transform(X_all[X_all.columns[1:]]).astype(np.float16)
Y = y_all[X_all['cp_type'] == 'trt_cp']
Y = Y[Y.columns[1:]].astype(np.int8)
def metric(y_true, y_pred):
    metrics = []
    for _target in train_targets.columns:
        metrics.append(log_loss(y_true.loc[:, _target], y_pred.loc[:, _target].astype(float), labels=[0, 1]))
    return np.mean(metrics)