Kaggle competition: Tabular Playground Series - Feb 2021
<SOS> metric: RMSE Kaggle data source: tabular-playground-series-feb-2021<randomize_order>
w1 = 0.2
w2 = 0.8
def augment(X, y, times=8):
pos_idx =(y == 1)
 nsample = times * pos_idx.sum()
X_sample = []
for i in range(X.shape[1]):
X_sample.append(np.random.choice(X[pos_idx, i], size=nsample))
X_sample = np.column_stack(X_sample)
 idx = np.arange(X.shape[0] + nsample)
 np.random.shuffle(idx)
 return np.vstack([X, X_sample])[idx], np.hstack([y, np.ones(nsample)])[idx]<prepare_x_and_y>
import numpy as np
import pandas as pd
nfold = 5
kfold = StratifiedKFold(n_splits=nfold, shuffle=True)
 nn_oof_tr, nn_oof_te = np.zeros(tr_zval.shape[0]), 0
lgb_oof_tr, lgb_oof_te = np.zeros(tr_zval.shape[0]), 0
nb_oof_tr, nb_oof_te = np.zeros(tr_zval.shape[0]), 0
for tr_idx, va_idx in kfold.split(tr_zval, target):
X_tr, y_tr = tr_zval[tr_idx], target[tr_idx]
X_va, y_va = tr_zval[va_idx], target[va_idx]
X_tr, y_tr = augment(X_tr, y_tr)
 model = Sequential([
layers.Dense(16, input_shape=(400, 1), activation='relu'),
layers.Flatten() ,
layers.Dense(1, activation='sigmoid')
 ])
 model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['binary_crossentropy'])
 earlystop = callbacks.EarlyStopping(patience=10)
 X_tr_re = X_tr[:, :, np.newaxis]
X_va_re = X_va[:, :, np.newaxis]
model.fit(X_tr_re, y_tr, validation_data=(X_va_re, y_va), epochs=100, verbose=2, batch_size=256, callbacks=[earlystop])
 nn_oof_tr[va_idx] = model.predict(X_va_re ).flatten()
X_te_re = te_zval[:, :, np.newaxis]
nn_oof_te += model.predict(X_te_re ).flatten() / nfold
param = {
'objective': 'binary',
'boost': 'gbdt',
'metric': 'auc',
'learning_rate': 0.01,
'num_leaves': 13,
'max_depth': -1,
'feature_fraction': 0.05,
'bagging_freq': 5,
'bagging_fraction': 0.4,
'min_data_in_leaf': 80,
'min_sum_hessian_in_leaf': 10,
'num_threads': 4
}
trn_data = lgb.Dataset(X_tr, y_tr)
 val_data = lgb.Dataset(X_va, y_va)
 clf = lgb.train(param, trn_data, 100000, valid_sets=(val_data), early_stopping_rounds=600, verbose_eval=600)
 lgb_oof_tr[va_idx] = clf.predict(X_va)
 lgb_oof_te += clf.predict(te_zval)/ nfold
bayes = GaussianNB()
bayes.fit(X_tr, y_tr)
 nb_oof_tr[va_idx] = bayes.predict_proba(X_va)[:, 1]
nb_oof_te += bayes.predict_proba(te_zval)[:, 1] / nfold<train_model>
%%time
df1 = pd.read_csv(".. /input/ensembling-starter-tps-feb-2021/submission.csv")
 df2 = pd.read_csv(".. /input/playground-series-february-21/submission_040.csv")
 blended_df = df1.copy(deep=True)
 blended_df['target'] = w1*df1['target'] + w2*df2['target']
print(blended_df.head() )
<save_to_csv><EOS> 
blended_df.to_csv("blended_df.csv", index=None )