Kaggle competition: University of Liverpool - Ion Switching
num_col = [col for col in train.columns if train[col].dtypes != object]
unique = [len(train[col].unique())for col in num_col]
num_unique = dict(zip(num_col,unique))
print('There are {} Numerical columns
'.format(len(num_col)))
 print('Numerical unique values: {}'.format(num_unique))<count_unique_values>
def read_data() :
print('loading and preparing the data')
 train1 = pd.read_csv('/kaggle/input/best-filter-and-featureengineering/final_train1.csv')
 train1 = reduce_mem_usage(train1)
 train2 = pd.read_csv('/kaggle/input/best-filter-and-featureengineering/final_train2.csv')
 train2 = reduce_mem_usage(train2)
 train3 = pd.read_csv('/kaggle/input/best-filter-and-featureengineering/final_train3.csv')
 train3 = reduce_mem_usage(train3)
 train = pd.concat([train1, train2, train3], axis = 1)
 del train1, train2, train3
gc.collect()
print('train data loaded')
 test1 = pd.read_csv('/kaggle/input/best-filter-and-featureengineering/final_test1.csv')
 test1 = reduce_mem_usage(test1)
 test2 = pd.read_csv('/kaggle/input/best-filter-and-featureengineering/final_test2.csv')
 test2 = reduce_mem_usage(test2)
 test3 = pd.read_csv('/kaggle/input/best-filter-and-featureengineering/final_test3.csv')
 test3 = reduce_mem_usage(test3)
 test = pd.concat([test1, test2, test3], axis = 1)
 del test1, test2, test3
gc.collect()
print('test data loaded')
 submission = pd.read_csv('/kaggle/input/liverpool-ion-switching/sample_submission.csv', dtype={'time':str})
 return train, test, submission
cat_col = [col for col in train.columns if train[col].dtypes == object]
unique = [len(train[col].unique())for col in cat_col]
cat_unique = dict(zip(cat_col,unique))
print('There are {} Categorical columns
'.format(len(cat_col)))
 print('Categorical unique values: {}'.format(cat_unique))<feature_engineering>
def run_lgb(train, test, y_train, params):
kf = StratifiedKFold(n_splits = 5, shuffle = True, random_state = 42)
 oof_pred = np.zeros(len(train))
y_pred = np.zeros(len(test))
for fold,(tr_ind, val_ind)in enumerate(kf.split(train, y_train)) :
x_train, x_val = train.iloc[tr_ind], train.iloc[val_ind]
y_trainlgb, y_val = y_train[tr_ind], y_train[val_ind]
train_set = lgb.Dataset(x_train, y_trainlgb)
 val_set = lgb.Dataset(x_val, y_val)
 model = lgb.train(params, train_set, num_boost_round = 10000, early_stopping_rounds = 50,
valid_sets = [train_set, val_set], verbose_eval = 100)
 oof_pred[val_ind] = model.predict(x_val)
 y_pred += model.predict(test)/ kf.n_splits
rmse_score = np.sqrt(metrics.mean_squared_error(y_train, oof_pred))
oof_pred = np.round(np.clip(oof_pred, 0, 10)).astype(int)
 round_y_pred = np.round(np.clip(y_pred, 0, 10)).astype(int)
 f1 = metrics.f1_score(y_train, oof_pred, average = 'macro')
 print(f'Our oof rmse score is {rmse_score}')
 print(f'Our oof macro f1 score is {f1}')
 return round_y_pred
all_data['Family_Group'] = all_data.SibSp + all_data.Parch<feature_engineering>
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}
round_y_pred = run_lgb(train, test, y_train, params)
 submission['open_channels'] = round_y_pred
submission.to_csv('submission.csv', index = False )