Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<choose_model_class>
warnings.filterwarnings('ignore')
 pd.options.display.max_columns = 40
kappa = KappaScore()
kappa.weights = "quadratic"<choose_model_class>
train_data = pd.read_csv("/kaggle/input/titanic/train.csv")
 test_data = pd.read_csv("/kaggle/input/titanic/test.csv" )
learn = cnn_learner(data, models.resnet50, metrics=[error_rate, kappa] )<train_model>
full_dataset = pd.concat([train_data, test_data])
 full_dataset.head()
learn.fit_one_cycle(1 )<train_model>
class KnownCabinTransformer(BaseEstimator, TransformerMixin):

 def __init__(self):
print("- KnownCabin transformer initiated -")
 def fit(self, X, y=None):
return self
def transform(self, X, y=None):
X.Cabin.fillna(0, inplace=True)
 X["KnownCabin"] = 0
X.loc[X.Cabin != 0, 'KnownCabin'] = 1
print("- KnownCabin transformer applied -")
 return X
learn.fit_one_cycle(1)
<train_model>
class TitleTransformer(BaseEstimator, TransformerMixin):

 def __init__(self):
print("- Title transformer initiated -")
 def fit(self, X, y=None):
return self
def transform(self, X, y=None):
X['Title'] = X.Name.str.extract('([A-Za-z]+)\.', expand=False)
 X['Title'] = X['Title'].replace(['Lady', 'Countess','Capt', 'Col',
'Don', 'Dr', 'Major', 'Rev',
'Sir', 'Jonkheer', 'Dona'],
'Rare')
 X['Title'] = X['Title'].replace(['Mlle', 'Ms'], 'Miss')
 X['Title'] = X['Title'].replace('Mme', 'Mrs')
 X['Title'] = X['Title'].fillna('Rare')
 title_dummies = pd.get_dummies(X['Title'], prefix="Title")
 X = pd.concat([X, title_dummies], axis=1)
 X = X.drop('Title', axis=1)
 print("- Title transformer applied -")
 return X
learn.unfreeze()
learn.fit_one_cycle(9, max_lr=slice(1e-4,1e-3))<define_variables>
class MissingFareTransformer(BaseEstimator, TransformerMixin):

 def __init__(self):
print("- MissingFare transformer initiated -")
 def fit(self, X, y=None):
return self
def transform(self, X, y=None):
X.loc[(X.Pclass == 1)&(X.Fare.isnull()), 'Fare'] = X.loc[X.Pclass == 1]["Fare"].median()
X.loc[(X.Pclass == 2)&(X.Fare.isnull()), 'Fare'] = X.loc[X.Pclass == 2]["Fare"].median()
X.loc[(X.Pclass == 3)&(X.Fare.isnull()), 'Fare'] = X.loc[X.Pclass == 3]["Fare"].median()
print("- MissingFare transformer applied -")
 return X
class EmbarkedTransformer(BaseEstimator, TransformerMixin):

 def __init__(self):
print("- MissingEmbarked transformer initiated -")
 def fit(self, X, y=None):
return self
def transform(self, X, y=None):
self.most_frequent = X["Embarked"].value_counts().index[0]
X["Embarked"].fillna(self.most_frequent, inplace=True)
 embarked_dummies = pd.get_dummies(X['Embarked'], prefix="Embarked")
 X = pd.concat([X, embarked_dummies], axis=1)
 X = X.drop('Embarked', axis=1)
 print("- MissingEmbarked transformer applied -")
 return X
class HypotheticalMissingsTransformer(BaseEstimator, TransformerMixin):

 def __init__(self):
print("- HypotheticalMissings transformer initiated -")
 def fit(self, X, y=None):
return self
def transform(self, X, y=None):
self.most_frequent_class = X["Pclass"].value_counts().index[0]
self.unknown_name = "unknown"
self.horizontal_family = 0
self.vertical_family = 0
X["Pclass"].fillna(self.most_frequent_class, inplace=True)
 X["Name"].fillna(self.unknown_name, inplace=True)
 X["SibSp"].fillna(self.horizontal_family, inplace=True)
 X["Parch"].fillna(self.vertical_family, inplace=True)
 X.loc[(X.Sex.isnull())&(X.Title_Master == 1), "Sex"] == "male"
X.loc[(X.Sex.isnull())&(X.Title_Mr == 1), "Sex"] == "male"
X.loc[(X.Sex.isnull())&(X.Title_Rare == 1), "Sex"] == "male"
X.loc[(X.Sex.isnull())&(X.Title_Miss == 1), "Sex"] == "female"
X.loc[(X.Sex.isnull())&(X.Title_Mrs == 1), "Sex"] == "female"
print("- HypotheticalMissings transformer applied -")
 return X
class GenderTransformer(BaseEstimator, TransformerMixin):

 def __init__(self):
print("- Gender transformer initiated -")
 def fit(self, X, y=None):
return self
def transform(self, X, y=None):
X['Sex'] = X['Sex'].map({'male': 0, 'female': 1})
 print("- Gender transformer applied -")
 return X
class PclassTransformer(BaseEstimator, TransformerMixin):

 def __init__(self):
print("- Pclass transformer initiated -")
 def fit(self, X, y=None):
return self
def transform(self, X, y=None):
pclass_dummies = pd.get_dummies(X['Pclass'], prefix='Pclass')
 X = pd.concat([X, pclass_dummies], axis=1)
 X = X.drop('Pclass', axis=1)
 print("- Pclass transformer applied -")
 return X
class FamilySizeTransformer(BaseEstimator, TransformerMixin):

 def __init__(self):
