Kaggle competition: University of Liverpool - Ion Switching
test = CustomImageItemList.from_csv_custom(path=path, csv_name='test.csv', imgIdx=1)
 data =(CustomImageItemList.from_csv_custom(path=path, csv_name='train.csv')
.split_by_rand_pct(0.2)
.label_from_df(cols='label')
.add_test(test, label=0)
.transform(get_transforms(do_flip = False, max_rotate = 0.,max_zoom = 1.) , size=49)
.databunch(bs=256, num_workers=16)
.normalize(mnist_stats))
data<define_variables>
train = pd.read_csv('.. /input/data-without-drift/train_clean.csv')
 test = pd.read_csv('.. /input/data-without-drift/test_clean.csv')
 train.head()
data.show_batch(rows=3, figsize=(12,9))<choose_model_class>
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
arch = models.resnet50
arch<choose_model_class>
batch_size = 500000
num_batches = 10
res = 1000
fs = 10000
nyq = 0.5 * fs
<choose_model_class>
observation_covariance =.0015
signal_kalman_train = Kalman1D(train.signal.values,observation_covariance)
 kalman = pd.DataFrame(signal_kalman_train,columns=['signal'] )
learn = cnn_learner(data, arch,pretrained = False, loss_func = nn.MultiMarginLoss() , metrics=[error_rate,accuracy], model_dir='.. /kaggle/working' )<train_model>
f1_unfiltered = []
f1_unfiltered_avg = []
f1_filtered = []
f1_filtered_avg = []
for batch in range(1,10,1):
p_trans = calc_markov_p_trans(train.open_channels[batch_size*(batch-1):batch_size*batch])
 p_signal_unfiltered, signal_bins = calc_markov_p_signal(train.open_channels[batch_size*(batch-1):batch_size*batch], train.signal[batch_size*(batch-1):batch_size*batch])
 signal_dig = digitize_signal(train.signal[batch_size*(batch-1):batch_size*batch], signal_bins)
 p_in = np.ones(len(p_trans)) / len(p_trans)
 viterbi_state = viterbi(p_trans, p_signal_unfiltered, p_in, signal_dig)
 print(f'Batch = {batch}')
 f1_unfiltered = f1_score(y_pred=viterbi_state, y_true=train.open_channels[batch_size*(batch-1):batch_size*batch], average='macro')
 print("Unfiltered - F1 macro =", f1_unfiltered)
 f1_unfiltered_avg += f1_unfiltered
p_trans = calc_markov_p_trans(train.open_channels[batch_size*(batch-1):batch_size*batch])
 p_signal_filtered, signal_bins = calc_markov_p_signal(train.open_channels[batch_size*(batch-1):batch_size*batch], kalman.signal[batch_size*(batch-1):batch_size*batch])
 signal_dig = digitize_signal(kalman.signal[batch_size*(batch-1):batch_size*batch], signal_bins)
 p_in = np.ones(len(p_trans)) / len(p_trans)
 viterbi_state = viterbi(p_trans, p_signal_filtered, p_in, signal_dig)
 f1_filtered = f1_score(y_pred=viterbi_state, y_true=train.open_channels[batch_size*(batch-1):batch_size*batch], average='macro')
 print("Kalman Filtered - F1 macro =", f1_filtered)
 f1_filtered_avg += f1_filtered
learn.fit_one_cycle(20)
 learn.save('stage-1-50' )<find_best_params>
train['signal_undrifted'] = signal_kalman_train
signal_kalman_test = Kalman1D(test.signal.values,observation_covariance)
 test['signal_undrifted'] = signal_kalman_test
interp = ClassificationInterpretation.from_learner(learn)
 losses,idxs = interp.top_losses()
len(data.valid_ds)==len(losses)==len(idxs )<load_from_csv>
def features(df):
df = df.sort_values(by=['time'] ).reset_index(drop=True)
 df.index =(( df.time * 10_000)- 1 ).values
df['batch'] = df.index // 50_000
df['batch_index'] = df.index -(df.batch * 50_000)
 df['batch_slices'] = df['batch_index'] // 5_000
