Kaggle competition: Mechanisms of Action (MoA) Prediction
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>
import numpy as np
import pandas as pd
import os
import random
from sklearn import preprocessing
from sklearn.metrics import log_loss
from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import PCA
from sklearn.preprocessing import QuantileTransformer
from sklearn.feature_selection import VarianceThreshold, SelectKBest
from sklearn.multioutput import MultiOutputClassifier
from xgboost import XGBClassifier
from category_encoders import CountEncoder
from sklearn.pipeline import Pipeline
import matplotlib.pyplot as plt
from sklearn.preprocessing import StandardScaler
import tensorflow as tf
import tensorflow.keras.backend as K
import tensorflow.keras.layers as L
import tensorflow.keras.models as M
from tensorflow.keras.callbacks import ReduceLROnPlateau, ModelCheckpoint
import tensorflow_addons as tfa
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']<load_from_csv>
train_features = pd.read_csv('.. /input/lish-moa/train_features.csv')
 train_targets_scored = pd.read_csv('.. /input/lish-moa/train_targets_scored.csv')
 train_targets_nonscored = pd.read_csv('.. /input/lish-moa/train_targets_nonscored.csv')
 test_features = pd.read_csv('.. /input/lish-moa/test_features.csv')
 train_drug = pd.read_csv(".. /input/lish-moa/train_drug.csv")
 data = train_features.append(test_features)
 ss = pd.read_csv('.. /input/lish-moa/sample_submission.csv' )
breedDf = pd.read_csv(breedPath)
 breedDf.head()<load_from_csv>
n_comp_GENES = 463
n_comp_CELLS = 60
VarianceThreshold_for_FS = 0.9
NFOLDS = 5
NSEEDS = 5
colorDf = pd.read_csv(colorPath)
 colorDf.head()<load_from_csv>
def seed_everything(seed=42):
random.seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 np.random.seed(seed)
 tf.random.set_seed(seed)
 seed_everything(seed=42 )
stateDf = pd.read_csv(statePath)
 stateDf.head()<categorify>
GENES = [col for col in train_features.columns if col.startswith('g-')]
CELLS = [col for col in train_features.columns if col.startswith('c-')]
def cleanTransformDataset(dataset, categoricalFeatures):
breedDf2 = breedDf.set_index('BreedID')
 idx = breedDf2.to_dict()
dataset.Breed1 = dataset.Breed1.map(idx['BreedName'])
 dataset.Breed2 = dataset.Breed2.map(idx['BreedName'])
 colorDf2 = colorDf.set_index('ColorID')
 idx = colorDf2.to_dict()
dataset.Color1 = dataset.Color1.map(idx['ColorName'])
 dataset.Color2 = dataset.Color2.map(idx['ColorName'])
 dataset.Color3 = dataset.Color3.map(idx['ColorName'])
 stateDf2 = stateDf.set_index('StateID')
 idx = stateDf2.to_dict()
dataset.State = dataset.State.map(idx['StateName'])
 dataset.Type = dataset.Type.map({1: 'Dog', 2: 'Cat'})
 dataset.Gender = dataset.Gender.map({1: 'Male', 2: 'Female', 3: 'Mixed'})
 dataset.MaturitySize = dataset.MaturitySize.map({1: 'Small', 2: 'Medium', 3: 'Large', 4: 'Extra Large', 0: 'Not Specified'})
 dataset.FurLength = dataset.FurLength.map({1: 'Short', 2: 'Medium', 3: 'Long', 0: 'Not Specified'})
 dataset.Vaccinated = dataset.Vaccinated.map({1: 'Yes', 2: 'No', 3: 'Not Sure'})
 dataset.Dewormed = dataset.Dewormed.map({1: 'Yes', 2: 'No', 3: 'Not Sure'})
 dataset.Sterilized = dataset.Sterilized.map({1: 'Yes', 2: 'No', 3: 'Not Sure'})
 dataset.Health = dataset.Health.map({1: 'Healthy', 2: 'Minor Injury', 3: 'Serious Injury', 0: 'Not Specified'})
 dataset[categoricalFeatures] = dataset[categoricalFeatures].astype('category')
 return dataset<create_dataframe>
col_names = GENES + CELLS
col_example_index = 300
col_example_name = col_names[300]
train = cleanTransformDataset(train, categoricalFeatures)
 test = cleanTransformDataset(test, list(set(categoricalFeatures)- set(['AdoptionSpeed'])) )<data_type_conversions>