print("- FamilySize transformer initiated -")
 def fit(self, X, y=None):
return self
def transform(self, X, y=None):
X["FamilySize"] = X["SibSp"] + X["Parch"] + 1
print("- FamilySize transformer applied -")
 return X
learn.model_dir = Path('/kaggle/working/' )<save_model>
class AgeStatusTransformer(BaseEstimator, TransformerMixin):

 def __init__(self):
print("- AgeStatus transformer initiated -")
 def fit(self, X, y=None):
return self
def transform(self, X, y=None):
X["AgeStatus"] = "known"
X.loc[(X["Age"] > 1)&(( X["Age"]*2)%2 != 0), "AgeStatus"] = "estimated"
X.loc[X["Age"].isnull() , "AgeStatus"] = "guessed"
age_status_dummies = pd.get_dummies(X['AgeStatus'], prefix="AgeStatus")
 X = pd.concat([X, age_status_dummies], axis=1)
 X = X.drop('AgeStatus', axis=1)
 print("- AgeStatus transformer applied -")
 return X
learn.save('/kaggle/working/FastAI_APTOS_epoch_11')
 learn.export('/kaggle/working/FastAI_APTOS_epoch_11.pkl' )<load_from_csv>
class TicketGroupingTransformer(BaseEstimator, TransformerMixin):

 def __init__(self):
print("- TicketGrouping transformer initiated -")
 def fit(self, X, y=None):
return self
def transform(self, X, y=None):
cat_feature = ['Ticket']
count_enc = ce.CountEncoder(cols=cat_feature)
 count_enc.fit(X[cat_feature])
 grouping_col = count_enc.transform(X[cat_feature] ).add_suffix("_grouping")
 X = pd.concat([X, grouping_col], axis=1)
 print("- TicketGrouping transformer applied -")
 return X
test_df = pd.read_csv(dat_path/'sample_submission.csv' )<create_dataframe>
class AgeGuessingTransformer(BaseEstimator, TransformerMixin):

 def __init__(self):
print("- AgeGuessing transformer initiated -")
 def fit(self, X, y=None):
return self
def transform(self, X, y=None):
X.loc[(X.Age.isnull() == True)&(X.Title_Master == 1), "Age"] = X.loc[X.Title_Master == 1]["Age"].median()
X.loc[(X.Age.isnull() == True)&(X.SibSp > 0)&(X.Title_Miss == 1), "Age"] = X.loc[(X.Title_Miss == 1)&(X.SibSp > 0)]["Age"].median()
X.loc[(X.Age.isnull() == True)&(X.Ticket_grouping == 1), "Age"] = X.loc[(X.Ticket_grouping == 1)]["Age"].median()
X.loc[(X.Age.isnull() == True)&(X.SibSp > 1), "Age"] = X.loc[(X.Age.isnull() == False)&(X.SibSp > 1)]["Age"].median()
X.loc[(X.Age.isnull()), "Age"] = X.loc[(X.Age.isnull() == False)&(X.Ticket_grouping > 1)]["Age"].median()
print("- AgeGuessing transformer applied -")
 return X
learn.data.add_test(ImageList.from_df(test_df,dat_path,folder='test_images',suffix='.png'))<init_hyperparams>
class AgeBinningTransformer(BaseEstimator, TransformerMixin):

 def __init__(self):
print("- AgeBinning transformer initiated -")
 def fit(self, X, y=None):
return self
def transform(self, X, y=None):
self.bins = [0, 2, 16, 25, 40, 50, np.inf]
self.age_cat = ["0_2", "2_16", "16_25", "25_40", "30_50", "50+"]
X["AgeBins"] = pd.cut(X["Age"], bins=self.bins, labels=self.age_cat)
 age_bins_dummies = pd.get_dummies(X['AgeBins'], prefix="AgeBins")
 X = pd.concat([X, age_bins_dummies], axis=1)
 print("- AgeBinning transformer applied -")
 return X
tta_params = {'beta':0.12, 'scale':1.0}<predict_on_test>
custom_pipeline = Pipeline([ 
("cabin_trans", KnownCabinTransformer()), 
("title_trans", TitleTransformer()), 
("hypo_missing_trans", HypotheticalMissingsTransformer()), 
("missing_fare_trans", MissingFareTransformer()), 
("embarked_trans", EmbarkedTransformer()), 
("gender_trans", GenderTransformer()), 
("class_trans", PclassTransformer()), 
("family_size_trans", FamilySizeTransformer()), 
("ticket_grp_trans", TicketGroupingTransformer()), 
("age_status_trans", AgeStatusTransformer()), 
("age_guessing_trans", AgeGuessingTransformer()), 
("age_binning_trans", AgeBinningTransformer()),
])
 full_dataset = custom_pipeline.fit_transform(full_dataset )
preds,y = learn.get_preds(DatasetType.Test)
 preds,y = learn.TTA(ds_type=DatasetType.Test, **tta_params )<prepare_output>
full_dataset["TS_Tragedy"] = "null"
i = 0
unique_tickets_subset = full_dataset.loc[full_dataset["Ticket_grouping"] > 2]["Ticket"].unique()
while i < len(unique_tickets_subset):
subset = full_dataset.loc[full_dataset.Ticket == unique_tickets_subset[i]]
try:
ratio = len(subset.loc[subset["Survived"] == 0])/ len(subset.loc[subset.Survived.isnull() == False])
 if ratio > 0.65 :
