Kaggle competition: University of Liverpool - Ion Switching
def read_data(base : os.path.abspath)-> Tuple[pd.DataFrame, pd.DataFrame, pd.DataFrame]:
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(os.path.join(base + '/sample_submission.csv'), dtype={'time': np.float32})
 return train, test, sub 
<create_dataframe>
%matplotlib inline
def batching(df : pd.DataFrame,
batch_size : int)-> pd.DataFrame :
df['group'] = df.groupby(df.index//batch_size, sort=False)['signal'].agg(['ngroup'] ).values
df['group'] = df['group'].astype(np.uint16)
 return df 
<create_dataframe>
data = pd.read_csv('.. /input/clean-2apply-kalman/train_clean_2apply_kalman.csv')
 data.head()
def lag_with_pct_change(df : pd.DataFrame,
shift_sizes : Optional[List]=[1, 2],
add_pct_change : Optional[bool]=False,
add_pct_change_lag : Optional[bool]=False)-> pd.DataFrame:
for shift_size in shift_sizes:
df['signal_shift_pos_'+str(shift_size)] = df.groupby('group')['signal'].shift(shift_size ).fillna(0)
 df['signal_shift_neg_'+str(shift_size)] = df.groupby('group')['signal'].shift(-1*shift_size ).fillna(0)
 if add_pct_change:
df['pct_change'] = df['signal'].pct_change()
if add_pct_change_lag:
for shift_size in shift_sizes:
df['pct_change_shift_pos_'+str(shift_size)] = df.groupby('group')['pct_change'].shift(shift_size ).fillna(0)
 df['pct_change_shift_neg_'+str(shift_size)] = df.groupby('group')['pct_change'].shift(-1*shift_size ).fillna(0)
 return df 
<create_dataframe>
def run_feat_enginnering(df : pd.DataFrame,
create_all_data_feats : bool,
batch_size : int)-> pd.DataFrame:
df = batching(df, batch_size=batch_size)
 if create_all_data_feats:
df = lag_with_pct_change(df, [1, 2, 3], add_pct_change=False, add_pct_change_lag=False)
 return df 
<feature_engineering>
def feature_selection(df : pd.DataFrame,
df_test : pd.DataFrame)-> Tuple[pd.DataFrame , pd.DataFrame, List]:
use_cols = [col for col in df.columns if col not in ['index','group', 'open_channels', 'time']]
df = df.replace([np.inf, -np.inf], np.nan)
 df_test = df_test.replace([np.inf, -np.inf], np.nan)
 for col in use_cols:
col_mean = pd.concat([df[col], df_test[col]], axis=0 ).mean()
df[col] = df[col].fillna(col_mean)
 df_test[col] = df_test[col].fillna(col_mean)
 gc.collect()
return df, df_test, use_cols 
<create_dataframe>
df = divide_and_add_features(data['signal'])
 df.head()
def run_cv_model_by_batch(train : pd.DataFrame,
test : pd.DataFrame,
splits : int,
batch_col : Text,
feats : List,
sample_submission: pd.DataFrame,
nn_epochs : int,
nn_batch_size : int)-> NoReturn:
seed_everything(SEED)
 K.clear_session()
config = tf.compat.v1.ConfigProto(intra_op_parallelism_threads=1,inter_op_parallelism_threads=1)
 sess = tf.compat.v1.Session(graph=tf.compat.v1.get_default_graph() , config=config)
 tf.compat.v1.keras.backend.set_session(sess)
 oof_ = np.zeros(( len(train), 11))
preds_ = np.zeros(( len(test), 11))
target = ['open_channels']
group = train['group']
kf = GroupKFold(n_splits=5)
 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)
 tr = pd.concat([pd.get_dummies(train.open_channels), train[['group']]], axis=1)
 tr.columns = ['target_'+str(i)for i in range(11)] + ['group']
target_cols = ['target_'+str(i)for i in range(11)]
train_tr = np.array(list(tr.groupby('group' ).apply(lambda x: x[target_cols].values)) ).astype(np.float32)
 train = np.array(list(train.groupby('group' ).apply(lambda x: x[feats].values)))
 test = np.array(list(test.groupby('group' ).apply(lambda x: x[feats].values)))
 for n_fold,(tr_idx, val_idx, val_orig_idx)in enumerate(new_splits[0:], start=0):
