Kaggle competition: Mechanisms of Action (MoA) Prediction
<SOS> metric: MeanColumnwiseLogLoss Kaggle data source: mechanisms-of-action-(moa)-prediction<split>
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
X_train, X_test, Y_train, Y_test = model_selection.train_test_split(X, Y, test_size=0.3, random_state=7)
 del X
del Y<init_hyperparams>
%%time
! python -W ignore.. /input/finalmoascripts/dae-book3b.py
def MacroF1Metric(preds, dtrain):
labels = dtrain.get_label()
preds = np.round(np.clip(preds, 0, 10)).astype(int)
 score = f1_score(labels, preds, average = 'macro')
 return('MacroF1Metric', score, True)
 params = {}
params['application']='root_mean_squared_error'
params['num_boost_round'] = 3000
params['learning_rate'] = 0.01
params['boosting_type'] = 'gbdt'
params['metric'] = 'rmse'
params['sub_feature'] = 0.833
params['num_leaves'] = 207
params['min_split_gain'] = 0.05
params['min_child_weight'] = 27
params['max_depth'] = -1
params['num_threads'] = 20
params['max_bin'] = 50
params['lambda_l2'] = 0.10
params['lambda_l1'] = 0.30
params['feature_fraction']= 0.833
params['bagging_fraction']= 0.979
params['seed']=1729
params['device_type'] = 'gpu'
model = lgb.train(params, lgb.Dataset(X_train, Y_train), 2000, lgb.Dataset(X_test, Y_test),verbose_eval=100, early_stopping_rounds=200, feval=MacroF1Metric )<drop_column>
%%time
! python -W ignore.. /input/finalmoascripts/dae-book3c.py
del X_train
del X_test
del Y_train
del Y_test<set_options>
%%time
! python -W ignore.. /input/finalmoascripts/cv-inference-efficientnet.py
gc.collect()<load_from_csv>
%%time
! python -W ignore.. /input/finalmoascripts/cv-inference-efficientnet-augment.py
test = pd.read_csv('/kaggle/input/ionswitchingkl/datasets/testK.csv')
 test = features(test )<save_to_csv>
%%time
! python -W ignore.. /input/finalmoascripts/inference_mx10.py
preds = model.predict(test[col], num_iteration=model.best_iteration)
 test['open_channels'] = np.round(np.clip(preds, 0, 10)).astype(int)
 test[['time','open_channels']].to_csv('submission.csv', index=False, float_format='%.4f' )<define_variables>
%%time
! python -W ignore.. /input/finalmoascripts/updated-inference-final-mx10-transfer-3.py
EPOCHS = 300
NNBATCHSIZE = 8
GROUP_BATCH_SIZE = 2048
SEED = 42
LR = 0.001
SPLITS = 5
def seed_everything(seed):
random.seed(seed)
 np.random.seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 tf.random.set_seed(seed )<load_from_csv>
%%time
! python -W ignore.. /input/finalmoascripts/inference_tabnet_dae.py
def read_data() :
train = pd.read_csv('/kaggle/input/data-without-drift/train_clean.csv', dtype={'time': np.float32, 'signal': np.float32, 'open_channels':np.int32})
 test = pd.read_csv('/kaggle/input/data-without-drift/test_clean.csv', dtype={'time': np.float32, 'signal': np.float32})
 sub = pd.read_csv('/kaggle/input/liverpool-ion-switching/sample_submission.csv', dtype={'time': np.float32})
 Y_train_proba = np.load("/kaggle/input/ion-shifted-rfc-proba/Y_train_proba.npy")
 Y_test_proba = np.load("/kaggle/input/ion-shifted-rfc-proba/Y_test_proba.npy")
 for i in range(11):
