Kaggle competition: Mechanisms of Action (MoA) Prediction
<SOS> metric: MeanColumnwiseLogLoss Kaggle data source: mechanisms-of-action-(moa)-prediction<sort_values>
from fastai.tabular.all import *
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from fastai.callback import *
from tqdm.notebook import tqdm
from ml_stratifiers import MultilabelStratifiedKFold
modelacc.sort(reverse=True)
 modelacc<predict_on_test>
pd.set_option('display.max_rows', 50)
 pd.set_option('display.max_columns', 50 )
pred=model.predict([test])
 soln=[]
for i in range(len(pred)) :
soln.append(np.argmax(pred[i])) 
<save_to_csv>
path = Path('.. /input/lish-moa' )
final=pd.DataFrame()
final['ImageId']=[i+1 for i in x_test.index]
final['Label']=soln
final.to_csv('newmnistcnn',index=False )<import_modules>
test_features = pd.read_csv('/kaggle/input/lish-moa/test_features.csv')
 train_features = pd.read_csv('/kaggle/input/lish-moa/train_features.csv')
 train_targets_scored = pd.read_csv('/kaggle/input/lish-moa/train_targets_scored.csv')
 train_targets_nonscored = pd.read_csv('/kaggle/input/lish-moa/train_targets_nonscored.csv')
 sample_submission = pd.read_csv('/kaggle/input/lish-moa/sample_submission.csv' )
import numpy as np
import pandas as pd
import os
import matplotlib.pyplot as plt
import seaborn as sns
import tensorflow as tf
from keras.preprocessing.image import ImageDataGenerator, load_img
from keras.layers import Conv2D, BatchNormalization, Activation, MaxPooling2D, GlobalAveragePooling2D, Dense, Flatten, Dropout
from keras.optimizers import RMSprop, Adam, SGD
from keras import regularizers
from keras.callbacks import CSVLogger, ModelCheckpoint, ReduceLROnPlateau<load_from_csv>
train_features.sig_id.nunique()
train = pd.read_csv('.. /input/digit-recognizer/train.csv')
 test = pd.read_csv('.. /input/digit-recognizer/test.csv')
 train.head(5 )<prepare_x_and_y>
train_features.cp_type.value_counts()
y = train['label']
train.drop('label', axis=1, inplace=True )<categorify>
train_features.cp_time.value_counts()
y = to_categorical(y, num_classes = 10)
 y[0]<train_model>
train_features.cp_dose.value_counts()
train = train.values.reshape(train.shape[0], 28, 28, 1)
 test = test.values.reshape(test.shape[0], 28, 28, 1)
 print('Reshaped Train set: ',train.shape, " & Reshaped Test Set", test.shape )<data_type_conversions>
trn_df = train_features.merge(train_targets_scored,on='sig_id',how='left' )
train = train.astype("float32")/255.0
test = test.astype("float32")/255.0<split>
targets = pd.read_csv('.. /input/lish-moa/train_targets_scored.csv' )
X_train, X_val, y_train, y_val = train_test_split(train, y, test_size=0.25, random_state=0)
 print("Number of samples in Training set :", X_train.shape[0])
 print("Number of samples in Validation set :", X_val.shape[0] )<train_model>
def make_folds(folds = 5, random_state = 0, stratify = True, scored = None):
drug = pd.read_csv('.. /input/lish-moa/train_drug.csv')
 if scored is None:
scored = pd.read_csv('.. /input/lish-moa/train_targets_scored.csv')
 targets = scored.columns[1:]
scored = scored.merge(drug, on='sig_id', how='left')
 vc = scored.drug_id.value_counts()
vc1 = vc.loc[vc<=18].index.sort_values()
vc2 = vc.loc[vc>18].index.sort_values()
dct1 = {}; dct2 = {}
if stratify:
skf = MultilabelStratifiedKFold(n_splits=folds, shuffle=True, random_state=random_state)
 else:
skf = KFold(n_splits=folds, shuffle=True, random_state=random_state)
 tmp = scored.groupby('drug_id')[targets].mean().loc[vc1]
for fold,(idxT,idxV)in enumerate(skf.split(tmp,tmp[targets])) :
dd = {k:fold for k in tmp.index[idxV].values}
dct1.update(dd)
 if stratify:
skf = MultilabelStratifiedKFold(n_splits=folds, shuffle=True, random_state=random_state)
 else:
skf = KFold(n_splits=folds, shuffle=True, random_state=random_state)
 tmp = scored.loc[scored.drug_id.isin(vc2)].reset_index(drop=True)
 for fold,(idxT,idxV)in enumerate(skf.split(tmp,tmp[targets])) :
dd = {k:fold for k in tmp.sig_id[idxV].values}
dct2.update(dd)
 scored['fold'] = np.nan
