Kaggle competition: University of Liverpool - Ion Switching
<SOS> metric: MacroFScore Kaggle data source: university-of-liverpool-ion-switching<define_search_space>
!pip install tensorflow-addons
param_range = np.linspace(0.1, 1, 10)
 param_range<define_variables>
sns.set()
for dirname, _, filenames in os.walk('/kaggle/input'):
for filename in filenames:
print(os.path.join(dirname, filename))
param_name = 'colsample_bytree'<define_variables>
train = pd.read_csv('/kaggle/input/data-without-drift/train_clean.csv')
 test = pd.read_csv('/kaggle/input/data-without-drift/test_clean.csv')
 NUM_CLASSES = train.open_channels.nunique()
SEQ_LENGTH = 1000
WIDTHS = np.power(2, np.arange(-4, 9), dtype=np.float32)
 train.shape, test.shape
param_name = 'subsample'<define_search_space>
def wavelet_transform(sig, wd):
widths = wd
wt = signal.cwt(sig.values, signal.ricker, widths)
 wt = wt.T
eps = np.max(wt)* 1e-2
s1 = np.log(np.abs(wt)+ eps)- np.log(eps)
 wt = s1 / np.max(s1)
 return wt
param_name = 'n_estimators'<define_search_space>
def features(df):
df = df.sort_values(by=['time'] ).reset_index(drop=True)
 df['batch'] = np.floor(df.index / 50_000 ).astype(int)
 df['batch_index'] = df.index -(df.batch * 50_000)
 df['batch_slices'] = df['batch_index'] // 5_000
df['batch_slices2'] = df.batch.astype(str ).str.zfill(3 ).astype(str ).str.cat(df.batch_slices.astype(str ).str.zfill(3 ).astype(str), sep='_')
 df.reset_index(drop=True, inplace=True)
 transforms = df.groupby('batch' ).signal.apply(wavelet_transform, wd=WIDTHS)
 transforms = np.concatenate(transforms)
 assert transforms.shape[0] == df.shape[0]
wt_dict = {}
for i in range(len(WIDTHS)) :
wt_dict['wt_'+str(i)] = transforms[:, i]
del transforms
wt_dict = pd.DataFrame(wt_dict)
 df = pd.concat([df, wt_dict], axis=1, sort=False)
 for c in ['batch_slices2']:
d = {}
d['mean_'+c] = df.groupby([c])['signal'].mean()
d['median_'+c] = df.groupby([c])['signal'].median()
d['max_'+c] = df.groupby([c])['signal'].max()
d['min_'+c] = df.groupby([c])['signal'].min()
d['std_'+c] = df.groupby([c])['signal'].std()
d['mean_abs_chg_'+c] = df.groupby([c])['signal'].apply(lambda x: np.mean(np.abs(np.diff(x))))
d['abs_max_'+c] = df.groupby([c])['signal'].apply(lambda x: np.max(np.abs(x)))
 d['abs_min_'+c] = df.groupby([c])['signal'].apply(lambda x: np.min(np.abs(x)))
 for v in d:
df[v] = df[c].map(d[v].to_dict())
 df['range_'+c] = df['max_'+c] - df['min_'+c]
df['maxtomin_'+c] = df['max_'+c] / df['min_'+c]
df['abs_avg_'+c] =(df['abs_min_'+c] + df['abs_max_'+c])/ 2
df['signal_shift_+1'] = [0,] + list(df['signal'].values[:-1])
 df['signal_shift_-1'] = list(df['signal'].values[1:])+ [0]
df['signal_shift_+2'] = [0, 0,] + list(df['signal'].values[:-2])
 df['signal_shift_-2'] = list(df['signal'].values[2:])+ [0, 0]
for i in df[df['batch_index']==0].index:
df.loc[i, 'signal_shift_+1'] = -99
for i in df[df['batch_index']==49999].index:
df.loc[i, 'signal_shift_-1'] = -99
for i in df[(df['batch_index']==0)|(df['batch_index']==1)].index:
df.loc[i, 'signal_shift_+2'] = -99
for i in df[(df['batch_index']==49999)|(df['batch_index']==49998)].index:
df.loc[i, 'signal_shift_-2'] = -99
return df
train = features(train)
 test = features(test )
param_range = [100, 200, 300, 400, 500, 600, 700, 800, 900, 1000]<prepare_x_and_y>
cols = [
'signal',
'mean_batch_slices2',
'median_batch_slices2',
'max_batch_slices2',
'min_batch_slices2',
'std_batch_slices2',
'mean_abs_chg_batch_slices2',
'abs_max_batch_slices2',
'abs_min_batch_slices2',
'range_batch_slices2',
'maxtomin_batch_slices2',
'abs_avg_batch_slices2',
'signal_shift_+1',
'signal_shift_-1',
'signal_shift_+2',
'signal_shift_-2',
]
cols.extend(['wt_'+str(i)for i in range(len(WIDTHS)) ])
 print(len(cols))
