Kaggle competition: University of Liverpool - Ion Switching
embarked_column_train = pd.get_dummies(df_train["Embarked"])
 embarked_column_test = pd.get_dummies(df_test["Embarked"])
 df_test.drop(["Embarked"],axis=1,inplace = True)
 df_train.drop(["Embarked"],axis=1,inplace = True)
 df_train = pd.concat([df_train,embarked_column_train],axis=1)
 df_test= pd.concat([df_test,embarked_column_test],axis=1 )<prepare_x_and_y>
gp = GP()
y = df_train.iloc[:,0]
X = df_train.iloc[:,1:]<split>
def maddest(d, axis=None):
return np.mean(np.absolute(d - np.mean(d, axis)) , axis)
 def high_pass_filter(x, low_cutoff=1000, sample_rate=10000):
nyquist = 0.5 * sample_rate
norm_low_cutoff = low_cutoff / nyquist
print(norm_low_cutoff)
 sos = butter(10, Wn=[norm_low_cutoff], btype='highpass', output='sos')
 filtered_sig = signal.sosfilt(sos, x)
 return filtered_sig
def denoise_signal(x, wavelet='db4', level=1):
coeff = pywt.wavedec(x, wavelet, mode="per")
 sigma =(1/0.6745)* maddest(coeff[-level])
 uthresh = sigma * np.sqrt(2*np.log(len(x)))
 coeff[1:] =(pywt.threshold(i, value=uthresh, mode='hard')for i in coeff[1:])
 return pywt.waverec(coeff, wavelet, mode='per' )
X_train,X_valid,y_train,y_valid = train_test_split(X,y,test_size = 0.3,random_state = 42 )<train_model>
base = '.. /input/clean-kalman/'
train = pd.read_csv(os.path.join(base + 'train_clean_kalman.csv'))
test = pd.read_csv(os.path.join(base + 'test_clean_kalman.csv'))
name = []
metric = []
def trainModel(alg,X_train,y_train,X_valid,y_valid):
model = alg().fit(X_train,y_train)
 y_pred = model.predict(X_valid)
 name.append(alg.__name__)
 metric.append(accuracy_score(y_valid,y_pred))
print(alg.__name__ , " Accuracy ", accuracy_score(y_valid,y_pred))<train_model>
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 // 25_000
df['batch_index'] = df.index -(df.batch * 25_000)
 df['batch_slices'] = df['batch_index'] // 2500
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'].mean()
d['median'+c] = df.groupby([c])['signal'].median()
d['max'+c] = df.groupby([c])['signal'].max()
d['min'+c] = df.groupby([c])['signal'].min()
d['std'+c] = df.groupby([c])['signal'].std()
d['mean_abs_chg'+c] = df.groupby([c])['signal'].apply(lambda x: np.mean(np.abs(np.diff(x))))
d['abs_max'+c] = df.groupby([c])['signal'].apply(lambda x: np.max(np.abs(x)))
 d['abs_min'+c] = df.groupby([c])['signal'].apply(lambda x: np.min(np.abs(x)))
 d['range'+c] = d['max'+c] - d['min'+c]
d['maxtomin'+c] = d['max'+c] / d['min'+c]
d['abs_avg'+c] =(d['abs_min'+c] + d['abs_max'+c])/ 2
for v in d:
df[v] = df[c].map(d[v].to_dict())
 df['signal_shift_+1'] = [0,] + list(df['signal'].values[:-1])
 df['signal_shift_-1'] = list(df['signal'].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
for c in [c1 for c1 in df.columns if c1 not in ['time', 'signal', 'open_channels', 'batch',
'batch_index', 'batch_slices', 'batch_slices2'
]]:
df[c+'_msignal'] = df[c] - df['signal']
return df
train = features(train)
 test = features(test)
 col = [c for c in train.columns if c not in ['time', 'open_channels', 'batch', 'batch_index', 'batch_slices',
'batch_slices2',
'mean_abs_chgbatch', 'meanbatch', 'rangebatch', 'stdbatch',
'maxbatch', 'medianbatch', 'abs_minbatch', 'abs_avgbatch']]
target = train['open_channels']
train = train[col]
models = [RandomForestClassifier,GradientBoostingClassifier,DecisionTreeClassifier,SVC,XGBClassifier]
for i in models:
trainModel(i,X_train,y_train,X_valid,y_valid )<create_dataframe>
train.replace(np.inf,np.nan,inplace=True)
 train.replace(-np.inf,np.nan,inplace=True)
 test.replace(np.inf,np.nan,inplace=True)
 test.replace(-np.inf,np.nan,inplace=True )
status = pd.DataFrame({"name" : name,
"training_metric" :metric
})
 status<train_model>
train_preds = gp.GrabPredictions(train[col].fillna(-999))
test_preds = gp.GrabPredictions(test[col].fillna(-999))
Gradient_boosting = GradientBoostingClassifier().fit(X,y )<save_to_csv>
print('Score:',f1_score(target.values,np.argmax(train_preds.values,axis=1),average='macro'))
sub = pd.DataFrame({"PassengerId":df_test.PassengerId, "Survived":Gradient_boosting.predict(df_test.drop("PassengerId", axis = 1)) })
 sub.to_csv("submission.csv", index = None )<import_modules>
test['open_channels'] = np.argmax(test_preds.values,axis=1)
 test[['time','open_channels']].to_csv('gpsubmission.csv', index=False, float_format='%.4f' )
import os
import json
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.colors as colors
from tqdm import tqdm<categorify>
def MacroF1Metric(preds, dtrain):
labels = dtrain.get_label()
preds = preds.reshape(( -1,11))
preds = np.argmax(preds,axis=1)
 score = f1_score(labels, preds, average = 'macro')
 return('MacroF1Metric', score, True )
<categorify><EOS> 
for c in range(11):
train['gp_'+str(int(c)) ] = train_preds.values[:,c]
test['gp_'+str(int(c)) ] = test_preds.values[:,c]
x1, x2, y1, y2 = model_selection.train_test_split(train, target, test_size=0.2, random_state=7)
 del train
gc.collect()
col = x1.columns
params = {'learning_rate': 0.1,
'max_depth': -1,
'num_leaves':2**7+1,
'objective': 'multiclass',
'num_class': 11,
'metric': 'multi_logloss',
'random_state': 7,
'n_jobs':4}
model = lgb.train(params, lgb.Dataset(x1, y1), 22222, lgb.Dataset(x2, y2), verbose_eval=0, early_stopping_rounds=250,feval=MacroF1Metric)
 preds_lgb =(model.predict(test[col], num_iteration=model.best_iteration))
oof_lgb =(model.predict(x2, num_iteration=model.best_iteration))
print('f1_score',f1_score(y2,np.argmax(oof_lgb,axis=1),average='macro'))
test['open_channels'] = np.argmax(preds_lgb,axis=1)
 test[['time','open_channels']].to_csv('lgbsubmission.csv', index=False, float_format='%.4f' )