scored['fold'] = scored.drug_id.map(dct1)
 scored.loc[scored.fold.isna() ,'fold'] = scored.loc[scored.fold.isna() ,'sig_id'].map(dct2)
 scored.fold = scored.fold.astype('int8')
 return scored[['sig_id','fold']].copy()
train_datagen = ImageDataGenerator(rotation_range=10,
zoom_range=0.1,
width_shift_range=0.1,
height_shift_range=0.1 
)
 training_set = train_datagen.flow(X_train, y_train,
batch_size=64 
)
 val_datagen = ImageDataGenerator()
val_set = val_datagen.flow(X_val, y_val,
batch_size=64
)<choose_model_class>
FOLDS = 10; SEED = 42
ff = make_folds(folds=FOLDS, random_state=SEED, stratify=True, scored=targets)
 trn_df['kfold'] = ff.fold.values
model = tf.keras.models.Sequential()
model.add(Conv2D(64, kernel_size=(5,5), padding='same', activation='relu', input_shape=(28,28,1)))
 model.add(Conv2D(64, kernel_size=(5,5), padding='same', activation='relu'))
model.add(BatchNormalization())
 model.add(MaxPooling2D(pool_size=(2,2)))
 model.add(Conv2D(128, kernel_size=(3,3), activation='relu', padding='same'))
model.add(Conv2D(128, kernel_size=(3,3), activation='relu', padding='same'))
model.add(BatchNormalization())
 model.add(MaxPooling2D(pool_size=(2,2)))
 model.add(Dropout(0.2))
