Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<set_options>
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
%reload_ext autoreload
%autoreload 2
%matplotlib inline<import_modules>
data_train = pd.read_csv(".. /input/train.csv")
 data_test = pd.read_csv(".. /input/test.csv" )
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>
def cabin_imputer(cabin):
if cabin != "Unknown":
return cabin[0]
return cabin
def age_to_cat(age):
if np.isnan(age):
return "Unknown"
elif age < 13:
return "Kid"
elif age <= 18:
return "Teen"
elif age > 60:
return "Elder"
else:
return "Adult"
def substrings_in_string(big_string, substrings):
for substring in substrings:
if big_string.find(substring)!= -1:
return substring
print(big_string)
 return np.nan
def replace_titles(x):
title=x['Title']
if title in ['Don', 'Major', 'Capt', 'Jonkheer', 'Rev', 'Col']:
return 'Mr'
elif title in ['Countess', 'Mme']:
return 'Mrs'
elif title in ['Mlle', 'Ms']:
return 'Miss'
elif title =='Dr':
return "Dr"
else:
return title
def clear_dataset(dataset):
dataset["Family"] = dataset["Parch"] + dataset["SibSp"] + 1
dataset["Alone"] = dataset["Family"] == 1
dataset.Cabin = dataset.Cabin.fillna("Unknown")
 dataset["Cabin"] = dataset["Cabin"].apply(cabin_imputer)
 dataset.Embarked = dataset.Embarked.fillna("S")
 dataset.Fare = dataset.groupby(by="Pclass")["Fare"].apply(lambda x: x.fillna(x.mean()))
dataset["Age_Cat"] = dataset.Age.apply(age_to_cat)
 dataset["Age"] = dataset.Age.fillna(dataset.Age.mean())
 title_list=['Mrs', 'Mr', 'Master', 'Miss', 'Major', 'Rev',
'Dr', 'Ms', 'Mlle','Col', 'Capt', 'Mme', 'Countess',
'Don', 'Jonkheer']
dataset['Title']=dataset['Name'].map(lambda x: substrings_in_string(x, title_list))
dataset['Title']=dataset.apply(replace_titles, axis=1)
 dataset["FareLog"] = dataset.Fare.apply(np.log1p)
 dataset["FarePerFamilyMember"] = dataset.Fare / dataset.Family
dataset.drop(["Ticket","Name"], axis=1, inplace=True)
 return dataset
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>
data_train = pd.read_csv(".. /input/train.csv")
 data_test = pd.read_csv(".. /input/test.csv")
 y_data = data_train.Survived
x_data_train = clear_dataset(data_train)
 x_data_test = clear_dataset(data_test )
PATH = '/kaggle/input/aptos2019-blindness-detection/'
train_img_path = PATH +'train_images/'
test_img_path = PATH +'test_images/'
train_file_name = PATH +'train.csv'
test_file_name = PATH +'test.csv'
bs=24
sz=224<load_from_csv>
x_data_train.drop(["PassengerId", "Survived"], axis=1,inplace=True)
 x_data_test.drop(["PassengerId"], axis=1,inplace=True )
df = pd.read_csv(PATH +'train.csv')
 df.head()<feature_engineering>
test_encoded = pd.get_dummies(x_data_test)
 train_encoded = pd.get_dummies(x_data_train)
 test_encoded= test_encoded.reindex(columns = train_encoded.columns, fill_value=0 )
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 )<categorify>
pca = PCA(n_components = 2, whiten= True)
 x_pca = pca.fit_transform(train_encoded)
 print("variance ratio: ", pca.explained_variance_ratio_)
 print("sum: ",sum(pca.explained_variance_ratio_))
data =(
 src.transform(get_transforms() ,size=224)
.databunch() 
.normalize(imagenet_stats)
)
 data<compute_test_metric>
