Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<categorify>
import numpy as np
import pandas as pd
import tensorflow as tf
from tensorflow.keras.layers import Dense, Dropout, BatchNormalization
from tensorflow.keras.models import Sequential
import optuna
from optuna.samplers import TPESampler
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score, f1_score
def macro_f1(y_true, y_pred):
y_true = tf.reshape(y_true, [-1, NUM_CLASSES])
 y_pred = tf.reshape(y_pred, [-1, NUM_CLASSES])
 threshold = tf.reduce_max(y_pred, axis=-1, keepdims=True)
 y_pred = tf.logical_and(y_pred >= threshold,
tf.abs(y_pred)> 1e-12)
 y_true = tf.cast(y_true, tf.bool)
 y_pred = tf.cast(y_pred, tf.bool)
 tp = tf.logical_and(tf.equal(y_true, True), tf.equal(y_pred, True))
tp = tf.cast(tp, tf.float32)
 tp = tf.reduce_sum(tp, axis=0)
 fp = tf.logical_and(tf.equal(y_true, False), tf.equal(y_pred, True))
fp = tf.cast(fp, tf.float32)
 fp = tf.reduce_sum(fp, axis=0)
 fn = tf.logical_and(tf.equal(y_true, True), tf.equal(y_pred, False))
fn = tf.cast(fn, tf.float32)
 fn = tf.reduce_sum(fn, axis=0)
 precision = tf.math.divide_no_nan(tp, tf.math.add(tp, fp))
recall = tf.math.divide_no_nan(tp, tf.math.add(tp, fn))
f1 = tf.math.divide_no_nan(tf.math.multiply_no_nan(2.0, tf.math.multiply_no_nan(precision, recall)) , tf.math.add(precision, recall))
f1 = tf.reduce_mean(f1)
 return f1<define_search_model>
train = pd.read_csv('/kaggle/input/titanic/train.csv')
 test = pd.read_csv('/kaggle/input/titanic/test.csv' )
def gated_residual_block(nb_filters, use_bias, i, x):
res_x = layers.Conv1D(nb_filters, 1, strides=1, padding='same', use_bias=use_bias )(x)
 tanh_out = layers.Conv1D(nb_filters, 2, dilation_rate=2**i, padding='same',
use_bias=use_bias, activation='tanh' )(x)
 sigm_out = layers.Conv1D(nb_filters, 2, dilation_rate=2**i, padding='same',
use_bias=use_bias, activation='sigmoid' )(x)
 gated_out = layers.Multiply()([tanh_out, sigm_out])
 gated_out = layers.Conv1D(nb_filters, 1, strides=1, padding='same', use_bias=use_bias )(gated_out)
 res_x = layers.Add()([res_x, gated_out])
 return res_x<categorify>
train['LastName'] = train['Name'].str.split(',', expand=True)[0]
test['LastName'] = test['Name'].str.split(',', expand=True)[0]
ds = pd.concat([train, test])
 sur = []
died = []
for index, row in ds.iterrows() :
s = ds[(ds['LastName']==row['LastName'])&(ds['Survived']==1)]
d = ds[(ds['LastName']==row['LastName'])&(ds['Survived']==0)]
s=len(s)
 if row['Survived'] == 1:
s-=1
d=len(d)
 if row['Survived'] == 0:
d-=1
sur.append(s)
 died.append(d)
 ds['FamilySurvived'] = sur
ds['FamilyDied'] = died
ds['FamilySize'] = ds['SibSp'] + ds['Parch'] + 1
ds['IsAlone'] = 0
ds.loc[ds['FamilySize'] == 1, 'IsAlone'] = 1
ds['Fare'] = ds['Fare'].fillna(train['Fare'].median())
 ds['Embarked'] = ds['Embarked'].fillna('Q')
 train = ds[ds['Survived'].notnull() ]
test = ds[ds['Survived'].isnull() ]
test = test.drop(['Survived'], axis=1)
 train['rich_woman'] = 0
