Kaggle competition: University of Liverpool - Ion Switching
base_param = {
'num_leaves': int(lgb_bo.max['params']['num_leaves']),
'max_bin': 63,
'min_data_in_leaf': int(lgb_bo.max['params']['min_data_in_leaf']),
'learning_rate': lgb_bo.max['params']['learning_rate'],
'min_sum_hessian_in_leaf': lgb_bo.max['params']['min_sum_hessian_in_leaf'],
'bagging_fraction': 1.0,
'bagging_freq': 5,
'feature_fraction': lgb_bo.max['params']['feature_fraction'],
'lambda_l1': lgb_bo.max['params']['lambda_l1'],
'lambda_l2': lgb_bo.max['params']['lambda_l2'],
'min_gain_to_split': lgb_bo.max['params']['min_gain_to_split'],
'max_depth': int(lgb_bo.max['params']['max_depth']),
'scale_pos_weight': int(lgb_bo.max['params']['scale_pos_weight']),
'save_binary': True,
'objective': 'regression',
'boosting_type': 'gbdt',
'verbose': 1,
'metric': 'mae',
'boost_from_average': False,
}<train_model>
EPOCHS = 180
NNBATCHSIZE = 16
GROUP_BATCH_SIZE = 4000
SEED = 321
LR = 0.0015
SPLITS = 6
def seed_everything(seed):
random.seed(seed)
 np.random.seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 tf.random.set_seed(seed )