x_train, x_val, y_train, y_val = train_test_split(train_encoded, y_data, test_size=0.25, random_state=42 )
learn = cnn_learner(data, base_arch=models.resnet34 ,metrics=[error_rate],model_dir='/kaggle/working',pretrained=True )<train_model>
def get_metrics(y_test, y_predicted):
precision = precision_score(y_test, y_predicted, pos_label=None,
average='weighted')
 recall = recall_score(y_test, y_predicted, pos_label=None,
average='weighted')
 f1 = f1_score(y_test, y_predicted, pos_label=None, average='weighted')
 accuracy = accuracy_score(y_test, y_predicted)
 return accuracy, precision, recall, f1
lr = 1e-2
learn.fit_one_cycle(2, lr )<save_model>
clf = RandomForestClassifier(n_estimators=300, max_depth=6,max_features=11,criterion="gini",n_jobs=-1, random_state=42)
 clf.fit(x_train, y_train)
 y_predicted = clf.predict(x_val)
 accuracy, precision, recall, f1 = get_metrics(y_val, y_predicted)
 print("accuracy = %.3f, precision = %.3f, recall = %.3f, f1 = %.3f" %(accuracy, precision, recall, f1))
learn.save('stage-1' )<train_model>
y_predicted = clf.predict(x_train)
 accuracy, precision, recall, f1 = get_metrics(y_train, y_predicted)
 print("accuracy = %.3f, precision = %.3f, recall = %.3f, f1 = %.3f" %(accuracy, precision, recall, f1))
learn.fit_one_cycle(5, slice(1e-4,lr/5))<import_modules>
clf = XGBClassifier(n_estimators= 300, learning_rate=0.3, max_depth=4)
 clf.fit(x_train, y_train)
 y_predicted = clf.predict(x_val)
 accuracy, precision, recall, f1 = get_metrics(y_val, y_predicted)
 print("accuracy = %.3f, precision = %.3f, recall = %.3f, f1 = %.3f" %(accuracy, precision, recall, f1))
learn.export('/kaggle/working/blindness-detection.pkl' )<define_variables>
y_predicted = clf.predict(x_train)
 accuracy, precision, recall, f1 = get_metrics(y_train, y_predicted)
 print("train accuracy = %.3f, precision = %.3f, recall = %.3f, f1 = %.3f" %(accuracy, precision, recall, f1))
train_csv = ".. /input/aptos2019-blindness-detection/train.csv"
test_csv = ".. /input/aptos2019-blindness-detection/test.csv"
train_dir = ".. /input/aptos2019-blindness-detection/train_images/"
test_dir = ".. /input/aptos2019-blindness-detection/test_images/"<load_from_csv>
clf = LogisticRegression(C=2.0, solver="newton-cg", penalty="l2", n_jobs=-1)
 clf.fit(x_train, y_train)
 y_predicted = clf.predict(x_val)
 accuracy, precision, recall, f1 = get_metrics(y_val, y_predicted)
 print("accuracy = %.3f, precision = %.3f, recall = %.3f, f1 = %.3f" %(accuracy, precision, recall, f1))
df = pd.read_csv(train_csv)
 size = 256,256<train_on_grid>
y_predicted = clf.predict(x_train)
 accuracy, precision, recall, f1 = get_metrics(y_train, y_predicted)
 print("train accuracy = %.3f, precision = %.3f, recall = %.3f, f1 = %.3f" %(accuracy, precision, recall, f1))
def load_image(path):
img = cv2.resize(cv2.cvtColor(cv2.imread(path), cv2.COLOR_BGR2RGB), size)
 img = get_cropped_image(img)
 return img<categorify>
clf = GaussianNB()
clf.fit(x_train, y_train)
 y_predicted = clf.predict(x_val)
 accuracy, precision, recall, f1 = get_metrics(y_val, y_predicted)
 print("accuracy = %.3f, precision = %.3f, recall = %.3f, f1 = %.3f" %(accuracy, precision, recall, f1))
labels = df["diagnosis"].values.tolist()
labels = keras.utils.to_categorical(labels )<split>
y_predicted = clf.predict(x_train)
 accuracy, precision, recall, f1 = get_metrics(y_train, y_predicted)
 print("train accuracy = %.3f, precision = %.3f, recall = %.3f, f1 = %.3f" %(accuracy, precision, recall, f1))
