Kaggle competition: Mechanisms of Action (MoA) Prediction
X_temp = X_temp.merge(img_features, how='left', on='PetID' )<define_variables>
import os
import gc
import pickle
import datetime
import numpy as np
import pandas as pd
from sklearn.model_selection import StratifiedKFold
from tqdm.notebook import tqdm
from time import time
train_df_ids = train[['PetID']]
test_df_ids = test[['PetID']]
train_df_imgs = pd.DataFrame(train_image_files)
 train_df_imgs.columns = ['image_filename']
train_imgs_pets = train_df_imgs['image_filename'].apply(lambda x: x.split(split_char)[-1].split('-')[0])
 test_df_imgs = pd.DataFrame(test_image_files)
 test_df_imgs.columns = ['image_filename']
test_imgs_pets = test_df_imgs['image_filename'].apply(lambda x: x.split(split_char)[-1].split('-')[0])
 train_df_imgs = train_df_imgs.assign(PetID=train_imgs_pets)
 test_df_imgs = test_df_imgs.assign(PetID=test_imgs_pets)
 def getSize(filename):
st = os.stat(filename)
 return st.st_size
def getDimensions(filename):
img_size = Image.open(filename ).size
return img_size
train_df_imgs['image_size'] = train_df_imgs['image_filename'].apply(getSize)
 train_df_imgs['temp_size'] = train_df_imgs['image_filename'].apply(getDimensions)
 train_df_imgs['width'] = train_df_imgs['temp_size'].apply(lambda x : x[0])
 train_df_imgs['height'] = train_df_imgs['temp_size'].apply(lambda x : x[1])
 train_df_imgs = train_df_imgs.drop(['temp_size'], axis=1)
 test_df_imgs['image_size'] = test_df_imgs['image_filename'].apply(getSize)
 test_df_imgs['temp_size'] = test_df_imgs['image_filename'].apply(getDimensions)
 test_df_imgs['width'] = test_df_imgs['temp_size'].apply(lambda x : x[0])
 test_df_imgs['height'] = test_df_imgs['temp_size'].apply(lambda x : x[1])
 test_df_imgs = test_df_imgs.drop(['temp_size'], axis=1)
 aggs = {
'image_size': ['sum', 'mean', 'var'],
'width': ['sum', 'mean', 'var'],
'height': ['sum', 'mean', 'var'],
}
agg_train_imgs = train_df_imgs.groupby('PetID' ).agg(aggs)
 new_columns = [
k + '_' + agg for k in aggs.keys() for agg in aggs[k]
]
agg_train_imgs.columns = new_columns
agg_train_imgs = agg_train_imgs.reset_index()
agg_test_imgs = test_df_imgs.groupby('PetID' ).agg(aggs)
 new_columns = [
k + '_' + agg for k in aggs.keys() for agg in aggs[k]
]
agg_test_imgs.columns = new_columns
agg_test_imgs = agg_test_imgs.reset_index()
agg_imgs = pd.concat([agg_train_imgs, agg_test_imgs], axis=0 ).reset_index(drop=True )<merge>
train_features = pd.read_csv('.. /input/lish-moa/train_features.csv')
 train_targets = pd.read_csv('.. /input/lish-moa/train_targets_scored.csv')
 test_features = pd.read_csv('.. /input/lish-moa/test_features.csv')
 train_targets_nonscored = pd.read_csv('.. /input/lish-moa/train_targets_nonscored.csv')
 df_train=train_features.copy()
df_train_targets=train_targets.copy()
df_test=test_features.copy()
df_train_targets_nonscored=train_targets_nonscored.copy()
ss = pd.read_csv('.. /input/lish-moa/sample_submission.csv')
 cols = [c for c in ss.columns.values if c != 'sig_id']
X_temp = X_temp.merge(agg_imgs, how='left', on='PetID' )<drop_column>
df_train = df_train[df_train['cp_type']!='ctl_vehicle']
df_test = df_test[df_test['cp_type']!='ctl_vehicle']
df_train_targets = df_train_targets.iloc[df_train.index]
df_train_targets_nonscored=df_train_targets_nonscored.iloc[df_train.index]
df_train.reset_index(drop=True, inplace=True)
 df_test.reset_index(drop=True, inplace=True)
 df_train_targets.reset_index(drop=True, inplace=True)
 df_train_targets_nonscored.reset_index(drop=True, inplace=True)
 del df_train_targets['sig_id']
del df_train_targets_nonscored['sig_id']
X_temp = X_temp.drop(to_drop_columns, axis=1 )<define_variables>
def preprocess(df):
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
def log_loss_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)
 def log_loss_metric_n(y_true, y_pred):
metrics = []
for _target in df_train_targets_nonscored.columns:
metrics.append(log_loss(y_true.loc[:, _target], y_pred.loc[:, _target].astype(float), labels = [0,1]))
return np.mean(metrics)
 train = preprocess(df_train)
 test = preprocess(df_test)
X_train_non_null = X_train.fillna(-1)
 X_test_non_null = X_test.fillna(-1 )<count_missing_values>