train[f"proba_{i}"] = Y_train_proba[:, i]
test[f"proba_{i}"] = Y_test_proba[:, i]
return train, test, sub
def batching(df, batch_size):
df['group'] = df.groupby(df.index//batch_size, sort=False)['signal'].agg(['ngroup'] ).values
df['group'] = df['group'].astype(np.uint16)
 return df
def normalize(train, test):
train_input_mean = train.signal.mean()
train_input_sigma = train.signal.std()
train['signal'] =(train.signal - train_input_mean)/ train_input_sigma
test['signal'] =(test.signal - train_input_mean)/ train_input_sigma
return train, test
def lag_with_pct_change(df, windows):
for window in windows:
df['signal_shift_pos_' + str(window)] = df.groupby('group')['signal'].shift(window ).fillna(0)
 df['signal_shift_neg_' + str(window)] = df.groupby('group')['signal'].shift(-1 * window ).fillna(0)
 return df
def run_feat_engineering(df, batch_size):
df = batching(df, batch_size = batch_size)
 df = lag_with_pct_change(df, [1, 2, 3])
 df['signal_2'] = df['signal'] ** 2
return df
def feature_selection(train, test):
features = [col for col in train.columns if col not in ['index', 'group', 'open_channels', 'time']]
train = train.replace([np.inf, -np.inf], np.nan)
 test = test.replace([np.inf, -np.inf], np.nan)
 for feature in features:
feature_mean = pd.concat([train[feature], test[feature]], axis = 0 ).mean()
train[feature] = train[feature].fillna(feature_mean)
 test[feature] = test[feature].fillna(feature_mean)
 return train, test, features<define_search_model>
%%time
! python -W ignore.. /input/finalmoascripts/inference-tabnet-1830.py
def cbr(x, out_layer, kernel, stride, dilation):
x = Conv1D(out_layer, kernel_size=kernel, dilation_rate=dilation, strides=stride, padding="same" )(x)
 x = BatchNormalization()(x)
 x = Activation("relu" )(x)
 return x
def se_block(x_in, layer_n):
x = GlobalAveragePooling1D()(x_in)
 x = Dense(layer_n//8, activation="relu" )(x)
 x = Dense(layer_n, activation="sigmoid" )(x)
 x_out=Multiply()([x_in, x])
 return x_out
def resblock(x_in, layer_n, kernel, dilation, use_se=True):
x = cbr(x_in, layer_n, kernel, 1, dilation)
 x = cbr(x, layer_n, kernel, 1, dilation)
 if use_se:
x = se_block(x, layer_n)
 x = Add()([x_in, x])
 return x<define_search_model>
%%time
! python -W ignore.. /input/finalmoascripts/inference_pytorch_nn_01833.py
def BUSUnet(input_shape=(None,1)) :
layer_n = 64
kernel_size = 7
depth = 2
input_layer = Input(input_shape)
 input_layer_1 = AveragePooling1D(5 )(input_layer)
 input_layer_2 = AveragePooling1D(25 )(input_layer)
 x = cbr(input_layer, layer_n, kernel_size, 1, 1)
 for i in range(depth):
x = resblock(x, layer_n, kernel_size, 1)
 out_0 = x
x = cbr(x, layer_n*2, kernel_size, 5, 1)
 for i in range(depth):
x = resblock(x, layer_n*2, kernel_size, 1)
 out_1 = x
x = Concatenate()([x, input_layer_1])
 x = cbr(x, layer_n*3, kernel_size, 5, 1)
 for i in range(depth):
x = resblock(x, layer_n*3, kernel_size, 1)
 out_2 = x
x = Concatenate()([x, input_layer_2])
 x = cbr(x, layer_n*4, kernel_size, 5, 1)
 for i in range(depth):
x = resblock(x, layer_n*4, kernel_size, 1)
 x = UpSampling1D(5 )(x)
 x = Concatenate()([x, out_2])
 x = cbr(x, layer_n*3, kernel_size, 1, 1)
 x = UpSampling1D(5 )(x)
 x = Concatenate()([x, out_1])
 x = cbr(x, layer_n*2, kernel_size, 1, 1)
 x = UpSampling1D(5 )(x)
 x = Concatenate()([x, out_0])
 x = cbr(x, layer_n, kernel_size, 1, 1)
 for i in range(depth):
x = resblock(x, layer_n, kernel_size, 1)
 out_0 = x
x = cbr(x, layer_n*2, kernel_size, 5, 1)
 for i in range(depth):
x = resblock(x, layer_n*2, kernel_size, 1)
 out_1 = x
x = Concatenate()([x, input_layer_1])
 x = cbr(x, layer_n*3, kernel_size, 5, 1)
 for i in range(depth):
x = resblock(x, layer_n*3, kernel_size, 1)
 x = UpSampling1D(5 )(x)
 x = Concatenate()([x, out_1])
 x = cbr(x, layer_n*2, kernel_size, 1, 1)
 x = UpSampling1D(5 )(x)
 x = Concatenate()([x, out_0])
 x = cbr(x, layer_n, kernel_size, 1, 1)
 x = Conv1D(11, kernel_size=kernel_size, strides=1, padding="same" )(x)
 out = Activation("softmax" )(x)
 model = models.Model(inputs = input_layer, outputs = out)
 opt = Adam(lr = LR)
 opt = tfa.optimizers.SWA(opt)
 model.compile(loss = losses.CategoricalCrossentropy() , optimizer = opt, metrics = ['accuracy'])
 return model<features_selection>
%%time
! python -W ignore.. /input/finalmoascripts/NN1835.py
def run_everything() :
print('Reading Data Started...')
 train, test, sample_submission = read_data()
train, test = normalize(train, test)
 print('Reading and Normalizing Data Completed')
 print('Creating Features')
 print('Feature Engineering Started...')
 train = run_feat_engineering(train, batch_size = GROUP_BATCH_SIZE)
 test = run_feat_engineering(test, batch_size = GROUP_BATCH_SIZE)
 train, test, features = feature_selection(train, test)
 print('Feature Engineering Completed...')
 print(f'Training BUSU-Net model with {SPLITS} folds of GroupKFold Started...')
 run_cv_model_by_batch(train, test, SPLITS, 'group', features, sample_submission, EPOCHS, NNBATCHSIZE)
 print('Training completed...')
 run_everything()<set_options>
%%time
! python -W ignore.. /input/finalmoascripts/tabnet-public-184-inference.py
%matplotlib inline
DEBUG = False<load_from_disk>
%%time
! python -W ignore.. /input/finalmoascripts/resnet-v64-infer.py
if(DEBUG):
train = pd.read_json('./train.json')
 test = pd.read_json('./test.json')
 else:
train = pd.read_json('.. /input/train.json')
 test = pd.read_json('.. /input/test.json')
 train.ingredients = train.ingredients.apply(lambda l: ", ".join(l))
test.ingredients = test.ingredients.apply(lambda l: ", ".join(l))
train.head()<feature_engineering>
test = pd.read_csv('/kaggle/input/lish-moa/test_features.csv' ).set_index('sig_id')
 print(test.shape )
vectorizerIngr = CountVectorizer(analyzer = "word", tokenizer = None, preprocessor = None, stop_words = None,\
max_features = 50000, binary = False, token_pattern=r' (.+? )(?:,\s|$)')
 vectorizerIngr.fit(train['ingredients'])
 bagOfWords = vectorizerIngr.transform(train['ingredients'])
 bagOfWordsTest = vectorizerIngr.transform(test['ingredients'])
 vectorizerWords = CountVectorizer(analyzer = "word", tokenizer = None, preprocessor = None, stop_words = None,\
max_features = 50000, binary = False, token_pattern=r'(\w+? )(?:,\s|\s|$)')
 vectorizerWords.fit(train['ingredients'])
 bagOfWordsWords = vectorizerWords.transform(train['ingredients'])
 bagOfWordsTestWords = vectorizerWords.transform(test['ingredients'])
 vectorizerTfDf = TfidfVectorizer(analyzer = "word", tokenizer = None, preprocessor = None, stop_words = None,\
max_features = 50000, binary = False, token_pattern=r'(\w+? )(?:,\s|\s|$)')
 vectorizerTfDf.fit(train['ingredients'])
 bagOfWordsTfDf = vectorizerTfDf.transform(train['ingredients'])
 bagOfWordsTestTfDf = vectorizerTfDf.transform(test['ingredients'] )<import_modules>
mx10 = pd.read_csv('submission_mx10.csv' ).set_index('sig_id')
 mx10_updated = pd.read_csv('submission_mx10_updated.csv' ).set_index('sig_id')
 tabnet_dae = pd.read_csv('submission_tabnet_01831.csv' ).set_index('sig_id')
 tabnet_1830 = pd.read_csv('submission_tabnet_01830.csv' ).set_index('sig_id')
 dae = pd.read_csv('submission_dae.csv' ).set_index('sig_id')
 dae_updated = pd.read_csv('submission_dae_updated.csv' ).set_index('sig_id')
 pytorch_nn = pd.read_csv('submission_pytorch_01833.csv' ).set_index('sig_id')
 keras_resnet = pd.read_csv('submission_RES_v64.csv' ).set_index('sig_id')
 effnet = pd.read_csv('submission_effnet.csv' ).set_index('sig_id')
 effnet_augment = pd.read_csv('submission_effnet_augment.csv' ).set_index('sig_id')
 tabnet_1840 = pd.read_csv('submission_tabnet_1840.csv' ).set_index('sig_id')
 NN_1835 = pd.read_csv('submission_NN1835.csv' ).set_index('sig_id' )
import warnings, sklearn
import scipy.sparse as sparse
from sklearn import *<categorify>
df = pd.DataFrame()
df['TabNet_DAE'] = tabnet_dae.values.flatten()
df['TabNet_1830'] = tabnet_1830.values.flatten()
df['TabNet_1840'] = tabnet_1840.values.flatten()
df['MX10'] = mx10.values.flatten()
df['MX10_updated'] = mx10_updated.values.flatten()
df['PyTorch NN'] = pytorch_nn.values.flatten()
df['NN_1835'] = NN_1835.values.flatten()
df['Keras ResNet'] = keras_resnet.values.flatten()
df['DAE_0.2'] = dae.values.flatten()
df['DAE_0.3'] = dae_updated.values.flatten()
df['EffNet'] = effnet.values.flatten()
df['EffNet_augment'] = effnet_augment.values.flatten()
bagOfWords = bagOfWords.astype('float')
 bagOfWordsWords = bagOfWordsWords.astype('float')
 kolvoWords = np.sum(bagOfWordsWords, axis=1)
 kolvoIngr = np.sum(bagOfWords, axis=1 )<train_on_grid>
sub =(( sub2 + sub7 + sub8)/3.+(sub1 + sub11)/2.+(sub3 + sub10)/2.+(sub4 + sub5 + sub9)/3.+(sub6 + sub12)/2.) /5.
 sub.reset_index(inplace=True )
lambdaKolvoIngr = 0.0606
lambdaKolvoWords = 0.05
lambdaTfDf = 1
lambdaBagOfW = 0.2121
lambdaWords = 0.116
data1 = sparse.hstack(( lambdaTfDf * bagOfWordsTfDf, \
bagOfWords * lambdaBagOfW, \
bagOfWordsWords * lambdaWords, \
kolvoWords*lambdaKolvoWords, \
kolvoIngr*lambdaKolvoIngr))
SVM = svm.LinearSVC(C=0.3)
 SVM.fit(data1, train['cuisine'] )<predict_on_test>
def scale(x,k):
x = x.copy()
idx = np.where(x!=1)[0]
y = k * x[idx] /(1-x[idx])
 x[idx] = y/(1+y)
 return x
bagOfWordsTest = bagOfWordsTest.astype('float')
 bagOfWordsTestWords = bagOfWordsTestWords.astype('float')
 kolvoWordsTest = np.sum(bagOfWordsTestWords, axis=1)
 kolvoIngrTest = np.sum(bagOfWordsTest, axis=1)
 dataTest = sparse.hstack(( lambdaTfDf * bagOfWordsTestTfDf, \
bagOfWordsTest * lambdaBagOfW, \
bagOfWordsTestWords * lambdaWords, \
kolvoWordsTest*lambdaKolvoWords, \
kolvoIngrTest*lambdaKolvoIngr))
class FuncCreator:
def __init__(self):
self.dict_cuisine = dict()
self.ind = 0
def __call__(self, it):
x = it.lower()
if not(x in self.dict_cuisine.keys()):
self.dict_cuisine[x] = self.ind
self.ind = self.ind + 1
return it
def getNameFromId(self, id):
for key, val in self.dict_cuisine.items() :
if(val == id):
return key
return "UNKNOW"
class MySVM:
def __init__(self, C=0.3):
self.SVMS = dict()
self.rep = FuncCreator()
self.keys=[]
self.C=C
def learn(self, data1, y, res, ar=[]):
self.rep = FuncCreator()
y.apply(self.rep)
 self.keys=np.array(list(self.rep.dict_cuisine.keys()))
i = 0
for key in self.keys:
ndata=y==key
if(i >= len(ar)) :
SVM = svm.LinearSVC(C=self.C)
 else:
SVM = svm.LinearSVC(C=ar[i])
 SVM.fit(data1, ndata)
 self.SVMS[key] = SVM
i = i + 1
def predict(self, data, ar=[]):
arr = np.zeros(( data.shape[0],len(self.keys)))
 i = 0
for key in self.keys:
if(len(ar)== 0 or(( len(ar)) < len(self.rep.dict_cuisine.keys()))):
arr[:,i]=self.SVMS[key].decision_function(data)
 else:
arr[:,i]=ar[i] * self.SVMS[key].decision_function(data)
 i = i+1
return self.keys[np.argmax(arr, axis=1)]
SVM = MySVM(C=0.29)
 SVM.learn(data1, train['cuisine'], 0, [0.25, 0.4, 0.25, 0.25, 0.1, 0.3, 0.3, 0.3, 0.2, 0.35, 0.35, 0.5, 0.25, 0.3, 0.3, 0.35, 0.6, 0.4, 0.55, 0.45])
 res = SVM.predict(dataTest )<save_to_csv>
train = pd.read_csv('/kaggle/input/lish-moa/train_targets_scored.csv')
 COLS = ['acetylcholine_receptor_agonist',
'acetylcholine_receptor_antagonist', 'adrenergic_receptor_agonist',
'adrenergic_receptor_antagonist',
'bacterial_cell_wall_synthesis_inhibitor',
'calcium_channel_blocker', 'cyclooxygenase_inhibitor',
'dna_inhibitor', 'dopamine_receptor_antagonist',
'estrogen_receptor_agonist', 'gaba_receptor_antagonist',
'glutamate_receptor_antagonist', 'histamine_receptor_antagonist',
'phosphodiesterase_inhibitor', 'serotonin_receptor_agonist',
'serotonin_receptor_antagonist', 'sodium_channel_inhibitor']
f =open("svm_output.csv", "w")
 f.write("id,cuisine
")
 i = 0
for item in test["id"]:
f.write(str(item))
f.write(",")
 f.write(res[i])
 f.write("
")
 i= i +1
f.close()<import_modules>
COLS = ['erbb2_inhibitor', 'atp-sensitive_potassium_channel_antagonist',
'aldehyde_dehydrogenase_inhibitor', 'antiarrhythmic',
'leukotriene_inhibitor', 'coagulation_factor_inhibitor',
'ras_gtpase_inhibitor', 'ubiquitin_specific_protease_inhibitor',
'elastase_inhibitor', 'nicotinic_receptor_agonist',
'protein_phosphatase_inhibitor', 'atm_kinase_inhibitor',
'retinoid_receptor_antagonist', 'lxr_agonist',
'autotaxin_inhibitor', 'tropomyosin_receptor_kinase_inhibitor',
'diuretic', 'monoacylglycerol_lipase_inhibitor',
'norepinephrine_reuptake_inhibitor', 'rna_polymerase_inhibitor',
'calcineurin_inhibitor', 'catechol_o_methyltransferase_inhibitor',
'laxative', 'steroid', 'pkc_inhibitor', 'glutamate_inhibitor',
'lipase_inhibitor', 'tlr_antagonist',
'nitric_oxide_production_inhibitor', 'antihistamine',
'ampk_activator', 'protein_tyrosine_kinase_inhibitor',
'caspase_activator', 'acat_inhibitor', 'pdk_inhibitor',
'anticonvulsant', 'p-glycoprotein_inhibitor',
'free_radical_scavenger', 'mineralocorticoid_receptor_antagonist',
'beta_amyloid_inhibitor', 'smoothened_receptor_antagonist',
'syk_inhibitor', 'analgesic', 'vitamin_b', 'tlr_agonist',
'antiviral', 'antimalarial',
'transient_receptor_potential_channel_antagonist',
'cck_receptor_antagonist', 'wnt_inhibitor',
'imidazoline_receptor_agonist', 'gonadotropin_receptor_agonist',
'phospholipase_inhibitor', 'insulin_secretagogue',
'thrombin_inhibitor']
warnings.filterwarnings("ignore")
 pd.set_option('max_colwidth',400)
 tqdm.pandas()<load_from_disk>
FACTOR = 7
ct = 0
for c in COLS:
t_sum = train[c].sum()
m1 = train[c].mean()
m2 = sub[c].mean()
ratio = m2/m1
m = FACTOR*6/t_sum/ratio
if m>1:
print(c)
 print('multiplier = %.3f'%m,'because t_sum = %i, ratio = %.2f'%(t_sum,ratio))
sub[c] = scale(sub[c].values,m)
 m3 = sub[c].mean()
r2 = m3/m1
r3 = m3/m2
print(' new ratio = %.1f, effective multiplier = %.3f'%(r2,r3))
print()
ct += 1
else:
print(c)
 print('
print()
print('Updated',ct,'targets' )
train_set = pd.read_json('.. /input/whats-cooking/train.json', orient='columns')
 test_set = pd.read_json('.. /input/whats-cooking/test.json', orient='columns' )<create_dataframe>
sub.to_csv('submission.csv',index=False)
 sub.head()
<count_unique_values><EOS> 
train = pd.read_csv('/kaggle/input/lish-moa/train_targets_scored.csv')
 idx = np.random.randint(1,207,10)
 train.iloc[:,idx].mean()