train_x, train_y = train[tr_idx], train_tr[tr_idx]
valid_x, valid_y = train[val_idx], train_tr[val_idx]
gc.collect()
shape_ =(None, train_x.shape[2])
 model = Classifier(shape_)
 cb_schedule = ReduceLROnPlateau(monitor='val_loss', factor=0.5, patience=1, verbose=0, mode='auto', min_delta=0.0001, cooldown=0, min_lr=0)
 cb_Early_Stop=EarlyStopping(monitor='val_loss',patience=10)
 cb_clr = CyclicLR(base_lr=1e-7, max_lr = 1e-4, step_size= int(1.0*(test.shape[0])/(nn_batch_size*4)) , mode='exp_range', gamma=1.0, scale_fn=None, scale_mode='cycle')
 cb_prg = tfa.callbacks.TQDMProgressBar(leave_epoch_progress=False,leave_overall_progress=False, show_epoch_progress=False,show_overall_progress=True)
 cb_lr_schedule = LearningRateScheduler(lr_schedule)
 model.fit(train_x,train_y,
epochs=nn_epochs,
callbacks=[cb_prg, cb_lr_schedule, MacroF1(model, valid_x,valid_y)],
batch_size=nn_batch_size,verbose=0,
validation_data=(valid_x,valid_y))
preds_f = model.predict(valid_x)
 f1_score_ = f1_score(np.argmax(valid_y, axis=2 ).reshape(-1), np.argmax(preds_f, axis=2 ).reshape(-1), average = 'macro')
 logger.info(f'Training fold {n_fold + 1} completed.macro f1 score : {f1_score_ :1.5f}')
 preds_f = preds_f.reshape(-1, preds_f.shape[-1])
 oof_[val_orig_idx,:] += preds_f
te_preds = model.predict(test)
 te_preds = te_preds.reshape(-1, te_preds.shape[-1])
 preds_ += te_preds / SPLITS
f1_score_ =f1_score(np.argmax(train_tr, axis=2 ).reshape(-1), np.argmax(oof_, axis=1), average = 'macro')
 logger.info(f'Training completed.oof macro f1 score : {f1_score_:1.5f}')
 sample_submission['open_channels'] = np.argmax(preds_, axis=1 ).astype(int)
 sample_submission.to_csv('submission.csv', index=False, float_format='%.4f')
 display(sample_submission.head())
 np.save('oof.npy', oof_)
 np.save('preds.npy', preds_)
 return 
<choose_model_class>
x_train, x_test, y_train, y_test = train_test_split(df.values, data['open_channels'].values, test_size=0.2)
 del data, df
print('x_train.shape=', x_train.shape)
 print('x_test.shape=', x_test.shape)
 print('y_train.shape=', y_train.shape)
 print('y_test.shape=', y_test.shape )
class CyclicLR(tf.keras.callbacks.Callback):
def __init__(self, base_lr=0.001, max_lr=0.006, step_size=2000., mode='triangular',
gamma=1., scale_fn=None, scale_mode='cycle'):
super(CyclicLR, self ).__init__()
self.base_lr = base_lr
self.max_lr = max_lr
self.step_size = step_size
self.mode = mode
self.gamma = gamma
if scale_fn == None:
if self.mode == 'triangular':
self.scale_fn = lambda x: 1.
 self.scale_mode = 'cycle'
elif self.mode == 'triangular2':
self.scale_fn = lambda x: 1 /(2.**(x - 1))
self.scale_mode = 'cycle'
elif self.mode == 'exp_range':
self.scale_fn = lambda x: gamma **(x)
 self.scale_mode = 'iterations'
else:
self.scale_fn = scale_fn
self.scale_mode = scale_mode
self.clr_iterations = 0.
 self.trn_iterations = 0.
 self.history = {}
self._reset()
def _reset(self, new_base_lr=None, new_max_lr=None,
new_step_size=None):

 if new_base_lr != None:
self.base_lr = new_base_lr
if new_max_lr != None:
self.max_lr = new_max_lr
if new_step_size != None:
self.step_size = new_step_size
self.clr_iterations = 0.
 def clr(self):
cycle = np.floor(1 + self.clr_iterations /(2 * self.step_size))
x = np.abs(self.clr_iterations / self.step_size - 2 * cycle + 1)
 if self.scale_mode == 'cycle':
return self.base_lr +(self.max_lr - self.base_lr)* np.maximum(0,(1 - x)) * self.scale_fn(cycle)
 else:
return self.base_lr +(self.max_lr - self.base_lr)* np.maximum(0,(1 - x)) * self.scale_fn(
 self.clr_iterations)
 def on_train_begin(self, logs={}):
logs = logs or {}
if self.clr_iterations == 0:
K.set_value(self.model.optimizer.lr, self.base_lr)
 else:
