Kaggle competition: Mechanisms of Action (MoA) Prediction
train_df = pd.read_csv('.. /input/train.csv')
 test_df = pd.read_csv('.. /input/test.csv' )<count_missing_values>
from tensorflow.keras.callbacks import ReduceLROnPlateau, EarlyStopping
from sklearn.model_selection import KFold, StratifiedKFold
from sklearn.preprocessing import LabelEncoder
from sklearn.metrics import log_loss
import tensorflow.keras.backend as K
import tensorflow.keras.layers as L
import tensorflow.keras.models as M
from tensorflow.keras import regularizers
import tensorflow_addons as tfa
from tqdm.notebook import tqdm
import tensorflow as tf
import pandas as pd
import numpy as np
import random
import time
import collections
import itertools
from sklearn.decomposition import PCA
train_df.isnull().values.any()<define_variables>
test_features = pd.read_csv('/kaggle/input/lish-moa/test_features.csv')
 train_targets = pd.read_csv('/kaggle/input/lish-moa/train_targets_scored.csv')
 train_features = pd.read_csv('/kaggle/input/lish-moa/train_features.csv')
 submission = pd.read_csv('/kaggle/input/lish-moa/sample_submission.csv')
 del test_features['sig_id'], train_features['sig_id'], train_targets['sig_id']
features = [c for c in train_df.columns if c not in ['ID_code', 'target']]
target = train_df['target']<init_hyperparams>
categorical = ['cp_type', 'cp_dose']
for feature in categorical:
trans = LabelEncoder()
train_features[feature] = trans.fit_transform(train_features[feature])
 test_features[feature] = trans.fit_transform(test_features[feature])
 time_mapping = {24:1, 48:2, 72:3}
train_features['cp_time'] = train_features['cp_time'].map(time_mapping)
 test_features['cp_time'] = test_features['cp_time'].map(time_mapping)
 train_features
param = {
'bagging_freq': 5,
'bagging_fraction': 0.33,
'boost_from_average':'false',
'boost': 'gbdt',
'feature_fraction': 0.05,
'learning_rate': 0.01,
'max_depth': -1,
'metric':'auc',
'min_data_in_leaf': 80,
'min_sum_hessian_in_leaf': 10.0,
'num_leaves': 13,
'num_threads': 12,
'tree_learner': 'serial',
'objective': 'binary',
'verbosity': 1
}<define_variables>
pca = PCA(n_components=800)
 train_features_pca = pca.fit_transform(train_features)
 test_features_pca = pca.transform(test_features)
 train_features_pca = pd.DataFrame(train_features_pca)
 test_features_pca = pd.DataFrame(test_features_pca)
 train_features = train_features_pca
test_features = test_features_pca
num_round = 100000
folds = StratifiedKFold(n_splits=12, shuffle=False, random_state=12345)
 oof = np.zeros(len(train_df))
predictions = np.zeros(len(test_df))<predict_on_test>
correlations = train_features.corr().abs().unstack().sort_values(kind='quicksort', ascending=False ).reset_index()
correlations = correlations[correlations['level_0'] != correlations['level_1']].reset_index()
correlations = correlations[correlations.iloc[:, 3] > 0.92]
c = collections.Counter(correlations.iloc[:, 3])
 print(correlations)
 drop_columns = correlations[::2]['level_0'].values
train_features = train_features.drop(columns=drop_columns)
 test_features = test_features.drop(columns=drop_columns)
 train = train_features.copy()
train
for fold_,(trn_idx, val_idx)in enumerate(folds.split(train_df.values, target.values)) :
print("Fold {}".format(fold_))
trn_data = lgb.Dataset(train_df.iloc[trn_idx][features], label=target.iloc[trn_idx])
 val_data = lgb.Dataset(train_df.iloc[val_idx][features], label=target.iloc[val_idx])
 clf = lgb.train(param, trn_data, num_round, valid_sets = [trn_data, val_data], verbose_eval=1000, early_stopping_rounds = 2500)
 oof[val_idx] = clf.predict(train_df.iloc[val_idx][features], num_iteration=clf.best_iteration)
 predictions += clf.predict(test_df[features], num_iteration=clf.best_iteration)/ folds.n_splits
print("CV score: {:<8.5f}".format(roc_auc_score(target, oof)) )<save_to_csv>
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(1400, activation="relu")) ,
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Dropout(0.4),
tfa.layers.WeightNormalization(tf.keras.layers.Dense(840, activation="sigmoid")) ,
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Dropout(0.3),
tfa.layers.WeightNormalization(tf.keras.layers.Dense(588, activation="sigmoid")) ,
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Dropout(0.2),
tfa.layers.WeightNormalization(tf.keras.layers.Dense(470, activation="relu")) ,
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Dropout(0.1),
tfa.layers.WeightNormalization(tf.keras.layers.Dense(206, activation="sigmoid"))
])
 model.compile(optimizer=tfa.optimizers.Lookahead(tf.optimizers.Adam() , sync_period=5),
loss='binary_crossentropy', metrics=["accuracy"] 
)
 return model
submission = pd.DataFrame({"ID_code": test_df.ID_code.values})
 submission["target"] = predictions
submission.to_csv("submission.csv", index=False )<set_options>
N_STARTS = 3
tf.random.set_seed(43)
 res = train_targets.copy()
submission.loc[:, train_targets.columns] = 0
res.loc[:, train_targets.columns] = 0
for seed in range(N_STARTS):
for n,(tr, te)in enumerate(KFold(n_splits=5, random_state=seed, shuffle=True ).split(train_targets)) :
print(f'Fold {n}')
 model = create_model(len(train.columns))
reduce_lr_loss = ReduceLROnPlateau(monitor='val_loss', factor=0.15, patience=3, verbose=1, epsilon=1e-4, mode='min')
 early_stop = EarlyStopping(monitor='val_loss', min_delta=0, patience=5, mode= 'min')
 model.fit(train.values[tr],
train_targets.values[tr],
validation_data=(train.values[te], train_targets.values[te]),
epochs=45, batch_size=64,
callbacks=[reduce_lr_loss, early_stop], verbose=2 
)
 test_predict = model.predict(test_features.values)
 val_predict = model.predict(train.values[te])
 submission.loc[:, train_targets.columns] += test_predict
res.loc[te, train_targets.columns] += val_predict
print('')
 submission.loc[:, train_targets.columns] /=(( n+1)* N_STARTS)
 res.loc[:, train_targets.columns] /= N_STARTS
pd.set_option('display.max_columns', 200)
 InteractiveShell.ast_node_interactivity = "all"
warnings.filterwarnings('ignore')
<load_from_csv>
metrics = []
for _target in train_targets.columns:
metrics.append(log_loss(train_targets.loc[:, _target], res.loc[:, _target]))
print(np.mean(metrics))
train_df = pd.read_csv('.. /input/train.csv')
 test_df = pd.read_csv('.. /input/test.csv' )<count_missing_values>
df = pd.read_csv("/kaggle/input/lish-moa/sample_submission.csv")
 df_test = pd.read_csv('/kaggle/input/lish-moa/test_features.csv')
 test_id = df_test['sig_id'].values
df_submit = pd.DataFrame(index=test_id, columns=df.columns.drop('sig_id'))
df_submit.index.name = 'sig_id'
df_submit[:] = 0
df_predict = submission.copy()
df_submit.loc[df_predict.sig_id, :] = df_predict[df.columns.drop('sig_id')].values
df_submit.loc[df_test[df_test.cp_type=='ctl_vehicle'].sig_id] = 0
df_submit.to_csv('submission.csv', index=True )