Kaggle competition: University of Liverpool - Ion Switching
<SOS> metric: MacroFScore Kaggle data source: university-of-liverpool-ion-switching<load_from_csv>
import numpy as np
import pandas as pd
from keras.utils.np_utils import to_categorical
import matplotlib.pyplot as plt
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
dig_df = pd.read_csv('.. /input/Kannada-MNIST/Dig-MNIST.csv')
 dig_df.head()<concatenate>
df=pd.read_csv('.. /input/liverpool-ion-switching/train.csv')
 test = pd.read_csv('.. /input/liverpool-ion-switching/test.csv' )
new_df = train_df.append(dig_df, ignore_index=True)
 new_df<categorify>
def log_posterior(theta, X, p_mean, p_cov, x_var):
inv_cov = np.linalg.inv(p_cov)
 determinant = np.prod(np.diagonal(p_cov))
prior = np.log(1./(2*np.pi*np.sqrt(determinant)))
 ll_constant = 1./(np.sqrt(2 * np.pi * x_var))
L = 0.5 * np.exp(-(1./(2*x_var)) *(X - theta[0])**2)+ \
0.5 * np.exp(-(1./(2*x_var)) *(X -(theta[0]+theta[1])) **2)
 L *= ll_constant
log_likelihood = np.sum(np.log(L))
assert not np.isnan(prior + log_likelihood)
 return np.squeeze(prior + log_likelihood)
 def get_noise(eps):

 return(np.random.multivariate_normal(np.array([0,0]), eps*np.eye(2)) ).reshape(( 2,1))
def get_info_for_contour(K=100):

 xlist = np.linspace(-1.5, 2.5, num=K)
 ylist = np.linspace(-3, 3, num=K)
 X_a,Y_a = np.meshgrid(xlist, ylist)
 Z_a = np.zeros(( K,K))
for i in range(K):
for j in range(K):
theta = np.array([[X_a[i,j]],[Y_a[i,j]]])
 Z_a[i,j] = log_posterior(theta, X, prior_mean, prior_cov, sigmax_sq)
 return X_a, Y_a, Z_a
def get_imgs_labels(path:Path, df):
labels = df['label']
path.mkdir(parents=True,exist_ok=True)
 imgs = df.loc[:, 'pixel0':'pixel783']
return imgs, labels<define_variables>
data = pd.merge(df, test)
 x_train, y_train, x_test, y_test = train_test_split(df.head(2000000), test, test_size=0.15 )
train_path1 = Path('.. /input1/train')
 valid_path1 = Path('.. /input1/valid')
 test_path1 = Path('.. /input1/test' )<define_variables>
def logistic_run() :
clf = LogisticRegression(random_state=0)
 clf.fit(np.array(x_train.head(300000)['open_channels'] ).reshape(-1, 1), np.array(y_train.open_channels ).reshape(-1, 1))
x = clf.predict(np.array(df['open_channels'] ).reshape(-1, 1))
x = np.array(x)
 x = pd.DataFrame({'time': df.time, 'open_channels': x})
 x.to_csv('log_sub.csv')
 x.head()
imgs1, labels1 = get_imgs_labels(train_path1, new_df )<split>
TARGET = 'open_channels'
TOTAL_FOLDS = 5
RANDOM_SEED = 529
MODEL_TYPE = 'LGBM'
LEARNING_RATE = 0.009
SHUFFLE = True
NUM_BOOST_ROUND = 500_000
EARLY_STOPPING_ROUNDS = 100
N_THREADS = -1
OBJECTIVE = 'regression'
METRIC = 'rmse'
NUM_LEAVES = 2**8+1
MAX_DEPTH = -1
FEATURE_FRACTION = 1
BAGGING_FRACTION = 1
BAGGING_FREQ = 0
train = pd.read_csv('.. /input/liverpool-ion-switching/train.csv')
 test = pd.read_csv('.. /input/liverpool-ion-switching/test.csv')
 ss = pd.read_csv('.. /input/liverpool-ion-switching/sample_submission.csv')
 train['train'] = True
test['train'] = False
tt = pd.concat([train, test], sort=False ).reset_index(drop=True)
 tt['train'] = tt['train'].astype('bool')
 run_id = "{:%m%d_%H%M}".format(datetime.now())
 def update_tracking(
 run_id, field, value, csv_file="./tracking.csv",
integer=False, digits=None, nround=6,
drop_broken_runs=False):

 try:
df = pd.read_csv(csv_file, index_col=[0])
 except:
df = pd.DataFrame()
if drop_broken_runs:
df = df.dropna(subset=['1_f1'])
 if integer:
value = round(value)
 elif digits is not None:
value = round(value, digits)
 df.loc[run_id, field] = value
