Kaggle competition: Mechanisms of Action (MoA) Prediction
params = {
'num_leaves': 144,
'learning_rate': 0.1,
'n_estimators': 800,
'max_depth':12,
'max_bin':55,
'bagging_fraction':0.8,
'bagging_freq':5,
'feature_fraction':0.9,
'verbose':50,
'early_stopping_rounds':100
}
lgbm_reg = lgb.LGBMRegressor(num_leaves=params['num_leaves'], learning_rate=params['learning_rate'],
n_estimators=params['n_estimators'], max_depth=params['max_depth'],
max_bin = params['max_bin'], bagging_fraction = params['bagging_fraction'],
bagging_freq = params['bagging_freq'], feature_fraction = params['feature_fraction'], 
)
 lgb_oof_preds, lgb_sub_preds, lgb_feature_importance_df = oof_model_preds(dataset, lgbm_reg, num_folds=4, params=params )<compute_test_metric>
data_dir = '.. /input/lish-moa/'
train_set = pd.read_csv('.. /input/lish-moa/train_features.csv')
 train_target = pd.read_csv('.. /input/lish-moa/train_targets_scored.csv')
 test_set = pd.read_csv('.. /input/lish-moa/test_features.csv')
 ss = pd.read_csv('.. /input/lish-moa/sample_submission.csv')
 ss.loc[:, train_target.columns[1:]] = 0
display_importances(lgb_feature_importance_df )<save_to_csv>
GENES = [col for col in train_set.columns if col.startswith('g-')]
CELLS = [col for col in train_set.columns if col.startswith('c-')]
drop_index = train_set[train_set.cp_type == 'ctl_vehicle'].index
add_index = test_set[test_set.cp_type == 'ctl_vehicle'].index
sub = pd.DataFrame()
sub['Id'] = test_df['Id']
sub['winPlacePerc'] = lgb_sub_preds
sub['winPlacePerc'][sub['winPlacePerc'] > 1] = 1
sub.to_csv('lgb_submission.csv',index=False )<load_from_csv>
def transform_features(df):
df = df.drop(df[df.cp_type == 'ctl_vehicle'].index)
 df = df.drop(['sig_id','cp_type'], axis=1)
 df['cp_time']=df['cp_time'].map({24: 1, 48: 2, 72: 3})
 df["cp_dose"] = df['cp_dose'].map({"D1": 1, "D2": 2})
 return df
X_train = transform_features(train_set)
 X_test = transform_features(test_set)
 train_target = train_target.drop(drop_index, axis = 0)
 y_train = train_target.drop(['sig_id'], axis=1 ).values
df_train = pd.read_csv('.. /input/train.csv')
 df_test = pd.read_csv('.. /input/test.csv' )<count_unique_values>
def rankgauss(D):
for col in(GENES+CELLS):
qt = preprocessing.QuantileTransformer(n_quantiles=100, random_state=0, output_distribution="normal")
 vec = D[col].values.reshape(-1,1)
 qt.fit(vec)
 D[col] = qt.transform(vec.reshape(-1,1))
return D
X_train = rankgauss(X_train)
 X_test = rankgauss(X_test )
df_train['Id'].nunique() 
<count_unique_values>
X_train.reset_index(drop=True, inplace=True)
 X_test.reset_index(drop=True, inplace=True )
df_train['groupId'].nunique() 
<count_unique_values>
n_comp = 600
data = pd.concat([X_train[GENES], X_test[GENES]])
 data2 =(PCA(n_components=n_comp, random_state=42 ).fit_transform(data[GENES]))
train2 = data2[:X_train.shape[0]]
test2 = data2[-X_test.shape[0]:]
train2 = pd.DataFrame(train2, columns=[f'pca_G-{i}' for i in range(n_comp)])
 test2 = pd.DataFrame(test2, columns=[f'pca_G-{i}' for i in range(n_comp)])
 X_train = pd.concat(( X_train, train2), axis=1)
 X_test = pd.concat(( X_test, test2), axis=1 )
df_train['matchId'].nunique() 
<filter>
n_comp = 50
data = pd.concat([X_train[CELLS], X_test[CELLS]])
 data2 =(PCA(n_components=n_comp, random_state=42 ).fit_transform(data[CELLS]))
train2 = data2[:X_train.shape[0]]
test2 = data2[-X_test.shape[0]:]
train2 = pd.DataFrame(train2, columns=[f'pca_G-{i}' for i in range(n_comp)])
 test2 = pd.DataFrame(test2, columns=[f'pca_G-{i}' for i in range(n_comp)])
 X_train = pd.concat(( X_train, train2), axis=1)
 X_test = pd.concat(( X_test, test2), axis=1 )
