Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<groupby>
import numpy as np
import pandas as pd
import tensorflow
import featuretools as ft
signal_means = train_df[['open_channels','signal']].groupby('open_channels' ).agg('mean')
 standard_dev = {
0: 0.24069495895789328,
1: 0.24661370964721863,
2: 0.2462656210881039,
3: 0.24593499463670376,
4: 0.24285132812035926,
5: 0.26328499147888296,
6: 0.24325060224204165,
7: 0.24219770330946155,
8: 0.24284409806346507,
9: 0.24213353276403837,
10: 0.24582776879974166,
11: 0.24496232384251448,
12: 0.24419957371390066,
13: 0.24390344537922137,
14: 0.24401429614362458,
15: 0.2650094712461518,
16: 0.26515878374200286,
17: 0.26653346413097956,
18: 0.2666851765378241,
19: 0.2661300555645225,
20: 0.4069574034485397,
21: 0.4057626703610925,
22: 0.40661851101785595,
23: 0.4014537024692114,
24: 0.40342408294931176,
25: 0.2864659504038526,
26: 0.28792615084155104,
27: 0.2861063013595865,
28: 0.28894890652916927,
29: 0.28746591555904344,
30: 0.24588901544435604,
31: 0.24562918245064735,
32: 0.24454967653259863,
33: 0.24712026725094094,
34: 0.24671466539168418,
35: 0.2944529092246321,
36: 0.2897700004860426,
38: 0.29459674149215787,
39: 0.27525129540509075,
40: 0.2855328333419974,
41: 0.28533031164314704,
42: 0.28859497286113045,
43: 0.28256191424770405,
44: 0.2841569645597404,
45: 0.4066384374459793,
46: 0.4061604936865585,
47: 0.40514687628102375,
48: 0.4067380853754845,
49: 0.40637947598259294,
50: 0.24073643515935061,
51: 0.2754600438388737,
52: 0.29036115205832036,
53: 0.2410069541187777,
54: 0.2448870602255716,
55: 0.40433399257591063,
56: 0.2861781553817768,
57: 0.40588197314984553,
58: 0.24015921663211293,
59: 0.2724222128619417,
60: 0.2388213978815562,
61: 0.2409860881280551,
62: 0.2404194193333159,
63: 0.2454693052384903,
64: 0.24194863011797696,
65: 0.2413491063034735,
66: 0.24715158528432513,
67: 0.24293092821730067,
68: 0.24327976456590195,
69: 0.24585838370130864
}<train_on_grid>
np.random.seed(7 )
class ViterbiClassifier:
def __init__(self):
self._p_trans = None
self._p_signal = None
def fit(self, mini_batch):
self._n_states = 11
self._states = list(range(self._n_states))
self._p_trans = self.markov_p_trans(mini_batch)
 self._dists = []
for s in np.arange(0, 11):
self._dists.append(( signal_means.loc[s,'signal'], standard_dev[mini_batch]))
return self
def predict(self, x, p_signal=None, proba=False):
if p_signal is None:
p_signal = self.markov_p_signal(x)
 preds, probs = self.viterbi(self._p_trans, p_signal[self._states], x)
 return preds, probs
def markov_p_signal(self, signal):
p_signal = np.zeros(( self._n_states, len(signal)))
 for k, dist in enumerate(self._dists):
p_signal[k, :] = norm.pdf(signal, *dist)
 return p_signal
def markov_p_trans(self, mini_batch):
transition = Transition_matrices[mini_batch]
TM = pd.DataFrame(index=range(11),columns=range(11))
TM.loc[transition.index,transition.columns] = transition
TM = TM.fillna(0)
 return TM.values
def viterbi(self, p_trans, p_signal, signal):
offset = 10**(-20)
 p_trans_tlog = np.transpose(np.log2(p_trans + offset))
p_signal_tlog = np.transpose(np.log2(p_signal + offset))
T1 = np.zeros(p_signal.shape)
 T2 = np.zeros(p_signal.shape)
 T1[:, 0] = p_signal_tlog[0, :]
