Kaggle competition: University of Liverpool - Ion Switching
<SOS> metric: MacroFScore Kaggle data source: university-of-liverpool-ion-switching<prepare_x_and_y>
warnings.filterwarnings("ignore")
 n_leaves = 32
depth = 6
n_shift = 2
target = 'target'
Id_Cols = ['id' ]
features = [x for x in train.columns if x not in [target]+Id_Cols]
X = train[features]
y = train['target']
X<prepare_x_and_y>
train = pd.read_csv('.. /input/data-without-drift-with-kalman-filter/train.csv')
 test = pd.read_csv('.. /input/data-without-drift-with-kalman-filter/test.csv')
 submission = pd.read_csv('.. /input/liverpool-ion-switching/sample_submission.csv' )
def RunForAll(algo , k , train , test , features , params):
val_set_pred = np.zeros(len(train))
test_set_pred = np.zeros(len(test))
X = train[features]
y = train['target']
kf = KFold(n_splits=k)
 for fold_,(train_index, val_index)in enumerate(kf.split(train , train['target'])) :
print(f'
---------------------Fold {fold_ + 1}-----------------')
 target = train['target']
X_train , y_train = train[features].iloc[train_index] , target.iloc[train_index]
X_val , y_val = train[features].iloc[val_index] , target.iloc[val_index]
X_train = X_train.abs()
X_test = test[features].abs()
_ = algo.fit(X_train , y_train , eval_set = [(X_val , y_val)] , **params)
 prediction_val = algo.predict(X_val)
 kf_score = np.sqrt(mean_squared_error(y_val , prediction_val))
print(f'
Score For Validation Sample is {kf_score}')
 val_set_pred[val_index] = prediction_val
y_val = target.iloc[val_index]
prediction_test = algo.predict(X_test)
 test_set_pred += prediction_test / k
val_score = np.sqrt(mean_squared_error(target,val_set_pred))
print(f'
Score for Validation set is {val_score}')
 return val_set_pred , test_set_pred , target<prepare_x_and_y>
def shift(df, n):
for i in range(1,n+1):
df['signal-' + str(i)] = [0]*i + list(df['signal'].values[:-i])
 df['signal+' + str(i)] = list(df['signal'].values[i:])+ [0]*i
return df
xgb=XGBRegressor(n_estimators = 4000 , learning_rate = 0.011)
 params = {'verbose' : False , 'early_stopping_rounds' : 100}
target = 'target'
Id_Cols = ['id' ]
features = [x for x in train.columns if x not in [target]+Id_Cols]
xgb_val_pred , xgb_test_preds , y_val_xgb = RunForAll(xgb,5, train , test , features , params )<compute_test_metric>
t = train[:500000]
outlier = list(t[(t.signal>-1)&(t.open_channels==0)].index)
 train.at[outlier, 'signal'] = -1.6
t = train[500000:1000000]
outlier = list(t[(t.signal>0)&(t.open_channels==0)].index)
 train.at[outlier, 'signal'] = -1.6
del t
xgb_val_pred , xgb_test_preds , y_val_xgb<choose_model_class>
dic_model = {}
d = {}
for i in range(10):
d['batch{0}'.format(i)] = shift(train[500000*i:500000*(i+1)], n_shift ).drop('time',axis=1)
 df1 = pd.concat([d['batch0'],d['batch1'],d['batch2'],d['batch3'],d['batch5'],d['batch6'],d['batch8']], axis=0 ).sample(frac=1)
 df2 = pd.concat([d['batch4'], d['batch9']], axis=0 )
lgb = LGBMRegressor(n_estimators = 100000, metric = 'rmse' , learning_rate = 0.01 , boosting_type = 'gbdt' ,
num_leaves = 200 , feature_fraction = 0.6 , lambda_l1 = 2 , lambda_l2 = 2 , min_child_samples = 50 , bagging_fraction = 0.4 ,
bagging_freq = 1 , verbosity=-1 , max_depth = 12 , max_bin = 200 ,
objective = 'regression')
 params = {'verbose' : 100 , 'early_stopping_rounds' : 1000 }
target = 'target'
Id_Cols = ['id' ]
features = [x for x in train.columns if x not in [target]+Id_Cols]
lgb_val_pred , lgb_test_preds , y_val_lgb = RunForAll(lgb,5, train , test , features , params )<prepare_x_and_y>
df1 = reduce_mem_usage(normal_distribution_feature(df1, 1))
df2 = reduce_mem_usage(normal_distribution_feature(df2, 2))
cb = CatBoostRegressor(n_estimators = 5000 , learning_rate = 0.005)
 params = {'verbose' : False , 'early_stopping_rounds' : 100}
target = 'target'
Id_Cols = ['id' ]
features = [x for x in train.columns if x not in [target]+Id_Cols]
cb_val_pred , cb_test_preds , y_val_cb = RunForAll(cb,5, train , test , features , params )<save_to_csv>
def fit_lgb(X, y, upper_bound):
lomodel = []
kf = KFold(n_splits=5, random_state=1, shuffle=False)
 params = {'objective': 'huber',
'num_leaves': n_leaves,
'max_depth':depth,
"metric": 'rmse',
'n_jobs': -1,
'random_state': 1
}
lopred=[]
for train_index, test_index in kf.split(X):
X_train, X_valid = X.iloc[train_index], X.iloc[test_index]
y_train, y_valid = y.iloc[train_index], y.iloc[test_index]
d_train = lgb.Dataset(X_train, label=y_train)
 d_valid = lgb.Dataset(X_valid, label=y_valid)
 model = lgb.train(params,
train_set=d_train,
num_boost_round=1000,
valid_sets=d_valid,
verbose_eval='None',
early_stopping_rounds=30,
learning_rates=lambda iter:max(0.1**(iter//10),0.001), 
)
 lomodel.append(model)
 pred = model.predict(X_valid, num_iteration=model.best_iteration)
 pred = np.round(np.clip(pred,0,upper_bound)).astype(int)
 lopred.append(pred)
 print(f1_score(y_valid, pred, average=None))
print('
')
 return lomodel,lopred
dic_model['lomodel1'],lopred1 = fit_lgb(df1.drop(['open_channels'], axis = 1), df1.open_channels, 5)
 dic_model['lomodel2'],lopred2 = fit_lgb(df2.drop(['open_channels'], axis = 1), df2.open_channels, 10 )
test['target'] = cb_test_preds
output = pd.DataFrame({'id' : test.id , 'target' : test.target})
 output.to_csv('submission_cb.csv' , index=False)
 output.head()<prepare_output>
lopred = lopred1+lopred2
pred = np.array([])
 for i in lopred:
pred = np.append(pred, i)
 print('f1 score for each group:',f1_score(np.concatenate(( df1.open_channels,df2.open_channels)) , pred, average=None),
'macro f1 score:',f1_score(np.concatenate(( df1.open_channels,df2.open_channels)) , pred, average='macro'))
<prepare_output><EOS> 
submission['open_channels'] = np.array(np.round(sub,0), np.int)
 submission.to_csv('submission.csv', index=False, float_format='%.4f' )