full_dataset.loc[full_dataset.Ticket == unique_tickets_subset[i], "TS_Tragedy"] = "yes"
else:
full_dataset.loc[full_dataset.Ticket == unique_tickets_subset[i], "TS_Tragedy"] = "no"
i += 1
except ZeroDivisionError:
i += 1
TST_dummies = pd.get_dummies(full_dataset['TS_Tragedy'], prefix="TST")
 full_dataset = pd.concat([full_dataset, TST_dummies], axis=1)
 full_dataset = full_dataset.drop('TS_Tragedy', axis=1)
 full_dataset.head(3)
test_df.diagnosis = preds.argmax(1)
 test_df.head()<save_to_csv>
full_dataset.set_index(full_dataset.PassengerId, verify_integrity = True, inplace=True)
 full_dataset.tail()
test_df.to_csv('submission.csv',index=False )<import_modules>
X_train = full_dataset.loc[full_dataset.Survived.isnull() == False]
y_train = X_train["Survived"].astype(int)
 X_test = full_dataset.loc[full_dataset.Survived.isnull() ]
useless_features = ["PassengerId", "Survived", "Name", "Age", "Ticket", "Fare", "Cabin", "AgeBins"]
X_train.drop(useless_features, axis=1, inplace=True)
 X_test.drop(useless_features, axis=1, inplace=True )
from random import randint
import tensorflow as tf
import json
import os
from PIL import Image
from glob import glob
from zipfile import ZipFile
import pandas as pd
from keras.optimizers import Adam
from keras.preprocessing.image import ImageDataGenerator
from keras.callbacks import ModelCheckpoint, Callback, EarlyStopping
from keras.models import Model, Input, load_model
from keras.layers import Dense, GlobalAveragePooling2D, Dropout
from keras.applications.nasnet import NASNetLarge
from keras import backend as K
from matplotlib import pyplot as plt
from matplotlib.image import imread
import os
import sys
import cv2
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.metrics import cohen_kappa_score, accuracy_score
import scipy
from tqdm import tqdm
from skimage.io import imsave<create_dataframe>
scaler = StandardScaler()
X_train_scaled = pd.DataFrame(scaler.fit_transform(X_train[["SibSp","Parch","FamilySize","Ticket_grouping"]]), columns=["sc_SibSp","sc_Parch","sc_FamilySize","sc_Ticket_grouping"], index=X_train.index)
 X_test_scaled = pd.DataFrame(scaler.fit_transform(X_test[["SibSp","Parch","FamilySize","Ticket_grouping"]]), columns=["sc_SibSp","sc_Parch","sc_FamilySize","sc_Ticket_grouping"], index=X_test.index)
 X_train = pd.concat([X_train, X_train_scaled], axis=1)
 X_train.drop(["SibSp","Parch","FamilySize","Ticket_grouping"], axis=1, inplace=True)
 X_test = pd.concat([X_test, X_test_scaled], axis=1)
 X_test.drop(["SibSp","Parch","FamilySize","Ticket_grouping"], axis=1, inplace=True)
 X_train.head(3 )
def process_csv(dataframe: pd.DataFrame, image_column_name: str,
label_column_name: str,
folder_with_images: str)-> pd.DataFrame:

 dataframe[image_column_name] = dataframe[image_column_name].apply(
 lambda x: f"{folder_with_images}{x}.png")
 dataframe[label_column_name] = dataframe[label_column_name].astype('str')
 return dataframe<choose_model_class>
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.svm import SVC
import xgboost as xgb
import lightgbm as lgb
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import VotingClassifier
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import cross_val_predict
from sklearn.metrics import confusion_matrix
from sklearn.model_selection import GridSearchCV
train_datagen = ImageDataGenerator(rescale=1./ 255,
rotation_range=15,
width_shift_range=0.1,
height_shift_range=0.1,
shear_range=0.01,
zoom_range=[0.9, 1.25],
horizontal_flip=True,
vertical_flip=True,
fill_mode='reflect',
data_format='channels_last',
brightness_range=[0.5, 1.5],
validation_split=0.3 )<load_from_csv>
param_grid = [
{'n_neighbors':[2,3,4,5,6,7,8], 'weights':['uniform', 'distance'], 'n_jobs':[-1] }
]
neigh = KNeighborsClassifier()
grid_search = GridSearchCV(neigh, param_grid, cv=5, scoring='accuracy')
 grid_search.fit(X_train, y_train)
 grid_search.best_params_
train_csv = pd.read_csv("/kaggle/input/aptos2019-blindness-detection/train.csv")
 train_csv = process_csv(
 dataframe=train_csv,
image_column_name="id_code",
label_column_name="diagnosis",
folder_with_images="/kaggle/dataset_with_ben/" )<create_dataframe>
neigh = KNeighborsClassifier(n_neighbors = 5, weights = 'uniform')
 neigh.fit(X_train, y_train)
 scores = cross_val_score(neigh, X_train, y_train, scoring="accuracy", cv=10)
 print(scores.mean() )
train_generator = train_datagen.flow_from_dataframe(
 dataframe=train_csv, x_col="id_code", y_col="diagnosis", subset="training",
batch_size=32, target_size=(299, 299))
val_generator = train_datagen.flow_from_dataframe(
 dataframe=train_csv, x_col="id_code", y_col="diagnosis",
subset="validation", batch_size=32, target_size=(299, 299))<init_hyperparams>

 log_reg = LogisticRegression(C = 0.001, penalty='none')
 log_reg.fit(X_train, y_train)
 scores = cross_val_score(log_reg, X_train, y_train, scoring="accuracy", cv=10)
 print("Logistic Regression mean score : ", scores.mean())

