Kaggle competition: University of Liverpool - Ion Switching
submit["open_channels"] = submit["open_channels"].astype(int)
 submit.to_csv('submission_voting.csv', index=False, float_format='%.4f' )<install_modules>
EPOCHS = 300
NNBATCHSIZE = 8
GROUP_BATCH_SIZE = 2048
SEED = 42
LR = 0.001
SPLITS = 5
def seed_everything(seed):
random.seed(seed)
 np.random.seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 tf.random.set_seed(seed )
!pip install --no-warn-conflicts -q tensorflow-addons<import_modules>
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
Add, AveragePooling1D, Multiply, GRU, GRUCell, LSTMCell, SimpleRNNCell, SimpleRNN, TimeDistributed, RNN,
RepeatVector, Conv1D, MaxPooling1D, Concatenate, GlobalAveragePooling1D, UpSampling1D)
 warnings.simplefilter('ignore')
 warnings.filterwarnings('ignore')
 pd.set_option('display.max_columns', 1000)
 pd.set_option('display.max_rows', 500)
 %matplotlib inline<define_variables>
def cbr(x, out_layer, kernel, stride, dilation):
x = Conv1D(out_layer, kernel_size=kernel, dilation_rate=dilation, strides=stride, padding="same" )(x)
 x = BatchNormalization()(x)
 x = Activation("relu" )(x)
 return x
def se_block(x_in, layer_n):
x = GlobalAveragePooling1D()(x_in)
 x = Dense(layer_n//8, activation="relu" )(x)
 x = Dense(layer_n, activation="sigmoid" )(x)
 x_out=Multiply()([x_in, x])
 return x_out
def resblock(x_in, layer_n, kernel, dilation, use_se=True):
x = cbr(x_in, layer_n, kernel, 1, dilation)
 x = cbr(x, layer_n, kernel, 1, dilation)
 if use_se:
x = se_block(x, layer_n)
 x = Add()([x_in, x])
 return x
EPOCHS=120
NNBATCHSIZE=16
BATCHSIZE = 50000
SEED = 321
SELECT = True
SPLITS = 5
LR = 0.001
fe_config = [ 
(True, 4000),
]<define_variables>
def BUSUnet(input_shape=(None,1)) :
layer_n = 64
kernel_size = 7
depth = 2
input_layer = Input(input_shape)
 input_layer_1 = AveragePooling1D(5 )(input_layer)
 input_layer_2 = AveragePooling1D(25 )(input_layer)
 x = cbr(input_layer, layer_n, kernel_size, 1, 1)
 for i in range(depth):
x = resblock(x, layer_n, kernel_size, 1)
 out_0 = x
x = cbr(x, layer_n*2, kernel_size, 5, 1)
 for i in range(depth):
x = resblock(x, layer_n*2, kernel_size, 1)
 out_1 = x
x = Concatenate()([x, input_layer_1])
 x = cbr(x, layer_n*3, kernel_size, 5, 1)
 for i in range(depth):
x = resblock(x, layer_n*3, kernel_size, 1)
 out_2 = x
x = Concatenate()([x, input_layer_2])
 x = cbr(x, layer_n*4, kernel_size, 5, 1)
 for i in range(depth):
x = resblock(x, layer_n*4, kernel_size, 1)
 x = UpSampling1D(5 )(x)
 x = Concatenate()([x, out_2])
 x = cbr(x, layer_n*3, kernel_size, 1, 1)
 x = UpSampling1D(5 )(x)
 x = Concatenate()([x, out_1])
 x = cbr(x, layer_n*2, kernel_size, 1, 1)
 x = UpSampling1D(5 )(x)
 x = Concatenate()([x, out_0])
 x = cbr(x, layer_n, kernel_size, 1, 1)
 for i in range(depth):
x = resblock(x, layer_n, kernel_size, 1)
 out_0 = x
x = cbr(x, layer_n*2, kernel_size, 5, 1)
 for i in range(depth):
x = resblock(x, layer_n*2, kernel_size, 1)
 out_1 = x
x = Concatenate()([x, input_layer_1])
 x = cbr(x, layer_n*3, kernel_size, 5, 1)
 for i in range(depth):
x = resblock(x, layer_n*3, kernel_size, 1)
 x = UpSampling1D(5 )(x)
 x = Concatenate()([x, out_1])
 x = cbr(x, layer_n*2, kernel_size, 1, 1)
 x = UpSampling1D(5 )(x)
 x = Concatenate()([x, out_0])
 x = cbr(x, layer_n, kernel_size, 1, 1)
 x = Conv1D(11, kernel_size=kernel_size, strides=1, padding="same" )(x)
 out = Activation("softmax" )(x)
 model = models.Model(inputs = input_layer, outputs = out)
 opt = Adam(lr = LR)
 opt = tfa.optimizers.SWA(opt)
 model.compile(loss = losses.CategoricalCrossentropy() , optimizer = opt, metrics = ['accuracy'])
 return model
def seed_everything(seed : int)-> NoReturn :
rn.seed(seed)
 np.random.seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 tf.random.set_seed(seed)
 seed_everything(SEED)
<load_from_csv>
def run_everything() :
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('Feature Engineering Completed...')
 print(f'Training BUSU-Net model with {SPLITS} folds of GroupKFold Started...')
 run_cv_model_by_batch(train, test, SPLITS, 'group', features, sample_submission, EPOCHS, NNBATCHSIZE)
 print('Training completed...')
 run_everything()