Kaggle competition: University of Liverpool - Ion Switching
data = pd.read_csv('.. /input/data-without-drift/train_clean.csv')
 data.head()<string_transform>
%matplotlib inline
df = divide_and_add_features(data['signal'])
 df.head()<split>
os.environ['PYTHONHASHSEED'] = '0'
np.random.seed(7)
 rn.seed(7 )
x_train, x_test, y_train, y_test = train_test_split(df.values, data['open_channels'].values, test_size=0.2)
 del data, df
print('x_train.shape=', x_train.shape)
 print('x_test.shape=', x_test.shape)
 print('y_train.shape=', y_train.shape)
 print('y_test.shape=', y_test.shape )<choose_model_class>
MLP_EPOCH_NUM = 30
LGBM_BOOST_NUM = 3000
FOLD_NUM = 4
def create_mpl(shape):

 X_input = L.Input(shape)
 X = L.Dense(150, activation='relu' )(X_input)
 X = L.Dense(150, activation='relu' )(X)
 X = L.Dense(125, activation='relu' )(X)
 X = L.Dense(100, activation='relu' )(X)
 X = L.Dense(75, activation='relu' )(X)
 X = L.Dense(50, activation='relu' )(X)
 X = L.Dense(25, activation='relu' )(X)
 X = L.Dense(11, activation='softmax' )(X)
 model = Model(inputs=X_input, outputs=X)
 return model
mlp = create_mpl(x_train[0].shape)
 mlp.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['sparse_categorical_accuracy'])
 print(mlp.summary() )<train_model>
data = pd.read_csv('.. /input/data-without-drift/train_clean.csv')
 data.iloc[478587:478588, [1]] = -2
data.iloc[478609:478610, [1]] = -2
data_ = data[3500000:3642922].append(data[3822754:4000000])
 data = data[:3500000].append(data[4000000:] ).reset_index().append(data_, ignore_index=True)
 data.head()
data[["signal", "open_channels"]].plot(figsize=(19,5), alpha=0.7 )
mlp.fit(x=x_train, y=y_train, epochs=30, batch_size=1024, class_weight=class_weight )<predict_on_test>
df = divide_and_add_features(data['signal'])
 df.head()
mlp_pred = mlp.predict(x_test)
 print('mlp_pred.shape=', mlp_pred.shape )<create_dataframe>
m_data = pd.read_csv('.. /input/viterbi/Viterbi_train.csv', names=["gb", "signal"])
 m_data_ = m_data[3500000:3642922].append(m_data[3822754:4000000])
 m_data = m_data[:3500000].append(m_data[4000000:] ).reset_index().append(m_data_, ignore_index=True)
 m_data.head()
m_data[["signal"]].plot(figsize=(19,5), alpha=0.7)
 df["m_signal"] = m_data["signal"][:-1].values/15
df = reduce_mem_usage(df)
 df_columns = df.columns
dataset = lgb.Dataset(x_train, label=y_train)
 params = {'boosting_type': 'gbdt',
'metric': 'rmse',
'objective': 'regression',
'n_jobs': -1,
'seed': 236,
'num_leaves': 280,
'learning_rate': 0.026623466966581126,
'max_depth': 73,
'lambda_l1': 2.959759088169741,
'lambda_l2': 1.331172832164913,
'bagging_fraction': 0.9655406551472153,
'bagging_freq': 9,
'colsample_bytree': 0.6867118652742716}<train_model>
def create_mpl(shape):

 X_input = L.Input(shape)
 X = L.Dense(150, activation='relu' )(X_input)
 X = L.Dense(125, activation='relu' )(X)
 X = L.Dense(75, activation='relu' )(X)
 X = L.Dense(50, activation='relu' )(X)
 X = L.Dense(50, activation='relu' )(X)
 X = L.Dense(25, activation='relu' )(X)
 X = L.Dense(11, activation='softmax' )(X)
 model = Model(inputs=X_input, outputs=X)
 return model
print('Training LGBM...')
 gbc = lgb.train(params, dataset,
num_boost_round=10000,
verbose_eval=100)
 print('LGBM trained!' )<predict_on_test>