forest_clf = RandomForestClassifier(max_depth = 6, min_samples_leaf=1, min_samples_split=2, n_estimators=100)
 forest_clf.fit(X_train, y_train)
 scores = cross_val_score(forest_clf, X_train, y_train, scoring="accuracy", cv=10)
 print("Random Forest mean score : ", scores.mean())

svc = SVC(C = 10, kernel='rbf', gamma='auto', probability=True)
 svc.fit(X_train, y_train)
 scores = cross_val_score(svc, X_train, y_train, scoring="accuracy", cv=10)
 print("SVC mean score: ", scores.mean())
 gbc = GradientBoostingClassifier(n_estimators=500, max_depth=3, learning_rate=0.01)
 gbc.fit(X_train, y_train)
 scores = cross_val_score(gbc, X_train, y_train, scoring="accuracy", cv=10)
 print("Gradient Boosting mean score : ", scores.mean())
 xgb_clf = xgb.XGBClassifier(n_estimators=500, max_depth=3, learning_rate=0.01)
 xgb_clf.fit(X_train, y_train)
 scores = cross_val_score(xgb_clf, X_train, y_train, scoring="accuracy", cv=10)
 print("XGB mean score : ", scores.mean())
class RAdam(Optimizer):
def __init__(self, lr, beta1=0.9, beta2=0.99, decay=0, **kwargs):
super(RAdam, self ).__init__(**kwargs)
 with K.name_scope(self.__class__.__name__):
self.lr = K.variable(lr)
 self._beta1 = K.variable(beta1, dtype="float32")
 self._beta2 = K.variable(beta2, dtype="float32")
 self._max_sma_length = 2 /(1 - self._beta2)
 self._iterations = K.variable(0)
 self._decay = K.variable(decay)
 def get_updates(self, loss, params):
grads = self.get_gradients(loss, params)
 self.updates = [K.update_add(self._iterations, 1)]
first_moments = [K.zeros(K.int_shape(p), dtype=K.dtype(p))
for(i, p)in enumerate(params)]
second_moments = [K.zeros(K.int_shape(p), dtype=K.dtype(p))
for(i, p)in enumerate(params)]
self.weights = [self._iterations] + first_moments + second_moments
bias_corrected_beta1 = K.pow(self._beta1, self._iterations)
 bias_corrected_beta2 = K.pow(self._beta2, self._iterations)
 for i,(curr_params, curr_grads)in enumerate(zip(params, grads)) :
new_first_moment = self._beta1 * first_moments[i] +(
 1 - self._beta1)* curr_grads
new_second_moment = self._beta2 * second_moments[i] +(
 1 - self._beta2)* K.square(curr_grads)
 self.updates.append(K.update(first_moments[i],
new_first_moment))
self.updates.append(K.update(second_moments[i],
new_second_moment))
bias_corrected_moving_average = new_first_moment /(
 1 - bias_corrected_beta1)
 sma_length = self._max_sma_length - 2 *(
 self._iterations * bias_corrected_beta2)/(
 1 - bias_corrected_beta2)
 variance_rectification_term = K.sqrt(
 self._max_sma_length *(sma_length - 4)*(sma_length - 2)/(
 sma_length *(self._max_sma_length - 4)* 
(self._max_sma_length - 2)+ K.epsilon()))
resulting_parameters = K.switch(
 sma_length > 5, variance_rectification_term *
bias_corrected_moving_average / K.sqrt(
 K.epsilon() + new_second_moment /(1 -
bias_corrected_beta2)) ,
bias_corrected_moving_average)
 resulting_parameters = curr_params - self.lr * resulting_parameters
self.updates.append(K.update(curr_params, resulting_parameters))
if self._decay != 0:
new_lr = self.lr *(1./(1.+ self._decay * K.cast(
 self._iterations, K.dtype(self._decay))))
self.updates.append(K.update(self.lr, new_lr))
return self.updates
def get_config(self):
config = {
"lr": float(K.get_value(self.lr)) ,
"beta1": float(K.get_value(self._beta1)) ,
"beta2": float(K.get_value(self._beta2)) ,
}
base_config = super(RAdam, self ).get_config()
return dict(list(base_config.items())+ list(config.items()))<choose_model_class>
voting_clf = VotingClassifier(estimators=[('gbc', gbc),('xgb', xgb_clf),('forest', forest_clf),('svc', svc),('lrc', log_reg),('knc', neigh)],
voting='hard')
 voting_clf.fit(X_train, y_train)
 scores = cross_val_score(voting_clf, X_train, y_train, scoring="accuracy", cv=10)
 print(scores.mean() )
sys.path.append(os.path.abspath('.. /input/kerasefficientnetsmaster/keras-efficientnets-master/keras-efficientnets-master/'))
def create_model() :
input_tensor = Input(( 299, 299, 3))
outputs = []
effnet = EfficientNetB7(input_shape=(299,299,3),
weights=sys.path.append(os.path.abspath('/kaggle/input/efficientnetb0b7-keras-weights/efficientnet-b7_weights_tf_dim_ordering_tf_kernels_autoaugment_notop.h5')) ,
include_top=False)
 xception_model = Xception(weights=None,include_top=False,input_shape=(299,299,3))
xception_model.load_weights("/kaggle/input/xception/xception_weights_tf_dim_ordering_tf_kernels_notop.h5")
 pretrained_models = [
effnet,xception_model
]
for i, model in enumerate(pretrained_models):
curr_output = model(input_tensor)
 curr_output = GlobalAveragePooling2D()(curr_output)
 curr_output = Dense(1024, activation="relu" )(curr_output)
 outputs.append(curr_output)
 output_tensor = Average()(outputs)
 output_tensor = Dense(5, activation="softmax" )(output_tensor)
 model = Model(input_tensor, output_tensor)
 return model<define_variables>