X = train[features]
y = train['target']<split>
def macro_f1(y_true, y_pred):
y_true = tf.reshape(y_true, [-1, NUM_CLASSES])
 y_pred = tf.reshape(y_pred, [-1, NUM_CLASSES])
 threshold = tf.reduce_max(y_pred, axis=-1, keepdims=True)
 y_pred = tf.logical_and(y_pred >= threshold,
tf.abs(y_pred)> 1e-12)
 y_true = tf.cast(y_true, tf.bool)
 y_pred = tf.cast(y_pred, tf.bool)
 tp = tf.logical_and(tf.equal(y_true, True), tf.equal(y_pred, True))
tp = tf.cast(tp, tf.float32)
 tp = tf.reduce_sum(tp, axis=0)
 fp = tf.logical_and(tf.equal(y_true, False), tf.equal(y_pred, True))
fp = tf.cast(fp, tf.float32)
 fp = tf.reduce_sum(fp, axis=0)
 fn = tf.logical_and(tf.equal(y_true, True), tf.equal(y_pred, False))
fn = tf.cast(fn, tf.float32)
 fn = tf.reduce_sum(fn, axis=0)
 precision = tf.math.divide_no_nan(tp, tf.math.add(tp, fp))
recall = tf.math.divide_no_nan(tp, tf.math.add(tp, fn))
f1 = tf.math.divide_no_nan(tf.math.multiply_no_nan(2.0, tf.math.multiply_no_nan(precision, recall)) , tf.math.add(precision, recall))
f1 = tf.reduce_mean(f1)
 return f1
def objective(trial,data=X,target=y):
train_x, test_x, train_y, test_y = train_test_split(data, target, test_size=0.15,random_state=42)
 param = {
'tree_method':'gpu_hist',
'lambda': trial.suggest_loguniform('lambda', 1e-3, 1),
'alpha': trial.suggest_loguniform('alpha', 1e-3, 1),
'colsample_bytree': trial.suggest_categorical('colsample_bytree', [0.1, 0.2, 0.3,0.5,0.7,0.9]),
'subsample': trial.suggest_categorical('subsample', [0.1, 0.2,0.3,0.4,0.5,0.8,1.0]),
'learning_rate': trial.suggest_categorical('learning_rate', [0.0008, 0.01, 0.015, 0.02,0.03, 0.05,0.08,0.1]),
'n_estimators': 4000,
'max_depth': trial.suggest_categorical('max_depth', [5,7,9,11,13,15,17,20,23,25]),
'random_state': 48,
'min_child_weight': trial.suggest_int('min_child_weight', 1, 400),
}
model = xgb.XGBRegressor(**param)
 model.fit(train_x,train_y,eval_set=[(test_x,test_y)],early_stopping_rounds=100,verbose=False)
 preds = model.predict(test_x)
 rmse = mean_squared_error(test_y, preds,squared=False)
 return rmse<train_model>
def gated_residual_block(nb_filters, use_bias, i, x):
res_x = layers.Conv1D(nb_filters, 1, strides=1, padding='same', use_bias=use_bias )(x)
 tanh_out = layers.Conv1D(nb_filters, 2, dilation_rate=2**i, padding='same',
use_bias=use_bias, activation='tanh' )(x)
 sigm_out = layers.Conv1D(nb_filters, 2, dilation_rate=2**i, padding='same',
use_bias=use_bias, activation='sigmoid' )(x)
 gated_out = layers.Multiply()([tanh_out, sigm_out])
 gated_out = layers.Conv1D(nb_filters, 1, strides=1, padding='same', use_bias=use_bias )(gated_out)
 res_x = layers.Add()([res_x, gated_out])
 return res_x
study = optuna.create_study(direction='minimize')
 study.optimize(objective, n_trials=50)
 print('Number of finished trials:', len(study.trials))
print('Best trial:', study.best_trial.params )<find_best_params>
def compute_receptive_field(dilation_depth, nb_stacks):
receptive_field = nb_stacks *(2**dilation_depth * 2)-(nb_stacks - 1)
 return receptive_field
compute_receptive_field(9, 1 )
study.best_trial.params<init_hyperparams>
best_model, best_r, best_score = wavenet(9, 1, 128, 0)
 print(f'Macro F1: {best_score}' )
<prepare_x_and_y><EOS> 
preds = best_model.predict(test[cols].values.reshape(-1,SEQ_LENGTH,len(cols)))
 preds = preds.reshape(-1, 11)
 preds = np.argmax(preds, axis=1)
 test['open_channels'] = preds
test[['time', 'open_channels']].to_csv('submission.csv', index=False, float_format='%.4f' )