for col in(GENES + CELLS):
transformer = QuantileTransformer(n_quantiles=100,random_state=0, output_distribution="normal")
 vec_len = len(train_features[col].values)
 vec_len_test = len(test_features[col].values)
 raw_vec = train_features[col].values.reshape(vec_len, 1)
 transformer.fit(raw_vec)
 train_features[col] = transformer.transform(raw_vec ).reshape(1, vec_len)[0]
test_features[col] = transformer.transform(test_features[col].values.reshape(vec_len_test, 1)).reshape(1, vec_len_test)[0]
def extraFeatures(train):
train['L_Color1'] =(pd.isnull(train['Color3'])& pd.isnull(train['Color2'])& pd.notnull(train['Color1'])).astype(int)
 train['L_Color2'] =(pd.isnull(train['Color3'])& pd.notnull(train['Color2'])& pd.notnull(train['Color1'])).astype(int)
 train['L_Color3'] =(pd.notnull(train['Color3'])& pd.notnull(train['Color2'])& pd.notnull(train['Color1'])).astype(int)
 train['L_Breed1'] =(pd.isnull(train['Breed2'])& pd.notnull(train['Breed1'])).astype(int)
 train['L_Breed2'] =(pd.notnull(train['Breed2'])& pd.notnull(train['Breed1'])).astype(int)
 train['Name_Length']= train['Name'].str.len()
train['L_Name_missing'] =(pd.isnull(train['Name'])).astype(int)
 train['L_Breed1_Siamese'] =(train['Breed1']=='Siamese' ).astype(int)
 train['L_Breed1_Persian']=(train['Breed1']=='Persian' ).astype(int)
 train['L_Breed1_Labrador_Retriever']=(train['Breed1']=='Labrador Retriever' ).astype(int)
 train['L_Breed1_Terrier']=(train['Breed1']=='Terrier' ).astype(int)
 train['L_Breed1_Golden_Retriever ']=(train['Breed1']=='Golden Retriever' ).astype(int)
 train['Description_Length']=train['Description'].str.len()
train['L_Fee_Free'] =(train['Fee']==0 ).astype(int)
 pets_total = train.groupby(['RescuerID'] ).size().reset_index(name='N_pets_total')
 train= pd.merge(train, pets_total, left_on='RescuerID', right_on='RescuerID', how='inner')
 train.count()
train['L_NoPhoto'] =(train['PhotoAmt']==0 ).astype(int)
 train['L_NoVideo'] =(train['VideoAmt']==0 ).astype(int)
 train['Log_Age']= np.log(train.Age + 1)
 train.loc[train['Quantity'] > 5, 'Quantity'] = 5
return train<feature_engineering>
data = pd.concat([pd.DataFrame(train_features[GENES]), pd.DataFrame(test_features[GENES])])
 data2 =(PCA(n_components=n_comp_GENES, random_state=42 ).fit_transform(data[GENES]))
train2 = data2[:train_features.shape[0]]; test2 = data2[-test_features.shape[0]:]
train2 = pd.DataFrame(train2, columns=[f'pca_G-{i}' for i in range(n_comp_GENES)])
 test2 = pd.DataFrame(test2, columns=[f'pca_G-{i}' for i in range(n_comp_GENES)])
 train_features = pd.concat(( train_features, train2), axis=1)
 test_features = pd.concat(( test_features, test2), axis=1 )
train = extraFeatures(train)
 test = extraFeatures(test )<count_missing_values>
data = pd.concat([pd.DataFrame(train_features[CELLS]), pd.DataFrame(test_features[CELLS])])
 data2 =(PCA(n_components=n_comp_CELLS, random_state=42 ).fit_transform(data[CELLS]))
train2 = data2[:train_features.shape[0]]; test2 = data2[-test_features.shape[0]:]
train2 = pd.DataFrame(train2, columns=[f'pca_C-{i}' for i in range(n_comp_CELLS)])
 test2 = pd.DataFrame(test2, columns=[f'pca_C-{i}' for i in range(n_comp_CELLS)])
 train_features = pd.concat(( train_features, train2), axis=1)
 test_features = pd.concat(( test_features, test2), axis=1 )
train.isna().sum()<feature_engineering>
data = train_features.append(test_features)
 data
train['AgeInterval'] = pd.Series(['0-3', '3-6', '6-12', '12-24', '24-48', '48-120', '>120'], dtype='category')
 train.loc[(train['Age'] >= 0)&(train['Age'] <= 3),'AgeInterval'] = '0-3'