preds = voting_clf.predict(X_test)
 output = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': preds})
 output.to_csv("submission.csv", index=False)
 output.head()
start_lr = 1e-10
end_lr = 1<compute_train_metric>
warnings.filterwarnings('ignore')
 pd.options.display.max_columns = 40
def kappa_loss(y_pred, y_true, y_pow=2, eps=1e-10, N=5, bsize=256, name='kappa'):

 with tf.name_scope(name):
y_true = tf.to_float(y_true)
 repeat_op = tf.to_float(tf.tile(tf.reshape(tf.range(0, N), [N, 1]), [1, N]))
repeat_op_sq = tf.square(( repeat_op - tf.transpose(repeat_op)))
 weights = repeat_op_sq / tf.to_float(( N - 1)** 2)
 pred_ = y_pred ** y_pow
try:
pred_norm = pred_ /(eps + tf.reshape(tf.reduce_sum(pred_, 1), [-1, 1]))
except Exception:
pred_norm = pred_ /(eps + tf.reshape(tf.reduce_sum(pred_, 1), [bsize, 1]))
hist_rater_a = tf.reduce_sum(pred_norm, 0)
 hist_rater_b = tf.reduce_sum(y_true, 0)
 conf_mat = tf.matmul(tf.transpose(pred_norm), y_true)
 nom = tf.reduce_sum(weights * conf_mat)
 denom = tf.reduce_sum(weights * tf.matmul(
 tf.reshape(hist_rater_a, [N, 1]), tf.reshape(hist_rater_b, [1, N])) /
tf.to_float(bsize))
return nom /(denom + eps )<choose_model_class>
train_data = pd.read_csv("/kaggle/input/titanic/train.csv")
 test_data = pd.read_csv("/kaggle/input/titanic/test.csv" )
reduce_lr = ReduceLROnPlateau(monitor='val_acc', factor=0.2,
patience=5, min_lr=1e-5 )<choose_model_class>
full_dataset = pd.concat([train_data, test_data])
 full_dataset.head()
callbacks = [
ModelCheckpoint(
 "best_weights.hdf5",
monitor='val_acc',
verbose=1, save_best_only=True,
save_weights_only=True),
EarlyStopping(monitor='val_acc', patience=5),
reduce_lr
]<train_model>
class KnownCabinTransformer(BaseEstimator, TransformerMixin):

 def __init__(self):
print("- KnownCabin transformer initiated -")
 def fit(self, X, y=None):
return self
def transform(self, X, y=None):
X.Cabin.fillna(0, inplace=True)
 X["KnownCabin"] = 0
X.loc[X.Cabin != 0, 'KnownCabin'] = 1
print("- KnownCabin transformer applied -")
 return X
model = create_model()
model.compile(optimizer=Adam(1e-4),
loss="categorical_crossentropy", metrics=["accuracy"])
 model.fit_generator(generator=train_generator,
steps_per_epoch=len(train_generator),
validation_data=val_generator,
validation_steps=len(val_generator),
epochs=10,
callbacks=callbacks )<normalization>
class TitleTransformer(BaseEstimator, TransformerMixin):

 def __init__(self):
print("- Title transformer initiated -")
 def fit(self, X, y=None):
return self
def transform(self, X, y=None):
X['Title'] = X.Name.str.extract('([A-Za-z]+)\.', expand=False)
 X['Title'] = X['Title'].replace(['Lady', 'Countess','Capt', 'Col',
'Don', 'Dr', 'Major', 'Rev',
'Sir', 'Jonkheer', 'Dona'],
'Rare')
 X['Title'] = X['Title'].replace(['Mlle', 'Ms'], 'Miss')
 X['Title'] = X['Title'].replace('Mme', 'Mrs')
 X['Title'] = X['Title'].fillna('Rare')
 title_dummies = pd.get_dummies(X['Title'], prefix="Title")
 X = pd.concat([X, title_dummies], axis=1)
 X = X.drop('Title', axis=1)
 print("- Title transformer applied -")
 return X
def test_time_augmentation(image, network_model):
datagen = ImageDataGenerator()
all_images = np.expand_dims(image, axis=0)
 flip_horizontal_image = np.expand_dims(datagen.apply_transform(
 x=image, transform_parameters={"flip_horizontal": True}), axis=0)
 all_images = np.append(all_images, flip_horizontal_image, axis=0)
 flip_vertical_image = np.expand_dims(datagen.apply_transform(
 x=image, transform_parameters={"flip_vertical": True}), axis=0)
 all_images = np.append(all_images, flip_vertical_image, axis=0)
 rotated_image = np.expand_dims(datagen.apply_transform(
 x=image, transform_parameters={"theta": randint(0, 15)}), axis=0)
 all_images = np.append(all_images, rotated_image, axis=0)
 prediction = int(np.argmax(np.mean(network_model.predict(all_images), axis=0)))
 return prediction<load_pretrained>
class MissingFareTransformer(BaseEstimator, TransformerMixin):

 def __init__(self):
print("- MissingFare transformer initiated -")
 def fit(self, X, y=None):
return self
def transform(self, X, y=None):
X.loc[(X.Pclass == 1)&(X.Fare.isnull()), 'Fare'] = X.loc[X.Pclass == 1]["Fare"].median()
X.loc[(X.Pclass == 2)&(X.Fare.isnull()), 'Fare'] = X.loc[X.Pclass == 2]["Fare"].median()
X.loc[(X.Pclass == 3)&(X.Fare.isnull()), 'Fare'] = X.loc[X.Pclass == 3]["Fare"].median()
print("- MissingFare transformer applied -")
 return X
