Kaggle competition: Titanic - Machine Learning from Disaster
y_train = np.vstack(( y_train, y_knowledge_train)).T
y_valid = np.vstack(( y_valid, y_knowledge_valid)).T
print(y_train.shape)
 y_train[0]<compute_test_metric>
df1 = pd.read_csv("/kaggle/input/titanic/train.csv")
 df1.head()
def knowledge_distillation_loss_withBE(y_true, y_pred, beta=0.1):
y_true, y_pred_teacher = y_true[: , :1], y_true[: , 1:]
y_pred, y_pred_stu = y_pred[: , :1], y_pred[: , 1:]
loss = beta*binary_crossentropy(y_true,y_pred)+(1-beta)*binary_crossentropy(y_pred_teacher, y_pred_stu)
 return loss<compute_test_metric>
df2 = pd.read_csv("/kaggle/input/titanic/test.csv")
 df2.head()
def auc_2(y_true, y_pred):
y_true = y_true[:, :1]
y_pred = y_pred[:, :1]
return tf.py_func(roc_auc_score,(y_true, y_pred), tf.double)
 def auc_3(y_true, y_pred):
y_true = y_true[:, :1]
y_pred = y_pred[:, 1:]
return tf.py_func(roc_auc_score,(y_true, y_pred), tf.double )<choose_model_class>
print(df1.isnull().sum())
 print("
****************
")
 print(df2.isnull().sum() )
model = create_model(( NUM_FEATURES,1), 2)
 model.compile(loss=knowledge_distillation_loss_withBE, optimizer='adam', metrics=[auc_2])
 checkpoint = ModelCheckpoint('student_model_BE.h5', monitor='val_auc_2', verbose=1,
save_best_only=True, mode='max', save_weights_only = True)
 reduceLROnPlat = ReduceLROnPlateau(monitor='val_auc_2', factor=0.5, patience=4,
verbose=1, mode='max', epsilon=0.0001)
 early = EarlyStopping(monitor="val_auc_2",
mode="max",
patience=9)
 callbacks_list = [checkpoint, reduceLROnPlat, early]
history = model.fit(x_train,y_train,
epochs=10,
batch_size = BATCH_SIZE,
validation_data=(x_valid, y_valid))<compute_test_metric>
df1['Age'].fillna(df1['Age'].median() ,inplace=True)
 df2['Age'].fillna(df2['Age'].median() ,inplace=True )
def knowledge_distillation_loss_withFL(y_true, y_pred, beta=0.1):
y_true, y_pred_teacher = y_true[: , :1], y_true[: , 1:]
y_pred, y_pred_stu = y_pred[: , :1], y_pred[: , 1:]
loss = beta*focal_loss(y_true,y_pred)+(1-beta)*binary_crossentropy(y_pred_teacher, y_pred_stu)
 return loss<choose_model_class>
df2['Fare'].fillna(df2['Fare'].median() ,inplace=True )
model = create_model(( NUM_FEATURES,1), 2)
 model.compile(loss=knowledge_distillation_loss_withFL, optimizer='adam', metrics=[auc_2, auc_3])
 checkpoint = ModelCheckpoint('student_model_FL.h5', monitor='val_auc_2', verbose=1,
save_best_only=True, mode='max', save_weights_only = True)
 reduceLROnPlat = ReduceLROnPlateau(monitor='val_auc_2', factor=0.5, patience=4,
verbose=1, mode='max', epsilon=0.0001)
 early = EarlyStopping(monitor="val_auc_2",
mode="max",
patience=9)
 callbacks_list = [checkpoint, reduceLROnPlat, early]
history = model.fit(x_train,y_train,
epochs=10,
batch_size = BATCH_SIZE,
callbacks=callbacks_list,
validation_data=(x_valid, y_valid))<split>
df1.drop(['PassengerId','Name','Ticket'],axis=1,inplace=True)
 df2.drop(['Name','Ticket'],axis=1,inplace=True )
def sigmoid(x, derivative=False):
return x*(1-x)if derivative else 1/(1+np.exp(-x))
TEMPERATURE = 2
y_knowledge_logit = logit(y_knowledge)
 y_temperature = sigmoid(y_knowledge_logit/TEMPERATURE)
 x_train, x_valid, y_train, y_valid, y_knowledge_train, y_knowledge_valid = train_test_split(train, y, y_temperature,
stratify=y, test_size=0.2, random_state=8)
<concatenate>
df1['family_members']=df1['Parch']+df1['SibSp']
df2['family_members']=df2['Parch']+df2['SibSp']
y_train = np.vstack(( y_train, y_knowledge_train)).T
y_valid = np.vstack(( y_valid, y_knowledge_valid)).T
print(y_train.shape)
 y_train[0]<choose_model_class>
df1.drop(['SibSp','Parch','Embarked'],axis=1,inplace=True)
 df2.drop(['SibSp','Parch','Embarked'],axis=1,inplace=True )
model = create_model(( NUM_FEATURES,1), 2)
 model.compile(loss=knowledge_distillation_loss_withFL, optimizer='adam', metrics=[auc_2,auc_3])
 checkpoint = ModelCheckpoint('student_model_FL.h5', monitor='val_auc_2', verbose=1,
save_best_only=True, mode='max', save_weights_only = True)
 reduceLROnPlat = ReduceLROnPlateau(monitor='val_auc_2', factor=0.5, patience=4,
verbose=1, mode='max', epsilon=0.0001)
 early = EarlyStopping(monitor="val_auc_2",
mode="max",
patience=9)
 callbacks_list = [checkpoint, reduceLROnPlat, early]
history = model.fit(x_train,y_train,
epochs=10,
batch_size = 1024,
callbacks=callbacks_list,
validation_data=(x_valid, y_valid))<prepare_x_and_y>
df1.groupby(df1['Cabin'].isnull())['Survived'].mean()
num_fold = 5
folds = list(StratifiedKFold(n_splits=num_fold, shuffle=True, random_state=7 ).split(train, y))
y_test_pred_log = np.zeros(len(train))
y_train_pred_log = np.zeros(len(train))
print(y_test_pred_log.shape)
 print(y_train_pred_log.shape)
 score = []
for j,(train_idx, valid_idx)in enumerate(folds):
print('
===================FOLD=',j)
 x_train, x_valid = train[train_idx], train[valid_idx]
y_train, y_valid = y[train_idx], y[valid_idx]
y_knowledge_train, y_knowledge_valid = y_temperature[train_idx], y_temperature[valid_idx]
y_train = np.vstack(( y_train, y_knowledge_train)).T
y_valid = np.vstack(( y_valid, y_knowledge_valid)).T
model = create_model(( NUM_FEATURES,1), 2)
 model.compile(loss=knowledge_distillation_loss_withFL, optimizer='adam', metrics=[auc_2, auc_3])
 checkpoint = ModelCheckpoint('student_model_FL.h5', monitor='val_auc_2', verbose=1,
save_best_only=True, mode='max', save_weights_only = True)
 reduceLROnPlat = ReduceLROnPlateau(monitor='val_auc_2', factor=0.5, patience=4,
verbose=1, mode='max', epsilon=0.0001)
 early = EarlyStopping(monitor="val_auc_2",
mode="max",
patience=9)
 callbacks_list = [checkpoint, reduceLROnPlat, early]
history = model.fit(x_train,y_train,
epochs=100,
batch_size = BATCH_SIZE,
callbacks=callbacks_list,
validation_data=(x_valid, y_valid))
model.load_weights('student_model_FL.h5')
 prediction = model.predict(x_valid,
batch_size=512,
verbose=1)
 score.append(roc_auc_score(y_valid[:,0], prediction[:,1]))
prediction = model.predict(test,
batch_size=512,
verbose=1)
 y_test_pred_log += np.squeeze(prediction[:, 1])
 prediction = model.predict(x_valid,
batch_size=512,
verbose=1)
 y_train_pred_log[valid_idx] += np.squeeze(prediction[:, 1])
 del x_train, x_valid, y_train, y_valid, y_knowledge_train, y_knowledge_valid
gc.collect() 
<compute_test_metric>
df1['Cabin_Alotted']=np.where(df1['Cabin'].isnull() ,0,1)
 df2['Cabin_Alotted']=np.where(df2['Cabin'].isnull() ,0,1 )
print("OOF score: ", roc_auc_score(y, y_train_pred_log/num_fold))
print("average {} folds score: ".format(num_fold), np.sum(score)/num_fold)
<save_to_csv>
df1.drop('Cabin',axis=1,inplace=True)
 df2.drop('Cabin',axis=1,inplace=True )
submit = pd.read_csv('.. /input/santander-customer-transaction-prediction/sample_submission.csv')
 submit['ID_code'] = id_code_test
submit['target'] = y_test_pred_log/num_fold
submit.to_csv('submission.csv', index=False)
 submit.head()<import_modules>
df1['Age']=np.where(df1['Age']<18,'child','Adult')
 df2['Age']=np.where(df2['Age']<18,'child','Adult' )
import numpy as np
import pandas as pd
import numba
from sympy import isprime, primerange
from math import sqrt
from sklearn.neighbors import KDTree
from tqdm import tqdm
from itertools import combinations, permutations
from functools import lru_cache<load_from_csv>
df1['Fare']=np.where(df1['Fare']<80,'low_cost','High_cost')
 df2['Fare']=np.where(df2['Fare']<80,'low_cost','High_cost' )
cities = pd.read_csv('.. /input/traveling-santa-2018-prime-paths/cities.csv', index_col=['CityId'])
 XY = np.stack(( cities.X.astype(np.float32), cities.Y.astype(np.float32)) , axis=1)
 is_not_prime = np.array([0 if isprime(i)else 1 for i in cities.index], dtype=np.int32 )<compute_test_metric>
y = df1["Survived"]
features = ["Pclass","Sex","Age","Fare","family_members","Cabin_Alotted"]
X = pd.get_dummies(df1[features])
 X_test = pd.get_dummies(df2[features])
 model = RandomForestClassifier(n_estimators=100, max_depth=5, random_state=1)
 model.fit(X, y)
 predictions = model.predict(X_test)
 output = pd.DataFrame({'PassengerId': df2.PassengerId, 'Survived': predictions})
 output.to_csv('my_submission.csv', index=False)
 print("Your submission was successfully saved!" )