Kaggle competition: Titanic - Machine Learning from Disaster
print(h2o.__version__)
 h2o.init(max_mem_size='16G' )<load_from_csv>
%matplotlib inline
train = h2o.import_file(".. /input/melanoma-train-test-creator/train_meta_size_3.csv")
 test = h2o.import_file(".. /input/melanoma-train-test-creator/test_meta_size_3.csv" )<prepare_x_and_y>
train_data = pd.read_csv("/kaggle/input/titanic/train.csv")
 train_data.head()
x = test.columns
y = 'target'<feature_engineering>
test_data = pd.read_csv("/kaggle/input/titanic/test.csv" )
train[y] = train[y].asfactor()<train_model>
aml = H2OAutoML(max_models=10000, seed=47, max_runtime_secs=3600)
 aml.train(x=x, y=y, training_frame=train, fold_column="fold" )<find_best_params>
def age_fill(columns):
age = columns[0]
pclass = columns[1]
if pd.isnull(age):
if pclass == 1:
return int(train_data[train_data['Pclass']==1]['Age'].mean())
 elif pclass == 2:
return int(train_data[train_data['Pclass']==2]['Age'].mean())
 else:
return int(train_data[train_data['Pclass']==3]['Age'].mean())
 else:
return age
aml.leader<predict_on_test>
train_data['Age'] = train_data[['Age','Pclass']].apply(age_fill, axis=1 )
preds = aml.predict(test )<load_from_csv>
sample_submission = pd.read_csv('.. /input/siim-isic-melanoma-classification/sample_submission.csv')
 sample_submission.head()<save_to_csv>
train_data.drop('Cabin', axis=1, inplace=True )
sample_submission['target'] = preds['p1'].as_data_frame().values
sample_submission.to_csv('submission.csv', index=False )<load_from_csv>
train_data.dropna(inplace=True )
def ensemble() :
stacked_0 = pd.read_csv('/kaggle/input/siim-isic-melanoma-classification/sample_submission.csv')
 stacked_1 = pd.read_csv('/kaggle/input/melanoma-submissions2/submissionImage.csv')
 stacked_2 = pd.read_csv('/kaggle/input/melanoma-submissions2/submissionTabular.csv')
 stacked_4 = pd.read_csv('/kaggle/input/melanoma-submissions2/submission_multiple_data_source.csv')
 stacked_3 = pd.read_csv('.. /input/csv001/datasets_766461_1344911_submission_rank-then-blend.csv')
 stacked_5 = pd.read_csv('.. /input/csv001/submission0.9504.csv')
 stacked_6 = pd.read_csv('.. /input/csv001/submission_efnet.csv')
 stacked_7 = pd.read_csv('.. /input/csv001/submission_models_blended.csv')
 sub = pd.DataFrame()
sub['image_name'] = stacked_0['image_name']
sub['target'] = np.exp(np.mean(
 [
stacked_1['target'].apply(lambda x: np.log2(x)) , \
stacked_2['target'].apply(lambda x: np.log2(x)) , \
stacked_4['target'].apply(lambda x: np.log2(x)) , \
stacked_3['target'].apply(lambda x: np.log2(x)) ,
stacked_5['target'].apply(lambda x: np.log2(x)) , \
stacked_6['target'].apply(lambda x: np.log2(x)) , \
stacked_7['target'].apply(lambda x: np.log2(x)) ,
], axis=0))
sub.to_csv('submission.csv', index=False, float_format='%.6f' )<load_pretrained>
sex = pd.get_dummies(train_data['Sex'],drop_first=True)
 embark = pd.get_dummies(train_data['Embarked'],drop_first=True )
ensemble() 
<load_from_csv>
train_data.drop(['Sex','Embarked', 'Name', 'Ticket'], axis=1, inplace=True )
stacked_n = pd.read_csv('submission.csv')
 stacked_n.head()<install_modules>
train_data = pd.concat([train_data, sex, embark], axis=1 )
!pip install efficientnet-pytorch<import_modules>
test_data['Age'] = test_data[['Age','Pclass']].apply(age_fill, axis=1 )
from pathlib import Path
import pandas as pd
from torch.utils.data import Dataset,DataLoader
from PIL import Image
from torchvision import transforms as T
import torch.nn as nn
import torch
import torch.nn.functional as F
from sklearn.model_selection import GroupKFold
import numpy as np
from fastprogress.fastprogress import master_bar, progress_bar
from sklearn.metrics import accuracy_score, roc_auc_score
from efficientnet_pytorch import EfficientNet
from torchvision import models
import pdb
import albumentations as A
from albumentations.pytorch.transforms import ToTensor
import matplotlib.pyplot as plt
import pickle<define_variables>
def fill_fare(col):
pclass = col[0]
fare = col[1]
if pd.isnull(fare):
if pclass == 1:
return int(train_data[train_data['Pclass']==1]['Fare'].mean())
 elif pclass == 2:
return int(train_data[train_data['Pclass']==2]['Fare'].mean())
 else:
return int(train_data[train_data['Pclass']==3]['Fare'].mean())
 else:
return fare
path = Path('.. /input/jpeg-melanoma-256x256/')
 df_path = Path('.. /input/melanoma-256x256/')
 im_sz = 256
bs = 16<load_from_csv>
test_data['Fare'] = test_data[['Pclass','Fare']].apply(fill_fare, axis=1 )
train_fnames = list_files(path/'train')
 df = pd.read_csv(df_path/'train.csv')
 df.head()<count_values>
test_data.drop('Cabin', axis=1, inplace=True )
df.target.value_counts() ,df.shape<split>
sex = pd.get_dummies(test_data['Sex'],drop_first=True)
 embark = pd.get_dummies(test_data['Embarked'],drop_first=True )