top_feats = [ 0, 1, 2, 3, 5, 6, 8, 9, 10, 11, 12, 14, 15,
16, 18, 19, 20, 21, 23, 24, 25, 27, 28, 29, 30, 31,
32, 33, 34, 35, 36, 37, 39, 40, 41, 42, 44, 45, 46,
48, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61,
63, 64, 65, 66, 68, 69, 70, 71, 72, 73, 74, 75, 76,
78, 79, 80, 81, 82, 83, 84, 86, 87, 88, 89, 90, 92,
93, 94, 95, 96, 97, 99, 100, 101, 103, 104, 105, 106, 107,
108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120,
121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 132, 133, 134,
135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147,
149, 150, 151, 152, 153, 154, 155, 157, 159, 160, 161, 163, 164,
165, 166, 167, 168, 169, 170, 172, 173, 175, 176, 177, 178, 180,
181, 182, 183, 184, 186, 187, 188, 189, 190, 191, 192, 193, 195,
197, 198, 199, 202, 203, 205, 206, 208, 209, 210, 211, 212, 213,
214, 215, 218, 219, 220, 221, 222, 224, 225, 227, 228, 229, 230,
231, 232, 233, 234, 236, 238, 239, 240, 241, 242, 243, 244, 245,
246, 248, 249, 250, 251, 253, 254, 255, 256, 257, 258, 259, 260,
261, 263, 265, 266, 268, 270, 271, 272, 273, 275, 276, 277, 279,
282, 283, 286, 287, 288, 289, 290, 294, 295, 296, 297, 299, 300,
301, 302, 303, 304, 305, 306, 308, 309, 310, 311, 312, 313, 315,
316, 317, 320, 321, 322, 324, 325, 326, 327, 328, 329, 330, 331,
332, 333, 334, 335, 338, 339, 340, 341, 343, 344, 345, 346, 347,
349, 350, 351, 352, 353, 355, 356, 357, 358, 359, 360, 361, 362,
363, 364, 365, 366, 368, 369, 370, 371, 372, 374, 375, 376, 377,
378, 379, 380, 382, 383, 384, 385, 386, 387, 388, 389, 390, 391,
392, 393, 394, 395, 397, 398, 399, 400, 401, 403, 405, 406, 407,
408, 410, 411, 412, 413, 414, 415, 417, 418, 419, 420, 421, 422,
423, 424, 425, 426, 427, 428, 429, 430, 431, 432, 433, 434, 435,
436, 437, 438, 441, 442, 443, 444, 445, 446, 447, 448, 449, 450,
452, 453, 454, 456, 457, 458, 459, 460, 461, 462, 463, 464, 465,
466, 468, 469, 471, 472, 473, 474, 475, 476, 477, 478, 479, 482,
483, 485, 486, 487, 488, 489, 491, 492, 494, 495, 496, 500, 501,
502, 503, 505, 506, 507, 509, 510, 511, 512, 513, 514, 516, 517,
518, 519, 521, 523, 525, 526, 527, 528, 529, 530, 531, 532, 533,
534, 536, 537, 538, 539, 540, 541, 542, 543, 544, 545, 546, 547,
549, 550, 553, 554, 555, 556, 557, 558, 559, 560, 561, 562, 563,
564, 565, 566, 567, 569, 570, 571, 572, 573, 574, 575, 577, 580,
581, 582, 583, 586, 587, 590, 591, 592, 593, 595, 596, 597, 598,
599, 600, 601, 602, 603, 605, 607, 608, 609, 611, 612, 613, 614,
615, 616, 617, 619, 622, 623, 625, 627, 630, 631, 632, 633, 634,
635, 637, 638, 639, 642, 643, 644, 645, 646, 647, 649, 650, 651,
652, 654, 655, 658, 659, 660, 661, 662, 663, 664, 666, 667, 668,
669, 670, 672, 674, 675, 676, 677, 678, 680, 681, 682, 684, 685,
686, 687, 688, 689, 691, 692, 694, 695, 696, 697, 699, 700, 701,
702, 703, 704, 705, 707, 708, 709, 711, 712, 713, 714, 715, 716,
717, 723, 725, 727, 728, 729, 730, 731, 732, 734, 736, 737, 738,
739, 740, 741, 742, 743, 744, 745, 746, 747, 748, 749, 750, 751,
752, 753, 754, 755, 756, 758, 759, 760, 761, 762, 763, 764, 765,
766, 767, 769, 770, 771, 772, 774, 775, 780, 781, 782, 783, 784,
785, 787, 788, 790, 793, 795, 797, 799, 800, 801, 805, 808, 809,
811, 812, 813, 816, 819, 820, 821, 822, 823, 825, 826, 827, 829,
831, 832, 833, 834, 835, 837, 838, 839, 840, 841, 842, 844, 845,
846, 847, 848, 850, 851, 852, 854, 855, 856, 858, 860, 861, 862,
864, 867, 868, 870, 871, 873, 874]
print(len(top_feats))
X_train_non_null.isnull().any().any() , X_test_non_null.isnull().any().any()<import_modules>
scaler = StandardScaler()
X = scaler.fit_transform(train.values[:, top_feats])
 x_tt = scaler.transform(test.values[:, top_feats] )
def confusion_matrix(rater_a, rater_b, min_rating=None, max_rating=None):

 assert(len(rater_a)== len(rater_b))
if min_rating is None:
min_rating = min(rater_a + rater_b)
 if max_rating is None:
max_rating = max(rater_a + rater_b)
 num_ratings = int(max_rating - min_rating + 1)
 conf_mat = [[0 for i in range(num_ratings)]
for j in range(num_ratings)]
for a, b in zip(rater_a, rater_b):
conf_mat[a - min_rating][b - min_rating] += 1
return conf_mat
def histogram(ratings, min_rating=None, max_rating=None):

 if min_rating is None:
min_rating = min(ratings)
 if max_rating is None:
max_rating = max(ratings)
 num_ratings = int(max_rating - min_rating + 1)
 hist_ratings = [0 for x in range(num_ratings)]
for r in ratings:
hist_ratings[r - min_rating] += 1
return hist_ratings
def quadratic_weighted_kappa(y, y_pred):

 rater_a = y
rater_b = y_pred
min_rating=None
max_rating=None
rater_a = np.array(rater_a, dtype=int)
 rater_b = np.array(rater_b, dtype=int)
 assert(len(rater_a)== len(rater_b))
if min_rating is None:
min_rating = min(min(rater_a), min(rater_b))
if max_rating is None:
max_rating = max(max(rater_a), max(rater_b))
conf_mat = confusion_matrix(rater_a, rater_b,
min_rating, max_rating)
 num_ratings = len(conf_mat)
 num_scored_items = float(len(rater_a))
hist_rater_a = histogram(rater_a, min_rating, max_rating)
 hist_rater_b = histogram(rater_b, min_rating, max_rating)
 numerator = 0.0
denominator = 0.0
for i in range(num_ratings):
for j in range(num_ratings):
expected_count =(hist_rater_a[i] * hist_rater_b[j]
/ num_scored_items)
 d = pow(i - j, 2.0)/ pow(num_ratings - 1, 2.0)
 numerator += d * conf_mat[i][j] / num_scored_items
denominator += d * expected_count / num_scored_items
return(1.0 - numerator / denominator )<train_on_grid>
N_STARTS =1
N_SPLITS =5
res = df_train_targets_nonscored.copy()
res.loc[:, df_train_targets_nonscored.columns] = 0
ss_non=pd.DataFrame(np.zeros(( df_test.shape[0],len(df_train_targets_nonscored.columns))),columns=df_train_targets_nonscored.columns)
 collect_tar_failure=[]