train.loc[(train['Age'] > 3)&(train['Age'] <= 6),'AgeInterval'] = '3-6'
train.loc[(train['Age'] > 6)&(train['Age'] <= 12),'AgeInterval'] = '6-12'
train.loc[(train['Age'] > 12)&(train['Age'] <= 24),'AgeInterval'] = '12-24'
train.loc[(train['Age'] > 24)&(train['Age'] <= 48),'AgeInterval'] = '24-48'
train.loc[(train['Age'] > 48)&(train['Age'] <= 120),'AgeInterval'] = '48-120'
train.loc[train['Age'] > 120,'AgeInterval'] = '>120'<filter>
var_thresh = VarianceThreshold(VarianceThreshold_for_FS)
 data_transformed = var_thresh.fit_transform(data.iloc[:, 4:])
 train_features_transformed = data_transformed[ : train_features.shape[0]]
test_features_transformed = data_transformed[-test_features.shape[0] : ]
train_features = pd.DataFrame(train_features[['sig_id','cp_type','cp_time','cp_dose']].values.reshape(-1, 4),\
columns=['sig_id','cp_type','cp_time','cp_dose'])
 train_features = pd.concat([train_features, pd.DataFrame(train_features_transformed)], axis=1)
 test_features = pd.DataFrame(test_features[['sig_id','cp_type','cp_time','cp_dose']].values.reshape(-1, 4),\
columns=['sig_id','cp_type','cp_time','cp_dose'])
 test_features = pd.concat([test_features, pd.DataFrame(test_features_transformed)], axis=1)
 train_features.shape
train = train[train.Fee <= 1500]<define_variables>
merged = train_features.merge(train_targets_scored, on='sig_id')
 merged = merged[merged['cp_type']!='ctl_vehicle'].reset_index(drop=True)
 X_test = test_features[test_features['cp_type']!='ctl_vehicle'].reset_index(drop=True)
 X_train = merged[train_features.columns]
y_train = merged[train_targets_scored.columns]
total = train[train.Fee == 0].size
feeFreePercent = [
train[(train.AdoptionSpeed == 0)&(train.Fee == 0)].size,
train[(train.AdoptionSpeed == 1)&(train.Fee == 0)].size,
train[(train.AdoptionSpeed == 2)&(train.Fee == 0)].size,
train[(train.AdoptionSpeed == 3)&(train.Fee == 0)].size,
train[(train.AdoptionSpeed == 4)&(train.Fee == 0)].size
] / total
total = train[train.Fee > 0].size
feePaidPercent = [
train[(train.AdoptionSpeed == 0)&(train.Fee > 0)].size,
train[(train.AdoptionSpeed == 1)&(train.Fee > 0)].size,
train[(train.AdoptionSpeed == 2)&(train.Fee > 0)].size,
train[(train.AdoptionSpeed == 3)&(train.Fee > 0)].size,
train[(train.AdoptionSpeed == 4)&(train.Fee > 0)].size
] / total
percents = np.append(feeFreePercent, feePaidPercent)
 types =(['free'] * 5)+(['paid'] * 5)
 feePercentDf = pd.DataFrame({ 'percent': percents, 'type': types })
 fig, ax = plt.subplots(figsize=(8,8))
sns.lineplot(y='percent', x=[0, 1, 2, 3, 4] * 2, hue='type', style='type', markers=True, data=feePercentDf )<define_variables>
X_train = X_train.drop('cp_type', axis=1)
 X_test = X_test.drop('cp_type', axis=1 )
total = train[train.Type == 'Dog'].size
dogPercent = [
train[(train.AdoptionSpeed == 0)&(train.Type == 'Dog')].size,
train[(train.AdoptionSpeed == 1)&(train.Type == 'Dog')].size,
train[(train.AdoptionSpeed == 2)&(train.Type == 'Dog')].size,
train[(train.AdoptionSpeed == 3)&(train.Type == 'Dog')].size,
train[(train.AdoptionSpeed == 4)&(train.Type == 'Dog')].size
] / total
total = train[train.Type == 'Cat'].size
catPercent = [
train[(train.AdoptionSpeed == 0)&(train.Type == 'Cat')].size,
train[(train.AdoptionSpeed == 1)&(train.Type == 'Cat')].size,
train[(train.AdoptionSpeed == 2)&(train.Type == 'Cat')].size,
train[(train.AdoptionSpeed == 3)&(train.Type == 'Cat')].size,
train[(train.AdoptionSpeed == 4)&(train.Type == 'Cat')].size
] / total
percents = np.append(dogPercent, catPercent)
 types =(['dog'] * 5)+(['cat'] * 5)
 typePercentDf = pd.DataFrame({ 'percent': percents, 'type': types })
 fig, ax = plt.subplots(figsize=(8,8))
