Kaggle competition: University of Liverpool - Ion Switching
column_train = [i for i in train_df.columns if i not in ['time', 'signal', 'open_channels']]
column_train<train_model>
import numpy as np
import pandas as pd
from catboost import cv, Pool
def custom_asymmetric_train(y_pred, y_true):
y_true = y_true.get_label()
residual =(y_true - y_pred ).astype('float')
 grad = np.where(residual < 0, -2*1.1*residual, -2*residual)
 hess = np.where(residual < 0, 2*1.1, 2)
 return grad, hess
def custom_asymmetric_valid(y_pred, y_true):
y_true = y_true.get_label()
residual =(y_true - y_pred ).astype('float')
 loss = np.where(residual < 0, residual**2, 1.15*(residual**2))
return 'custom_asymmetric_eval', np.mean(loss), False
def eval_gini(y_true, y_prob):
return np.sqrt(((y_prob - y_true)** 2 ).mean() )<split>
train = pd.read_csv('.. /input/data-without-drift/train_clean.csv', dtype={'time':np.float32,'signal':np.float32,'open_channels':np.int32})
 test = pd.read_csv('.. /input/data-without-drift/test_clean.csv', dtype={'time':np.float32,'signal':np.float32})
 sub = pd.read_csv('.. /input/liverpool-ion-switching/sample_submission.csv', dtype={'time':np.float32,'open_channels':np.int32})
 Y_train_proba = np.load(".. /input/ion-shifted-rfc-proba/Y_train_proba.npy")
 Y_test_proba = np.load(".. /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]
train['proba'] = np.argmax(Y_train_proba,axis=1)
 test['proba'] = np.argmax(Y_test_proba,axis=1 )
def model_train(model_type, params, train_df=train_df, test_df=test_df):
predict = np.zeros([len(test_df)])
 importances = np.zeros([len(column_train), FOLD_GET])
 for idx,(train_index, val_index)in enumerate(kf.split(train_df, train_df['open_channels'])) :
if idx % 2 == 0:
print(f'Fold_{idx}:')
 train_ = train_df.iloc[train_index]
val_ = train_df.iloc[val_index]
train_.reset_index(inplace=True, drop=True)
 val_.reset_index(inplace=True, drop=True)
 if model_type=='xgb':
train_dataset = lgb.DMatrix(train_.loc[:, column_train].values, label=train_.loc[:, 'open_channels'].values, feature_name=column_train)
 val_dataset = lgb.DMatrix(val_.loc[:, column_train].values, label=val_.loc[:, 'open_channels'].values, feature_name=column_train)
 clf = xgb.train(params, train_dataset, 500, valid_sets=[(val_dataset, 'valid')],
verbose_eval=100, early_stopping_rounds = 100)
 predict += clf.predict(test_df.loc[:, column_train], ntree_limit=clf.best_ntree_limit)/FOLD_GET
lgb.plot_importance(clf, max_num_features=10)
 elif model_type=='lgb':
train_dataset = lgb.Dataset(train_.loc[:, column_train].values, label=train_.loc[:, 'open_channels'].values, feature_name=column_train)
 val_dataset = lgb.Dataset(val_.loc[:, column_train].values, label=val_.loc[:, 'open_channels'].values, feature_name=column_train)
 clf = lgb.train(params, train_dataset, 4000, valid_sets=[train_dataset, val_dataset], feval=custom_asymmetric_valid,
fobj=custom_asymmetric_train, verbose_eval=500, early_stopping_rounds = 400)
 predict += clf.predict(test_df.loc[:, column_train], ntree_limit=clf.best_iteration)/FOLD_GET
del clf
gc.collect()
return predict, importances<split>
train.loc[train.index % 2 ==0,'id'] = 1
train.loc[train.index % 2 !=0,'id'] = 2
x_train = train[train.id ==1].drop(['open_channels','id'],axis=1)
 y_train = train[train.id ==1]['open_channels']
x_val = train[train.id ==2].drop(['open_channels','id'],axis=1)
 y_val = train[train.id ==2]['open_channels']
print(x_train.shape)
 print(y_train.shape)
 print(x_val.shape)
 print(y_val.shape )
train_index, val_index = list(kf.split(train_df, train_df['open_channels'])) [0]
train_ = train_df.iloc[train_index]
val_ = train_df.iloc[val_index]
train_.reset_index(inplace=True, drop=True)
 val_.reset_index(inplace=True, drop=True)
 gc.collect()<concatenate>
cat = CatBoostClassifier(task_type='GPU',iterations=10000,loss_function='MultiClass',od_type='Iter',od_wait=500)
 cat.fit(x_train,y_train,verbose=25, eval_set =(x_val,y_val),cat_features=[13])
%%time
def lgb_bayesian(
 num_leaves,
min_data_in_leaf,
learning_rate,
min_sum_hessian_in_leaf,
feature_fraction,
lambda_l1,
lambda_l2,
min_gain_to_split,
max_depth,
scale_pos_weight):
num_leaves = int(num_leaves)
 min_data_in_leaf = int(min_data_in_leaf)
 max_depth = int(max_depth)
 scale_pos_weight = int(scale_pos_weight)
 assert type(num_leaves)== int
assert type(min_data_in_leaf)== int
assert type(max_depth)== int
assert type(scale_pos_weight)== int
param = {
'num_leaves': num_leaves,
'max_bin': 63,
'min_data_in_leaf': min_data_in_leaf,
'learning_rate': learning_rate,
'min_sum_hessian_in_leaf': min_sum_hessian_in_leaf,
'bagging_fraction': 1.0,
'bagging_freq': 5,
'feature_fraction': feature_fraction,
'lambda_l1': lambda_l1,
'lambda_l2': lambda_l2,
'min_gain_to_split': min_gain_to_split,
'max_depth': max_depth,
'scale_pos_weight': scale_pos_weight,
'save_binary': True,
'objective': 'regression',
'boosting_type': 'gbdt',
'verbose': 1,
'metric': 'mae',
'boost_from_average': False,
}
lgb_train = lgb.Dataset(train_.loc[:, column_train].values, label=train_.loc[:, 'open_channels'].values)
 lgb_valid = lgb.Dataset(val_.loc[:, column_train].values, label=val_.loc[:, 'open_channels'].values)
 clf = lgb.train(param, lgb_train, 1000, valid_sets=[lgb_valid],feval=custom_asymmetric_valid,
fobj=custom_asymmetric_train, verbose_eval=250, early_stopping_rounds = 400)
 predictions = clf.predict(val_.loc[:, column_train].values, num_iteration=clf.best_iteration)
 score = eval_gini(val_.loc[:, "open_channels"].values, predictions)
 return 1-score
bounds_lgb = {
'num_leaves':(5, 20),
'min_data_in_leaf':(5, 20),
'learning_rate':(0.01, 0.2),
'min_sum_hessian_in_leaf':(0.00001, 0.01),
'feature_fraction':(0.05, 0.5),
'lambda_l1':(0, 5.0),
'lambda_l2':(0, 5.0),
'min_gain_to_split':(0, 1.0),
'max_depth':(3,15),
'scale_pos_weight':(1, 10)
 }
lgb_bo = BayesianOptimization(lgb_bayesian, bounds_lgb, random_state=37)
 print(lgb_bo.space.keys)
 lgb_bo.maximize(init_points=5, n_iter=5, acq='ucb', xi=0.0, alpha=1e-6)
 lgb_bo.max['params']<init_hyperparams>
y_preds = cat.predict(test)
 sub['open_channels'] = y_preds
sub.to_csv('submission_cat.csv',index=False,float_format='%.4f' )