T2[:, 0] = 0
for j in range(1, p_signal.shape[1]):
for i in range(len(p_trans)) :
T1[i, j] = np.max(T1[:, j - 1] + p_trans_tlog[:, i] + p_signal_tlog[j, i])
 T2[i, j] = np.argmax(T1[:, j - 1] + p_trans_tlog[:, i] + p_signal_tlog[j, i])
 x = np.empty(p_signal.shape[1], 'B')
 x[-1] = np.argmax(T1[:, p_signal.shape[1] - 1])
 for i in reversed(range(1, p_signal.shape[1])) :
x[i - 1] = T2[x[i], i]
return x, T1
class PosteriorDecoder:
def __init__(self):
self._p_trans = None
self._p_signal = None
def fit(self, mini_batch):
self._n_states = 11
self._states = list(range(self._n_states))
self._p_trans = self.markov_p_trans(mini_batch)
 self._dists = []
for s in np.arange(0, 11):
self._dists.append(( signal_means.loc[s,'signal'], standard_dev[mini_batch]))
return self
def predict(self, x, p_signal=None, proba=False):
if p_signal is None:
p_signal = self.markov_p_signal(x)
 preds,probs = self.posterior_decoding(self._p_trans, p_signal[self._states])
 return preds, probs
def markov_p_signal(self, signal):
p_signal = np.zeros(( self._n_states, len(signal)))
 for k, dist in enumerate(self._dists):
p_signal[k, :] = norm.pdf(signal, *dist)
 return p_signal
def markov_p_trans(self, mini_batch):
transition = Transition_matrices[mini_batch]
TM = pd.DataFrame(index=range(11),columns=range(11))
TM.loc[transition.index,transition.columns] = transition
TM = TM.fillna(0)
 return TM.values
def forward(self, p_trans, p_signal):

 T1 = np.zeros(p_signal.shape)
 T1[:, 0] = p_signal[:, 0]
T1[:, 0] /= np.sum(T1[:, 0])
 for j in range(1, p_signal.shape[1]):
for i in range(len(p_trans)) :
T1[i, j] = p_signal[i, j] * np.sum(T1[:, j - 1] * p_trans[i, :])
 T1[:, j] /= np.sum(T1[:, j])
 return T1
def backward(self, p_trans, p_signal):

 T1 = np.zeros(p_signal.shape)
 T1[:, -1] = p_signal[:, -1]
T1[:, -1] /= np.sum(T1[:, -1])
 for j in range(p_signal.shape[1] - 2, -1, -1):
for i in range(len(p_trans)) :
T1[i, j] = np.sum(T1[:, j + 1] * p_trans[:, i] * p_signal[:, j + 1])
 T1[:, j] /= np.sum(T1[:, j])
 return T1
def posterior_decoding(self, p_trans, p_signal):
fwd = self.forward(p_trans, p_signal)
 bwd = self.backward(p_trans, p_signal)
 preds = np.empty(p_signal.shape[1], 'B')
 probs = np.zeros(( 11,fwd.shape[1]))
for i in range(p_signal.shape[1]):
preds[i] = np.argmax(fwd[:, i] * bwd[:, i])
 probs[:,i] = fwd[:, i] * bwd[:, i]
return preds,probs<categorify>
train_data=pd.read_csv('.. /input/train.csv')
 test_data=pd.read_csv('.. /input/test.csv' )
oof_probs = []
oof_predictions = []
for mini_batch in range(50):
if mini_batch in Transition_matrices:
signal = train_df[train_df.mini_batch==mini_batch].signal.values
open_channels = train_df[train_df.mini_batch==mini_batch].open_channels.values
viterbi = PosteriorDecoder().fit(mini_batch)
 viterbi_predictions,viterbi_probabilities = viterbi.predict(signal)
 print(mini_batch,accuracy_score(viterbi_predictions,open_channels))
