Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<define_variables>
%matplotlib inline
tez_path = '.. /input/tez-lib'
effnet_path = '.. /input/efficientnet-pytorch/'
sys.path.append(tez_path)
 sys.path.append(effnet_path )<import_modules>
test_df = pd.read_csv("/kaggle/input/titanic/test.csv")
 train_df = pd.read_csv("/kaggle/input/titanic/train.csv" )
import os
import albumentations
import pandas as pd
import numpy as np
import tez
from tez.datasets import ImageDataset
import torch
import torch.nn as nn
from torch.nn import functional as F
from efficientnet_pytorch import EfficientNet
from tqdm import tqdm<feature_engineering>
total = train_df.isnull().sum().sort_values(ascending=False)
 percent_1 = train_df.isnull().sum() /train_df.isnull().count() *100
percent_2 =(round(percent_1, 1)).sort_values(ascending=False)
 missing_data = pd.concat([total, percent_2], axis=1, keys=['Total', '%'])
 missing_data.head(5 )
class LeafModel(nn.Module):
def __init__(self, num_classes):
super().__init__()
self.effnet = EfficientNet.from_name("efficientnet-b4")
 self.dropout = nn.Dropout(0.2)
 self.out = nn.Linear(1792, num_classes)
 self.step_scheduler_after = "epoch"
def forward(self, image, targets=None):
batch_size, _, _, _ = image.shape
x = self.effnet.extract_features(image)
 x = F.adaptive_avg_pool2d(x, 1 ).reshape(batch_size, -1)
 outputs = self.out(self.dropout(x))
return outputs, None, None
def load(self, model_path, device="cuda"):
self.device = device
if next(self.parameters() ).device != self.device:
self.to(self.device)
 model_dict = torch.load(model_path, map_location=device)
 self.load_state_dict(model_dict["state_dict"])
 def predict(self, dataset, sampler=None, batch_size=16, n_jobs=1, collate_fn=None):
if next(self.parameters() ).device != self.device:
self.to(self.device)
 if batch_size == 1:
n_jobs = 0
data_loader = torch.utils.data.DataLoader(
 dataset, batch_size=batch_size, num_workers=4, sampler=sampler, collate_fn=collate_fn, pin_memory=True 
)
 if self.training:
self.eval()
final_output = []
tk0 = tqdm(data_loader, total=len(data_loader))
for b_idx, data in enumerate(tk0):
with torch.no_grad() :
out = self.predict_one_step(data)
 out = out.cpu().detach().numpy()
yield out
tk0.set_postfix(stage="test")
 tk0.close()
def model_fn(self, data):
for key, value in data.items() :
data[key] = value.to(self.device)
 output, loss, metrics = self(**data)
 return output, loss, metrics
def predict_one_step(self, data):
output, _, _ = self.model_fn(data)
 return output<set_options>
data = [train_df, test_df]
for dataset in data:
dataset['relatives'] = dataset['SibSp'] + dataset['Parch']
dataset.loc[dataset['relatives'] > 0, 'not_alone'] = 0
dataset.loc[dataset['relatives'] == 0, 'not_alone'] = 1
dataset['not_alone'] = dataset['not_alone'].astype(int)
 train_df['not_alone'].value_counts()
test_aug = albumentations.Compose([
albumentations.RandomResizedCrop(512, 512),
albumentations.Transpose(p=0.5),
albumentations.HorizontalFlip(p=0.5),
albumentations.VerticalFlip(p=0.5),
albumentations.HueSaturationValue(
 hue_shift_limit=0.2,
sat_shift_limit=0.2,
val_shift_limit=0.2,
p=0.5 
),
albumentations.RandomBrightnessContrast(
 brightness_limit=(-0.1,0.1),
contrast_limit=(-0.1, 0.1),
p=0.5 
),
albumentations.Normalize(
 mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225],
max_pixel_value=255.0,
p=1.0 
)
 ], p=1.)<load_from_csv>
train_df = train_df.drop(['PassengerId'], axis=1 )
dfx = pd.read_csv(".. /input/cassava-leaf-disease-classification/sample_submission.csv")
 image_path = ".. /input/cassava-leaf-disease-classification/test_images/"
test_image_paths = [os.path.join(image_path, x)for x in dfx.image_id.values]
test_targets = dfx.label.values
test_dataset = ImageDataset(
 image_paths=test_image_paths,
targets=test_targets,
augmentations=test_aug,
)<load_from_csv>
deck = {"A": 1, "B": 2, "C": 3, "D": 4, "E": 5, "F": 6, "G": 7, "U": 8}
data = [train_df, test_df]
for dataset in data:
dataset['Cabin'] = dataset['Cabin'].fillna("U0")
 dataset['Deck'] = dataset['Cabin'].map(lambda x: re.compile("([a-zA-Z]+)" ).search(x ).group())
 dataset['Deck'] = dataset['Deck'].map(deck)
 dataset['Deck'] = dataset['Deck'].fillna(0)
 dataset['Deck'] = dataset['Deck'].astype(int)
 train_df = train_df.drop(['Cabin'], axis=1)
 test_df = test_df.drop(['Cabin'], axis=1 )
train_dfx = pd.read_csv(".. /input/cassava-leaf-disease-classification/train.csv")
 model0 = LeafModel(num_classes=train_dfx.label.nunique())
 model0.load(".. /input/single-model/efficentnet_model_fold0.bin", device='cuda' )<predict_on_test>
data = [train_df, test_df]
for dataset in data:
mean = train_df["Age"].mean()
std = test_df["Age"].std()
is_null = dataset["Age"].isnull().sum()
rand_age = np.random.randint(mean - std, mean + std, size = is_null)
 age_slice = dataset["Age"].copy()