K.set_value(self.model.optimizer.lr, self.clr())
 def on_batch_end(self, epoch, logs=None):
logs = logs or {}
self.trn_iterations += 1
self.clr_iterations += 1
K.set_value(self.model.optimizer.lr, self.clr())
<compute_train_metric>
def create_mpl(shape):

 X_input = L.Input(shape)
 X = L.Dense(150, activation='relu' )(X_input)
 X = L.Dense(150, activation='relu' )(X)
 X = L.Dense(125, activation='relu' )(X)
 X = L.Dense(100, activation='relu' )(X)
 X = L.Dense(75, activation='relu' )(X)
 X = L.Dense(50, activation='relu' )(X)
 X = L.Dense(25, activation='relu' )(X)
 X = L.Dense(11, activation='softmax' )(X)
 model = Model(inputs=X_input, outputs=X)
 return model
mlp = create_mpl(x_train[0].shape)
 mlp.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['sparse_categorical_accuracy'])
 print(mlp.summary() )
def categorical_focal_loss(gamma=2.0, alpha=0.25):

 def focal_loss(y_true, y_pred):
epsilon = K.epsilon()
y_pred = K.clip(y_pred, epsilon, 1.0-epsilon)
 cross_entropy = -y_true*K.log(y_pred)
 weight = alpha * y_true * K.pow(( 1-y_pred), gamma)
 loss = weight * cross_entropy
loss = K.sum(loss, axis=1)
 return loss
return focal_loss 
<set_options>
mlp.fit(x=x_train, y=y_train, epochs=30, batch_size=1024, class_weight=class_weight )
class Attention(Layer):

 def __init__(self, hidden_size,
num_heads = 4,
attention_dropout=.1,
trainable=True,
name='Attention'):
if hidden_size % num_heads != 0:
raise ValueError("Hidden size must be evenly divisible by the number of heads.")
 self.hidden_size = hidden_size
self.num_heads = num_heads
self.trainable = trainable
self.attention_dropout = attention_dropout
self.dense = tf.keras.layers.Dense(self.hidden_size, use_bias=False)
 super(Attention, self ).__init__(name=name)
 def split_heads(self, x):

 with tf.name_scope("split_heads"):