collect_tar_log012=[]
collect_tar=[]
for tar in tqdm(range(df_train_targets_nonscored.shape[1])) :
print(tar)
 start_time = time()
targets = df_train_targets_nonscored.values[:, tar]
if targets.sum() >= N_SPLITS:
for seed in range(N_STARTS):
skf = StratifiedKFold(n_splits = N_SPLITS, random_state = seed, shuffle = True)
 for n,(tr, te)in enumerate(skf.split(targets, targets)) :
x_tr, x_val = X[tr], X[te]
y_tr, y_val = targets[tr], targets[te]
if y_tr.sum() >= 5:
model = SVC(probability = True, cache_size = 2000)
 model.fit(x_tr, y_tr)
 ss_non.loc[:, df_train_targets_nonscored.columns[tar]] += model.predict_proba(x_tt)[:, 1] /(N_SPLITS * N_STARTS)
 res.loc[te, df_train_targets_nonscored.columns[tar]] += model.predict_proba(x_val)[:, 1] / N_STARTS
else:
print(f'Target {tar}: Seed {seed}: Fold {n}: SVC probabilistic output failure.')
 collect_tar_failure.append(tar)
 model = SVC(cache_size = 2000)
 model.fit(x_tr, y_tr)
 ss_non.loc[:, df_train_targets_nonscored.columns[tar]] += model.predict(x_tt)/(N_SPLITS * N_STARTS)
 res.loc[te, df_train_targets_nonscored.columns[tar]] += model.predict(x_val)/ N_STARTS
score = log_loss(df_train_targets_nonscored.loc[:, df_train_targets_nonscored.columns[tar]], res.loc[:, df_train_targets_nonscored.columns[tar]])
 if(score<=0.02)and(tar not in collect_tar_failure):
collect_tar_log012.append(tar)
 if tar not in collect_tar_failure:
collect_tar.append(tar)
 print(f'[{str(datetime.timedelta(seconds = time() - start_time)) [2:7]}] Target {tar}:', score )
class OptimizedRounder(object):
def __init__(self):
self.coef_ = 0
def _kappa_loss(self, coef, X, y):
preds = pd.cut(X, [-np.inf] + list(np.sort(coef)) + [np.inf], labels = [0, 1, 2, 3, 4])
 return -cohen_kappa_score(y, preds, weights='quadratic')
 def fit(self, X, y):
loss_partial = partial(self._kappa_loss, X = X, y = y)
 initial_coef = [0.5, 1.5, 2.5, 3.5]
self.coef_ = sp.optimize.minimize(loss_partial, initial_coef, method='nelder-mead')
 def predict(self, X, coef):
preds = pd.cut(X, [-np.inf] + list(np.sort(coef)) + [np.inf], labels = [0, 1, 2, 3, 4])
 return preds
def coefficients(self):
return self.coef_['x']<init_hyperparams>
print(f'Model OOF Metric: {log_loss_metric_n(df_train_targets_nonscored, res)}')
xgb_params = {
'eval_metric': 'rmse',
'seed': 1337,
'eta': 0.0123,
'subsample': 0.8,
'colsample_bytree': 0.85,
'tree_method': 'gpu_hist',
'device': 'gpu',
'silent': 1,
}<prepare_x_and_y>
df_train_svc = pd.DataFrame(np.array(res.iloc[:,collect_tar_log012]), columns=[f'svc-{i}' for i in range(len(collect_tar_log012)) ])
 df_test_svc = pd.DataFrame(np.array(ss_non.iloc[:,collect_tar_log012]), columns=[f'svc-{i}' for i in range(len(collect_tar_log012)) ] )
def run_xgb(params, X_train, X_test):
n_splits = 10
verbose_eval = 1000
num_rounds = 60000
early_stop = 500
kf = StratifiedKFold(n_splits=n_splits, shuffle=True, random_state=1337)
 oof_train = np.zeros(( X_train.shape[0]))
oof_test = np.zeros(( X_test.shape[0], n_splits))
i = 0
for train_idx, valid_idx in kf.split(X_train, X_train['AdoptionSpeed'].values):
X_tr = X_train.iloc[train_idx, :]
X_val = X_train.iloc[valid_idx, :]
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)
 d_train = xgb.DMatrix(data=X_tr, label=y_tr, feature_names=X_tr.columns)
 d_valid = xgb.DMatrix(data=X_val, label=y_val, feature_names=X_val.columns)
 watchlist = [(d_train, 'train'),(d_valid, 'valid')]
model = xgb.train(dtrain=d_train, num_boost_round=num_rounds, evals=watchlist,
early_stopping_rounds=early_stop, verbose_eval=verbose_eval, params=params)
 valid_pred = model.predict(xgb.DMatrix(X_val, feature_names=X_val.columns), ntree_limit=model.best_ntree_limit)
 test_pred = model.predict(xgb.DMatrix(X_test, feature_names=X_test.columns), ntree_limit=model.best_ntree_limit)
 oof_train[valid_idx] = valid_pred
oof_test[:, i] = test_pred
i += 1
return model, oof_train, oof_test<train_model>
data_all = pd.concat([df_train, df_test], ignore_index=True)
 print(data_all.shape)
 data_all.head()
model, oof_train, oof_test = run_xgb(xgb_params, X_train_non_null, X_test_non_null )<compute_train_metric>
def process_data(data):
data = pd.get_dummies(data, columns=['cp_time','cp_dose'])
 return data
optR = OptimizedRounder()
optR.fit(oof_train, X_train['AdoptionSpeed'].values)
 coefficients = optR.coefficients()
valid_pred = optR.predict(oof_train, coefficients)
 qwk = quadratic_weighted_kappa(X_train['AdoptionSpeed'].values, valid_pred)
 print("QWK = ", qwk )<predict_on_test>
data_all_numerical=data_all.loc[:,numerical_features]
coefficients_ = coefficients.copy()
coefficients_[0] = 1.66
coefficients_[1] = 2.13
coefficients_[3] = 2.85
train_predictions = optR.predict(oof_train, coefficients_ ).astype(np.int8)
 print(f'train pred distribution: {Counter(train_predictions)}')
 test_predictions = optR.predict(oof_test.mean(axis=1), coefficients_ ).astype(np.int8)
 print(f'test pred distribution: {Counter(test_predictions)}' )<count_values>