test['rich_woman'] = 0
train['men_3'] = 0
test['men_3'] = 0
train.loc[(train['Pclass']<=2)&(train['Sex']=='female'), 'rich_woman'] = 1
test.loc[(test['Pclass']<=2)&(test['Sex']=='female'), 'rich_woman'] = 1
train.loc[(train['Pclass']==3)&(train['Sex']=='male'), 'men_3'] = 1
test.loc[(test['Pclass']==3)&(test['Sex']=='male'), 'men_3'] = 1
train['rich_woman'] = train['rich_woman'].astype(np.int8)
 test['rich_woman'] = test['rich_woman'].astype(np.int8)
 train["Cabin"] = pd.Series([i[0] if not pd.isnull(i)else 'X' for i in train['Cabin']])
 test['Cabin'] = pd.Series([i[0] if not pd.isnull(i)else 'X' for i in test['Cabin']])
 train = train.drop(['PassengerId', 'Ticket', 'LastName', 'SibSp', 'Parch'], axis=1)
 test = test.drop(['PassengerId', 'Ticket', 'LastName', 'SibSp', 'Parch'], axis=1)
 categorical = ['Pclass', 'Sex', 'Embarked', 'Cabin']
for cat in categorical:
train = pd.concat([train, pd.get_dummies(train[cat], prefix=cat)], axis=1)
 train = train.drop([cat], axis=1)
 test = pd.concat([test, pd.get_dummies(test[cat], prefix=cat)], axis=1)
 test = test.drop([cat], axis=1)
 train = train.drop(['Sex_male', 'Name'], axis=1)
 test = test.drop(['Sex_male', 'Name'], axis=1)
 train = train.fillna(-1)
 test = test.fillna(-1)
 train.head()
def compute_receptive_field(dilation_depth, nb_stacks):
receptive_field = nb_stacks *(2**dilation_depth * 2)-(nb_stacks - 1)
 return receptive_field
compute_receptive_field(9, 1 )<find_best_params>
EPOCHS = 15
initial_keras_params = {
'layers_number': 1,
'n_units_l_0': 128,
'activation_l_0': 'relu',
'dropout_l_0': 0.5,
'lr': 0.001
}
best_model, best_r, best_score = wavenet(9, 1, 128, 0)
 print(f'Macro F1: {best_score}' )<save_to_csv>
def keras_classifier(parameters):
model = Sequential()
layers_number = int(parameters['layers_number'])
 for i in range(layers_number):
model.add(Dense(int(parameters['n_units_l_' + str(i)]), activation=parameters['activation_l_' + str(i)]))
model.add(Dropout(int(parameters['dropout_l_' + str(i)])))
 model.add(Dense(2, activation='softmax'))
model.compile(
 loss='categorical_crossentropy',
optimizer=tf.keras.optimizers.Adam(lr=float(parameters['lr'])) ,
metrics=['accuracy'] 
)
 return model
preds = best_model.predict(test[cols].values.reshape(-1,SEQ_LENGTH,len(cols)))
 preds = preds.reshape(-1, 11)
 preds = np.argmax(preds, axis=1)
 test['open_channels'] = preds
test[['time', 'open_channels']].to_csv('submission.csv', index=False, float_format='%.4f' )<load_pretrained>
model = keras_classifier(initial_keras_params )
archive_train=zipfile.ZipFile('/kaggle/input/whats-cooking/train.json.zip','r')
 archive_train<load_from_disk>
y = train['Survived']
y = tf.keras.utils.to_categorical(y, num_classes=2, dtype='float32')
 X = train.drop(['Survived', 'Cabin_T'], axis=1)
 X_test = test.copy()
X, X_val, y, y_val = train_test_split(X, y, random_state=0, test_size=0.2, shuffle=False )
train_data=pd.read_json(archive_train.read('train.json'))
train_data.head()<load_pretrained>
model.fit(X, y, validation_split=0.2, epochs=EPOCHS, batch_size=32 )
archive_test=zipfile.ZipFile('/kaggle/input/whats-cooking/test.json.zip','r')
 test_data=pd.read_json(archive_test.read('test.json'))