class EmbarkedTransformer(BaseEstimator, TransformerMixin):

 def __init__(self):
print("- MissingEmbarked transformer initiated -")
 def fit(self, X, y=None):
return self
def transform(self, X, y=None):
self.most_frequent = X["Embarked"].value_counts().index[0]
X["Embarked"].fillna(self.most_frequent, inplace=True)
 embarked_dummies = pd.get_dummies(X['Embarked'], prefix="Embarked")
 X = pd.concat([X, embarked_dummies], axis=1)
 X = X.drop('Embarked', axis=1)
 print("- MissingEmbarked transformer applied -")
 return X
class HypotheticalMissingsTransformer(BaseEstimator, TransformerMixin):

 def __init__(self):
print("- HypotheticalMissings transformer initiated -")
 def fit(self, X, y=None):
return self
def transform(self, X, y=None):
self.most_frequent_class = X["Pclass"].value_counts().index[0]
self.unknown_name = "unknown"
self.horizontal_family = 0
self.vertical_family = 0
X["Pclass"].fillna(self.most_frequent_class, inplace=True)
 X["Name"].fillna(self.unknown_name, inplace=True)
 X["SibSp"].fillna(self.horizontal_family, inplace=True)
 X["Parch"].fillna(self.vertical_family, inplace=True)
 X.loc[(X.Sex.isnull())&(X.Title_Master == 1), "Sex"] == "male"
X.loc[(X.Sex.isnull())&(X.Title_Mr == 1), "Sex"] == "male"
X.loc[(X.Sex.isnull())&(X.Title_Rare == 1), "Sex"] == "male"
X.loc[(X.Sex.isnull())&(X.Title_Miss == 1), "Sex"] == "female"
X.loc[(X.Sex.isnull())&(X.Title_Mrs == 1), "Sex"] == "female"
print("- HypotheticalMissings transformer applied -")
 return X
class GenderTransformer(BaseEstimator, TransformerMixin):

 def __init__(self):
print("- Gender transformer initiated -")
 def fit(self, X, y=None):
return self
def transform(self, X, y=None):
X['Sex'] = X['Sex'].map({'male': 0, 'female': 1})
 print("- Gender transformer applied -")
 return X
class PclassTransformer(BaseEstimator, TransformerMixin):

 def __init__(self):
print("- Pclass transformer initiated -")
 def fit(self, X, y=None):
return self
def transform(self, X, y=None):
pclass_dummies = pd.get_dummies(X['Pclass'], prefix='Pclass')
 X = pd.concat([X, pclass_dummies], axis=1)
 X = X.drop('Pclass', axis=1)
 print("- Pclass transformer applied -")
 return X
class FamilySizeTransformer(BaseEstimator, TransformerMixin):

 def __init__(self):
print("- FamilySize transformer initiated -")
 def fit(self, X, y=None):
return self
def transform(self, X, y=None):
X["FamilySize"] = X["SibSp"] + X["Parch"] + 1
print("- FamilySize transformer applied -")
 return X
model.load_weights("best_weights.hdf5" )<save_to_csv>
class AgeStatusTransformer(BaseEstimator, TransformerMixin):

 def __init__(self):
print("- AgeStatus transformer initiated -")
 def fit(self, X, y=None):
return self
def transform(self, X, y=None):
X["AgeStatus"] = "known"
X.loc[(X["Age"] > 1)&(( X["Age"]*2)%2 != 0), "AgeStatus"] = "estimated"
X.loc[X["Age"].isnull() , "AgeStatus"] = "guessed"
age_status_dummies = pd.get_dummies(X['AgeStatus'], prefix="AgeStatus")
 X = pd.concat([X, age_status_dummies], axis=1)
 X = X.drop('AgeStatus', axis=1)
 print("- AgeStatus transformer applied -")
 return X
test_csv = pd.read_csv("/kaggle/input/aptos2019-blindness-detection/test.csv")
 predicted_csv = pd.DataFrame(columns=["id_code", "diagnosis"])
 for id_code in test_csv["id_code"]:
filename = f"/kaggle/input/aptos2019-blindness-detection/test_images/{id_code}.png"
img = imread(filename)
 img = cv2.resize(img, dsize=(299, 299), interpolation=cv2.INTER_CUBIC)
 prediction = test_time_augmentation(img, model)
 predicted_csv = predicted_csv.append(
 {'id_code':id_code ,"diagnosis": prediction}, ignore_index=True)
 with open("submission.csv", "w")as f:
f.write(predicted_csv.to_csv(index=False))<choose_model_class>
class TicketGroupingTransformer(BaseEstimator, TransformerMixin):

 def __init__(self):
print("- TicketGrouping transformer initiated -")
 def fit(self, X, y=None):
return self
def transform(self, X, y=None):
cat_feature = ['Ticket']
count_enc = ce.CountEncoder(cols=cat_feature)
 count_enc.fit(X[cat_feature])
 grouping_col = count_enc.transform(X[cat_feature] ).add_suffix("_grouping")
 X = pd.concat([X, grouping_col], axis=1)
 print("- TicketGrouping transformer applied -")
 return X
sys.path.append(os.path.abspath('.. /input/efficientnet/efficientnet-master/efficientnet-master/'))
def create_effnetB5_model(input_shape, n_out):
model = Sequential()
base_model = EfficientNetB5(weights = None,
include_top = False,
input_shape = input_shape)
 base_model.name = 'base_model'
model.add(base_model)
 model.add(Dropout(0.25))