df = df.round(nround)
 df.to_csv(csv_file)
 update_tracking(run_id, 'model_type', MODEL_TYPE)
 update_tracking(run_id, 'seed', RANDOM_SEED, integer=True)
 update_tracking(run_id, 'nfolds', TOTAL_FOLDS, integer=True)
 update_tracking(run_id, 'lr', LEARNING_RATE)
 update_tracking(run_id, 'shuffle', SHUFFLE)
 update_tracking(run_id, 'boost_rounds', NUM_BOOST_ROUND)
 update_tracking(run_id, 'es_rounds', EARLY_STOPPING_ROUNDS)
 update_tracking(run_id, 'threads', N_THREADS)
 update_tracking(run_id, 'objective', OBJECTIVE)
 update_tracking(run_id, 'metric', METRIC)
 update_tracking(run_id, 'num_leaves', NUM_LEAVES)
 update_tracking(run_id, 'max_depth', MAX_DEPTH)
 update_tracking(run_id, 'feature_fraction', FEATURE_FRACTION)
 update_tracking(run_id, 'bagging_fraction', BAGGING_FRACTION)
 update_tracking(run_id, 'bagging_freq', BAGGING_FREQ)
 tt = tt.sort_values(by=['time'] ).reset_index(drop=True)
 tt.index =(( tt.time * 10_000)- 1 ).values
tt['batch'] = tt.index // 50_000
tt['batch_index'] = tt.index -(tt.batch * 50_000)
 tt['batch_slices'] = tt['batch_index'] // 5_000
tt['batch_slices2'] = tt.apply(lambda r: '_'.join(
 [str(r['batch'] ).zfill(3), str(r['batch_slices'] ).zfill(3)]), axis=1)
 tt['signal_batch_min'] = tt.groupby('batch')['signal'].transform('min')
 tt['signal_batch_max'] = tt.groupby('batch')['signal'].transform('max')
 tt['signal_batch_std'] = tt.groupby('batch')['signal'].transform('std')
 tt['signal_batch_mean'] = tt.groupby('batch')['signal'].transform('mean')
 tt['mean_abs_chg_batch'] = tt.groupby(['batch'])['signal'].transform(
 lambda x: np.mean(np.abs(np.diff(x))))
tt['abs_max_batch'] = tt.groupby(
 ['batch'])['signal'].transform(lambda x: np.max(np.abs(x)))
 tt['abs_min_batch'] = tt.groupby(
 ['batch'])['signal'].transform(lambda x: np.min(np.abs(x)))
 tt['range_batch'] = tt['signal_batch_max'] - tt['signal_batch_min']
tt['maxtomin_batch'] = tt['signal_batch_max'] / tt['signal_batch_min']
tt['abs_avg_batch'] =(tt['abs_min_batch'] + tt['abs_max_batch'])/ 2
tt['signal_batch_5k_min'] = tt.groupby(
 'batch_slices2')['signal'].transform('min')
 tt['signal_batch_5k_max'] = tt.groupby(
 'batch_slices2')['signal'].transform('max')
 tt['signal_batch_5k_std'] = tt.groupby(
 'batch_slices2')['signal'].transform('std')
 tt['signal_batch_5k_mean'] = tt.groupby(
 'batch_slices2')['signal'].transform('mean')
 tt['mean_abs_chg_batch_5k'] = tt.groupby(['batch_slices2'])[
'signal'].transform(lambda x: np.mean(np.abs(np.diff(x))))
tt['abs_max_batch_5k'] = tt.groupby(['batch_slices2'])[
'signal'].transform(lambda x: np.max(np.abs(x)))
 tt['abs_min_batch_5k'] = tt.groupby(['batch_slices2'])[
'signal'].transform(lambda x: np.min(np.abs(x)))
 tt['range_batch_5k'] = tt['signal_batch_5k_max'] - tt['signal_batch_5k_min']