def get_train_val_split(df):
df = df[df.tfrecord != -1].reset_index(drop=True)
 train_tf_records = list(range(len(df.tfrecord.unique())))[:12]
split_cond = df.tfrecord.apply(lambda x: x in train_tf_records)
 train_df = df[split_cond].reset_index()
valid_df = df[~split_cond].reset_index()
return train_df,valid_df<categorify>
test_data.drop(['Sex','Embarked', 'Name', 'Ticket'], axis=1, inplace=True )
class MelanomaDataset(Dataset):
def __init__(self,df,im_path,transforms=None,is_test=False):
self.df = df
self.im_path = im_path
self.transforms = transforms
self.is_test = is_test
def __getitem__(self,idx):
img_path = f"{self.im_path}/{self.df.iloc[idx]['image_name']}.jpg"
img = Image.open(img_path)
 if self.transforms:
img = self.transforms(**{"image": np.array(img)})["image"]
if self.is_test:
return img
target = self.df.iloc[idx]['target']
return img,torch.tensor([target],dtype=torch.float32)
 def __len__(self):
return self.df.shape[0] 
<choose_model_class>
test_data = pd.concat([test_data, sex, embark], axis=1 )
class MelanomaEfficientNet(nn.Module):
def __init__(self,model_name='efficientnet-b0',pool_type=F.adaptive_avg_pool2d):
super().__init__()
self.pool_type = pool_type
self.backbone = EfficientNet.from_pretrained(model_name)
 in_features = getattr(self.backbone,'_fc' ).in_features
self.classifier = nn.Linear(in_features,1)
 def forward(self,x):
features = self.pool_type(self.backbone.extract_features(x),1)
 features = features.view(x.size(0),-1)
 return self.classifier(features )<train_model>
y = train_data["Survived"]
features = ['Pclass','Age','SibSp','Parch','Fare','male','Q','S']
X = train_data[features]
X_test = test_data[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_data.PassengerId, 'Survived': predictions})
 output.to_csv('my_submission.csv', index=False)
 print("Your submission was successfully saved!" )
def get_device() :
return torch.device("cuda")if torch.cuda.is_available() else torch.device("cpu")
 def get_model(model_name='efficientnet-b0',lr=1e-5,wd=0.01,freeze_backbone=False,opt_fn=torch.optim.AdamW,device=None):
device = device if device else get_device()
model = MelanomaEfficientNet(model_name=model_name)
 if freeze_backbone:
for parameter in model.backbone.parameters() :
parameter.requires_grad = False
opt = opt_fn(model.parameters() ,lr=lr,weight_decay=wd)
 model = model.to(device)
 return model, opt
def training_step(xb,yb,model,loss_fn,opt,device,scheduler):
xb,yb = xb.to(device), yb.to(device)
 out = model(xb)
 opt.zero_grad()
loss = loss_fn(out,yb)
 loss.backward()
opt.step()
scheduler.step()
return loss.item()
def validation_step(xb,yb,model,loss_fn,device):
xb,yb = xb.to(device), yb.to(device)
 out = model(xb)
 loss = loss_fn(out,yb)
 out = torch.sigmoid(out)
 return loss.item() ,out
def get_data(train_df,valid_df,train_tfms,test_tfms,bs):
train_ds = MelanomaDataset(df=train_df,im_path=path/'train',transforms=train_tfms)
 valid_ds = MelanomaDataset(df=valid_df,im_path=path/'train',transforms=test_tfms)
 train_dl = DataLoader(dataset=train_ds,batch_size=bs,shuffle=True,num_workers=4)
 valid_dl = DataLoader(dataset=valid_ds,batch_size=bs*2,shuffle=False,num_workers=4)
 return train_dl,valid_dl<train_model>
LogModel = LogisticRegression()
LogModel.fit(X,y )
def fit(epochs,model,train_dl,valid_dl,opt,device=None,loss_fn=F.binary_cross_entropy_with_logits):
device = device if device else get_device()
scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(opt, len(train_dl)*epochs)
 val_rocs = []
mb = master_bar(range(epochs))
mb.write(['epoch','train_loss','valid_loss','val_roc'],table=True)
 for epoch in mb:
trn_loss,val_loss = 0.0,0.0
val_preds = np.zeros(( len(valid_dl.dataset),1))
val_targs = np.zeros(( len(valid_dl.dataset),1))
model.train()
for xb,yb in progress_bar(train_dl,parent=mb):
trn_loss += training_step(xb,yb,model,loss_fn,opt,device,scheduler)
 trn_loss /= mb.child.total
model.eval()
with torch.no_grad() :
for i,(xb,yb)in enumerate(progress_bar(valid_dl,parent=mb)) :
loss,out = validation_step(xb,yb,model,loss_fn,device)
 val_loss += loss
bs = xb.shape[0]
val_preds[i*bs:i*bs+bs] = out.cpu().numpy()
val_targs[i*bs:i*bs+bs] = yb.cpu().numpy()
val_loss /= mb.child.total
val_roc = roc_auc_score(val_targs.reshape(-1),val_preds.reshape(-1))
val_rocs.append(val_roc)
 mb.write([epoch,f'{trn_loss:.6f}',f'{val_loss:.6f}',f'{val_roc:.6f}'],table=True)
 return model,val_rocs<load_from_csv>
Logpred = LogModel.predict(X_test )
<set_options><EOS> 
output = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': Logpred})
 output.to_csv('my_submission.csv', index=False)
 print("Your submission was successfully saved!" )