def variance_threshold(dataframe):
'return dataframe containing all features with higher variance are the threshold'
selector=VarianceThreshold(threshold=0.90)
 selector.fit_transform(dataframe)
 mask=selector.get_support(indices=False)
 dataframe_s=dataframe.iloc[:,mask]
print('number of features removed',dataframe.shape[1]-dataframe_s.shape[1])
 return dataframe_s
def scaling_data(scaler_name,dataframe):
'Apply a scaling method to a dataframe, which include exclusively numerical data'
'quantile_normal, rank_gauss, min_max, standard, gaussian_yeo, normal_l2, robust_scaler'
if scaler_name=='rank_gauss':
scaler =GaussRankScaler()
elif scaler_name=='standard':
scaler= StandardScaler()
elif scaler_name=='min_max':
scaler= MinMaxScaler()
elif scaler_name=='gaussian_yeo':
scaler=PowerTransformer(method='yeo-johnson', standardize=True, copy=False)
 elif scaler_name=='robust_scaler':
scaler=RobustScaler(with_centering=True, with_scaling=True, quantile_range=(25.0, 75.0), copy=False)
 elif scaler=='quantile_normal':
scaler=QuantileTransformer(n_quantiles=1000, output_distribution='normal', random_state=None, copy=False)
 elif scaler=='normal_l2':
scaler=Normalizer(norm='l2',copy=False)
 dataframe.loc[:,:]=scaler.fit_transform(dataframe.loc[:,:])
 return dataframe
Counter(train_predictions )<count_values>
data_all_var=variance_threshold(data_all_numerical)
 scaler_name='rank_gauss'
data_all_sc=scaling_data(scaler_name,data_all_numerical)
Counter(test_predictions )<save_to_csv>
data_all_sc.isnull().values.any()
submission = pd.DataFrame({'PetID': test['PetID'].values, 'AdoptionSpeed': test_predictions})
 submission.to_csv('submission.csv', index=False)
 submission.head()<import_modules>
def PCA_descriptors(data,ncompo_genes,ncompo_cells):
'introduce PCA descriptors'
data_all=data.copy()
GENES = [col for col in data_all.columns if col.startswith('g-')]
CELLS = [col for col in data_all.columns if col.startswith('c-')]
pca_genes = PCA(n_components=ncompo_genes, random_state=42 ).fit_transform(data_all[GENES])
 pca_cells = PCA(n_components=ncompo_cells, random_state=42 ).fit_transform(data_all[CELLS])
 pca_genes = pd.DataFrame(pca_genes, columns=[f'pca_g-{i}' for i in range(ncompo_genes)])
 pca_cells = pd.DataFrame(pca_cells, columns=[f'pca_c-{i}' for i in range(ncompo_cells)])
 data_pca = pd.concat([pca_genes, pca_cells], axis=1)
 return data_pca
import gc
import glob
import os
import json
import matplotlib.pyplot as plt
import pprint
import numpy as np
import pandas as pd
from joblib import Parallel, delayed
from tqdm import tqdm
from PIL import Image
from sklearn.metrics import cohen_kappa_score, make_scorer
from sklearn.model_selection import GridSearchCV, cross_val_score, train_test_split
import scipy as sp
from sklearn.model_selection import train_test_split, GridSearchCV
from functools import partial
from collections import Counter
import random
import math<compute_test_metric>
scaler_name='gaussian_yeo'
data_all_g=scaling_data(scaler_name,data_all_numerical)
 ncompo_genes = 70
ncompo_cells = 15
data_pca=PCA_descriptors(data_all_g,ncompo_genes,ncompo_cells)
def metric(y1,y2):
return cohen_kappa_score(y1, y2, weights = 'quadratic')
 scorer = make_scorer(metric )<find_best_model_class>
data_all_new=pd.concat([data_cat,data_all_sc, data_pca],axis=1)
 data_all_new.head()
def split_score(model, x_train, y_train, x_test, n=10):
y_pre = np.zeros(x_train.shape[0])
 oof_test = np.zeros(( x_test.shape[0], n))
kfold = StratifiedKFold(n_splits=n, random_state=4)
 aaa = 0
for train_index, test_index in kfold.split(x_train, y_train):
model.fit(x_train.iloc[train_index], y_train.iloc[train_index])
 y_pre[test_index] = model.predict(x_train.iloc[test_index])
 test_pred = model.predict(x_test)
 oof_test[:, aaa] = test_pred
print(aaa)
 aaa += 1
print("{}折后的Kappa加权得分为:带补充".format(n))
return y_pre, oof_test
def fix_y(y, coef):
y_fix = np.copy(y)
 for i, pred in enumerate(y_fix):
if pred < coef[0]:
y_fix[i] = 0
elif pred >= coef[0] and pred < coef[1]:
y_fix[i] = 1
elif pred >= coef[1] and pred < coef[2]:
y_fix[i] = 2
elif pred >= coef[2] and pred < coef[3]:
y_fix[i] = 3
else:
y_fix[i] = 4
return y_fix
def _kappa_loss(y, y_true, coef):
y_fix = np.copy(y)
 for i, pred in enumerate(y_fix):
if pred < coef[0]:
y_fix[i] = 0
elif pred >= coef[0] and pred < coef[1]:
y_fix[i] = 1
elif pred >= coef[1] and pred < coef[2]:
y_fix[i] = 2
elif pred >= coef[2] and pred < coef[3]:
y_fix[i] = 3
else:
y_fix[i] = 4
loss = metric(y_fix, y_true)
 return -loss
def search_coef(x1, x2):
loss_partial = partial(_kappa_loss, x1, x2)
 initial_coef = [1.55, 2.05, 2.5, 3]
coef = sp.optimize.basinhopping(loss_partial, initial_coef, niter=500, T=1,
stepsize=0.2, minimizer_kwargs={"method": 'nelder-mead'},
take_step=None, accept_test=None, callback=None,
interval=100, disp=True, niter_success=10, seed=None)
 return coef<load_from_csv>
rows_train=df_train.shape[0]
train=data_all_new[:rows_train]
test=data_all_new[df_train.shape[0]:]
test.reset_index(drop=True,inplace=True )
df_train = pd.read_csv('.. /input/petfinder-adoption-prediction/train/train.csv')
 df_test = pd.read_csv('.. /input/petfinder-adoption-prediction/test/test.csv')
 train = df_train.copy()