sns.lineplot(y='percent', x=[0, 1, 2, 3, 4] * 2, hue='type', style='type', markers=True, data=typePercentDf )<feature_engineering>
X_train = X_train.drop('sig_id', axis=1)
 X_test = X_test.drop('sig_id', axis=1)
 y_train = y_train.drop('sig_id', axis=1 )
strayCat = ['Domestic Short Hair', 'Domestic Medium Hair', 'Domestic Long Hair']
strayDog = ['Mixed Breed']
train.loc[(train.Breed1.isin(strayDog)) &(train.Type == 'Dog'), 'Breed1Type'] = 'Stray'
train.loc[(train.Breed1.isin(strayCat)) &(train.Type == 'Cat'), 'Breed1Type'] = 'Stray'
train.loc[(~train.Breed1.isin(strayDog)) &(train.Type == 'Dog'), 'Breed1Type'] = 'Breed'
train.loc[(~train.Breed1.isin(strayCat)) &(train.Type == 'Cat'), 'Breed1Type'] = 'Breed'<filter>
ce = CountEncoder(cols=["cp_dose","cp_time"])
 X_train_encoded = ce.fit_transform(X_train)
 X_test_encoded = ce.transform(X_test )
total = train[(train.Type == 'Dog')&(train.Breed1Type == 'Stray')].size
dogPercentStray = [
train[(train.AdoptionSpeed == 0)&(train.Type == 'Dog')&(train.Breed1Type == 'Stray')].size,
train[(train.AdoptionSpeed == 1)&(train.Type == 'Dog')&(train.Breed1Type == 'Stray')].size,
train[(train.AdoptionSpeed == 2)&(train.Type == 'Dog')&(train.Breed1Type == 'Stray')].size,
train[(train.AdoptionSpeed == 3)&(train.Type == 'Dog')&(train.Breed1Type == 'Stray')].size,
train[(train.AdoptionSpeed == 4)&(train.Type == 'Dog')&(train.Breed1Type == 'Stray')].size
] / total
total = train[(train.Type == 'Dog')&(train.Breed1Type == 'Breed')].size
dogPercentBreed = [
train[(train.AdoptionSpeed == 0)&(train.Type == 'Dog')&(train.Breed1Type == 'Breed')].size,
train[(train.AdoptionSpeed == 1)&(train.Type == 'Dog')&(train.Breed1Type == 'Breed')].size,
train[(train.AdoptionSpeed == 2)&(train.Type == 'Dog')&(train.Breed1Type == 'Breed')].size,
train[(train.AdoptionSpeed == 3)&(train.Type == 'Dog')&(train.Breed1Type == 'Breed')].size,
train[(train.AdoptionSpeed == 4)&(train.Type == 'Dog')&(train.Breed1Type == 'Breed')].size
] / total
total = train[(train.Type == 'Cat')&(train.Breed1Type == 'Stray')].size
catPercentStray = [
train[(train.AdoptionSpeed == 0)&(train.Type == 'Cat')&(train.Breed1Type == 'Stray')].size,
train[(train.AdoptionSpeed == 1)&(train.Type == 'Cat')&(train.Breed1Type == 'Stray')].size,
train[(train.AdoptionSpeed == 2)&(train.Type == 'Cat')&(train.Breed1Type == 'Stray')].size,
train[(train.AdoptionSpeed == 3)&(train.Type == 'Cat')&(train.Breed1Type == 'Stray')].size,
train[(train.AdoptionSpeed == 4)&(train.Type == 'Cat')&(train.Breed1Type == 'Stray')].size
] / total
total = train[(train.Type == 'Cat')&(train.Breed1Type == 'Breed')].size
catPercentBreed = [
train[(train.AdoptionSpeed == 0)&(train.Type == 'Cat')&(train.Breed1Type == 'Breed')].size,
train[(train.AdoptionSpeed == 1)&(train.Type == 'Cat')&(train.Breed1Type == 'Breed')].size,
train[(train.AdoptionSpeed == 2)&(train.Type == 'Cat')&(train.Breed1Type == 'Breed')].size,
train[(train.AdoptionSpeed == 3)&(train.Type == 'Cat')&(train.Breed1Type == 'Breed')].size,
train[(train.AdoptionSpeed == 4)&(train.Type == 'Cat')&(train.Breed1Type == 'Breed')].size
] / total
percents = np.append(dogPercentStray, dogPercentBreed)
 percents = np.append(percents, catPercentStray)
 percents = np.append(percents, catPercentBreed)
 types =(['dog-stray'] * 5 + ['dog-breed'] * 5 + ['cat-stray'] * 5 + ['cat-breed'] * 5)
 typePercentDf = pd.DataFrame({ 'percent': percents, 'type': types })
 fig, ax = plt.subplots(figsize=(8,8))
