Kaggle competition: University of Liverpool - Ion Switching
<SOS> metric: MacroFScore Kaggle data source: university-of-liverpool-ion-switching<split>
import os
import gc
import numpy as np
import pandas as pd
import time
import math
from numba import jit
from math import log, floor
import scipy
from scipy import signal
from scipy.signal import butter, deconvolve
from sklearn.neighbors import KDTree
from pathlib import Path
from sklearn.utils import shuffle
import seaborn as sns
from matplotlib import colors
import matplotlib.pyplot as plt
from matplotlib.colors import Normalize
import lightgbm as lgb
from tqdm import tqdm
from sklearn.model_selection import GroupKFold, KFold
from sklearn.metrics import cohen_kappa_score, accuracy_score, mean_squared_error, f1_score
predictors = train.drop(['Survived', 'PassengerId'], axis = 1)
 target = train['Survived']
x_train, x_val, y_train, y_val = train_test_split(predictors, target, test_size = 0.22, random_state = 0 )<predict_on_test>
DATA_PATH = ".. /input/liverpool-ion-switching"
train_df = pd.read_csv(os.path.join(DATA_PATH, 'train.csv'))
test_df = pd.read_csv(os.path.join(DATA_PATH, 'test.csv'))
submission_df = pd.read_csv(os.path.join(DATA_PATH, 'sample_submission.csv'))
gaussian = GaussianNB()
gaussian.fit(x_train, y_train)
 y_pred = gaussian.predict(x_val)
 acc_gaussian = round(accuracy_score(y_pred, y_val)*100, 2)
 acc_gaussian<compute_train_metric>
def get_stats(df):
stats = pd.DataFrame(index=df.columns, columns=['na_count', 'n_unique', 'type', 'memory_usage'])
 for col in df.columns:
stats.loc[col] = [df[col].isna().sum() , df[col].nunique(dropna=False), df[col].dtypes, df[col].memory_usage(deep=True, index=False)/ 1024**2]
stats.loc['Overall'] = [stats['na_count'].sum() , stats['n_unique'].sum() , None, df.memory_usage(deep=True ).sum() / 1024**2]
return stats
def print_header() :
print('col conversion dtype na uniq size')
 print()
def print_values(name, conversion, col):
template = '{:10} {:16} {:>7} {:2} {:6} {:1.2f}MB'
print(template.format(name, conversion, str(col.dtypes), col.isna().sum() , col.nunique(dropna=False), col.memory_usage(deep=True, index=False)/ 1024 ** 2))
logreg = LogisticRegression()
logreg.fit(x_train, y_train)
 y_pred = logreg.predict(x_val)
 acc_logreg = round(accuracy_score(y_pred, y_val)*100, 2)
 acc_logreg<compute_train_metric>
get_stats(test_df )
svc = SVC()
svc.fit(x_train, y_train)
 y_pred = svc.predict(x_val)
 acc_svc = round(accuracy_score(y_pred, y_val)*100, 2)
 acc_svc<compute_train_metric>
def average_signal_smoothing(signal, kernel_size=3, stride=1):
sample = []
start = 0
end = kernel_size
while end <= len(signal):
start = start + stride
end = end + stride
sample.extend(np.ones(end - start)*np.mean(signal[start:end]))
return np.array(sample[::kernel_size] )
linear_svc = LinearSVC()
linear_svc.fit(x_train, y_train)
 y_pred = linear_svc.predict(x_val)
 acc_linear_svc = round(accuracy_score(y_pred, y_val)*100, 2)
 acc_linear_svc<compute_train_metric>
train_df['train'] = True
test_df['train'] = False
all_data = pd.concat([train_df, test_df], sort=False ).reset_index(drop=True)
 all_data['train'] = all_data['train'].astype('bool')
 all_data = all_data.sort_values(by=['time'] ).reset_index(drop=True)
 all_data.index =(( all_data.time * 10_000)- 1 ).values
all_data['batch'] = all_data.index // 50_000
all_data['batch_index'] = all_data.index -(all_data.batch * 50_000)
 all_data['batch_slices'] = all_data['batch_index'] // 5_000
all_data['batch_slices2'] = all_data.apply(lambda r: '_'.join([str(r['batch'] ).zfill(3), str(r['batch_slices'] ).zfill(3)]), axis=1)
 all_data['signal_batch_min'] = all_data.groupby('batch')['signal'].transform('min')
 all_data['signal_batch_max'] = all_data.groupby('batch')['signal'].transform('max')
 all_data['signal_batch_std'] = all_data.groupby('batch')['signal'].transform('std')
 all_data['signal_batch_mean'] = all_data.groupby('batch')['signal'].transform('mean')
 all_data['signal_batch_median'] = all_data.groupby('batch')['signal'].transform('median')
 all_data['signal_batch_skew'] = all_data.groupby('batch')['signal'].transform('skew')
 all_data['mean_abs_chg_batch'] = all_data.groupby(['batch'])['signal'].transform(lambda x: np.mean(np.abs(np.diff(x))))