batch_size = tf.shape(x)[0]
length = tf.shape(x)[1]
depth =(self.hidden_size // self.num_heads)
 x = tf.reshape(x, [batch_size, length, self.num_heads, depth])
 return tf.transpose(x, [0, 2, 1, 3])
 def combine_heads(self, x):

 with tf.name_scope("combine_heads"):
batch_size = tf.shape(x)[0]
length = tf.shape(x)[2]
x = tf.transpose(x, [0, 2, 1, 3])
 return tf.reshape(x, [batch_size, length, self.hidden_size])
 def call(self, inputs):

 q = self.dense(inputs)
 k = self.dense(inputs)
 v = self.dense(inputs)
 q = self.split_heads(q)
 k = self.split_heads(k)
 v = self.split_heads(v)
 depth =(self.hidden_size // self.num_heads)
 q *= depth ** -0.5
logits = tf.matmul(q, k, transpose_b=True)
 weights = tf.nn.softmax(logits, name="attention_weights")
 if self.trainable:
weights = tf.nn.dropout(weights, 1.0 - self.attention_dropout)
 attention_output = tf.matmul(weights, v)
 attention_output = self.combine_heads(attention_output)
 attention_output = self.dense(attention_output)
 return attention_output
def compute_output_shape(self, input_shape):
return tf.TensorShape(input_shape )<categorify>
mlp_pred = mlp.predict(x_test)
 print('mlp_pred.shape=', mlp_pred.shape )
def Classifier(shape_):
def wave_block(x,filters,kernel_size,n):
dilation_rates = [2**i for i in range(n)]
x = Conv1D(filters=filters,
kernel_size=1,
padding='same' )(x)
 res_x = x
for dilation_rate in dilation_rates:
tanh_out = Conv1D(filters=filters,
kernel_size=kernel_size,
padding='same',
activation = 'tanh',
dilation_rate=dilation_rate )(x)
 sigm_out = Conv1D(filters=filters,
kernel_size=kernel_size,
padding='same',
activation = 'sigmoid',
dilation_rate=dilation_rate )(x)
 x = Multiply()([tanh_out,sigm_out])
 x = Conv1D(filters = filters,
kernel_size = 1,
padding='same' )(x)
 res_x = Add()([res_x,x])
 return res_x
inp = Input(shape=(shape_))
x = wave_block(inp,16,3,12)
 x = wave_block(x, 32,3,8)
 x = wave_block(x, 64,3,4)
 x = wave_block(x, 128,3,1)
 out = Dense(11, activation='softmax', name='out' )(x)
 model = models.Model(inputs=inp, outputs=out)
 opt = Adam(lr=LR)
 opt = tfa.optimizers.SWA(opt)
 model.compile(loss=losses.CategoricalCrossentropy() , optimizer=opt, metrics=['accuracy'])
 return model<predict_on_test>
!nvidia-smi
class MacroF1(Callback):
def __init__(self, model, inputs, targets):
self.model = model
self.inputs = inputs
self.targets = np.argmax(targets, axis=2 ).reshape(-1)
 def on_epoch_end(self, epoch, logs):
pred = np.argmax(self.model.predict(self.inputs), axis=2 ).reshape(-1)
 score = f1_score(self.targets, pred, average="macro")
 print(f' F1Macro: {score:.5f}')
<normalization>
dataset = lgb.Dataset(x_train, label=y_train)
 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,
}
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<choose_model_class>
gc.collect()
def Kalman1D(observations,damping=1):
observation_covariance = damping
initial_value_guess = observations[0]
transition_matrix = 1
transition_covariance = 0.1
initial_value_guess
kf = KalmanFilter(
 initial_state_mean=initial_value_guess,
initial_state_covariance=observation_covariance,
observation_covariance=observation_covariance,
transition_covariance=transition_covariance,
transition_matrices=transition_matrix 
)
 pred_state, state_cov = kf.smooth(observations)
 return pred_state<feature_engineering>
print('Training LGBM...')
 gbc = lgb.train(params, dataset,
num_boost_round=10000,
verbose_eval=100)
 print('LGBM trained!' )
def run_everything(fe_config : List)-> NoReturn:
not_feats_cols = ['time']
target_col = ['open_channels']
init_logger()
with timer(f'Reading Data'):
logger.info('Reading Data Started...')
 base = os.path.abspath('/kaggle/input/liverpool-ion-switching/')
 train, test, sample_submission = read_data(base)
 observation_covariance =.0015
train['signal'] = Kalman1D(train.signal.values,observation_covariance)
 test['signal'] = Kalman1D(test.signal.values,observation_covariance)
 train, test = normalize(train, test)
 logger.info('Reading and Normalizing Data Completed...')
 with timer(f'Creating Features'):
logger.info('Feature Enginnering Started...')
 for config in fe_config:
train = run_feat_enginnering(train, create_all_data_feats=config[0], batch_size=config[1])
 test = run_feat_enginnering(test, create_all_data_feats=config[0], batch_size=config[1])
 train, test, feats = feature_selection(train, test)
 logger.info('Feature Enginnering Completed...')
 with timer(f'Running Wavenet model'):
logger.info(f'Training Wavenet model with {SPLITS} folds of GroupKFold Started...')
 run_cv_model_by_batch(train, test, splits=SPLITS, batch_col='group', feats=feats, sample_submission=sample_submission, nn_epochs=EPOCHS, nn_batch_size=NNBATCHSIZE)
 logger.info(f'Training completed...')
<load_from_csv>
gbc_pred = gbc.predict(x_test)
 print('gbc_pred.shape=', gbc_pred.shape )
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<split>
def convert_to_one_hot(pred):

 pred = np.clip(pred, 0, 10)
 pred = 1 - np.abs(pred.reshape(( -1,1)) - np.arange(11))
pred = np.clip(pred, 0, 1)
 return pred
gbc_pred = convert_to_one_hot(gbc_pred)
 print('gbc_pred.shape=', gbc_pred.shape )
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<init_hyperparams>
f1s = []
alphas = np.linspace(0,1,101)
 for alpha in tqdm(alphas):
y_pred = alpha*mlp_pred +(1 - alpha)*gbc_pred
f1 = f1_score(y_test, np.argmax(y_pred, axis=-1), average='macro')
 f1s.append(f1)
 f1s = np.array(f1s)
 best_alpha = alphas[np.argmax(f1s)]
print('best_f1=', f1s.max())
 print('best_alpha=', best_alpha )
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 )<load_from_csv>
def plot_cm(y_true, y_pred, title):
figsize=(16,16)
 y_pred = y_pred.astype(int)
 cm = confusion_matrix(y_true, y_pred, labels=np.unique(y_true))
cm_sum = np.sum(cm, axis=1, keepdims=True)
 cm_perc = cm / cm_sum.astype(float)* 100