sns.lineplot(y='percent', x=[0, 1, 2, 3, 4] * 4, hue='type', style='type', markers=True, data=typePercentDf, palette='coolwarm' )<define_variables>
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train_encoded)
 X_test_scaled = scaler.transform(X_test_encoded )
total = train[(train.MaturitySize == 'Large')].size
largePercent = [
train[(train.AdoptionSpeed == 0)&(train.MaturitySize == 'Large')].size,
train[(train.AdoptionSpeed == 1)&(train.MaturitySize == 'Large')].size,
train[(train.AdoptionSpeed == 2)&(train.MaturitySize == 'Large')].size,
train[(train.AdoptionSpeed == 3)&(train.MaturitySize == 'Large')].size,
train[(train.AdoptionSpeed == 4)&(train.MaturitySize == 'Large')].size
] / total
total = train[(train.MaturitySize == 'Medium')].size
mediumPercent = [
train[(train.AdoptionSpeed == 0)&(train.MaturitySize == 'Medium')].size,
train[(train.AdoptionSpeed == 1)&(train.MaturitySize == 'Medium')].size,
train[(train.AdoptionSpeed == 2)&(train.MaturitySize == 'Medium')].size,
train[(train.AdoptionSpeed == 3)&(train.MaturitySize == 'Medium')].size,
train[(train.AdoptionSpeed == 4)&(train.MaturitySize == 'Medium')].size
] / total
total = train[(train.MaturitySize == 'Small')].size
smallPercent = [
train[(train.AdoptionSpeed == 0)&(train.MaturitySize == 'Small')].size,
train[(train.AdoptionSpeed == 1)&(train.MaturitySize == 'Small')].size,
train[(train.AdoptionSpeed == 2)&(train.MaturitySize == 'Small')].size,
train[(train.AdoptionSpeed == 3)&(train.MaturitySize == 'Small')].size,
train[(train.AdoptionSpeed == 4)&(train.MaturitySize == 'Small')].size
] / total
percents = np.append(largePercent, mediumPercent)
 percents = np.append(percents, smallPercent)
 types =(['Large'] * 5 + ['Medium'] * 5 + ['Small'] * 5)
 typePercentDf = pd.DataFrame({ 'percent': percents, 'type': types })
 fig, ax = plt.subplots(figsize=(8,8))
sns.lineplot(y='percent', x=[0, 1, 2, 3, 4] * 3, hue='type', style='type', markers=True, data=typePercentDf, palette='coolwarm' )<define_variables>
X_train = pd.DataFrame(X_train_scaled, columns=X_train.columns.to_list())
 X_test = pd.DataFrame(X_test_scaled, columns=X_test.columns.to_list() )
total = train[(train.FurLength == 'Long')].size
longPercent = [
train[(train.AdoptionSpeed == 0)&(train.FurLength == 'Long')].size,
train[(train.AdoptionSpeed == 1)&(train.FurLength == 'Long')].size,
train[(train.AdoptionSpeed == 2)&(train.FurLength == 'Long')].size,
train[(train.AdoptionSpeed == 3)&(train.FurLength == 'Long')].size,
train[(train.AdoptionSpeed == 4)&(train.FurLength == 'Long')].size
] / total
total = train[(train.FurLength == 'Medium')].size
mediumPercent = [
train[(train.AdoptionSpeed == 0)&(train.FurLength == 'Medium')].size,
train[(train.AdoptionSpeed == 1)&(train.FurLength == 'Medium')].size,
train[(train.AdoptionSpeed == 2)&(train.FurLength == 'Medium')].size,
train[(train.AdoptionSpeed == 3)&(train.FurLength == 'Medium')].size,
train[(train.AdoptionSpeed == 4)&(train.FurLength == 'Medium')].size
] / total
total = train[(train.FurLength == 'Short')].size
shortPercent = [
train[(train.AdoptionSpeed == 0)&(train.FurLength == 'Short')].size,
train[(train.AdoptionSpeed == 1)&(train.FurLength == 'Short')].size,
train[(train.AdoptionSpeed == 2)&(train.FurLength == 'Short')].size,
train[(train.AdoptionSpeed == 3)&(train.FurLength == 'Short')].size,
train[(train.AdoptionSpeed == 4)&(train.FurLength == 'Short')].size
] / total
percents = np.append(longPercent, mediumPercent)
 percents = np.append(percents, shortPercent)
 types =(['Long'] * 5 + ['Medium'] * 5 + ['Short'] * 5)
 typePercentDf = pd.DataFrame({ 'percent': percents, 'type': types })
 fig, ax = plt.subplots(figsize=(8,8))