test_data.head()<count_values>
preds = model.predict(X_val)
 preds = np.argmax(preds, axis=1)
 print('accuracy: ', accuracy_score(np.argmax(y_val, axis=1), preds))
print('f1-score: ', f1_score(np.argmax(y_val, axis=1), preds))
train_data['cuisine'].value_counts()<define_variables>
def create_model(trial):
n_layers = trial.suggest_int("layers_number", 1, 2)
 model = Sequential()
for i in range(n_layers):
num_hidden = trial.suggest_int("n_units_l_{}".format(i), 2, 16)
 activation = trial.suggest_categorical('activation_l_{}'.format(i), ['relu', 'sigmoid', 'tanh', 'elu'])
 model.add(Dense(num_hidden, activation=activation))
dropout = trial.suggest_uniform("dropout_l_{}".format(i), 0.1, 0.4)
 model.add(Dropout(dropout))
model.add(Dense(2, activation='softmax'))
lr = trial.suggest_loguniform("lr", 1e-5, 1e-1)
 model.compile(
 loss='categorical_crossentropy',
optimizer=tf.keras.optimizers.Adam(lr=lr),
metrics=['accuracy'] 
)
 return model
train_ingredients_count={}
for i in range(len(train_data)) :
for j in train_data['ingredients'][i]:
if j in train_ingredients_count.keys() :
train_ingredients_count[j]+=1
else:
train_ingredients_count[j]=1<define_variables>
def objective(trial):
model = create_model(trial)
 epochs = trial.suggest_int("epochs", 3, 20)
 batch = trial.suggest_int("batch", 1, X.shape[0] / 4)
 model.fit(
 X,
y,
batch_size=batch,
epochs=epochs,
verbose=0 
)
 preds = model.predict(X_val)
 return accuracy_score(np.argmax(y_val, axis=1), np.argmax(preds, axis=1))
test_ingredients_count={}
for i in range(len(test_data)) :
for j in test_data['ingredients'][i]:
if j in test_ingredients_count.keys() :
test_ingredients_count[j]+=1
else:
test_ingredients_count[j]=1<define_variables>
def optimize() :
sampler = TPESampler(seed=666)
 study = optuna.create_study(direction="maximize", sampler=sampler)
 study.optimize(objective, n_trials=80)
 return study.best_params
ingredients_missing_train=[]
for i in test_ingredients_count.keys() :
if i not in train_ingredients_count.keys() :
ingredients_missing_train.append(i)
 train_ingredients_count[i]=0
print(len(ingredients_missing_train))<define_variables>
params = optimize()
ingredients_missing_test=[]
for i in train_ingredients_count.keys() :
if i not in test_ingredients_count.keys() :
ingredients_missing_test.append(i)
 test_ingredients_count[i]=0
print(len(ingredients_missing_test))<feature_engineering>
epochs = params['epochs']
batch = params['batch']
del params['epochs']
del params['batch']
opt_model = keras_classifier(params)
 opt_model.fit(X, y, validation_split=0.2, epochs=epochs, batch_size=batch )
for i in train_ingredients_count.keys() :
train_data[i]=np.zeros(len(train_data))<feature_engineering>
preds = opt_model.predict(X_val)
 preds = np.argmax(preds, axis=1)
 print('accuracy: ', accuracy_score(np.argmax(y_val, axis=1), preds))
print('f1-score: ', f1_score(np.argmax(y_val, axis=1), preds))
for i in test_ingredients_count.keys() :
test_data[i]=np.zeros(len(test_data))<filter>
preds = opt_model.predict(X_test)
 preds = np.argmax(preds, axis=1)
 preds = preds.astype(np.int16 )
<filter><EOS> 
submission = pd.read_csv('.. /input/titanic/gender_submission.csv')
 submission['Survived'] = preds
submission.to_csv('submission.csv', index=False )