def lgb_Metric(preds, dtrain):
labels = dtrain.get_label()
num_labels = 11
preds = preds.reshape(num_labels, len(preds)//num_labels)
 preds = np.argmax(preds, axis=0)
 score = f1_score(labels, preds, average="macro")
 return('KaggleMetric', score, True)
 lgb_r_params = {
'objective': 'multiclass',
'num_class': 11,
'metric': 'multi_logloss',
'learning_rate': 0.00987173774816051,
'lambda_l1': 0.00031963798315506463,
'lambda_l2': 0.18977456778807847,
'num_leaves': 171,
'feature_fraction': 0.58733782457345,
'bagging_fraction': 0.7057826081907392,
'bagging_freq': 4}
gbc_pred = gbc.predict(x_test)
 print('gbc_pred.shape=', gbc_pred.shape )<categorify>
test_data = pd.read_csv('.. /input/data-without-drift/test_clean.csv')
 test_df = divide_and_add_features(test_data['signal'])
 test_df = reduce_mem_usage(test_df)
 m_data = pd.read_csv('.. /input/viterbi/Viterbi_test.csv' ).rename(columns={"open_channels": "signal"})
 m_data_ = m_data[3500000:3642922].append(m_data[3822754:4000000])
 m_data = m_data[:3500000].append(m_data[4000000:] ).reset_index().append(m_data_, ignore_index=True)
 m_data.head()
m_data[["signal"]].plot(figsize=(19,5), alpha=0.7)
 test_df["m_signal"] = m_data["signal"].values/15
test_df = reduce_mem_usage(test_df)
 test_df.shape
def convert_to_one_hot(pred):

 pred = np.clip(pred, 0, 10)
 pred = 1 - np.abs(pred.reshape(( -1,1)) - np.arange(11))
pred = np.clip(pred, 0, 1)
 return pred
gbc_pred = convert_to_one_hot(gbc_pred)
 print('gbc_pred.shape=', gbc_pred.shape )<find_best_params>
kf = KFold(n_splits=FOLD_NUM, shuffle=True, random_state=42)
 preds = np.zeros(2000000*11 ).reshape(( 2000000, 11))
X = df
y = data['open_channels']
y_values = data['open_channels'].values
for i,(tdx, vdx)in enumerate(kf.split(X, y)) :
print(f'Fold : {i}')
 X_train, X_valid, y_train, y_valid = X.iloc[tdx], X.iloc[vdx], y_values[tdx], y_values[vdx]
print(f"sep: {X_train.shape}, {X_valid.shape}, {y_train.shape}, {y_valid.shape}")
 mlp = create_mpl(X_train.values[0].shape)
 mlp.compile(optimizer="adam", loss='sparse_categorical_crossentropy', metrics=['sparse_categorical_accuracy'])
 class_weight = get_class_weight(y_train)
 mlp.fit(x=X_train.values, y=y_train, epochs=MLP_EPOCH_NUM, batch_size=1024, class_weight=class_weight,
validation_data=(X_valid.values, y_valid), verbose=0)
 mlp_pred = mlp.predict(X_valid.values)
 f1_mlp = f1_score(y_valid, np.argmax(mlp_pred, axis=-1), average='macro')
 print(f"f1 score is :{f1_mlp}")
 plt.figure(1)
 plt.plot(mlp.history.history['loss'], 'b', label='loss')
 plt.plot(mlp.history.history['val_loss'], 'r', label='loss')
 plt.xlabel('epochs')
 plt.legend()
plt.figure(2)
 plt.plot(mlp.history.history['sparse_categorical_accuracy'], 'g', label='sparse_categorical_accuracy')
 plt.plot(mlp.history.history['val_sparse_categorical_accuracy'], 'r', label='val_sparse_categorical_accuracy')
 plt.xlabel('epochs')
 plt.legend()
plt.show()
lgb_dataset = lgb.Dataset(X_train.values, label=y_train, weight=class_weight[y_train])
 lgb_valid_dataset = lgb.Dataset(X_valid.values, label=y_valid, weight=class_weight[y_valid])
 print('Training LGBM...')
 gbc = lgb.train(lgb_r_params, lgb_dataset, LGBM_BOOST_NUM, valid_names=["train", "valid"],
valid_sets=[lgb_dataset, lgb_valid_dataset], verbose_eval=-1,
feval=lgb_Metric, early_stopping_rounds=10)
 print('LGBM trained!')
 gbc_pred = gbc.predict(X_valid.values, num_iteration=gbc.best_iteration)
 attr2 = {k: v for k, v in zip(df_columns, gbc.feature_importance())if 200 > v and v>0}
print("weak fe
print(attr2)
 print("
print(f1_score(y_valid, np.argmax(gbc_pred, axis=1), average='macro'))
f1s = []
alphas = []
for alpha in tqdm(np.linspace(0,1,101)) :
y_pred = alpha*mlp_pred +(1 - alpha)*gbc_pred
f1 = f1_score(y_valid, np.argmax(y_pred, axis=1), average='macro')
 f1s.append(f1)
 alphas.append(alpha)
 f1s = np.array(f1s)
 alphas = np.array(alphas)
 best_alpha = alphas[np.argmax(f1s)]
print('best_f1=', f1s.max())
 print('best_alpha=', best_alpha)
 plt.plot(alphas, f1s)
 plt.title('f1_score for ensemble')
 plt.xlabel('alpha')
 plt.ylabel('f1_score')
 plt.show()
mlp_pred = mlp.predict(test_df)
 gbc_pred = gbc.predict(test_df, num_iteration=gbc.best_iteration)
 pred = best_alpha*mlp_pred +(1 - best_alpha)*gbc_pred
preds += pred
print(f"f1_mlp is {f1_mlp}")
 gc.collect()
f1s = []
alphas = np.linspace(0,1,101)
 for alpha in tqdm(alphas):
y_pred = alpha*mlp_pred +(1 - alpha)*gbc_pred
f1 = f1_score(y_test, np.argmax(y_pred, axis=-1), average='macro')
 f1s.append(f1)
 f1s = np.array(f1s)
 best_alpha = alphas[np.argmax(f1s)]
print('best_f1=', f1s.max())
 print('best_alpha=', best_alpha )<compute_test_metric>
pred = np.argmax(preds, axis=1)
 print('Writing submission...')
 submission = pd.DataFrame()
submission['time'] = test_data['time']
submission['open_channels'] = pred
submission.to_csv('mlp_gbc_submission.csv', index=False, float_format='%.4f')
 print('Submission finished!' )