sns.lineplot(y='percent', x=[0, 1, 2, 3, 4] * 3, hue='type', style='type', markers=True, data=typePercentDf, palette='coolwarm' )<define_variables>
num_columns = X_train.shape[1]
total = train[(train.Vaccinated == 'Yes')].size
yesPercent = [
train[(train.AdoptionSpeed == 0)&(train.Vaccinated == 'Yes')].size,
train[(train.AdoptionSpeed == 1)&(train.Vaccinated == 'Yes')].size,
train[(train.AdoptionSpeed == 2)&(train.Vaccinated == 'Yes')].size,
train[(train.AdoptionSpeed == 3)&(train.Vaccinated == 'Yes')].size,
train[(train.AdoptionSpeed == 4)&(train.Vaccinated == 'Yes')].size
] / total
total = train[(train.Vaccinated == 'No')].size
noPercent = [
train[(train.AdoptionSpeed == 0)&(train.Vaccinated == 'No')].size,
train[(train.AdoptionSpeed == 1)&(train.Vaccinated == 'No')].size,
train[(train.AdoptionSpeed == 2)&(train.Vaccinated == 'No')].size,
train[(train.AdoptionSpeed == 3)&(train.Vaccinated == 'No')].size,
train[(train.AdoptionSpeed == 4)&(train.Vaccinated == 'No')].size
] / total
total = train[(train.Vaccinated == 'Not Sure')].size
notSurePercent = [
train[(train.AdoptionSpeed == 0)&(train.Vaccinated == 'Not Sure')].size,
train[(train.AdoptionSpeed == 1)&(train.Vaccinated == 'Not Sure')].size,
train[(train.AdoptionSpeed == 2)&(train.Vaccinated == 'Not Sure')].size,
train[(train.AdoptionSpeed == 3)&(train.Vaccinated == 'Not Sure')].size,
train[(train.AdoptionSpeed == 4)&(train.Vaccinated == 'Not Sure')].size
] / total
percents = np.append(yesPercent, noPercent)
 percents = np.append(percents, notSurePercent)
 types =(['Yes'] * 5 + ['No'] * 5 + ['Not Sure'] * 5)
 typePercentDf = pd.DataFrame({ 'percent': percents, 'type': types })
 fig, ax = plt.subplots(figsize=(8,8))
sns.lineplot(y='percent', x=[0, 1, 2, 3, 4] * 3, hue='type', style='type', markers=True, data=typePercentDf, palette='coolwarm' )<define_variables>
def create_model(num_columns):
model = tf.keras.Sequential([
tf.keras.layers.Input(num_columns),
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Dropout(0.2),
tfa.layers.WeightNormalization(tf.keras.layers.Dense(2048, activation="relu")) ,
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Dropout(0.5),
tfa.layers.WeightNormalization(tf.keras.layers.Dense(1048, activation="relu")) ,
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Dropout(0.5),
tfa.layers.WeightNormalization(tf.keras.layers.Dense(206, activation="sigmoid"))
])
 model.compile(optimizer=tfa.optimizers.Lookahead(tf.optimizers.Adam() , sync_period=10),
loss='binary_crossentropy', 
)
 return model