images, x_val, labels, y_val = train_test_split(images, labels, test_size = 0.15 )<define_variables>
clf = BernoulliNB(alpha=0.2)
 clf.fit(x_train, y_train)
 y_predicted = clf.predict(x_val)
 accuracy, precision, recall, f1 = get_metrics(y_val, y_predicted)
 print("accuracy = %.3f, precision = %.3f, recall = %.3f, f1 = %.3f" %(accuracy, precision, recall, f1))
train_aug = ImageDataGenerator(horizontal_flip = True,
zoom_range = 0.25,
rotation_range = 360,
vertical_flip = True)
 train_generator = train_aug.flow(images, labels, batch_size = 8 )<choose_model_class>
y_predicted = clf.predict(x_train)
 accuracy, precision, recall, f1 = get_metrics(y_train, y_predicted)
 print("train accuracy = %.3f, precision = %.3f, recall = %.3f, f1 = %.3f" %(accuracy, precision, recall, f1))
input_layer = Input(shape =(256,256,3))
base_model = DenseNet121(include_top = False, input_tensor = input_layer, weights = ".. /input/densenet-keras/DenseNet-BC-121-32-no-top.h5")
 x = GlobalAveragePooling2D()(base_model.output)
 x = Dropout(0.5 )(x)
 out = Dense(5, activation = 'softmax' )(x)
 model = Model(inputs = input_layer, outputs = out )<choose_model_class>
clf = SVC(C=40)
 clf.fit(x_train, y_train)
 y_predicted = clf.predict(x_val)
 accuracy, precision, recall, f1 = get_metrics(y_val, y_predicted)
 print("accuracy = %.3f, precision = %.3f, recall = %.3f, f1 = %.3f" %(accuracy, precision, recall, f1))
optimizer = keras.optimizers.Adam(lr=3e-4)
 es = EarlyStopping(monitor='val_loss', mode='min', patience = 5, restore_best_weights = True)
 rlrop = ReduceLROnPlateau(monitor='val_loss', mode='min', patience = 2, factor = 0.5, min_lr=1e-6)
 callback_list = [es, rlrop]
model.compile(optimizer = optimizer, loss = "categorical_crossentropy", metrics = ["accuracy"] )<train_model>
y_predicted = clf.predict(x_train)
 accuracy, precision, recall, f1 = get_metrics(y_train, y_predicted)
 print("train accuracy = %.3f, precision = %.3f, recall = %.3f, f1 = %.3f" %(accuracy, precision, recall, f1))
model.fit_generator(generator = train_generator, steps_per_epoch = len(train_generator), epochs = 20, validation_data =(x_val, y_val), callbacks = callback_list )<set_options>
clf = RandomForestClassifier(n_estimators=100, max_depth=6,max_features=11, min_samples_leaf=0.0001,criterion="gini",n_jobs=-1, random_state=42)
 accuracies = cross_val_score(clf, train_encoded, y_data, cv=5, scoring="accuracy")
 print("CV accuracy", accuracies.mean() )
del train_generator, images
gc.collect()<predict_on_test>
clf = clf = LogisticRegression(C=0.9, solver="newton-cg", penalty="l2", n_jobs=-1)
 accuracies = cross_val_score(clf, train_encoded, y_data, cv=5, scoring="accuracy")
 print("CV accuracy", accuracies.mean() )
predprobs = model.predict(test_images )<define_variables>
submission = pd.read_csv(".. /input/gender_submission.csv" )
<save_to_csv><EOS> 
clf = RandomForestClassifier(n_estimators=100, max_depth=6,max_features=11, min_samples_leaf=0.0001,criterion="gini",n_jobs=-1, random_state=42)
 clf.fit(train_encoded, y_data)
 test_preds = clf.predict(test_encoded)
 submission.Survived = test_preds
submission.to_csv('rf_submission.csv', index=False )