model.add(Flatten())
 model.add(Dense(256, activation='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.5))
model.add(Dense(256, activation='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.3))
model.add(Dense(10, activation='softmax'))
model.compile(optimizer='adam',
loss='categorical_crossentropy',
metrics=['accuracy'] 
)
 model.summary()<choose_model_class>
df = trn_df.copy()
reduce_lr = ReduceLROnPlateau(monitor='val_loss',
factor=0.2,
patience=4,
verbose=1,
min_delta=0.0001 )<train_model>
sig_ids = test_features[test_features['cp_type'] == 'ctl_vehicle']['sig_id'].values
steps_per_epoch = training_set.n // training_set.batch_size
validation_steps = val_set.n // val_set.batch_size
hist = model.fit(x=training_set,
validation_data=val_set,
epochs=35,
callbacks=[reduce_lr],
steps_per_epoch=steps_per_epoch,
validation_steps=validation_steps
)<compute_test_metric>
cat_names = ['cp_type', 'cp_time', 'cp_dose']
cont_names = [c for c in train_features.columns if c not in cat_names and c != 'sig_id']
_, acc_val = model.evaluate(val_set)
 _, acc_tr = model.evaluate(val_set)
 print("
Final Accuracy on training set : {:.2f}% & accuracy on validation is set: {:.2f}%".format(acc_tr*100, acc_val*100))<predict_on_test>
def get_data(fold):
val_idx = df[df.kfold==fold].index
dls = TabularDataLoaders.from_df(df, path=path, y_names=cols,
cat_names = cat_names,
cont_names = cont_names,
procs = [Categorify, FillMissing, Normalize],
valid_idx=val_idx,
bs=64)
 return dls
pred = model.predict(test)
 res = np.argmax(pred, axis=1)
 submission = pd.DataFrame({"ImageId":[i+1 for i in range(len(test)) ],
"Label": res})
 submission.head(10 )<save_to_csv>
test_sc = []
for i in tqdm(range(FOLDS)) :
dls = get_data(i)
 learn = tabular_learner(dls , y_range=(0,1), layers=[1024, 512, 512, 256], loss_func = BCELossFlat() , model_dir='/kaggle/working/')
 name = 'best_model_' + str(i)
 cb = SaveModelCallback(monitor='valid_loss',fname=name ,mode='min')
 lr = 9e-3
learn.fit_one_cycle(10, slice(lr/(2.6**4),lr),cbs=cb)
 learn.load(name)
 test_dl = learn.dls.test_dl(test_features)
 sub = learn.get_preds(dl=test_dl)
 test_sc.append(sub[0].numpy())
 learn.export('/kaggle/working/'+name+'.pkl')
 test_sc = np.array(test_sc )
submission.to_csv("submission.csv", index=False )<save_to_csv>
avg_prds = test_sc.mean(axis=0 )
submission.to_csv("submission.csv", index=False )<import_modules>
submission = sample_submission.copy()
submission[cols] = avg_prds
submission.loc[submission['sig_id'].isin(test_features.loc[test_features['cp_type'] =='ctl_vehicle', 'sig_id']), train_targets_scored.columns[1:]] = 0
submission['atp-sensitive_potassium_channel_antagonist'] = 0
submission['erbb2_inhibitor'] = 0
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from keras.utils import to_categorical
from keras.models import Sequential
from keras.layers import Dense, Conv2D, MaxPool2D, Flatten, BatchNormalization, Dropout
from keras.callbacks import EarlyStopping, ReduceLROnPlateau
from keras.preprocessing.image import ImageDataGenerator
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix
import itertools<load_from_csv>
submission.to_csv('submission_tabular.csv',index=False )
train = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')
 test = pd.read_csv('/kaggle/input/digit-recognizer/test.csv' )<prepare_x_and_y>
from fastai.basics import *
from pytorch_tabnet import *
from fast_tabnet.core import *
X_train = train.iloc[:,1:]
y_train = train.iloc[:,0]<categorify>
test_sc_tab = []
lr = 9e-3
for i in tqdm(range(FOLDS)) :
dls = get_data(i)
 emb_szs = get_emb_sz(dls)
 model = TabNetModel(emb_szs, len(dls.cont_names), dls.c, n_d=8, n_a=32, n_steps=1);
opt_func = partial(Adam, wd=0.01, eps=1e-5)
 learn = Learner(dls, model, BCEWithLogitsLossFlat() , opt_func=opt_func, lr=lr, model_dir='/kaggle/working/')
 name = 'best_model_tabnet_' + str(i)
 cb = SaveModelCallback(monitor='valid_loss',fname=name ,mode='min')
 lr = 9e-3
learn.fit_one_cycle(30, slice(lr/(2.6**4),lr),cbs=cb)
 learn.load(name)
 test_dl = learn.dls.test_dl(test_features)
 sub = learn.get_preds(dl=test_dl)
 test_sc_tab.append(sub[0].numpy())
 learn.export('/kaggle/working/'+name+'.pkl')
 test_sc_tab = np.array(test_sc_tab )
X_train = X_train.values.reshape(-1, 28, 28, 1)/255.
 test = test.values.reshape(-1, 28, 28, 1)/255.
 y_train = to_categorical(y_train, 10 )<split>
avg_prds_tab = test_sc_tab.mean(axis=0 )
random_seed = 0
X_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size=0.2, random_state=random_seed )<choose_model_class>
submission_tab = sample_submission.copy()
submission_tab[cols] = avg_prds_tab
submission_tab.loc[submission_tab['sig_id'].isin(test_features.loc[test_features['cp_type'] =='ctl_vehicle', 'sig_id']), train_targets_scored.columns[1:]] = 0
submission_tab['atp-sensitive_potassium_channel_antagonist'] = 0
submission_tab['erbb2_inhibitor'] = 0
datagen = ImageDataGenerator(
 rotation_range=10,
width_shift_range=0.1,
height_shift_range=0.1,
zoom_range=0.1
)<choose_model_class>
submission_tab.to_csv('submission_tabnet.csv',index=False )
model = Sequential()
model.add(Conv2D(32,(5,5), padding='same', input_shape=X_train.shape[1:], activation='relu'))
model.add(Conv2D(32,(5,5), padding='same', activation='relu'))
model.add(MaxPool2D(2,2))
model.add(Conv2D(64,(3,3), padding='same', activation='relu'))
model.add(Conv2D(64,(3,3), padding='same', activation='relu'))
model.add(MaxPool2D(2,2))
model.add(Flatten())
 model.add(Dense(256, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(10, activation='softmax'))
model.summary()<choose_model_class>
final_prds = np.array(( list(avg_prds),list(avg_prds_tab)) ).mean(axis=0 )
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'] )<train_model>
fin_wt_prds = avg_prds*(0.75)+ avg_prds_tab*(0.25 )
EPOCHS = 30
BATCH_SIZE = 20
callback_list = [
ReduceLROnPlateau(monitor='val_loss', factor=0.5, patience=1),
EarlyStopping(monitor='val_loss', min_delta=0.0005, patience=4)
 ]
history = model.fit(datagen.flow(X_train, y_train, batch_size=BATCH_SIZE),
epochs=EPOCHS,
callbacks=callback_list,
validation_data=(X_val, y_val),
steps_per_epoch=X_train.shape[0] // BATCH_SIZE )<save_to_csv>
submission_fin = sample_submission.copy()
submission_fin[cols] = fin_wt_prds
submission_fin.loc[submission_fin['sig_id'].isin(test_features.loc[test_features['cp_type'] =='ctl_vehicle', 'sig_id']), train_targets_scored.columns[1:]] = 0
submission_fin['atp-sensitive_potassium_channel_antagonist'] = 0
submission_fin['erbb2_inhibitor'] = 0
<import_modules><EOS> 
results = submission_fin.copy()
for cl in cols:
results[cl].clip(0.0002, 0.999, inplace = True)
 results.to_csv('submission.csv',index=False )