test = df_test.copy()
labels_breed = pd.read_csv('.. /input/petfinder-adoption-prediction/breed_labels.csv')
 labels_state = pd.read_csv('.. /input/petfinder-adoption-prediction/color_labels.csv')
 labels_color = pd.read_csv('.. /input/petfinder-adoption-prediction/state_labels.csv' )<create_dataframe>
train=pd.concat([train,df_train_svc],axis=1)
 test=pd.concat([test,df_test_svc],axis=1)
 train.tail()
def extract_sentiment_feature(i, x):
feature_sentiment = pd.DataFrame()
if x == 'train':
set_file = 'train'
else:
set_file = 'test'
file_name = '.. /input/petfinder-adoption-prediction/{}_sentiment/{}.json'.format(set_file,i)
 try:
with open(file_name, 'r', encoding='utf-8')as f:
sentiment_file = json.load(f)
 token = [x['name'] for x in sentiment_file['entities']]
token = ' '.join(token)
 sentences_sentiment = [x['sentiment'] for x in sentiment_file['sentences']]
sentences_sentiment = pd.DataFrame.from_dict(
 sentences_sentiment, orient='columns')
 docementSentiment_magnitude = sentiment_file['documentSentiment']['magnitude']
documentSentiment_score = sentiment_file['documentSentiment']['score']
new = pd.DataFrame(
 {'PetID' : i,
'magnitude_sum' : sentences_sentiment['magnitude'].sum(axis=0),
'score_sum' : sentences_sentiment['score'].sum(axis=0),
'magnitude_mean': sentences_sentiment['magnitude'].mean(axis=0),
'score_mean' : sentences_sentiment['score'].mean(axis=0),
'magnitude_var' : sentences_sentiment['magnitude'].var(axis=0),
'score_var' : sentences_sentiment['score'].var(axis=0),
'document_magnitude' : [docementSentiment_magnitude],
'document_score' : [documentSentiment_score]})
 feature_sentiment = feature_sentiment.append(new)
 except:
print('{}没找到'.format(file_name))
for each in feature_sentiment.columns:
if each not in ['PetID','token']:
feature_sentiment[each] = feature_sentiment[each].astype(float)
 return feature_sentiment
train_feature_sentiment = Parallel(n_jobs=8, verbose=1 )(
 delayed(extract_sentiment_feature )(i, 'train')for i in train.PetID)
 train_feature_sentiment = [x for x in train_feature_sentiment]
train_feature_sentiment = pd.concat(train_feature_sentiment, ignore_index=True, sort=False)
 test_feature_sentiment = Parallel(n_jobs=8, verbose=1 )(
 delayed(extract_sentiment_feature )(i, 'test')for i in test.PetID)
 test_feature_sentiment = [x for x in test_feature_sentiment]
test_feature_sentiment = pd.concat(test_feature_sentiment, ignore_index=True, sort=False )<load_from_csv>
sample_submission = pd.read_csv('.. /input/lish-moa/sample_submission.csv')
 train=pd.concat([train,df_train_targets],axis=1)
 target = train[df_train_targets.columns]
target_cols = target.columns.values.tolist()
picture_metadata = pd.read_csv(r'.. /input/32111111/picture.csv')
 picture_size = pd.read_csv(r'.. /input/32111111/picture_size.csv')
 img_features = pd.read_csv(r'.. /input/32111111/img_features.csv')
 x_train = df_train.merge(train_feature_sentiment, how='left', on='PetID')
 x_train = x_train.merge(picture_metadata, how='left', on='PetID')
 x_train = x_train.merge(picture_size, how='left', on='PetID')
 x_train = x_train.merge(img_features, how='left', on='PetID')
 y_train = x_train['AdoptionSpeed']
x_train.drop(['AdoptionSpeed'], axis=1, inplace=True)
 x_test = df_test.merge(test_feature_sentiment, how='left', on='PetID')
 x_test = x_test.merge(picture_metadata, how='left', on='PetID')
 x_test = x_test.merge(picture_size, how='left', on='PetID')
 x_test = x_test.merge(img_features, how='left', on='PetID')
<feature_engineering>
folds = train.copy()
mskf = MultilabelStratifiedKFold(n_splits=7)
 for f,(t_idx, v_idx)in enumerate(mskf.split(X=train, y=target)) :
folds.loc[v_idx, 'kfold'] = int(f)
 folds['kfold'] = folds['kfold'].astype(int)
 folds
col_text = ['Description']
x = x_train.append(x_test ).reset_index()
x = x[['Description', 'PetID']]
n_components = 50
x[col_text] = x[col_text].fillna('MISSING')
 text_features = []
for i in ['Description']:
svd_ = TruncatedSVD(n_components=n_components)
 tfv = CountVectorizer(min_df=3,
max_df=0.9,
stop_words = 'english')
 tfidf_col = tfv.fit_transform(x.loc[:, i])
 svd_col = svd_.fit_transform(tfidf_col)
 svd_col = pd.DataFrame(svd_col)
 svd_col = svd_col.add_prefix('SVD_{}_'.format(i))
text_features.append(svd_col)
 x.drop(i, axis=1, inplace=True)
 text_features = pd.concat(text_features, axis=1)
 x = pd.concat([x, text_features], axis=1)
 x_train = x_train.merge(x, how='left', on='PetID')
 x_test = x_test.merge(x, how='left', on='PetID')
<categorify>
def seed_everything(seed=42):
random.seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 np.random.seed(seed)
 torch.manual_seed(seed)
 torch.cuda.manual_seed(seed)
 torch.backends.cudnn.deterministic = True
seed_everything(seed=42 )
x = x_train.append(x_test)
 x = x[['PetID', 'Description']]
x['Description'] = x['Description'].fillna("Missing")
 x['Description'] = x['Description'].apply(lambda x:TextBlob(x))
x['polarity'] = x['Description'].apply(lambda x:x.sentiment[0])
 x['subjectivity'] = x['Description'].apply(lambda x:x.sentiment[1])
 bin=[-2,0,0.3,2]
x['polarity'] = pd.cut(x['polarity'], bins=bin, labels=range(3))
x['polarity'] = x['polarity'].astype(np.int32)
 x_train = x_train.merge(x[['PetID', 'polarity']], how='left', on='PetID')
 x_test = x_test.merge(x[['PetID', 'polarity']], how='left', on='PetID' )<feature_engineering>