age_slice[np.isnan(age_slice)] = rand_age
dataset["Age"] = age_slice
dataset["Age"] = train_df["Age"].astype(int)
 train_df["Age"].isnull().sum()
model_list = [model0]
def run_inference(model):
final_preds = None
for j in range(20):
preds = model.predict(test_dataset, batch_size=64, n_jobs=-1)
 temp_preds = None
for p in preds:
if temp_preds is None:
temp_preds = p
else:
temp_preds = np.vstack(( temp_preds, p))
if final_preds is None:
final_preds = temp_preds
else:
final_preds += temp_preds
final_preds /= 20
final_preds = final_preds.argmax(axis=1)
 return final_preds<save_to_csv>
common_value = 'S'
data = [train_df, test_df]
for dataset in data:
dataset['Embarked'] = dataset['Embarked'].fillna(common_value )
dfx.label = run_inference(model_list[0])
 dfx.to_csv("submission.csv", index=False )<install_modules>
data = [train_df, test_df]
for dataset in data:
dataset['Fare'] = dataset['Fare'].fillna(0)
 dataset['Fare'] = dataset['Fare'].astype(int)
 data = [train_df, test_df]
titles = {"Mr": 1, "Miss": 2, "Mrs": 3, "Master": 4, "Rare": 5}
for dataset in data:
dataset['Title'] = dataset.Name.str.extract('([A-Za-z]+)\.', expand=False)
 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')
 dataset['Title'] = dataset['Title'].map(titles)
 dataset['Title'] = dataset['Title'].fillna(0)
 train_df = train_df.drop(['Name'], axis=1)
 test_df = test_df.drop(['Name'], axis=1)
 genders = {"male": 0, "female": 1}
data = [train_df, test_df]
for dataset in data:
dataset['Sex'] = dataset['Sex'].map(genders)
 train_df['Ticket'].describe()
train_df = train_df.drop(['Ticket'], axis=1)
 test_df = test_df.drop(['Ticket'], axis=1 )
! pip install '.. /input/efficientnet-pytorch/EfficientNet-PyTorch/EfficientNet-PyTorch-master/' 
<import_modules>
ports = {"S": 0, "C": 1, "Q": 2}
data = [train_df, test_df]
for dataset in data:
dataset['Embarked'] = dataset['Embarked'].map(ports )
import os
import gc
import cv2
import time
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import torch
import torch.nn as nn
import albumentations as A
from efficientnet_pytorch import EfficientNet<load_pretrained>
data = [train_df, test_df]
for dataset in data:
dataset['Age'] = dataset['Age'].astype(int)
 dataset.loc[ dataset['Age'] <= 11, 'Age'] = 0
dataset.loc[(dataset['Age'] > 11)&(dataset['Age'] <= 18), 'Age'] = 1
dataset.loc[(dataset['Age'] > 18)&(dataset['Age'] <= 22), 'Age'] = 2
dataset.loc[(dataset['Age'] > 22)&(dataset['Age'] <= 27), 'Age'] = 3
dataset.loc[(dataset['Age'] > 27)&(dataset['Age'] <= 33), 'Age'] = 4
dataset.loc[(dataset['Age'] > 33)&(dataset['Age'] <= 40), 'Age'] = 5
dataset.loc[(dataset['Age'] > 40)&(dataset['Age'] <= 66), 'Age'] = 6
dataset.loc[ dataset['Age'] > 66, 'Age'] = 6
device=torch.device("cuda" if torch.cuda.is_available() else "cpu")
 effnet_b1=EfficientNet.from_pretrained('efficientnet-b1',
weights_path='.. /input/efficientnet-pytorch/efficientnet-b1-dbc7070a.pth',
include_top=True )<define_variables>
data = [train_df, test_df]
for dataset in data:
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)&(dataset['Fare'] <= 99), 'Fare'] = 3
dataset.loc[(dataset['Fare'] > 99)&(dataset['Fare'] <= 250), 'Fare'] = 4
dataset.loc[ dataset['Fare'] > 250, 'Fare'] = 5
dataset['Fare'] = dataset['Fare'].astype(int )
CFG = {
"IMG_SIZE": 384,
"BATCH_SIZE": 16,
"IMG_FOLDER": ".. /input/cassava-leaf-disease-classification/train_images",
"EPOCHS": 10,
"NUM_FOLDS": 5,
"device": device
}
print(device )<categorify>
data = [train_df, test_df]
for dataset in data:
dataset['Age_Class']= dataset['Age']* dataset['Pclass']
class CassavaModel(nn.Module):
def __init__(self, _backbone):
super(CassavaModel, self ).__init__()
self._backbone=_backbone
self._backbone._fc=nn.Linear(in_features=_backbone._fc.in_features,
out_features=5,
bias=True)
 def forward(self, x):
x=self._backbone(x)
 return x<load_pretrained>
for dataset in data:
dataset['Fare_Per_Person'] = dataset['Fare']/(dataset['relatives']+1)
 dataset['Fare_Per_Person'] = dataset['Fare_Per_Person'].astype(int)
 train_df.head(10 )
models=[]
for model_name in os.listdir('.. /input/enesemble'):
model_path=os.path.join('.. /input/enesemble', model_name)
 model=CassavaModel(effnet_b1)
 model.load_state_dict(torch.load(model_path))
model=model.to(device)
 models.append(model )<normalization>
X_train = train_df.drop("Survived", axis=1)
 Y_train = train_df["Survived"]
X_test = test_df.drop("PassengerId", axis=1 ).copy()
decision_tree = DecisionTreeClassifier()
decision_tree.fit(X_train, Y_train)
 predictions = decision_tree.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!" )