train_df.loc[train_df.mini_batch==mini_batch,'prediction'] = viterbi_predictions
oof_probs.append(viterbi_probabilities)
 oof_predictions.append(viterbi_predictions)
 oof_probs = np.transpose(np.concatenate(oof_probs,axis=1))
oof_predictions = np.concatenate(oof_predictions)
 np.save('oof_probs.npy', oof_probs)
 np.save('oof_predictions.npy', oof_predictions )<create_dataframe>
train_data['Age']=train_data['Age'].fillna(train_data['Age'].mean())
 train_data=train_data.drop(['Cabin'],axis=1 )
print(f1_score(train_df.open_channels,train_df.prediction,average='macro'))
pd.DataFrame(confusion_matrix(train_df.open_channels,train_df.prediction))<categorify>
train_data['Embarked']=train_data['Embarked'].fillna(method='ffill' )
test_probs = []
test_predictions = []
for mini_batch in tqdm(range(50,70)) :
if mini_batch in Transition_matrices:
signal = test_df[test_df.mini_batch==mini_batch].signal.values
viterbi = PosteriorDecoder().fit(mini_batch)
 viterbi_predictions,viterbi_probabilities = viterbi.predict(signal)
 test_probs.append(viterbi_probabilities)
 test_predictions.append(viterbi_predictions)
 test_probs = np.transpose(np.concatenate(test_probs,axis=1))
np.save('test_probs.npy', test_probs)
 test_predictions = np.concatenate(test_predictions)
 np.save('test_predictions.npy', test_predictions )<save_to_csv>
train_data['Sex']=train_data['Sex'].apply(lambda x:1 if x=='male' else 0)
 train_data['Embarked']=train_data['Embarked'].apply(lambda x:1 if x=='S'else 2 if x=='C' else 3)
 train_data['Fare']=train_data['Fare'].apply(lambda x: x/513)
submission_df = pd.read_csv('/kaggle/input/liverpool-ion-switching/sample_submission.csv')
 submission_df['open_channels'] = test_predictions
submission_df.to_csv("submission.csv", float_format='%.4f', index=False)
 submission_df.open_channels.value_counts()<data_type_conversions>
X=train_data[['Pclass','Sex','Age','SibSp','Parch','Fare','Embarked']]
Y=train_data[['Survived']]
for oc in range(11):
print("Open Channels",oc)
 print(f1_score(( train_df.open_channels==oc ).astype(int),(train_df.prediction==oc ).astype(int)) )<load_from_csv>