class MoADataset:
def __init__(self, features, targets):
self.features = features
self.targets = targets
def __len__(self):
return(self.features.shape[0])
 def __getitem__(self, idx):
dct = {
'x' : torch.tensor(self.features[idx, :], dtype=torch.float),
'y' : torch.tensor(self.targets[idx, :], dtype=torch.float)
 }
return dct
class TestDataset:
def __init__(self, features):
self.features = features
def __len__(self):
return(self.features.shape[0])
 def __getitem__(self, idx):
dct = {
'x' : torch.tensor(self.features[idx, :], dtype=torch.float)
 }
return dct
x_train['IsFree'] = x_train['Fee'].apply(lambda x:True if x == 0 else False)
 x_test['IsFree'] = x_test['Fee'].apply(lambda x:True if x == 0 else False)
 x_train['Year'] = x_train['Age'].apply(lambda x:math.floor(x/12))
x_test['Year'] = x_test['Age'].apply(lambda x:math.floor(x/12))
x = x_train.append(x_test)
 x['Age_qcut'] = pd.qcut(x['Age'], 5, duplicates='drop')
 x['Age_qcut'] = pd.factorize(x['Age_qcut'])[0]
x_train = x_train.merge(x[['PetID','Age_qcut']], how='left', on='PetID')
 x_test = x_test.merge(x[['PetID','Age_qcut']], how='left', on='PetID')
 x = x_train.append(x_test ).reset_index()
Breed1_count = x.groupby('Breed1' ).size().to_frame('Breed1_count' ).reset_index()
x_train = x_train.merge(Breed1_count, how='left', on='Breed1')
 x_test = x_test.merge(Breed1_count, how='left', on='Breed1')
 a = x['Breed1'].value_counts().sort_values(ascending = False ).cumsum() /len(x)
 rare1_index = a[a > 0.85].index.tolist()
x_train['IsRare1'] = x_train['Breed1'].isin(rare1_index ).apply(lambda x:True if x == True else False)
 x_test['IsRare1'] = x_test['Breed1'].isin(rare1_index ).apply(lambda x:True if x == True else False)
 rare2_index = a[a > 0.72].index.tolist()
x_train['IsRare2'] = x_train['Breed1'].isin(rare2_index ).apply(lambda x:True if x == True else False)
 x_test['IsRare2'] = x_test['Breed1'].isin(rare2_index ).apply(lambda x:True if x == True else False)
 x_train['Is_COMMON'] = x_train['Breed1'].apply(lambda x:True if(x == 265 or x == 307 or x == 266)else False)
 x_test['Is_COMMON'] = x_test['Breed1'].apply(lambda x:True if(x == 265 or x == 307 or x == 266)else False)
 bin=[-0.5,0.5,1.5,4.5,1000]
x_train['Photo_cut'] = pd.cut(x_train['PhotoAmt'], bins=bin, labels=range(4)).astype(np.int32)
 x_test['Photo_cut'] = pd.cut(x_test['PhotoAmt'], bins=bin, labels=range(4)).astype(np.int32)
 x_train['Is_rare_color1'] = x_train['Color1'].apply(lambda x:True if x==5 or x==6 or x==7 else False)
 x_test['Is_rare_color1'] = x_test['Color1'].apply(lambda x:True if x==5 or x==6 or x==7 else False)
 x_train['Is_rare_color2'] = x_train['Color2'].apply(lambda x:True if x==6 else False)
 x_test['Is_rare_color2'] = x_test['Color2'].apply(lambda x:True if x==6 else False)
 x_train['Is_less_than_2month']= x_train['Age'].apply(lambda x:True if x<3 else False)
 x_test['Is_less_than_2month'] = x_test['Age'].apply(lambda x:True if x<3 else False)
<merge>
def train_fn(model, optimizer, scheduler, loss_fn, dataloader, device):
model.train()
final_loss = 0
for data in dataloader:
optimizer.zero_grad()
inputs, targets = data['x'].to(device), data['y'].to(device)
 outputs = model(inputs)
 loss = loss_fn(outputs, targets)
 loss.backward()
optimizer.step()
scheduler.step()
final_loss += loss.item()
final_loss /= len(dataloader)
 return final_loss
def valid_fn(model, loss_fn, dataloader, device):
model.eval()
final_loss = 0
valid_preds = []
for data in dataloader:
inputs, targets = data['x'].to(device), data['y'].to(device)
 outputs = model(inputs)
 loss = loss_fn(outputs, targets)
 final_loss += loss.item()
valid_preds.append(outputs.sigmoid().detach().cpu().numpy())
 final_loss /= len(dataloader)
 valid_preds = np.concatenate(valid_preds)
 return final_loss, valid_preds
def inference_fn(model, dataloader, device):
model.eval()
preds = []
for data in dataloader:
inputs = data['x'].to(device)
 with torch.no_grad() :
outputs = model(inputs)
 preds.append(outputs.sigmoid().detach().cpu().numpy())
 preds = np.concatenate(preds)
 return preds
df = df_train.append(df_test)
 data_rescuer = df.groupby(['RescuerID'])['PetID'].size().reset_index()
data_rescuer.columns = ['RescuerID', 'RescuerID_count']
x_train = x_train.merge(data_rescuer, how='left', on='RescuerID')
 x_test = x_test.merge(data_rescuer, how='left', on='RescuerID')
 x = x_train.append(x_test)
 x['RescuerID_count_cut'] = pd.qcut(x['RescuerID_count'], 5, labels=range(4), duplicates='drop' ).astype(np.int32)
 x_train = x_train.merge(x[['PetID', 'RescuerID_count_cut']], how='left', on='PetID')
 x_test = x_test.merge(x [['PetID', 'RescuerID_count_cut']], how='left', on='PetID')
<merge>
class SmoothBCEwLogits(_WeightedLoss):
def __init__(self, weight=None, reduction='mean', smoothing=0.0):
super().__init__(weight=weight, reduction=reduction)
 self.smoothing = smoothing
self.weight = weight
self.reduction = reduction
@staticmethod
def _smooth(targets:torch.Tensor, n_labels:int, smoothing=0.0):
assert 0 <= smoothing < 1
with torch.no_grad() :
targets = targets *(1.0 - smoothing)+ 0.5 * smoothing
return targets
def forward(self, inputs, targets):
targets = SmoothBCEwLogits._smooth(targets, inputs.size(-1),
self.smoothing)
 loss = F.binary_cross_entropy_with_logits(inputs, targets,self.weight)
 if self.reduction == 'sum':