model.add(Dense(1024))
model.add(LeakyReLU())
 model.add(GlobalAveragePooling2D())
 model.add(Dropout(0.5))
model.add(Dense(n_out, activation = 'sigmoid'))
return model<load_pretrained>
class AgeGuessingTransformer(BaseEstimator, TransformerMixin):

 def __init__(self):
print("- AgeGuessing transformer initiated -")
 def fit(self, X, y=None):
return self
def transform(self, X, y=None):
X.loc[(X.Age.isnull() == True)&(X.Title_Master == 1), "Age"] = X.loc[X.Title_Master == 1]["Age"].median()
X.loc[(X.Age.isnull() == True)&(X.SibSp > 0)&(X.Title_Miss == 1), "Age"] = X.loc[(X.Title_Miss == 1)&(X.SibSp > 0)]["Age"].median()
X.loc[(X.Age.isnull() == True)&(X.Ticket_grouping == 1), "Age"] = X.loc[(X.Ticket_grouping == 1)]["Age"].median()
X.loc[(X.Age.isnull() == True)&(X.SibSp > 1), "Age"] = X.loc[(X.Age.isnull() == False)&(X.SibSp > 1)]["Age"].median()
X.loc[(X.Age.isnull()), "Age"] = X.loc[(X.Age.isnull() == False)&(X.Ticket_grouping > 1)]["Age"].median()
print("- AgeGuessing transformer applied -")
 return X
PRETRAINED_MODEL = '.. /input/efficientnetb5-blindness-detector/blindness_detector_best_qwk.h5'
IMAGE_HEIGHT = 340
IMAGE_WIDTH = 340
num_classes = 5
class_text = ['Normal', 'Mild', 'Moderate', 'Severe', 'Proliferative']
print('Creating model...')
 model = create_effnetB5_model(input_shape =(IMAGE_HEIGHT, IMAGE_WIDTH, 3), n_out = num_classes)
 print('Restoring model from ' + PRETRAINED_MODEL + '...')
 model.load_weights(PRETRAINED_MODEL)
 model.summary()<load_from_csv>
class AgeBinningTransformer(BaseEstimator, TransformerMixin):

 def __init__(self):
print("- AgeBinning transformer initiated -")
 def fit(self, X, y=None):
return self
def transform(self, X, y=None):
self.bins = [0, 2, 16, 25, 40, 50, np.inf]
self.age_cat = ["0_2", "2_16", "16_25", "25_40", "30_50", "50+"]
X["AgeBins"] = pd.cut(X["Age"], bins=self.bins, labels=self.age_cat)
 age_bins_dummies = pd.get_dummies(X['AgeBins'], prefix="AgeBins")
 X = pd.concat([X, age_bins_dummies], axis=1)
 print("- AgeBinning transformer applied -")
 return X
submit = pd.read_csv('.. /input/aptos2019-blindness-detection/sample_submission.csv')
 predicted = []
print("Making predictions...")
 for i, name in tqdm(enumerate(submit['id_code'])) :
path = os.path.join('.. /input/aptos2019-blindness-detection/test_images/', name + '.png')
 image = cv2.imread(path)
 image = process_image(image)
 X = np.array(( image[np.newaxis])/ 255)
 raw_prediction = model.predict(X)> 0.5
prediction = raw_prediction.astype(int ).sum(axis = 1)- 1
predicted.append(prediction[0] )<save_to_csv>
custom_pipeline = Pipeline([ 
("cabin_trans", KnownCabinTransformer()), 
("title_trans", TitleTransformer()), 
("hypo_missing_trans", HypotheticalMissingsTransformer()), 
("missing_fare_trans", MissingFareTransformer()), 
("embarked_trans", EmbarkedTransformer()), 
("gender_trans", GenderTransformer()), 
("class_trans", PclassTransformer()), 
("family_size_trans", FamilySizeTransformer()), 
("ticket_grp_trans", TicketGroupingTransformer()), 
("age_status_trans", AgeStatusTransformer()), 
("age_guessing_trans", AgeGuessingTransformer()), 
("age_binning_trans", AgeBinningTransformer()),
])
 full_dataset = custom_pipeline.fit_transform(full_dataset )
submit['diagnosis'] = predicted
submit.to_csv('submission.csv', index = False)
 submit.head(10 )<set_options>
full_dataset["TS_Tragedy"] = "null"
i = 0
unique_tickets_subset = full_dataset.loc[full_dataset["Ticket_grouping"] > 2]["Ticket"].unique()
while i < len(unique_tickets_subset):
subset = full_dataset.loc[full_dataset.Ticket == unique_tickets_subset[i]]
try:
ratio = len(subset.loc[subset["Survived"] == 0])/ len(subset.loc[subset.Survived.isnull() == False])
 if ratio > 0.65 :
full_dataset.loc[full_dataset.Ticket == unique_tickets_subset[i], "TS_Tragedy"] = "yes"
else:
full_dataset.loc[full_dataset.Ticket == unique_tickets_subset[i], "TS_Tragedy"] = "no"
i += 1
except ZeroDivisionError:
i += 1
TST_dummies = pd.get_dummies(full_dataset['TS_Tragedy'], prefix="TST")
 full_dataset = pd.concat([full_dataset, TST_dummies], axis=1)
 full_dataset = full_dataset.drop('TS_Tragedy', axis=1)
 full_dataset.head(3)
%reload_ext autoreload
%autoreload 2
%matplotlib inline<import_modules>
full_dataset.set_index(full_dataset.PassengerId, verify_integrity = True, inplace=True)
 full_dataset.tail()
