Kaggle competition: Titanic - Machine Learning from Disaster
learn = create_cnn(data, models.resnet101, metrics=accuracy, path='.', model_dir='.' )<load_pretrained>
train_df = pd.read_csv('/kaggle/input/titanic/train.csv' )
learn.load('.. /input/dataset/histopathologic_cancer_detection' )<train_model>
test_df = pd.read_csv('/kaggle/input/titanic/test.csv')
 test_df.head()
learn.unfreeze()
learn.fit_one_cycle(16,slice(1e-6,1e-4))<save_model>
women = train_df[train_df['Sex'] == 'female']['Survived']
sum(women)/len(women)* 100
learn.save('histopathologic_cancer_detection' )<import_modules>
men = train_df[train_df['Sex'] == 'male']['Survived']
sum(men)/len(men)* 100
try:
DatasetType
except: 
<predict_on_test>
train_df['SibSp'].value_counts()
try:
preds, _ = learn.TTA(ds_type=DatasetType.Test)
 print('Predict w/ test time augument done.')
 except:
preds, _ = learn.get_preds(DatasetType.Test )<define_variables>
y = train_df['Survived']
features = ["Sex", "Pclass", "SibSp", "Parch"]
X = pd.get_dummies(train_df[features])
 X_test = pd.get_dummies(test_df[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': test_df.PassengerId, 'Survived': predictions})
 output.to_csv('my_submission.csv', index=False)
 print("Your submission was successfully saved!" )
ids = [file.stem for file in data.test_ds.x.items]
ids[:5]<load_from_csv>
train_df['Survived'].value_counts()
df=pd.read_csv('.. /input/histopathologic-cancer-detection/sample_submission.csv' ).set_index('id')
 df.head()<feature_engineering>
train_df[['Pclass','Survived']].groupby(['Pclass'], as_index=False ).mean().sort_values(by='Survived', ascending='False' )
df.loc[ids,'label'] = preds[:,1].numpy()
df = df.reset_index()
df.head()<save_to_csv>
train_df[['Sex','Survived']].groupby(['Sex'], as_index=False ).mean().sort_values(by='Survived', ascending='False' )
df.to_csv('submission.csv', index=False )<import_modules>
train_df[['SibSp','Survived']].groupby(['SibSp'], as_index=False ).mean().sort_values(by='Survived', ascending='False' )
import numpy as np
import pandas as pd
import cv2
import os
from glob import glob
import matplotlib.pyplot as plt
import random
from sklearn.model_selection import train_test_split
from sklearn.metrics import roc_curve, auc, roc_auc_score
from keras_preprocessing.image import ImageDataGenerator
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten, BatchNormalization, Activation
from keras.layers import Conv2D, MaxPool2D
from keras.callbacks import EarlyStopping, ReduceLROnPlateau
from IPython.display import clear_output 
<load_from_csv>
train_df[['Parch','Survived']].groupby(['Parch'], as_index=False ).mean().sort_values(by='Survived', ascending='False' )
path = ".. /input/"
labels = pd.read_csv(path + 'train_labels.csv')
 train_path = path + 'train/'
test_path = path + 'test/'<merge>
train_df[['Embarked','Survived']].groupby(['Embarked'], as_index=False ).mean().sort_values(by='Survived', ascending='False' )
df = pd.DataFrame({'path': glob(os.path.join(train_path,'*.tif')) })
 df['id'] = df.path.map(lambda x:(( x.split("n")[2].split('.')[0])[1:]))
df = df.merge(labels, on = "id")
 df.head(3 )<split>
for dataset in combine:
dataset['Title'] = dataset.Name.str.extract('([A-Za-z]+)\.', expand=False)
 pd.crosstab(train_df['Title'], train_df['Sex'] )
def readImage(path):
bgr_img = cv2.imread(path)
 b,g,r = cv2.split(bgr_img)
 rgb_img = cv2.merge([r,g,b])
 return rgb_img<define_variables>
for dataset in combine:
dataset['Title'] = dataset['Title'].replace(['Lady', 'Countess','Capt', 'Col', 'Don', 'Dr', 'Major', 'Rev', 'Sir', 'Jonkheer', 'Dona'], 'Rare')
 dataset['Title'] = dataset['Title'].replace('Mlle', 'Miss')
 dataset['Title'] = dataset['Title'].replace('Ms', 'Miss')
 dataset['Title'] = dataset['Title'].replace('Mme', 'Mrs')
 train_df[['Title', 'Survived']].groupby(['Title'], as_index=False ).mean()
IMG_SIZE = 196
BATCH_SIZE = 128<train_model>
title_mapping = {"Mr": 1, "Miss": 2, "Mrs": 3, "Master": 4, "Rare": 5}
for dataset in combine:
dataset['Title'] = dataset['Title'].map(title_mapping)
 dataset['Title'] = dataset['Title'].fillna(0)
 train_df.head()
test_list = os.listdir(test_path)
 train_list = os.listdir(train_path)
 print("There are " + str(len(train_list)) + " training examples.")
 print("There are " + str(len(test_list)) + " test examples." )<split>
train_df = train_df.drop(['Name', 'PassengerId'], axis=1)
 test_df = test_df.drop(['Name'], axis=1)
 combine = [train_df, test_df]
train_df.shape, test_df.shape
df['label'] = df['label'].astype(str)
 train, valid = train_test_split(df, test_size=0.2, stratify = df['label'])
<concatenate>
for dataset in combine:
dataset['Sex'] = dataset['Sex'].map({'female': 1, 'male': 0} ).astype(int)
 train_df.head()
def crop_centre(image, crop_length):
original_size = image.shape[0]
centre = original_size // 2
lower_bound = centre - crop_length // 2
upper_bound = centre + crop_length // 2
image = image[(lower_bound):(upper_bound),(lower_bound):(upper_bound)]
return image<choose_model_class>
guess_ages = np.zeros(( 2,3))
guess_ages
train_datagen = ImageDataGenerator(rescale=1./255,
vertical_flip = True,
horizontal_flip = True,
rotation_range=90,
zoom_range=0.2,
width_shift_range=0.1,
height_shift_range=0.1,
shear_range=0.05,
channel_shift_range=0.1)
 test_datagen = ImageDataGenerator(rescale = 1./255 )<create_dataframe>
for dataset in combine:
for i in range(0, 2):
for j in range(0, 3):
guess_df = dataset[(dataset['Sex'] == i)& \ 
(dataset['Pclass'] == j+1)]['Age'].dropna()
age_guess = guess_df.median()
guess_ages[i,j] = int(age_guess/0.5 + 0.5)* 0.5
for i in range(0, 2):
for j in range(0, 3):
dataset.loc[(dataset.Age.isnull())&(dataset.Sex == i)&(dataset.Pclass == j+1),\
'Age'] = guess_ages[i,j]
dataset['Age'] = dataset['Age'].astype(int)
 train_df.head()
train_generator = train_datagen.flow_from_dataframe(dataframe = train,
directory = None,
x_col = 'path',
y_col = 'label',
target_size =(IMG_SIZE,IMG_SIZE),
class_mode = "binary",
batch_size=BATCH_SIZE,
seed = 110318,
shuffle = True )<create_dataframe>
train_df['AgeBand'] = pd.cut(train_df['Age'], 5)
 train_df[['AgeBand', 'Survived']].groupby(['AgeBand'], as_index=False ).mean().sort_values(by='AgeBand', ascending=True )
valid_generator = test_datagen.flow_from_dataframe(dataframe = valid,
directory = None,
x_col = 'path',
y_col = 'label',
target_size =(IMG_SIZE,IMG_SIZE),
class_mode = 'binary',
batch_size = BATCH_SIZE,
shuffle = False )<choose_model_class>
for dataset in combine:
dataset.loc[ dataset['Age'] <= 16, 'Age'] = 0
dataset.loc[(dataset['Age'] > 16)&(dataset['Age'] <= 32), 'Age'] = 1
dataset.loc[(dataset['Age'] > 32)&(dataset['Age'] <= 48), 'Age'] = 2
dataset.loc[(dataset['Age'] > 48)&(dataset['Age'] <= 64), 'Age'] = 3
dataset.loc[ dataset['Age'] > 64, 'Age']
train_df.head()
dropout_fc = 0.5
conv_base = ResNet50(weights = 'imagenet', include_top = False, input_shape =(IMG_SIZE,IMG_SIZE,3))
my_model = Sequential()
my_model.add(conv_base)
 my_model.add(Flatten())
 my_model.add(Dense(256, use_bias=False))
my_model.add(BatchNormalization())
 my_model.add(Activation("relu"))
my_model.add(Dropout(dropout_fc))
my_model.add(Dense(1, activation = "sigmoid")) 
<set_options>
train_df = train_df.drop(['AgeBand'], axis=1)
 combine = [train_df, test_df]
train_df.head()
conv_base.Trainable=True
set_trainable=False
for layer in conv_base.layers:
if layer.name == 'res5a_branch2a':
set_trainable = True
if set_trainable:
layer.trainable = True
else:
layer.trainable = False<choose_model_class>
for dataset in combine:
dataset['FamilySize'] = dataset['SibSp'] + dataset['Parch'] + 1
train_df[['FamilySize', 'Survived']].groupby(['FamilySize'], as_index=False ).mean().sort_values(by='Survived', ascending=False )
my_model.compile(optimizers.Adam(0.001), loss = "binary_crossentropy", metrics = ["accuracy"] )<define_variables>
for dataset in combine:
dataset['IsAlone'] = 0
dataset.loc[dataset['FamilySize'] == 1, 'IsAlone'] = 1
train_df[['IsAlone', 'Survived']].groupby(['IsAlone'], as_index=False ).mean()
train_step_size = train_generator.n // train_generator.batch_size
valid_step_size = valid_generator.n // valid_generator.batch_size<choose_model_class>
train_df = train_df.drop(['Parch', 'SibSp', 'FamilySize'], axis=1)
 test_df = test_df.drop(['Parch', 'SibSp', 'FamilySize'], axis=1)
 combine = [train_df, test_df]
train_df.head()
earlystopper = EarlyStopping(monitor='val_loss', patience=3, verbose=2, restore_best_weights=True)
 reduce = ReduceLROnPlateau(monitor='val_loss', patience=1, verbose=1, factor=0.1 )<train_model>
for dataset in combine:
dataset['Age*Class'] = dataset.Age * dataset.Pclass
train_df.loc[:, ['Age*Class', 'Age', 'Pclass']].head(10 )
history = my_model.fit_generator(train_generator,
steps_per_epoch = train_step_size,
epochs = 10,
validation_data = valid_generator,
validation_steps = valid_step_size,
callbacks = [reduce, earlystopper],
verbose = 2)
<create_dataframe>
freq_port = train_df.Embarked.dropna().mode() [0]
freq_port
roc_validation_generator = ImageDataGenerator(rescale=1./255 ).flow_from_dataframe(valid,
x_col = 'path',
y_col = 'label',
target_size =(IMG_SIZE,IMG_SIZE),
class_mode = 'binary',
batch_size = BATCH_SIZE,
shuffle = False)
 predictions = my_model.predict_generator(roc_validation_generator, steps=len(roc_validation_generator), verbose=2)
 false_positive_rate, true_positive_rate, threshold = roc_curve(roc_validation_generator.classes, predictions)
 area_under_curve = auc(false_positive_rate, true_positive_rate)
 plt.plot([0, 1], [0, 1], 'k--')
 plt.plot(false_positive_rate, true_positive_rate, label='AUC = {:.3f}'.format(area_under_curve))
plt.xlabel('False positive rate')
 plt.ylabel('True positive rate')
 plt.title('ROC curve')
 plt.legend(loc='best')
 plt.savefig('ROC_PLOT.png', bbox_inches='tight')
 plt.show()<feature_engineering>
for dataset in combine:
dataset['Embarked'] = dataset['Embarked'].fillna(freq_port)
 train_df[['Embarked', 'Survived']].groupby(['Embarked'], as_index=False ).mean().sort_values(by='Survived', ascending=False )
testdf = pd.DataFrame({'path': glob(os.path.join(test_path, '*.tif')) })
 testdf['id'] = testdf.path.map(lambda x:(x.split("/")[3].split('.')[0]))
testdf.head(3 )<set_options>
for dataset in combine:
dataset['Embarked'] = dataset['Embarked'].map({'S': 0, 'C': 1, 'Q': 2} ).astype(int)
 train_df.head()
tta_datagen = ImageDataGenerator(rescale=1./255,
vertical_flip = True,
horizontal_flip = True,
rotation_range=90,
zoom_range=0.2,
width_shift_range=0.1,
height_shift_range=0.1,
shear_range=0.05,
channel_shift_range=0.1 )<prepare_output>
test_df['Fare'].fillna(test_df['Fare'].dropna().median() , inplace=True)
 test_df.head()
tta_steps = 5
submission = pd.DataFrame()
for index in range(0, len(testdf)) :
data_frame = pd.DataFrame({'path': testdf.iloc[index,0]}, index=[index])
 data_frame['id'] = data_frame.path.map(lambda x: x.split('/')[3].split('.')[0])
 img_path = data_frame.iloc[0,0]
test_img = cv2.imread(img_path)
 test_img = cv2.resize(test_img,(IMG_SIZE,IMG_SIZE))
test_img = np.expand_dims(test_img, axis = 0)
 predictionsTTA = []
for i in range(0, tta_steps):
preds = my_model.predict_generator(tta_datagen.flow_from_dataframe(dataframe = data_frame,
directory = None,
x_col = 'path',
target_size =(IMG_SIZE, IMG_SIZE),
class_mode = None,
batch_size = 1,
shuffle = False), steps = 1)
 predictionsTTA.append(preds)
 clear_output()
prediction_entry = np.array(np.mean(predictionsTTA, axis=0))
data_frame['label'] = prediction_entry
submission = pd.concat([submission, data_frame[['id', 'label']]])
<save_to_csv>
for dataset in combine:
dataset.loc[ dataset['Fare'] <= 7.91, 'Fare'] = 0
dataset.loc[(dataset['Fare'] > 7.91)&(dataset['Fare'] <= 14.454), 'Fare'] = 1
dataset.loc[(dataset['Fare'] > 14.454)&(dataset['Fare'] <= 31), 'Fare'] = 2
dataset.loc[ dataset['Fare'] > 31, 'Fare'] = 3
dataset['Fare'] = dataset['Fare'].astype(int)
 combine = [train_df, test_df]
train_df.head(10 )
submission.to_csv('submission.csv', index=False, header=True )<install_modules>
X_train = train_df.drop("Survived", axis=1)
 Y_train = train_df["Survived"]
X_test = test_df.drop("PassengerId", axis=1 ).copy()
X_train.shape, Y_train.shape, X_test.shape
! pip install albumentations
! pip install pretrainedmodels<set_options>
logreg = LogisticRegression()
logreg.fit(X_train, Y_train)
 Y_pred = logreg.predict(X_test)
 acc_log = round(logreg.score(X_train, Y_train)* 100, 2)
 acc_log
%matplotlib inline
train_on_gpu = True
cudnn.benchmark = True<feature_engineering>
knn = KNeighborsClassifier(n_neighbors = 3)
 knn.fit(X_train, Y_train)
 Y_pred = knn.predict(X_test)
 acc_knn = round(knn.score(X_train, Y_train)* 100, 2)
 acc_knn
SEED = 323
base_dir = '.. /input/'
def seed_everything(seed=SEED):
random.seed(seed)
 os.environ['PYHTONHASHSEED'] = str(seed)
 np.random.seed(seed)
 torch.manual_seed(seed)
 torch.cuda.manual_seed(seed)
 torch.backends.cudnn.deterministic = True
seed_everything(SEED )<load_from_csv>
model = RandomForestClassifier(n_estimators=100, max_depth=5, random_state=1)
 model.fit(X_train, Y_train)
 predictions = model.predict(X_test)
 model.score(X_train, Y_train)
 acc_random_forest = round(model.score(X_train, Y_train)* 100, 2)
 acc_random_forest
labels = pd.read_csv(base_dir+'train_labels.csv' )<count_values>
output = pd.DataFrame({'PassengerId': test_df.PassengerId, 'Survived': predictions})
 output.to_csv('my_submission.csv', index=False)
 print("Your second submission was successfully saved!" )