loss = loss.sum()
elif self.reduction == 'mean':
loss = loss.mean()
return loss
x_train['HasSecondBreed'] = x_train['Breed2'].map(lambda x:True if x != 0 else False)
 x_test['HasSecondBreed'] = x_test['Breed2'].map(lambda x:True if x != 0 else False)
 train_breed_main = x_train[['Breed1']].merge(
 labels_breed, how='left',
left_on='Breed1', right_on='BreedID',
suffixes=('', '_main_breed'))
train_breed_main = train_breed_main.iloc[:, 2:]
train_breed_main = train_breed_main.add_prefix('main_breed_')
 train_breed_second = x_train[['Breed2']].merge(
 labels_breed, how='left',
left_on='Breed2', right_on='BreedID',
suffixes=('', '_second_breed'))
train_breed_second = train_breed_second.iloc[:, 2:]
train_breed_second = train_breed_second.add_prefix('second_breed_')
 x_train = pd.concat(
 [x_train, train_breed_main, train_breed_second], axis=1)
 test_breed_main = x_test[['Breed1']].merge(
 labels_breed, how='left',
left_on='Breed1', right_on='BreedID',
suffixes=('', '_main_breed'))
test_breed_main = test_breed_main.iloc[:, 2:]
test_breed_main = test_breed_main.add_prefix('main_breed_')
 test_breed_second = x_test[['Breed2']].merge(
 labels_breed, how='left',
left_on='Breed2', right_on='BreedID',
suffixes=('', '_second_breed'))
test_breed_second = test_breed_second.iloc[:, 2:]
test_breed_second = test_breed_second.add_prefix('second_breed_')
 x_test = pd.concat(
 [x_test, test_breed_main, test_breed_second], axis=1)
 print(x_train.shape, x_test.shape)
 categorical_columns = ['main_breed_BreedName', 'second_breed_BreedName']
x_train['True_Pure'] = False
x_train.loc[(x_train['main_breed_BreedName'] != 'Mixed Breed')& 
(( x_train['main_breed_BreedName'] == x_train['second_breed_BreedName'])| 
(x_train['second_breed_BreedName'].isnull())) ,'True_Pure'] = True
x_test['True_Pure'] = False
x_test.loc[(x_test['main_breed_BreedName'] != 'Mixed Breed')& 
(( x_test['main_breed_BreedName'] == x_test['second_breed_BreedName'])| 
(x_test['second_breed_BreedName'].isnull())) ,'True_Pure'] = True
x_train['Is_Pure_Dog'] =(x_train['True_Pure'] == True)&(x_train['Type'] == 1)
 x_test['Is_Pure_Dog'] =(x_test['True_Pure'] == True)&(x_test['Type'] == 1)
 x_train.drop(['main_breed_BreedName', 'second_breed_BreedName', 'main_breed_Type', 'second_breed_Type'], axis=1, inplace=True)
 x_test.drop(['main_breed_BreedName', 'second_breed_BreedName', 'main_breed_Type', 'second_breed_Type'], axis=1, inplace=True )<drop_column>
class Model(nn.Module):
def __init__(self, num_features, num_targets, hidden_size):
super(Model, self ).__init__()
self.batch_norm1 = nn.BatchNorm1d(num_features)
 self.dense1 = nn.utils.weight_norm(nn.Linear(num_features, hidden_size))
self.batch_norm2 = nn.BatchNorm1d(hidden_size)
 self.dropout2 = nn.Dropout(0.26)
 self.dense2 = nn.utils.weight_norm(nn.Linear(hidden_size, hidden_size))
self.batch_norm3 = nn.BatchNorm1d(hidden_size)
 self.dropout3 = nn.Dropout(0.26)
 self.dense3 = nn.utils.weight_norm(nn.Linear(hidden_size, num_targets))
def forward(self, x):
x = self.batch_norm1(x)
 x = F.leaky_relu(self.dense1(x))
x = self.batch_norm2(x)
 x = self.dropout2(x)
 x = F.leaky_relu(self.dense2(x))
x = self.batch_norm3(x)
 x = self.dropout3(x)
 x = self.dense3(x)
 return x
class LabelSmoothingLoss(nn.Module):
def __init__(self, classes, smoothing=0.0, dim=-1):
super(LabelSmoothingLoss, self ).__init__()
self.confidence = 1.0 - smoothing
self.smoothing = smoothing
self.cls = classes
self.dim = dim
def forward(self, pred, target):
pred = pred.log_softmax(dim=self.dim)
 with torch.no_grad() :
true_dist = torch.zeros_like(pred)
 true_dist.fill_(self.smoothing /(self.cls - 1))
true_dist.scatter_(1, target.data.unsqueeze(1), self.confidence)
 return torch.mean(torch.sum(-true_dist * pred, dim=self.dim))
drop_columns = ['Name', 'RescuerID', 'Description', 'PetID', 'token', 'annots_top_desc']
drop_columns = ['Name', 'RescuerID', 'Description', 'PetID']
x_train.drop(drop_columns, axis=1, inplace=True)
 x_test.drop(drop_columns, axis=1, inplace=True)
 x_train = x_train.fillna(0)
 x_test = x_test.fillna(0)
<data_type_conversions>
feature_cols = [c for c in folds.columns if c not in target_cols]
feature_cols = [c for c in feature_cols if c not in ['kfold','sig_id']]
len(feature_cols )
c = ['Breed1', 'Breed2', 'Gender', 'Color1', 'Color2', 'Color3',
'Vaccinated', 'Dewormed', 'Sterilized', 'Health', 'Quantity',
'State', 'IsFree', 'Year', 'Age_qcut', 'IsRare1', 'IsRare2',
'Is_COMMON', 'Photo_cut', 'Is_rare_color1', 'Is_rare_color2', 'Is_less_than_2month',
'RescuerID_count_cut', 'HasSecondBreed', 'True_Pure', 'Is_Pure_Dog']
for each in c:
x_train[each] = x_train[each].astype('category')
 x_test[each] = x_test[each].astype('category' )<choose_model_class>
DEVICE =('cuda' if torch.cuda.is_available() else 'cpu')
 EPOCHS =25