%%time
pred_lgb, importances_lgb = model_train('lgb', base_param )<set_options>
def read_data() :
train = pd.read_csv('/kaggle/input/data-without-drift/train_clean.csv', dtype={'time': np.float32, 'signal': np.float32, 'open_channels':np.int32})
 test = pd.read_csv('/kaggle/input/data-without-drift/test_clean.csv', dtype={'time': np.float32, 'signal': np.float32})
 sub = pd.read_csv('/kaggle/input/liverpool-ion-switching/sample_submission.csv', dtype={'time': np.float32})
 Y_train_proba = np.load("/kaggle/input/ion-shifted-rfc-proba/Y_train_proba.npy")
 Y_test_proba = np.load("/kaggle/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]
return train, test, sub
def batching(df, batch_size):
df['group'] = df.groupby(df.index//batch_size, sort=False)['signal'].agg(['ngroup'] ).values
df['group'] = df['group'].astype(np.uint16)
 return df
def normalize(train, test):
train_input_mean = train.signal.mean()
train_input_sigma = train.signal.std()
train['signal'] =(train.signal - train_input_mean)/ train_input_sigma
test['signal'] =(test.signal - train_input_mean)/ train_input_sigma
return train, test
def lag_with_pct_change(df, windows):
for window in windows:
df['signal_shift_pos_' + str(window)] = df.groupby('group')['signal'].shift(window ).fillna(0)
 df['signal_shift_neg_' + str(window)] = df.groupby('group')['signal'].shift(-1 * window ).fillna(0)
 return df
def run_feat_engineering(df, batch_size):
df = batching(df, batch_size = batch_size)
 df = lag_with_pct_change(df, [1, 2, 3])
 df['signal_2'] = df['signal'] ** 2
return df
def feature_selection(train, test):
features = [col for col in train.columns if col not in ['index', 'group', 'open_channels', 'time']]
train = train.replace([np.inf, -np.inf], np.nan)
 test = test.replace([np.inf, -np.inf], np.nan)
 for feature in features:
feature_mean = pd.concat([train[feature], test[feature]], axis = 0 ).mean()
train[feature] = train[feature].fillna(feature_mean)
 test[feature] = test[feature].fillna(feature_mean)
 return train, test, features
del train_df, test_df
gc.collect()<categorify>
train, test, sample_submission = read_data()
train, test = normalize(train, test )
def pred_proc(pred):
pred = np.round(np.clip(pred, 0, 10))
return pred.astype(int)
 pred = pred_proc(pred_lgb)
 set(pred )<save_to_csv>
train = run_feat_engineering(train, batch_size = GROUP_BATCH_SIZE)
 test = run_feat_engineering(test, batch_size = GROUP_BATCH_SIZE)
 train, test, features = feature_selection(train, test )
submission = pd.read_csv(sub_path)
 submission['open_channels'] = pred
submission.to_csv("submission.csv", index=False, float_format='%.4f')
 submission.tail()<import_modules>
train['signal_rolling_mean_1h'] = train['signal'].rolling(window = 100 ).mean().fillna(0)
 test['signal_rolling_mean_1h'] = test['signal'].rolling(window = 100 ).mean().fillna(0)
 train['signal_rolling_std_1h'] = train['signal'].rolling(window = 100 ).std().fillna(0)
 test['signal_rolling_std_1h'] = test['signal'].rolling(window = 100 ).std().fillna(0)
 train['signal_rolling_mean_1t'] = train['signal'].rolling(window = 1000 ).mean().fillna(0)
 test['signal_rolling_mean_1t'] = test['signal'].rolling(window = 1000 ).mean().fillna(0)
 train['signal_rolling_std_1t'] = train['signal'].rolling(window = 1000 ).std().fillna(0)
 test['signal_rolling_std_1t'] = test['signal'].rolling(window = 1000 ).std().fillna(0 )
import numpy as np
import pandas as pd
import os 
<load_from_csv>
from sklearn.model_selection import train_test_split
from sklearn.metrics import f1_score
import optuna.integration.lightgbm as lgb
import optuna, os, uuid, pickle
train = pd.read_csv('/kaggle/input/liverpool-ion-switching/train.csv')
 test = pd.read_csv('/kaggle/input/liverpool-ion-switching/test.csv' )<set_options>
X = train.drop(['time', 'open_channels'], axis = 1)
 y = train['open_channels']
X_test = test.drop(['time'], axis = 1)
 X_train, X_val, y_train, y_val = train_test_split(X, y)
 lgb_train = lgb.Dataset(X_train, y_train)
 lgb_eval = lgb.Dataset(X_val, y_val, reference=lgb_train)
 params = {
"objective": "multiclass",
"num_class": 11,
"metric": "multi_logloss",
"verbosity": -1,
"boosting_type": "gbdt",
'num_boost_round': 3,
}
best_params, tuning_history = dict() , list()
model = lgb.train(
 params,
lgb_train,
valid_sets=[lgb_train, lgb_eval],
verbose_eval=100,
early_stopping_rounds=100,
best_params=best_params,
tuning_history=tuning_history, 
)
 print("Best Params:", best_params)
 print("Tuning history:", tuning_history )
warnings.simplefilter('ignore')
 warnings.filterwarnings('ignore')
 pd.set_option('display.max_columns', 1000)
 pd.set_option('display.max_rows', 500)
<define_variables>
best_params = model.params
best_params
EPOCHS = 150
NNBATCHSIZE = 32
GROUP_BATCH_SIZE = 4000
SEED = 123
LR = 0.001
SPLITS = 5
outdir = 'wavenet_models'
flip = False
noise = False
if not os.path.exists(outdir):
os.makedirs(outdir)
 def seed_everything(seed):
random.seed(seed)
 np.random.seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 tf.random.set_seed(seed )<load_from_csv>
model = lgb.train(best_params, lgb_train, valid_sets=lgb_eval, num_boost_round=3)
 y_pred = model.predict(X_test, num_iteration=model.best_iteration)
 y_pred_max = np.argmax(y_pred, axis=1 )
def read_data() :
train = pd.read_csv('/kaggle/input/clean-kalman/train_clean_kalman.csv', dtype={'time': np.float32, 'signal': np.float32, 'open_channels':np.int32})
 test = pd.read_csv('/kaggle/input/clean-kalman/test_clean_kalman.csv', dtype={'time': np.float32, 'signal': np.float32})
 Y_train_proba = np.load("/kaggle/input/ion-shifted-rfc-proba/Y_train_proba.npy")
 Y_test_proba = np.load("/kaggle/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]
sub = pd.read_csv('/kaggle/input/liverpool-ion-switching/sample_submission.csv', dtype={'time': np.float32})
 return train, test, sub
def batching(df, batch_size):
df['group'] = df.groupby(df.index//batch_size, sort=False)['signal'].agg(['ngroup'] ).values
df['group'] = df['group'].astype(np.uint16)
 return df
def normalize(train, test):
train_input_mean = train.signal.mean()
train_input_sigma = train.signal.std()
train['signal'] =(train.signal - train_input_mean)/ train_input_sigma
test['signal'] =(test.signal - train_input_mean)/ train_input_sigma
return train, test
def lag_with_pct_change(df, windows):
for window in windows:
df['signal_shift_pos_' + str(window)] = df.groupby('group')['signal'].shift(window ).fillna(0)
 df['signal_shift_neg_' + str(window)] = df.groupby('group')['signal'].shift(-1 * window ).fillna(0)
 return df
def run_feat_engineering(df, batch_size):
df = batching(df, batch_size = batch_size)
 df = lag_with_pct_change(df, [1, 2, 3])
 df['signal_2'] = df['signal'] ** 2
return df
def feature_selection(train, test):
features = [col for col in train.columns if col not in ['index', 'group', 'open_channels', 'time']]
train = train.replace([np.inf, -np.inf], np.nan)
 test = test.replace([np.inf, -np.inf], np.nan)
 for feature in features:
feature_mean = pd.concat([train[feature], test[feature]], axis = 0 ).mean()
train[feature] = train[feature].fillna(feature_mean)
 test[feature] = test[feature].fillna(feature_mean)
 return train, test, features
def split(GROUP_BATCH_SIZE=4000, SPLITS=5):
print('Reading Data Started...')
 train, test, sample_submission = read_data()
train, test = normalize(train, test)
 print('Reading and Normalizing Data Completed')
 print('Creating Features')
 print('Feature Engineering Started...')
 train = run_feat_engineering(train, batch_size=GROUP_BATCH_SIZE)
 test = run_feat_engineering(test, batch_size=GROUP_BATCH_SIZE)
 train, test, features = feature_selection(train, test)
 print(train.head())
 print('Feature Engineering Completed...')
 target = ['open_channels']
group = train['group']
kf = GroupKFold(n_splits=SPLITS)
 splits = [x for x in kf.split(train, train[target], group)]
new_splits = []
for sp in splits:
new_split = []
new_split.append(np.unique(group[sp[0]]))
new_split.append(np.unique(group[sp[1]]))
new_split.append(sp[1])
 new_splits.append(new_split)
 target_cols = ['open_channels']
print(train.head() , train.shape)
 train_tr = np.array(list(train.groupby('group' ).apply(lambda x: x[target_cols].values)) ).astype(np.float32)
 train = np.array(list(train.groupby('group' ).apply(lambda x: x[features].values)))
 test = np.array(list(test.groupby('group' ).apply(lambda x: x[features].values)))
 print(train.shape, test.shape, train_tr.shape)
 return train, test, train_tr, new_splits<normalization>
sample_submission['open_channels'] = y_pred_max
sample_submission.to_csv('submission_wavenet_lgbm.csv', index=False, float_format='%.4f' )