all_data['median_abs_chg_batch'] = all_data.groupby(['batch'])['signal'].transform(lambda x: np.median(np.abs(np.diff(x))))
all_data['abs_max_batch'] = all_data.groupby(['batch'])['signal'].transform(lambda x: np.max(np.abs(x)))
 all_data['abs_min_batch'] = all_data.groupby(['batch'])['signal'].transform(lambda x: np.min(np.abs(x)))
 all_data['abs_mean_batch'] = all_data.groupby(['batch'])['signal'].transform(lambda x: np.mean(np.abs(x)))
 all_data['abs_median_batch'] = all_data.groupby(['batch'])['signal'].transform(lambda x: np.median(np.abs(x)))
 all_data['moving_average_batch_1000_mean'] = all_data.groupby(['batch'])['signal'].rolling(window=1000 ).mean().mean(skipna=True)
 all_data['range_batch'] = all_data['signal_batch_max'] - all_data['signal_batch_min']
all_data['maxtomin_batch'] = all_data['signal_batch_max'] / all_data['signal_batch_min']
all_data['abs_avg_batch'] =(all_data['abs_min_batch'] + all_data['abs_max_batch'])/ 2
all_data['signal_batch_5k_min'] = all_data.groupby('batch_slices2')['signal'].transform('min')
 all_data['signal_batch_5k_max'] = all_data.groupby('batch_slices2')['signal'].transform('max')
 all_data['signal_batch_5k_std'] = all_data.groupby('batch_slices2')['signal'].transform('std')
 all_data['signal_batch_5k_mean'] = all_data.groupby('batch_slices2')['signal'].transform('mean')
 all_data['signal_batch_5k_median'] = all_data.groupby('batch_slices2')['signal'].transform('median')
 all_data['signal_batch_5k_mad'] = all_data.groupby('batch_slices2')['signal'].transform('mad')
 all_data['mean_abs_chg_batch_5k'] = all_data.groupby(['batch_slices2'])['signal'].transform(lambda x: np.mean(np.abs(np.diff(x))))
all_data['median_abs_chg_batch_5k'] = all_data.groupby(['batch'])['signal'].transform(lambda x: np.median(np.abs(np.diff(x))))
all_data['abs_max_batch_5k'] = all_data.groupby(['batch_slices2'])['signal'].transform(lambda x: np.max(np.abs(x)))
 all_data['abs_min_batch_5k'] = all_data.groupby(['batch_slices2'])['signal'].transform(lambda x: np.min(np.abs(x)))
 all_data['abs_mean_batch_5k'] = all_data.groupby(['batch_slices2'])['signal'].transform(lambda x: np.mean(np.abs(x)))
 all_data['abs_median_batch_5k'] = all_data.groupby(['batch_slices2'])['signal'].transform(lambda x: np.median(np.abs(x)))
 all_data['moving_average_batch_5k_1000_mean'] = all_data.groupby(['batch_slices2'])['signal'].rolling(window=1000 ).mean().mean(skipna=True)
 all_data['range_batch_5k'] = all_data['signal_batch_5k_max'] - all_data['signal_batch_5k_min']
all_data['maxtomin_batch_5k'] = all_data['signal_batch_5k_max'] / all_data['signal_batch_5k_min']
all_data['abs_avg_batch_5k'] =(all_data['abs_min_batch_5k'] + all_data['abs_max_batch_5k'])/ 2
all_data['signal_shift+1'] = all_data.groupby(['batch'] ).shift(1)['signal']
all_data['signal_shift-1'] = all_data.groupby(['batch'] ).shift(-1)['signal']
all_data['signal_shift+2'] = all_data.groupby(['batch'] ).shift(2)['signal']
all_data['signal_shift-2'] = all_data.groupby(['batch'] ).shift(-2)['signal']
all_data['signal_shift+1_5k'] = all_data.groupby(['batch_slices2'] ).shift(1)['signal']
all_data['signal_shift-1_5k'] = all_data.groupby(['batch_slices2'] ).shift(-1)['signal']
all_data['signal_shift+2_5k'] = all_data.groupby(['batch_slices2'] ).shift(2)['signal']
all_data['signal_shift-2_5k'] = all_data.groupby(['batch_slices2'] ).shift(-2)['signal']
all_data['abs_max_signal_shift+1_5k'] = all_data['signal_shift+1_5k'].transform(lambda x: np.max(np.abs(x)))
 all_data['abs_max_signal_shift-1_5k'] = all_data['signal_shift-1_5k'].transform(lambda x: np.max(np.abs(x)))