BATCH_SIZE = 128
LEARNING_RATE = 1e-3
WEIGHT_DECAY = 1e-5
NFOLDS = 7
EARLY_STOPPING_STEPS = 10
EARLY_STOP = False
num_features=len(feature_cols)
 num_targets=len(target_cols)
 hidden_size=1300
print(num_features,num_targets )
model_lgb = LGBMRegressor(
 learning_rate = 0.01,
n_estimators = 2000,
max_depth = 4,
num_leaves = 12 ,
subsample = 0.8,
colsample_bytree = 0.8,
n_jobs = -1,
random_state = 44,
objective = 'regression',
eval_metric = 'scorer',
min_child_samples = 15 
)
<train_model>
def run_training(fold, seed):
seed_everything(seed)
 train = folds
test_ = test
trn_idx = train[train['kfold'] != fold].index
val_idx = train[train['kfold'] == fold].index
train_df = train[train['kfold'] != fold].reset_index(drop=True)
 valid_df = train[train['kfold'] == fold].reset_index(drop=True)
 x_train, y_train = train_df[feature_cols].values, train_df[target_cols].values
x_valid, y_valid = valid_df[feature_cols].values, valid_df[target_cols].values
train_dataset = MoADataset(x_train, y_train)
 valid_dataset = MoADataset(x_valid, y_valid)
 trainloader = torch.utils.data.DataLoader(train_dataset, batch_size=BATCH_SIZE, shuffle=True)
 validloader = torch.utils.data.DataLoader(valid_dataset, batch_size=BATCH_SIZE, shuffle=False)
 model = Model(
 num_features=num_features,
num_targets=num_targets,
hidden_size=hidden_size, 
)
 model.to(DEVICE)
 optimizer = torch.optim.Adam(model.parameters() , lr=LEARNING_RATE, weight_decay=WEIGHT_DECAY)
 scheduler = optim.lr_scheduler.OneCycleLR(optimizer=optimizer, pct_start=0.1, div_factor=1e3,
max_lr=1e-2, epochs=EPOCHS, steps_per_epoch=len(trainloader))
loss_fn = nn.BCEWithLogitsLoss()
loss_tr = SmoothBCEwLogits(smoothing =0.001)
 early_stopping_steps = EARLY_STOPPING_STEPS
early_step = 0
oof = np.zeros(( len(train), target.shape[1]))
best_loss = np.inf
for epoch in range(EPOCHS):
train_loss = train_fn(model, optimizer,scheduler, loss_tr, trainloader, DEVICE)
 print(f"FOLD: {fold}, EPOCH: {epoch}, train_loss: {train_loss}")
 valid_loss, valid_preds = valid_fn(model, loss_fn, validloader, DEVICE)
 print(f"FOLD: {fold}, EPOCH: {epoch}, valid_loss: {valid_loss}")
 if valid_loss < best_loss:
best_loss = valid_loss
oof[val_idx] = valid_preds
torch.save(model.state_dict() , f"FOLD{fold}_.pth")
 elif(EARLY_STOP == True):
early_step += 1
if(early_step >= early_stopping_steps):
break
x_test = test_[feature_cols].values
testdataset = TestDataset(x_test)
 testloader = torch.utils.data.DataLoader(testdataset, batch_size=BATCH_SIZE, shuffle=False)
 model = Model(
 num_features=num_features,
num_targets=num_targets,
hidden_size=hidden_size, 
)
 model.load_state_dict(torch.load(f"FOLD{fold}_.pth"))
model.to(DEVICE)
 predictions = np.zeros(( len(test_), target.iloc[:, 1:].shape[1]))
predictions = inference_fn(model, testloader, DEVICE)
 return oof, predictions
y_lgb, y_test_pre = split_score(model_lgb, x_train, y_train, x_test)
 y_test_pre = y_test_pre.mean(axis=1)
 coe = search_coef(y_lgb, y_train)
 best_lgb_coe = coe['x']
print('lgb的最佳系数为{}'.format(best_lgb_coe))
model_lgb.fit(x_train, y_train)
 result_lgb_fix = fix_y(y_test_pre, best_lgb_coe)
 print('lgb后的分布:',Counter(result_lgb_fix))<save_to_csv>
def run_k_fold(NFOLDS, seed):
oof = np.zeros(( len(train), len(target_cols)))
 predictions = np.zeros(( len(test), len(target_cols)))
 for fold in range(NFOLDS):
oof_, pred_ = run_training(fold, seed)
 predictions += pred_ / NFOLDS
oof += oof_
return oof, predictions
submission_lgb = pd.DataFrame({'PetID': df_test['PetID'].values, 'AdoptionSpeed': result_lgb_fix.astype(np.int32)})
 submission_lgb.to_csv('submission.csv', index=False )<import_modules>
SEED = [0,1,2]
oof = np.zeros(( len(train), len(target_cols)))
 predictions = np.zeros(( len(test), len(target_cols)))
 for seed in SEED:
oof_, predictions_ = run_k_fold(NFOLDS, seed)
 oof += oof_ / len(SEED)
 predictions += predictions_ / len(SEED)
 train[target_cols] = oof
test[target_cols] = predictions
def kappa(y_true, y_pred):
return cohen_kappa_score(y_true, y_pred, weights='quadratic')
 def warn(*args, **kwargs):
pass
warnings.warn = warn
print(os.listdir(".. /input"))<load_from_csv>
def log_loss_metric(y_true, y_pred):
metrics = []
for _target in df_train_targets.columns:
metrics.append(log_loss(y_true.loc[:, _target], y_pred.loc[:, _target].astype(float), labels = [0,1]))
return np.mean(metrics)
 y_true = df_train_targets[target_cols].values
y_pred = oof
score = 0
for i in range(len(target_cols)) :
score_ = log_loss(y_true[:, i], y_pred[:, i])
 print(i,target_cols[i],score_)
 score += score_ / target.shape[1]
print("CV log_loss: ", score )
<create_dataframe><EOS> 
test_pred=test[target_cols]
sig_id = test_features[test_features['cp_type']!='ctl_vehicle'].sig_id.reset_index(drop=True)
 test_pred['sig_id'] = sig_id
sub = pd.merge(test_features[['sig_id']], test_pred, on='sig_id', how='left')
 sub.fillna(0, inplace=True)
 sub.to_csv('submission.csv', index=False )