df['batch_slices2'] = df.apply(lambda r: '_'.join([str(r['batch'] ).zfill(3), str(r['batch_slices'] ).zfill(3)]), axis=1)
 for c in ['batch','batch_slices2']:
d = {}
d['mean'+c] = df.groupby([c])['signal_undrifted'].mean()
d['median'+c] = df.groupby([c])['signal_undrifted'].median()
d['max'+c] = df.groupby([c])['signal_undrifted'].max()
d['min'+c] = df.groupby([c])['signal_undrifted'].min()
d['std'+c] = df.groupby([c])['signal_undrifted'].std()
d['mean_abs_chg'+c] = df.groupby([c])['signal_undrifted'].apply(lambda x: np.mean(np.abs(np.diff(x))))
d['abs_max'+c] = df.groupby([c])['signal_undrifted'].apply(lambda x: np.max(np.abs(x)))
 d['abs_min'+c] = df.groupby([c])['signal_undrifted'].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_undrifted'].values[:-1])
 df['signal_shift_-1'] = list(df['signal_undrifted'].values[1:])+ [0]
for i in df[df['batch_index']==0].index:
df['signal_shift_+1'][i] = np.nan
for i in df[df['batch_index']==49999].index:
df['signal_shift_-1'][i] = np.nan
df['signal_shift_+2'] = [0,] + [1,] + list(df['signal_undrifted'].values[:-2])
 df['signal_shift_-2'] = list(df['signal_undrifted'].values[2:])+ [0] + [1]
for i in df[df['batch_index']==0].index:
df['signal_shift_+2'][i] = np.nan
for i in df[df['batch_index']==1].index:
df['signal_shift_+2'][i] = np.nan
for i in df[df['batch_index']==49999].index:
df['signal_shift_-2'][i] = np.nan
for i in df[df['batch_index']==49998].index:
df['signal_shift_-2'][i] = np.nan
for c in [c1 for c1 in df.columns if c1 not in ['time', 'signal_undrifted', 'open_channels', 'batch', 'batch_index', 'batch_slices', 'batch_slices2']]:
df[c+'_msignal'] = df[c] - df['signal_undrifted']
return df
train = features(train)
 test = features(test )
tmp_df = pd.read_csv(path+'sample_submission.csv')
 tmp_df.head()<predict_on_test>
def f1_score_calc(y_true, y_pred):
return f1_score(y_true, y_pred, average="macro")
 def lgb_Metric(preds, dtrain):
labels = dtrain.get_label()
preds = np.round(np.clip(preds, 0, 10)).astype(int)
 score = f1_score(labels, preds, average="macro")
 return('KaggleMetric', score, True)
 def train_model_classification(X, X_test, y, params, model_type='lgb', eval_metric='f1score',
columns=None, plot_feature_importance=False, model=None,
verbose=50, early_stopping_rounds=200, n_estimators=2000):
columns = X.columns if columns == None else columns
X_test = X_test[columns]
metrics_dict = {
'f1score': {'lgb_metric_name': lgb_Metric,}
}
result_dict = {}
oof = np.zeros(len(X))
prediction = np.zeros(( len(X_test)))
 scores = []
feature_importance = pd.DataFrame()

 if True:
X_train, X_valid, y_train, y_valid = model_selection.train_test_split(X, y, test_size=0.3, random_state=7)
 if model_type == 'lgb':
model = lgb.train(params, lgb.Dataset(X_train, y_train),
n_estimators, lgb.Dataset(X_valid, y_valid),
verbose_eval=verbose, early_stopping_rounds=early_stopping_rounds, feval=lgb_Metric)
 preds = model.predict(X, num_iteration=model.best_iteration)
 y_pred = model.predict(X_test, num_iteration=model.best_iteration)
 if model_type == 'xgb':
