Kaggle competition: Titanic - Machine Learning from Disaster
df_train = pd.read_csv(train_csv)
 df_train["id_code"] = df_train["id_code"].apply(lambda x:x+".png")
 df_train["diagnosis"] = df_train['diagnosis'].astype('str')
 train_datagen = ImageDataGenerator(rescale = 1/255.,
horizontal_flip = True,
vertical_flip = False,
width_shift_range = 0.1,
height_shift_range = 0.1,
fill_mode = 'nearest',
validation_split = 0.15,
zoom_range = 0.2,
preprocessing_function=preprocess_image,
rotation_range = 10)
 train_generator = train_datagen.flow_from_dataframe(
 dataframe = df_train,
directory = image_dataset,
validation_split = 0.2,
x_col = 'id_code',
y_col = 'diagnosis',
target_size =(224,224),
class_mode = 'categorical',
batch_size = 64,
shaffle=True,
subset = 'training' 
)
 val_generator = train_datagen.flow_from_dataframe(
 dataframe = df_train,
x_col = 'id_code',
y_col = 'diagnosis',
directory = image_dataset,
class_mode = "categorical",
batch_size = 64,
shaffle=True,
target_size =(224,224),
subset = "validation" 
)
 optimizer = Adam(lr=0.0005)
 save_callback = ModelCheckpoint(filepath='Blindness-detection.h5', monitor='val_loss', save_best_only=True)
 model.compile(loss = 'categorical_crossentropy',optimizer = 'adam',metrics = ['accuracy'])
 history = model.fit_generator(
 train_generator,
epochs = 70,
steps_per_epoch = 20,
validation_data = val_generator,
validation_steps = 7,
callbacks = [reduce_lr,early_stop,save_callback]
)<feature_engineering>
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import missingno as msno
import seaborn as sns
test_datagen = ImageDataGenerator(rescale=1./255,
preprocessing_function=preprocess_image)
 sample_df = pd.read_csv(sample_csv)
 sample_df["id_code"]=sample_df["id_code"].apply(lambda x:x+".png")
 test_generator = test_datagen.flow_from_dataframe(
 dataframe=sample_df,
directory = test_dataset,
x_col="id_code",
target_size =(224,224),
batch_size = 1,
shuffle = False,
class_mode = None 
)
 preds = model.predict_generator(
 test_generator,
steps=len(test_generator.filenames)
)
<compute_train_metric>
titanic_train_df = pd.read_csv('.. /input/titanic/train.csv')
 titanic_test_df = pd.read_csv('.. /input/titanic/test.csv')
 titanic_test_df1 = pd.read_csv('.. /input/titanic/test.csv' )
y_true = val_generator.classes
y_pred = np.argmax(model.predict_generator(val_generator),axis=1)
 print(confusion_matrix(y_true,y_pred))
target_names = ['0','1','2','3','4']
print(classification_report(val_generator.classes, y_pred, target_names=target_names))<save_to_csv>
titanic_train_df_survived = titanic_train_df[titanic_train_df['Survived'] == 1]
filenames= test_generator.filenames
results=pd.DataFrame({"id_code":filenames,
"diagnosis":np.argmax(preds,axis = 1)})
 results['id_code'] = results['id_code'].map(lambda x: str(x)[:-4])
 results.to_csv("submission.csv",index=False )<set_options>
survived_gender = titanic_train_df_survived.groupby('Sex', as_index=False ).count()
%reload_ext autoreload
%autoreload 2
%matplotlib inline<import_modules>
titanic_train_df_embark = titanic_train_df.groupby('Embarked', as_index=False ).count()
titanic_train_df_survived_embark = titanic_train_df_survived.groupby('Embarked', as_index=False ).count()
import fastai
from fastai import *
from fastai.vision import *
from fastai.callbacks import *
import cv2
import pandas as pd
import matplotlib.pyplot as plt<set_options>
titanic_train_df.drop(columns=['Name','Cabin','Ticket'] , inplace=True )
print('Make sure cudnn is enabled:', torch.backends.cudnn.enabled )<set_options>
titanic_test_df.drop(columns=['Name','Cabin','Ticket'] , inplace=True)
def seed_everything(seed):
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 = 1667
seed_everything(SEED )<load_from_csv>
le=LabelEncoder()
le.fit(titanic_train_df['Sex'] )
def get_2015_df() :
base_image_dir = os.path.join('.. ', 'input/resized-2015-2019-blindness-detection-images/')
 train_dir = os.path.join(base_image_dir,'resized train 15/')
 df = pd.read_csv(os.path.join(base_image_dir, 'labels/trainLabels15.csv'))
df.columns = ['image', 'diagnosis']
df['path'] = df['image'].map(lambda x: os.path.join(train_dir,'{}.jpg'.format(x)))
 df = df.drop(columns=['image'])
 df = df.sample(frac=1 ).reset_index(drop=True)
 return df 
<load_from_csv>
titanic_train_df['Sex']= le.transform(titanic_train_df['Sex'] )
def get_df_2019() :
base_image_dir = os.path.join('.. ', 'input/resized-2015-2019-blindness-detection-images/')
 train_dir = os.path.join(base_image_dir,'resized train 19/')
 df = pd.read_csv(os.path.join(base_image_dir, 'labels/trainLabels19.csv'))