annot = np.empty_like(cm ).astype(str)
 nrows, ncols = cm.shape
for i in range(nrows):
for j in range(ncols):
c = cm[i, j]
p = cm_perc[i, j]
if i == j:
s = cm_sum[i]
annot[i, j] = '%.1f%%
%d/%d' %(p, c, s)
 elif c == 0:
annot[i, j] = ''
else:
annot[i, j] = '%.1f%%
%d' %(p, c)
 cm = pd.DataFrame(cm, index=np.unique(y_true), columns=np.unique(y_true))
cm.index.name = 'Actual'
cm.columns.name = 'Predicted'
fig, ax = plt.subplots(figsize=figsize)
 plt.title(title)
 sns.heatmap(cm, cmap='viridis', annot=annot, fmt='', ax=ax )
def read_data() :
print('Reading training, testing and submission data...')
 train = pd.read_csv('/kaggle/input/data-without-drift/train_clean.csv')
 test = pd.read_csv('/kaggle/input/data-without-drift/test_clean.csv')
 submission = pd.read_csv('/kaggle/input/liverpool-ion-switching/sample_submission.csv', dtype={'time':str})
 print('Train set has {} rows and {} columns'.format(train.shape[0], train.shape[1]))
print('Test set has {} rows and {} columns'.format(test.shape[0], test.shape[1]))
return train, test, submission
def get_batch(train, test):
batch = 50
total_batches = 14
train['set'] = 'train'
test['set'] = 'test'
data = pd.concat([train, test])
 for i in range(int(total_batches)) :
data.loc[(data['time'] > i * batch)&(data['time'] <=(i + 1)* batch), 'batch'] = i + 1
train = data[data['set'] == 'train']
test = data[data['set'] == 'test']
train.drop(['set'], inplace = True, axis = 1)
 test.drop(['set'], inplace = True, axis = 1)
 del data
return train, test
def reduce_mem_usage(df, verbose=True):
numerics = ['int16', 'int32', 'int64', 'float16', 'float32', 'float64']
start_mem = df.memory_usage().sum() / 1024**2
for col in df.columns:
if col!='open_channels':
col_type = df[col].dtypes
if col_type in numerics:
c_min = df[col].min()
c_max = df[col].max()
if str(col_type)[:3] == 'int':
if c_min > np.iinfo(np.int8 ).min and c_max < np.iinfo(np.int8 ).max:
df[col] = df[col].astype(np.int8)
 elif c_min > np.iinfo(np.int16 ).min and c_max < np.iinfo(np.int16 ).max:
df[col] = df[col].astype(np.int16)
 elif c_min > np.iinfo(np.int32 ).min and c_max < np.iinfo(np.int32 ).max:
df[col] = df[col].astype(np.int32)
 elif c_min > np.iinfo(np.int64 ).min and c_max < np.iinfo(np.int64 ).max:
df[col] = df[col].astype(np.int64)
 else:
if c_min > np.finfo(np.float16 ).min and c_max < np.finfo(np.float16 ).max:
df[col] = df[col].astype(np.float16)
 elif c_min > np.finfo(np.float32 ).min and c_max < np.finfo(np.float32 ).max:
df[col] = df[col].astype(np.float32)
 else:
df[col] = df[col].astype(np.float64)
 end_mem = df.memory_usage().sum() / 1024**2
if verbose: print('Mem.usage decreased to {:5.2f} Mb({:.1f}% reduction)'.format(end_mem, 100 *(start_mem - end_mem)/ start_mem))
return df<categorify>
def submit_result(mlp, gbc, alpha):
print('Reading data...')
 data = pd.read_csv('.. /input/clean-2apply-kalman/test_clean_2apply_kalman.csv')
 print('Feature engineering...')
 df = divide_and_add_features(data['signal'])
 print('Predicting MLP...')
 mlp_pred = mlp.predict(df.values)
 print('Predicting GBC...')
 gbc_pred = gbc.predict(df.values)
 gbc_pred = convert_to_one_hot(gbc_pred)
 print('Ensembling...')
 y_pred = alpha*mlp_pred +(1 - alpha)*gbc_pred
y_pred = np.argmax(y_pred, axis=-1)
 print('Writing submission...')
 submission = pd.DataFrame()
submission['time'] = data['time']
submission['open_channels'] = y_pred
submission.to_csv('submission.csv', index=False, float_format='%.4f')
 print('Submission finished!' )
<prepare_x_and_y><EOS> 
submit_result(mlp, gbc, best_alpha )