from fastai import *
from fastai.vision import *
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import os
import scipy as sp
from functools import partial
from sklearn import metrics
from collections import Counter
from fastai.callbacks import *
import PIL
import cv2<set_options>
X_train = full_dataset.loc[full_dataset.Survived.isnull() == False]
y_train = X_train["Survived"].astype(int)
 X_test = full_dataset.loc[full_dataset.Survived.isnull() ]
useless_features = ["PassengerId", "Survived", "Name", "Age", "Ticket", "Fare", "Cabin", "AgeBins"]
X_train.drop(useless_features, axis=1, inplace=True)
 X_test.drop(useless_features, axis=1, inplace=True )
def seed_everything(seed=1358):
random.seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 np.random.seed(seed)
 torch.manual_seed(seed)
 torch.cuda.manual_seed(seed)
 torch.backends.cudnn.deterministic = True
seed_everything()<define_variables>
scaler = StandardScaler()
X_train_scaled = pd.DataFrame(scaler.fit_transform(X_train[["SibSp","Parch","FamilySize","Ticket_grouping"]]), columns=["sc_SibSp","sc_Parch","sc_FamilySize","sc_Ticket_grouping"], index=X_train.index)
 X_test_scaled = pd.DataFrame(scaler.fit_transform(X_test[["SibSp","Parch","FamilySize","Ticket_grouping"]]), columns=["sc_SibSp","sc_Parch","sc_FamilySize","sc_Ticket_grouping"], index=X_test.index)
 X_train = pd.concat([X_train, X_train_scaled], axis=1)
 X_train.drop(["SibSp","Parch","FamilySize","Ticket_grouping"], axis=1, inplace=True)
 X_test = pd.concat([X_test, X_test_scaled], axis=1)
 X_test.drop(["SibSp","Parch","FamilySize","Ticket_grouping"], axis=1, inplace=True)
 X_train.head(3 )
PATH = Path('.. /input/aptos2019-blindness-detection' )<load_from_csv>
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.svm import SVC
import xgboost as xgb
import lightgbm as lgb
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import VotingClassifier
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import cross_val_predict
from sklearn.metrics import confusion_matrix
from sklearn.model_selection import GridSearchCV
df = pd.read_csv(PATH/'train.csv')
 df.head()<count_values>
param_grid = [
{'n_neighbors':[2,3,4,5,6,7,8], 'weights':['uniform', 'distance'], 'n_jobs':[-1] }
]
neigh = KNeighborsClassifier()
grid_search = GridSearchCV(neigh, param_grid, cv=5, scoring='accuracy')
 grid_search.fit(X_train, y_train)
 grid_search.best_params_
df.diagnosis.value_counts()<feature_engineering>
neigh = KNeighborsClassifier(n_neighbors = 5, weights = 'uniform')
 neigh.fit(X_train, y_train)
 scores = cross_val_score(neigh, X_train, y_train, scoring="accuracy", cv=10)
 print(scores.mean() )
tfms = get_transforms(do_flip=True, flip_vert=True, max_rotate=0.10, max_zoom=1.3, max_warp=0.0, max_lighting=0.2 )<normalization>

 log_reg = LogisticRegression(C = 0.001, penalty='none')
 log_reg.fit(X_train, y_train)
 scores = cross_val_score(log_reg, X_train, y_train, scoring="accuracy", cv=10)
 print("Logistic Regression mean score : ", scores.mean())

forest_clf = RandomForestClassifier(max_depth = 6, min_samples_leaf=1, min_samples_split=2, n_estimators=100)
 forest_clf.fit(X_train, y_train)
 scores = cross_val_score(forest_clf, X_train, y_train, scoring="accuracy", cv=10)
 print("Random Forest mean score : ", scores.mean())

svc = SVC(C = 10, kernel='rbf', gamma='auto', probability=True)
 svc.fit(X_train, y_train)
 scores = cross_val_score(svc, X_train, y_train, scoring="accuracy", cv=10)
 print("SVC mean score: ", scores.mean())
 gbc = GradientBoostingClassifier(n_estimators=500, max_depth=3, learning_rate=0.01)
 gbc.fit(X_train, y_train)
 scores = cross_val_score(gbc, X_train, y_train, scoring="accuracy", cv=10)
 print("Gradient Boosting mean score : ", scores.mean())
 xgb_clf = xgb.XGBClassifier(n_estimators=500, max_depth=3, learning_rate=0.01)
 xgb_clf.fit(X_train, y_train)
 scores = cross_val_score(xgb_clf, X_train, y_train, scoring="accuracy", cv=10)
 print("XGB mean score : ", scores.mean())
data =(
 src.transform(tfms,size=128)
.databunch() 
.normalize(imagenet_stats)
)
 data<compute_train_metric>
voting_clf = VotingClassifier(estimators=[('gbc', gbc),('xgb', xgb_clf),('forest', forest_clf),('svc', svc),('lrc', log_reg),('knc', neigh)],
voting='hard')
 voting_clf.fit(X_train, y_train)
 scores = cross_val_score(voting_clf, X_train, y_train, scoring="accuracy", cv=10)
 print(scores.mean() )
def quadratic_kappa(y_hat, y):
return torch.tensor(cohen_kappa_score(torch.round(y_hat), y, weights='quadratic'),device='cuda:0')
 learn = cnn_learner(data, base_arch=models.resnet50 ,metrics=[quadratic_kappa],model_dir='/kaggle',pretrained=True )<find_best_params>
preds = voting_clf.predict(X_test)
 output = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': preds})
 output.to_csv("submission.csv", index=False)
 output.head()