df['path'] = df['id_code'].map(lambda x: os.path.join(train_dir,'{}.jpg'.format(x)))
 df = df.drop(columns=['id_code'])
 df = df.sample(frac=1 ).reset_index(drop=True)
 test_df = pd.read_csv('.. /input/aptos2019-blindness-detection/sample_submission.csv')
 return df, test_df 
<feature_engineering>
titanic_test_df['Sex'] = le.transform(titanic_test_df['Sex'] )
base_image_dir = os.path.join('.. ', 'input/aptos2019-blindness-detection/')
 train_dir = os.path.join(base_image_dir,'train_images/')
 df = pd.read_csv(os.path.join(base_image_dir, 'train.csv'))
df['path'] = df['id_code'].map(lambda x: os.path.join(train_dir,'{}.png'.format(x)))
 df = df.drop(columns=['id_code'])
 df = df.sample(frac=1 ).reset_index(drop=True)
 df.head(10 )<normalization>
leembark=LabelEncoder()
leembark.fit(titanic_train_df['Embarked'].astype(str))
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)
 data =(
 src.transform(tfms,size=128)
.databunch() 
.normalize(imagenet_stats)
 )<compute_test_metric>
titanic_train_df['Embarked'] = leembark.transform(titanic_train_df['Embarked'].astype(str))
titanic_test_df['Embarked'] = leembark.transform(titanic_test_df['Embarked'].astype(str))
def quadratic_kappa(y_hat, y):
return torch.tensor(cohen_kappa_score(torch.round(y_hat), y, weights='quadratic'),device='cuda:0' )<choose_model_class>
titanic_train_df_x = titanic_train_df.drop(columns=['Survived'] )
learn = cnn_learner(data, base_arch=models.resnet50 ,metrics=[quadratic_kappa],model_dir='/kaggle',pretrained=True )<train_model>
titanic_train_df_y =titanic_train_df['Survived'].to_frame()
learn.fit_one_cycle(3, 1e-2 )<normalization>
si = SimpleImputer(missing_values=np.nan, strategy='mean' )
learn.data = data =(
 src.transform(tfms,size=256)
.databunch() 
.normalize(imagenet_stats)
)
 learn.lr_find()
learn.recorder.plot()<train_model>
titanic_train_df_x = si.fit_transform(titanic_train_df_x )
lr = 1e-2
learn.fit_one_cycle(5, lr )<find_best_params>
titanic_test_df = si.transform(titanic_test_df )
learn.unfreeze()
learn.lr_find()
learn.recorder.plot()<train_model>
sc = StandardScaler()
learn.fit_one_cycle(10, slice(1e-6,1e-4))<set_options>
titanic_train_df_x = sc.fit_transform(titanic_train_df_x )
learn.export()
learn.save('resnet_old_image_weight' )<find_best_params>
titanic_test_df = sc.transform(titanic_test_df )
interp = ClassificationInterpretation.from_learner(learn)
 losses,idxs = interp.top_losses()
len(data.valid_ds)==len(losses)==len(idxs )<predict_on_test>
from sklearn.svm import SVC
valid_preds = learn.get_preds(ds_type=DatasetType.Valid )<import_modules>
clf_svm = SVC(C=0.8 )
import numpy as np
import pandas as pd
import os
import scipy as sp
from functools import partial
from sklearn import metrics
from collections import Counter
import json<compute_test_metric>
clf_svm.fit(titanic_train_df_x, titanic_train_df_y )
class OptimizedRounder(object):
def __init__(self):
self.coef_ = 0
def _kappa_loss(self, coef, X, y):
X_p = np.copy(X)
 for i, pred in enumerate(X_p):
if pred < coef[0]:
X_p[i] = 0
elif pred >= coef[0] and pred < coef[1]:
X_p[i] = 1
elif pred >= coef[1] and pred < coef[2]:
X_p[i] = 2
elif pred >= coef[2] and pred < coef[3]:
X_p[i] = 3
else:
X_p[i] = 4
ll = metrics.cohen_kappa_score(y, X_p, weights='quadratic')
 return -ll
def fit(self, X, y):
loss_partial = partial(self._kappa_loss, X=X, y=y)
 initial_coef = [0.5, 1.5, 2.5, 3.5]
self.coef_ = sp.optimize.minimize(loss_partial, initial_coef, method='nelder-mead')
 print(-loss_partial(self.coef_['x']))
def predict(self, X, coef):
X_p = np.copy(X)
 for i, pred in enumerate(X_p):
if pred < coef[0]:
X_p[i] = 0
elif pred >= coef[0] and pred < coef[1]:
X_p[i] = 1
elif pred >= coef[1] and pred < coef[2]:
X_p[i] = 2
elif pred >= coef[2] and pred < coef[3]:
X_p[i] = 3
else:
X_p[i] = 4
return X_p
def coefficients(self):
return self.coef_['x']<train_model>
svm_pred = clf_svm.predict(titanic_test_df )
optR = OptimizedRounder()
optR.fit(valid_preds[0],valid_preds[1] )<load_from_csv>
result_df = pd.DataFrame({'PassengerID':titanic_test_df1['PassengerId'],'Survived':svm_pred} )
<save_to_csv><EOS> 
result_df.to_csv('csv_to_submit.csv', index = False)