total = train[(train.Dewormed == 'Yes')].size
yesPercent = [
train[(train.AdoptionSpeed == 0)&(train.Dewormed == 'Yes')].size,
train[(train.AdoptionSpeed == 1)&(train.Dewormed == 'Yes')].size,
train[(train.AdoptionSpeed == 2)&(train.Dewormed == 'Yes')].size,
train[(train.AdoptionSpeed == 3)&(train.Dewormed == 'Yes')].size,
train[(train.AdoptionSpeed == 4)&(train.Dewormed == 'Yes')].size
] / total
total = train[(train.Dewormed == 'No')].size
noPercent = [
train[(train.AdoptionSpeed == 0)&(train.Dewormed == 'No')].size,
train[(train.AdoptionSpeed == 1)&(train.Dewormed == 'No')].size,
train[(train.AdoptionSpeed == 2)&(train.Dewormed == 'No')].size,
train[(train.AdoptionSpeed == 3)&(train.Dewormed == 'No')].size,
train[(train.AdoptionSpeed == 4)&(train.Dewormed == 'No')].size
] / total
total = train[(train.Dewormed == 'Not Sure')].size
notSurePercent = [
train[(train.AdoptionSpeed == 0)&(train.Dewormed == 'Not Sure')].size,
train[(train.AdoptionSpeed == 1)&(train.Dewormed == 'Not Sure')].size,
train[(train.AdoptionSpeed == 2)&(train.Dewormed == 'Not Sure')].size,
train[(train.AdoptionSpeed == 3)&(train.Dewormed == 'Not Sure')].size,
train[(train.AdoptionSpeed == 4)&(train.Dewormed == 'Not Sure')].size
] / total
percents = np.append(yesPercent, noPercent)
 percents = np.append(percents, notSurePercent)
 types =(['Yes'] * 5 + ['No'] * 5 + ['Not Sure'] * 5)
 typePercentDf = pd.DataFrame({ 'percent': percents, 'type': types })
 fig, ax = plt.subplots(figsize=(8,8))
sns.lineplot(y='percent', x=[0, 1, 2, 3, 4] * 3, hue='type', style='type', markers=True, data=typePercentDf, palette='coolwarm' )<define_variables>
res = y_train.copy()
ss.loc[:, y_train.columns] = 0
res.loc[:, y_train.columns] = 0
control_mask = test_features['cp_type']!='ctl_vehicle'
total = train[(train.Sterilized == 'Yes')].size
yesPercent = [
train[(train.AdoptionSpeed == 0)&(train.Sterilized == 'Yes')].size,
train[(train.AdoptionSpeed == 1)&(train.Sterilized == 'Yes')].size,
train[(train.AdoptionSpeed == 2)&(train.Sterilized == 'Yes')].size,
train[(train.AdoptionSpeed == 3)&(train.Sterilized == 'Yes')].size,
train[(train.AdoptionSpeed == 4)&(train.Sterilized == 'Yes')].size
] / total
total = train[(train.Sterilized == 'No')].size
noPercent = [
train[(train.AdoptionSpeed == 0)&(train.Sterilized == 'No')].size,
train[(train.AdoptionSpeed == 1)&(train.Sterilized == 'No')].size,
train[(train.AdoptionSpeed == 2)&(train.Sterilized == 'No')].size,
train[(train.AdoptionSpeed == 3)&(train.Sterilized == 'No')].size,
train[(train.AdoptionSpeed == 4)&(train.Sterilized == 'No')].size
] / total
total = train[(train.Sterilized == 'Not Sure')].size
notSurePercent = [
train[(train.AdoptionSpeed == 0)&(train.Sterilized == 'Not Sure')].size,
train[(train.AdoptionSpeed == 1)&(train.Sterilized == 'Not Sure')].size,
train[(train.AdoptionSpeed == 2)&(train.Sterilized == 'Not Sure')].size,
train[(train.AdoptionSpeed == 3)&(train.Sterilized == 'Not Sure')].size,
train[(train.AdoptionSpeed == 4)&(train.Sterilized == 'Not Sure')].size
] / total
percents = np.append(yesPercent, noPercent)
 percents = np.append(percents, notSurePercent)
 types =(['Yes'] * 5 + ['No'] * 5 + ['Not Sure'] * 5)
 typePercentDf = pd.DataFrame({ 'percent': percents, 'type': types })
 fig, ax = plt.subplots(figsize=(8,8))
sns.lineplot(y='percent', x=[0, 1, 2, 3, 4] * 3, hue='type', style='type', markers=True, data=typePercentDf, palette='coolwarm' )<define_variables>
for seed in range(NSEEDS):
mskf = MultilabelStratifiedKFold(n_splits=NFOLDS, random_state=seed, shuffle=True)
 for n,(tr, te)in enumerate(mskf.split(X_train, y_train)) :
print(f'Fold {n}')
 X_trn, X_val = X_train.iloc[tr,:], X_train.iloc[te,:]
y_trn, y_val = y_train.iloc[tr,:], y_train.iloc[te,:]
model = create_model(num_columns)
 checkpoint_path = f'repeat:{seed}_Fold:{n}.hdf5'