tt['maxtomin_batch_5k'] = tt['signal_batch_5k_max'] / tt['signal_batch_5k_min']
tt['abs_avg_batch_5k'] =(tt['abs_min_batch_5k'] + tt['abs_max_batch_5k'])/ 2
tt['signal_shift+1'] = tt.groupby(['batch'] ).shift(1)['signal']
tt['signal_shift-1'] = tt.groupby(['batch'] ).shift(-1)['signal']
tt['signal_shift+2'] = tt.groupby(['batch'] ).shift(2)['signal']
tt['signal_shift-2'] = tt.groupby(['batch'] ).shift(-2)['signal']
for c in ['signal_batch_min', 'signal_batch_max',
'signal_batch_std', 'signal_batch_mean',
'mean_abs_chg_batch', 'abs_max_batch',
'abs_min_batch',
'range_batch', 'maxtomin_batch', 'abs_avg_batch',
'signal_shift+1', 'signal_shift-1',
'signal_batch_5k_min', 'signal_batch_5k_max',
'signal_batch_5k_std',
'signal_batch_5k_mean', 'mean_abs_chg_batch_5k',
'abs_max_batch_5k', 'abs_min_batch_5k',
'range_batch_5k', 'maxtomin_batch_5k',
'abs_avg_batch_5k','signal_shift+2','signal_shift-2']:
tt[f'{c}_msignal'] = tt[c] - tt['signal']
FEATURES = ['signal',
'signal_batch_min',
'signal_batch_max',
'signal_batch_std',
'signal_batch_mean',
'mean_abs_chg_batch',
'range_batch',
'maxtomin_batch',
'signal_batch_5k_min',
'signal_batch_5k_max',
'signal_batch_5k_std',
'signal_batch_5k_mean',
'mean_abs_chg_batch_5k',
'abs_max_batch_5k',
'abs_min_batch_5k',
'range_batch_5k',
'maxtomin_batch_5k',
'abs_avg_batch_5k',
'signal_shift+1',
'signal_shift-1',
'signal_batch_max_msignal',
'signal_batch_std_msignal',
'mean_abs_chg_batch_msignal',
'abs_max_batch_msignal',
'abs_min_batch_msignal',
'range_batch_msignal',
'maxtomin_batch_msignal',
'abs_avg_batch_msignal',
'signal_shift+1_msignal',
'signal_shift-1_msignal',
'signal_batch_5k_min_msignal',
'signal_batch_5k_max_msignal',
'signal_batch_5k_std_msignal',
'signal_batch_5k_mean_msignal',
'mean_abs_chg_batch_5k_msignal',
'abs_max_batch_5k_msignal',
'abs_min_batch_5k_msignal',
'maxtomin_batch_5k_msignal',
'abs_avg_batch_5k_msignal',
'signal_shift+2',
'signal_shift-2']
print('.... : FEATURE LIST :.... ')
 print([f for f in FEATURES])
 update_tracking(run_id, 'n_features', len(FEATURES), integer=True)
 update_tracking(run_id, 'target', TARGET)
 def lgb_Metric(preds, dtrain):
labels = dtrain.get_label()
print(preds.shape)
 print(preds)
 preds = np.argmax(preds, axis=0)
 score = f1_score(labels, preds, average='macro')
 return('KaggleMetric', score, True)
 tt['train'] = tt['train'].astype('bool')
 train = tt.query('train' ).copy()
test = tt.query('not train' ).copy()
train['open_channels'] = train['open_channels'].astype(int)
 X = train[FEATURES]
X_test = test[FEATURES]
y = train[TARGET].values
sub = test[['time']].copy()
groups = train['batch']
if OBJECTIVE == 'multiclass':
NUM_CLASS = 11
else:
NUM_CLASS = 1
params = {'learning_rate': LEARNING_RATE,
'max_depth': MAX_DEPTH,
'num_leaves': NUM_LEAVES,
'feature_fraction': FEATURE_FRACTION,
'bagging_fraction': BAGGING_FRACTION,
'bagging_freq': BAGGING_FREQ,
'n_jobs': N_THREADS,
'seed': RANDOM_SEED,
'metric': METRIC,
'objective': OBJECTIVE,
'num_class': NUM_CLASS
}
kfold = KFold(n_splits=TOTAL_FOLDS, shuffle=SHUFFLE, random_state=RANDOM_SEED)
 oof_df = train[['signal', 'open_channels']].copy()
fi_df = pd.DataFrame(index=FEATURES)
 fold = 1
for tr_idx, val_idx in kfold.split(X, y, groups=groups):
print(f'====== Fold {fold:0.0f} of {TOTAL_FOLDS} ======')
 X_tr, X_val = X.iloc[tr_idx], X.iloc[val_idx]
y_tr, y_val = y[tr_idx], y[val_idx]
train_set = lgb.Dataset(X_tr, y_tr)
 val_set = lgb.Dataset(X_val, y_val)
 model = lgb.train(params,
train_set,
num_boost_round=NUM_BOOST_ROUND,
early_stopping_rounds=EARLY_STOPPING_ROUNDS,
valid_sets=[train_set, val_set],
verbose_eval=50)
 if OBJECTIVE == 'multi_class':
preds = model.predict(X_val, num_iteration=model.best_iteration)
 preds = np.argmax(preds, axis=1)
 test_preds = model.predict(X_test, num_iteration=model.best_iteration)
 test_preds = np.argmax(test_preds, axis=1)
 elif OBJECTIVE == 'regression':
preds = model.predict(X_val, num_iteration=model.best_iteration)
 preds = np.round(np.clip(preds, 0, 10)).astype(int)
 test_preds = model.predict(X_test, num_iteration=model.best_iteration)
 test_preds = np.round(np.clip(test_preds, 0, 10)).astype(int)
 oof_df.loc[oof_df.iloc[val_idx].index, 'oof'] = preds