perceptron = Perceptron()
perceptron.fit(x_train, y_train)
 y_pred = perceptron.predict(x_val)
 acc_perceptron = round(accuracy_score(y_pred, y_val)*100, 2)
 acc_perceptron<compute_train_metric>
for c in ['signal_batch_mean','signal_batch_median',
'mean_abs_chg_batch','abs_max_batch','abs_min_batch', 'abs_median_batch',
'moving_average_batch_1000_mean',
'range_batch','abs_avg_batch',
'signal_batch_5k_mean', 'signal_batch_5k_median', 'signal_batch_5k_mad',
'moving_average_batch_5k_1000_mean',
'mean_abs_chg_batch_5k','abs_max_batch_5k', 'abs_min_batch_5k', 'abs_median_batch_5k',
'range_batch_5k','abs_avg_batch_5k',
'signal_shift+1', 'signal_shift+2', 'signal_shift-1', 'signal_shift-2',
'signal_shift+1_5k', 'signal_shift+2_5k', 'signal_shift-1_5k', 'signal_shift-2_5k',
'abs_max_signal_shift+1_5k', 'abs_max_signal_shift-1_5k'
]:
all_data[f'{c}_msignal'] = all_data[c] - all_data['signal']
decision_tree = DecisionTreeClassifier()
decision_tree.fit(x_train, y_train)
 y_pred = decision_tree.predict(x_val)
 acc_decision_tree = round(accuracy_score(y_pred, y_val)*100 ,2)
 acc_decision_tree<compute_train_metric>
FEATURES = [f for f in all_data.columns if f not in ['open_channels','index','time','train','batch',
'signal_batch_max', 'signal_batch_mad', 'maxtomin_batch'
'batch_index','batch_slices','batch_slices2', 'median_abs_chg_batch_5k',
'abs_mean_batch', 'abs_median_batch', 'abs_avg_batch', 'signal_batch_median',
'abs_max_batch', 'abs_max_batch_5k', 'abs_median_batch_5k',
'moving_average_batch_1000_mean', 'moving_average_batch_5k_1000_mean']]
print('.... : FEATURE LIST :.... ')
 print([f for f in FEATURES])
 print(f"Features: {len(FEATURES)}" )
random_forest = RandomForestClassifier()
random_forest.fit(x_train, y_train)
 y_pred = random_forest.predict(x_val)
 acc_random_forest = round(accuracy_score(y_pred, y_val)*100, 2)
 acc_random_forest<compute_train_metric>
TARGET = 'open_channels'
all_data['train'] = all_data['train'].astype('bool')
 train_df = all_data.query('train' ).copy()
test_df = all_data.query('not train' ).copy()
train_df['open_channels'] = train_df['open_channels'].astype(int)
 X = train_df[FEATURES]
X_test = test_df[FEATURES]
y = train_df[TARGET].values
sub = test_df[['time']].copy()
groups = train_df['batch']
oof_df = train_df[['signal','open_channels']].copy()
knn = KNeighborsClassifier()
knn.fit(x_train, y_train)
 y_pred = knn.predict(x_val)
 acc_knn = round(accuracy_score(y_pred, y_val)*100, 2)
 acc_knn<compute_train_metric>
del all_data
del train_df
del test_df
gc.collect()
gbc = GradientBoostingClassifier()
gbc.fit(x_train, y_train)
 y_pred = gbc.predict(x_val)
 acc_gbc = round(accuracy_score(y_pred, y_val)*100, 2)
 acc_gbc<train_model>
TOTAL_FOLDS = 7
MODEL_TYPE = 'LGBM'
SHUFFLE = True
NUM_BOOST_ROUND = 3_500
EARLY_STOPPING_ROUNDS = 50
VERBOSE_EVAL = 500
RANDOM_SEED = 99943
params = {'learning_rate': 0.05,
'max_depth': -1,
'num_leaves': 2**8+1,
'feature_fraction': 0.8,
'bagging_fraction': 0.8,
'bagging_freq': 0,
'n_jobs': 8,
'seed': RANDOM_SEED,
'metric': 'rmse',
'objective' : 'regression',
'num_class': 1
}
kfold = KFold(n_splits=TOTAL_FOLDS, shuffle=SHUFFLE, random_state=RANDOM_SEED)
 feature_importance_df = pd.DataFrame()
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 = VERBOSE_EVAL)
 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']))
fold_importance_df = pd.DataFrame()
fold_importance_df["Feature"] = FEATURES
fold_importance_df["importance"] = model.feature_importance()
fold_importance_df["fold"] = fold_ + 1
feature_importance_df = pd.concat([feature_importance_df, fold_importance_df], axis=0)
 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']))
<create_dataframe><EOS> 
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[['time','open_channels']].to_csv('./submission.csv',
index=False,
float_format='%0.4f' )