reduce_lr_loss = ReduceLROnPlateau(monitor='val_loss', factor=0.1, patience=3, verbose=1, epsilon=1e-4, mode='min')
 cb_checkpt = ModelCheckpoint(checkpoint_path, monitor = 'val_loss', verbose = 0, save_best_only = True,
save_weights_only = True, mode = 'min')
 model.fit(X_trn.values,
y_trn.values,
validation_data=(X_val.values, y_val.values),
epochs=35, batch_size=128,
callbacks=[reduce_lr_loss, cb_checkpt], verbose=2 
)
 model.load_weights(checkpoint_path)
 test_predict = model.predict(X_test.values)
 val_predict = model.predict(X_val.values)
 ss.loc[control_mask, y_train.columns] += test_predict
res.loc[te, y_train.columns] += val_predict
print('')
 ss.loc[:, y_train.columns] /=(NFOLDS * NSEEDS)
 res.loc[:, y_train.columns] /= NSEEDS
total = train[(train.Health == 'Healthy')].size
healthyPercent = [
train[(train.AdoptionSpeed == 0)&(train.Health == 'Healthy')].size,
train[(train.AdoptionSpeed == 1)&(train.Health == 'Healthy')].size,
train[(train.AdoptionSpeed == 2)&(train.Health == 'Healthy')].size,
train[(train.AdoptionSpeed == 3)&(train.Health == 'Healthy')].size,
train[(train.AdoptionSpeed == 4)&(train.Health == 'Healthy')].size
] / total
total = train[(train.Health == 'Minor Injury')].size
minorPercent = [
train[(train.AdoptionSpeed == 0)&(train.Health == 'Minor Injury')].size,
train[(train.AdoptionSpeed == 1)&(train.Health == 'Minor Injury')].size,
train[(train.AdoptionSpeed == 2)&(train.Health == 'Minor Injury')].size,
train[(train.AdoptionSpeed == 3)&(train.Health == 'Minor Injury')].size,
train[(train.AdoptionSpeed == 4)&(train.Health == 'Minor Injury')].size
] / total
total = train[(train.Health == 'Serious Injury')].size
seriousPercent = [
train[(train.AdoptionSpeed == 0)&(train.Health == 'Serious Injury')].size,
train[(train.AdoptionSpeed == 1)&(train.Health == 'Serious Injury')].size,
train[(train.AdoptionSpeed == 2)&(train.Health == 'Serious Injury')].size,
train[(train.AdoptionSpeed == 3)&(train.Health == 'Serious Injury')].size,
train[(train.AdoptionSpeed == 4)&(train.Health == 'Serious Injury')].size
] / total
percents = np.append(healthyPercent, minorPercent)
 percents = np.append(percents, seriousPercent)
 types =(['Healthy'] * 5 + ['Minor Injury'] * 5 + ['Serious Injury'] * 5)
 typePercentDf = pd.DataFrame({ 'percent': percents, 'type': types })
 fig, ax = plt.subplots(figsize=(8,8))
sns.lineplot(y='percent', x=[0, 1, 2, 3, 4] * 3, hue='type', style='type', markers=True, data=typePercentDf, palette='coolwarm' )<split>
def metric(y_true, y_pred):
metrics = []
for _target in y_train.columns:
metrics.append(log_loss(y_true.loc[:, _target], y_pred.loc[:, _target].astype(float), labels=[0,1]))
return np.mean(metrics )
target = 'AdoptionSpeed'
categorical = ['Type', 'Breed1', 'Gender', 'Color1', 'MaturitySize', 'FurLength', 'Vaccinated', 'Dewormed', 'Sterilized', 'Health', 'State']
features = categorical + numericalFeatures
data = train[features + [target]].dropna() 
<create_dataframe>
print(f'OOF Metric: {metric(y_train, res)}' )
def generateConfusionMatrix(y_real, y_predicted):
cm = pd.DataFrame()
cm['Satisfaction'] = y_real
cm['Predict'] = y_predicted
mappingSatisfaction = {0:'Same Day', 1: 'First Week', 2: 'First Month', 3: '2-3 Month', 4: 'Non-Adopted >100'}
mappingPredict = {0.0:'Same Day', 1.0: 'First Week', 2.0: 'First Month', 3.0: '2-3 Month', 4.0: 'Non-Adopted >100'}
cm = cm.replace({'Satisfaction': mappingSatisfaction, 'Predict': mappingPredict})
 return pd.crosstab(cm['Satisfaction'], cm['Predict'], margins=True)
<train_model>
ss.to_csv('submission.csv', index=False )