sub[f'open_channels_fold{fold}'] = test_preds
f1 = f1_score(oof_df.loc[oof_df.iloc[val_idx].index]['open_channels'],
oof_df.loc[oof_df.iloc[val_idx].index]['oof'],
average='macro')
 rmse = np.sqrt(mean_squared_error(oof_df.loc[oof_df.index.isin(val_idx)]['open_channels'],
oof_df.loc[oof_df.index.isin(val_idx)]['oof']))
update_tracking(run_id, f'{fold}_best_iter', model.best_iteration, integer=True)
 update_tracking(run_id, f'{fold}_rmse', rmse)
 update_tracking(run_id, f'{fold}_f1', f1)
 fi_df[f'importance_{fold}'] = model.feature_importance()
print(f'Fold {fold} - validation f1: {f1:0.5f}')
 print(f'Fold {fold} - validation rmse: {rmse:0.5f}')
 fold += 1
oof_f1 = f1_score(oof_df['open_channels'],
oof_df['oof'],
average='macro')
 oof_rmse = np.sqrt(mean_squared_error(oof_df['open_channels'],
oof_df['oof']))
update_tracking(run_id, f'oof_f1', oof_f1)
 update_tracking(run_id, f'oof_rmse', oof_rmse)
 s_cols = [s for s in sub.columns if 'open_channels' in s]
sub['open_channels'] = sub[s_cols].median(axis=1 ).astype(int)
 sub.to_csv(f'./pred_x_{oof_f1:0.6}.csv', index=False)
 sub[['time', 'open_channels']].to_csv(f'./sub_x_{oof_f1:0.10f}.csv',
index=False,
float_format='%0.4f')
 oof_df.to_csv(f'./oof_x_{oof_f1:0.6}.csv', index=False)
 fi_df['importance'] = fi_df.sum(axis=1)
 fi_df.to_csv(f'./fi_x_{oof_f1:0.6}.csv', index=True)
 fig, ax = plt.subplots(figsize=(15, 30))
fi_df.sort_values('importance')['importance'] \ 
.plot(kind='barh',
figsize=(15, 30),
title=f'x - Feature Importance',
ax=ax)
 plt.savefig(f'./x__{oof_f1:0.6}.png' )
X_train1, X_test1, y_train1, y_test1 \
= train_test_split(imgs1, labels1, test_size=0.08, random_state=17, stratify=labels1 )<count_unique_values>
!pip install pyhmc --quiet
!pip install triangle --quiet
create_subdirs(train_path1, labels1.nunique())
 create_subdirs(valid_path1, labels1.nunique() )<train_model>
import pyhmc
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import xgboost as xgb
save_imgs(train_path1, X_train1, y_train1)
 save_imgs(valid_path1, X_test1, y_test1)
 save_imgs(test_path1, np.array(test_df.drop(['id'], axis=1)) , [] )<feature_engineering>
df_train = pd.read_csv(".. /input/liverpool-ion-switching/train.csv")
 train_time = df_train["time"].values.reshape(-1,500000)
 train_signal = df_train["signal"].values.reshape(-1,500000)
 train_opench = df_train["open_channels"].values.reshape(-1,500000 )
tfms = get_transforms(do_flip=False, flip_vert=False, max_rotate= 10.0,
max_zoom=1.1, max_lighting=0.15, max_warp=0.15,
p_affine=0.75, p_lighting=0.75 )<categorify>
def markov_p(data):
channel_range = np.unique(data)
 channel_bins = np.append(channel_range, 11)
 data_next = np.roll(data, -1)
 matrix = []
for i in channel_range:
current_row = np.histogram(data_next[data == i], bins=channel_bins)[0]
current_row = current_row / np.sum(current_row)
 matrix.append(current_row)
 return np.array(matrix)
 p03 = markov_p(train_opench[3])
 p04 = markov_p(train_opench[4])
 eig_values, eig_vectors = np.linalg.eig(np.transpose(p03))
print("Eigenvalues :", eig_values )
db1 =(ImageList.from_folder('.. /input1/')
.split_by_folder() 
.label_from_folder() 
.add_test_folder(test_path1)
.transform(tfms, size=64)
.databunch(bs=512)
.normalize(mnist_stats))<define_variables>
prob = [.3,.3,.2,.1,.05,.05]
data = train_signal[4][1:7]
len(data)
 stat = []
for i in range(1, 1000):
mc_signal = np.random.choice(data, 500, p=prob)
 df = pd.DataFrame(index=range(500), data={'mc_signal': mc_signal,})
 stat.append([df['mc_signal'].sum().round(0)] )
db1.show_batch(rows=4, figsize=(6,6))<choose_model_class>
ks(arsig, 'norm' )
<train_model><EOS> 
ks(p03, 'norm' )