df_train[df_train['groupId']==24]<count_values>
var_thresh = VarianceThreshold(0.8)
 data = X_train.append(X_test)
 data_transformed = var_thresh.fit_transform(data)
 X_train = data_transformed[ : X_train.shape[0]]
X_test = data_transformed[-X_test.shape[0] : ]
show_count_sum(train_, 'assists')
<count_values>
def logloss(y_true, y_pred):
y_pred = tf.clip_by_value(y_pred,p_min,p_max)
 return -backend.mean(y_true*backend.log(y_pred)+(1-y_true)*backend.log(1-y_pred))
show_count_sum(train_, 'boosts' )<count_values>
n_train = X_train.shape[0]
n_test = X_test.shape[0]
n_features = X_train.shape[1]
n_labels = y_train.shape[1]
epsilon = 1E-3
p_min = epsilon
p_max = 1-epsilon
show_count_sum(train_, 'DBNOs' )<count_values>
sgd = tf.keras.optimizers.SGD()
adamw = tfa.optimizers.AdamW(weight_decay = 0.0001)
 adam = tf.keras.optimizers.Adam()
radam = tfa.optimizers.RectifiedAdam()
lookahead_radam = tfa.optimizers.Lookahead(radam)
 lookahead_adamw = tfa.optimizers.Lookahead(adamw)
 def create_model(n_features, opt):
model = Sequential([
layers.Input(n_features),
layers.BatchNormalization() ,
layers.Dropout(0.466),
tfa.layers.WeightNormalization(layers.Dense(1024)) ,
layers.LeakyReLU() ,
layers.Dropout(0.466),
layers.BatchNormalization() ,
tfa.layers.WeightNormalization(layers.Dense(2048)) ,
layers.LeakyReLU() ,
layers.Dropout(0.466),
layers.BatchNormalization() ,
tfa.layers.WeightNormalization(layers.Dense(2048)) ,
layers.LeakyReLU() ,
layers.Dropout(0.466),
layers.BatchNormalization() ,
tfa.layers.WeightNormalization(tf.keras.layers.Dense(n_labels, activation="sigmoid"))
])
 model.compile(optimizer= opt,
loss=losses.BinaryCrossentropy(label_smoothing=1E-6), metrics=logloss)
 return model
show_count_sum(train_, 'headshotKills' )<count_values>
n_folds = 5
seed = 42
test_pred = np.zeros(( n_test, n_labels))
oof_pred = np.zeros(( n_train, n_labels))
mskf = MultilabelStratifiedKFold(n_splits = n_folds, random_state = seed, shuffle = True)
 for n,(tr, te)in enumerate(mskf.split(X_train, y_train)) :
print(f'Starting fold: {n}')
 model = create_model(n_features, 'adam')
 reduce_lr = callbacks.ReduceLROnPlateau(monitor='val_loss', factor=0.3, patience=5, verbose=1, mode='min',min_lr=1E-5)
 early_stopping = callbacks.EarlyStopping(monitor='val_logloss', min_delta=1E-4, patience=10, mode='min',restore_best_weights=True)
 model.fit(
 X_train[tr],
y_train[tr],
validation_data=(X_train[te], y_train[te]),
epochs=80,
batch_size=128,
callbacks = [reduce_lr,early_stopping] 
)
 test_pred += model.predict(X_test)/n_folds
oof_pred[te,:] += model.predict(X_train[te] )
show_count_sum(train_, 'heals' )<count_values>
print(f'OOF log loss: {log_loss(np.ravel(y_train), np.ravel(np.clip(oof_pred, p_min, p_max)))}' )
show_count_sum(train_, 'weaponsAcquired' )<count_values>
print(f'OOF log loss: {log_loss(np.ravel(y_train), np.ravel(oof_pred)) }' )
show_count_sum(train_, 'winPoints' )<count_values>
predictions = np.clip(test_pred.copy() , p_min,p_max)
 for pos in add_index:
predictions = np.insert(predictions, pos, values=np.zeros(206), axis=0 )
show_count_sum(train_, 'revives' )<prepare_x_and_y>
ss = pd.DataFrame(predictions, columns=train_target.columns[1:])
 ss.insert(0,'sig_id', test_set['sig_id'].values)
 ss.to_csv('submission.csv',index = False)
 ss.describe()