train_set = xgb.DMatrix(X_train, y_train)
 val_set = xgb.DMatrix(X_valid, y_valid)
 model = xgb.train(params, train_set, num_boost_round=2222, evals=[(train_set, 'train'),(val_set, 'val')],
verbose_eval=verbose, early_stopping_rounds=early_stopping_rounds)
 preds = model.predict(xgb.DMatrix(X))
y_pred = model.predict(xgb.DMatrix(X_test))
if model_type == 'cat':
model = CatBoostRegressor(params)
 model.fit(X_train, y_train)
 y_pred_valid = np.round(np.clip(preds, 0, 10)).astype(int)
 y_pred = model.predict(X_test, num_iteration=model.best_iteration)
 y_pred = np.round(np.clip(y_pred, 0, 10)).astype(int)
 oof = preds
scores.append(f1_score_calc(y, np.round(np.clip(preds,0,10)).astype(int)))
 prediction += y_pred
if model_type == 'lgb' and plot_feature_importance:
fold_importance = pd.DataFrame()
fold_importance["feature"] = columns
fold_importance["importance"] = model.feature_importances_
fold_importance["fold"] = fold_n + 1
feature_importance = pd.concat([feature_importance, fold_importance], axis=0)
 print('FINAL score: {0:.4f}, std: {1:.4f}.'.format(np.mean(scores), np.std(scores)))
 result_dict['oof'] = oof
result_dict['prediction'] = prediction
result_dict['scores'] = scores
result_dict['model'] = model
if model_type == 'lgb':
if plot_feature_importance:
feature_importance["importance"] /= folds.n_splits
cols = feature_importance[["feature", "importance"]].groupby("feature" ).mean().sort_values(
 by="importance", ascending=False)[:50].index
best_features = feature_importance.loc[feature_importance.feature.isin(cols)]
plt.figure(figsize=(16, 12)) ;
sns.barplot(x="importance", y="feature", data=best_features.sort_values(by="importance", ascending=False)) ;
plt.title('LGB Features(avg over folds)');
result_dict['feature_importance'] = feature_importance
return result_dict
for i in range(0,5000):
img = learn.data.test_ds[i][0]
tmp_df.loc[i]=[i,int(learn.predict(img)[1])]
tmp_df<save_to_csv>
good_columns = [c for c in train.columns if c not in ['time', 'signal','open_channels', 'batch', 'batch_index', 'batch_slices', 'batch_slices2']]
X = train[good_columns].copy()
y = train['open_channels']
X_test = test[good_columns].copy()
del train, test
tmp_df.to_csv('submission.csv',index=False )<set_options>
params_xgb = {'colsample_bytree': 0.375,'learning_rate': 0.1,'max_depth': 10, 'subsample': 1, 'objective':'reg:squarederror',
'eval_metric':'rmse'}
result_dict_xgb = train_model_classification(X=X[0:500000*8-1], X_test=X_test, y=y[0:500000*8-1], params=params_xgb, model_type='xgb', eval_metric='f1score', plot_feature_importance=False,
verbose=50, early_stopping_rounds=250 )
defaults.device = torch.device('cuda' )<load_from_csv>
params_lgb = {'learning_rate': 0.1, 'max_depth': 7, 'num_leaves':2**7+1, 'metric': 'rmse', 'random_state': 7, 'n_jobs':-1}
result_dict_lgb = train_model_classification(X=X[0:500000*8-1], X_test=X_test, y=y[0:500000*8-1], params=params_lgb, model_type='lgb', eval_metric='f1score', plot_feature_importance=False,
verbose=50, early_stopping_rounds=250, n_estimators=3000 )
train_df = pd.read_csv('.. /input/Kannada-MNIST/train.csv')
 train_df.head()<load_from_csv>
preds_ensemble = 0.50 * result_dict_lgb['prediction'] + 0.50 * result_dict_xgb['prediction']
<load_from_csv><EOS> 
sub = pd.read_csv('/kaggle/input/liverpool-ion-switching/sample_submission.csv')
 sub['open_channels'] = np.array(np.round(preds_ensemble,0), np.int)
 sub.to_csv('submission.csv', index=False, float_format='%.4f')
 sub.head(10 )