Kaggle competition: University of Liverpool - Ion Switching
base_voting_hard.fit(X_transformed, y)
 base_voting_soft.fit(X_transformed, y)
 y_pred_base_hard = base_voting_hard.predict(test_final_transformed)
 y_pred_base_soft = base_voting_hard.predict(test_final_transformed )<save_to_csv>
gp = GP()
test_df = pd.DataFrame(pd.read_csv('.. /input/titanic/test.csv')['PassengerId'])
 pd.DataFrame(data = {'PassengerId': test_df.PassengerId,
'Survived': y_pred_base_hard.astype(int)} ).to_csv('01-Baseline_Hard_voting.csv', index = False)
 pd.DataFrame(data = {'PassengerId': test_df.PassengerId,
'Survived': y_pred_base_soft.astype(int)} ).to_csv('02-Baseline_Soft_voting.csv', index = False)
 pd.DataFrame(data = {'PassengerId': test_df.PassengerId,
'Survived': y_pred_tuned_hd.astype(int)} ).to_csv('03-Tuned_Hard_Voting.csv', index = False)
 pd.DataFrame(data = {'PassengerId': test_df.PassengerId,
'Survived': y_pred_tuned_sf.astype(int)} ).to_csv('04-Tuned_Soft_Voting.csv', index = False )<load_from_csv>
train = pd.read_csv('.. /input/data-without-drift/train_clean.csv')
 test = pd.read_csv('.. /input/data-without-drift/test_clean.csv' )
df1 = pd.read_csv("/kaggle/input/titanic/train.csv")
 df1.head()<load_from_csv>
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' )
df2 = pd.read_csv("/kaggle/input/titanic/test.csv")
 df2.head()<count_missing_values>
base = '.. /input/data-without-drift/'
train = pd.read_csv(os.path.join(base + 'train_clean.csv'))
test = pd.read_csv(os.path.join(base + 'test_clean.csv'))
print(df1.isnull().sum())
 print("
****************
")
 print(df2.isnull().sum() )<data_type_conversions>
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]
df1['Age'].fillna(df1['Age'].median() ,inplace=True)
 df2['Age'].fillna(df2['Age'].median() ,inplace=True )<data_type_conversions>
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 )
df2['Fare'].fillna(df2['Fare'].median() ,inplace=True )<drop_column>
train_preds = gp.GrabPredictions(train[col].fillna(-999))
test_preds = gp.GrabPredictions(test[col].fillna(-999))
df1.drop(['PassengerId','Name','Ticket'],axis=1,inplace=True)
 df2.drop(['Name','Ticket'],axis=1,inplace=True )<feature_engineering>
print('Score:',f1_score(target.values,np.argmax(train_preds.values,axis=1),average='macro'))
df1['family_members']=df1['Parch']+df1['SibSp']
df2['family_members']=df2['Parch']+df2['SibSp']<drop_column>
test['open_channels'] = np.argmax(test_preds.values,axis=1)
 test[['time','open_channels']].to_csv('gpsubmission.csv', index=False, float_format='%.4f' )
df1.drop(['SibSp','Parch','Embarked'],axis=1,inplace=True)
 df2.drop(['SibSp','Parch','Embarked'],axis=1,inplace=True )<groupby>
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 )
<feature_engineering><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' )