model = Sequential()
model.add(Dense(12, input_dim=7, activation='relu'))
model.add(Dense(8, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
def read_data(inp1, inp2):
train = pd.read_csv(inp1 + 'train.csv', dtype={'time': np.float32, 'signal': np.float32, 'open_channels':np.int32})
 test = pd.read_csv(inp1 + 'test.csv', dtype={'time': np.float32, 'signal': np.float32})
 Y_train_proba = np.load(inp2 + "Y_train_proba.npy")
 Y_test_proba = np.load(inp2 + "Y_test_proba.npy")
 for i in range(11):
train[f"proba_{i}"] = Y_train_proba[:, i]
test[f"proba_{i}"] = Y_test_proba[:, i]
return train, test 
<import_modules>
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'] )
from sklearn.metrics import f1_score 
<load_from_disk>
model.fit(X, Y, epochs=75, batch_size=100 )
proba_tr, proba_te = read_data('/kaggle/input/liverpool-ion-switching/','/kaggle/input/ion-shifted-rfc-proba/' )<sort_values>
scores = model.evaluate(X, Y)
 print("
%s: %.2f%%" %(model.metrics_names[1], scores[1]*100))
proba_tr.sort_values('time', ignore_index = True, inplace = True)
 proba_tr_value = proba_tr[['proba_0', 'proba_1', 'proba_2','proba_3', 'proba_4', 'proba_5', 'proba_6', 'proba_7', 'proba_8','proba_9', 'proba_10']].values
proba_tr_pred = np.argmax(proba_tr_value, axis=-1 )<prepare_output>
test_data['Age']=test_data['Age'].fillna(test_data['Age'].mean())
 test_data['Fare']=test_data['Fare'].fillna(test_data['Fare'].mean())
 test_data['Fare']=test_data['Fare'].apply(lambda x: x/513)
 test_data=test_data.drop(['Cabin'],axis=1)
 test_data['Embarked']=test_data['Embarked'].fillna(method='ffill')
 test_data['Sex']=test_data['Sex'].apply(lambda x:1 if x=='male' else 0)
 test_data['Embarked']=test_data['Embarked'].apply(lambda x:1 if x=='S'else 2 if x=='C' else 3 )
proba_tr['pred'] = proba_tr_pred<define_search_space>
test_data.isnull().sum()
for i in [0,1],[2,6],[3,7],[5,8],[4,9]:
j1 = i[0] + 1
j2 = i[1] + 1
batch = j1; a = 500000*(batch-1); b = 500000*batch
batch = j2; c = 500000*(batch-1); d = 500000*batch
print(j1,j2)
 pred_temp = np.concatenate([proba_tr.pred.values[a:b], proba_tr.pred.values[c:d]] ).reshape(( -1,1))
real_temp = np.concatenate([proba_tr.open_channels.values[a:b], proba_tr.open_channels.values[c:d]] ).reshape(( -1,1))
print(i);print('f1 score =',f1_score(pred_temp,real_temp,average='macro'))<compute_train_metric>
X=test_data[['Pclass','Sex','Age','SibSp','Parch','Fare','Embarked']]
f1_score(proba_tr.pred,proba_tr.open_channels,average='macro' )<save_to_csv>
predict=model.predict(X )
proba_te.sort_values('time', ignore_index = True, inplace = True)
 temp = proba_te[['proba_0','proba_1','proba_2','proba_3','proba_4','proba_5','proba_6','proba_7','proba_8','proba_9','proba_10']].values
temp_pred = pd.DataFrame(np.argmax(temp, axis=-1))
temp_pred.columns = ['open_channels']
x = datetime.datetime.now()
x = x.strftime("%Y%m%d%H%M%S")
 test_ori = pd.read_csv('/kaggle/input/liverpool-ion-switching/' + 'test.csv')
 pd.concat([test_ori[['time']],temp_pred], axis = 1 ).to_csv('sub'+x+'.csv.gz', compression='gzip', index = False, float_format='%.4f')
<save_to_csv>
final_out=pd.DataFrame(columns=['PassengerId','Survived'] )
pd.concat([test_ori[['time']],temp_pred], axis = 1 ).to_csv('sub'+x+'.csv', index = False, float_format='%.4f' )<install_modules>
final_out['PassengerId']=test_data['PassengerId']
!pip install tensorflow-addons<set_options>
final_out['Survived']=list(predict )
sns.set()
for dirname, _, filenames in os.walk('/kaggle/input'):
for filename in filenames:
print(os.path.join(dirname, filename)) 
<load_from_csv>
final_out['Survived']=final_out['Survived'].fillna(method='ffill' )
train = pd.read_csv('/kaggle/input/data-without-drift/train_clean.csv')
 test = pd.read_csv('/kaggle/input/data-without-drift/test_clean.csv')
 NUM_CLASSES = train.open_channels.nunique()
SEQ_LENGTH = 1000
WIDTHS = np.power(2, np.arange(-4, 9), dtype=np.float32)
 train.shape, test.shape<normalization>
final_out['Survived']=final_out['Survived'].apply(lambda x:int(round(x[0])) )
def wavelet_transform(sig, wd):
widths = wd
wt = signal.cwt(sig.values, signal.ricker, widths)
 wt = wt.T
eps = np.max(wt)* 1e-2
s1 = np.log(np.abs(wt)+ eps)- np.log(eps)
 wt = s1 / np.max(s1)
 return wt<data_type_conversions>
final_out.reset_index()
final_out.to_csv('./final_predictions.csv',index=False )
<define_variables><EOS> 
final_out['Survived'].value_counts()