Kaggle competition: Titanic - Machine Learning from Disaster
test_folder=".. /input/cassava-leaf-disease-classification/test_images"
class TestDataset(torch.utils.data.Dataset):
def __init__(self, augmentation):
self.test_images=os.listdir(test_folder)
 self.augmentation=augmentation
def __len__(self):
return len(self.test_images)
 def __getitem__(self, idx):
image_id=self.test_images[idx]
image_path=os.path.join(test_folder, image_id)
 img=read_image(image_path)
 if self.augmentation:
img=self.augmentation(image=img)['image']
img=torch.tensor(img ).permute(2, 1, 0)
 return(image_id, img )<create_dataframe>
pd.set_option('display.max_rows', 1000)
 seed =45
plt.style.use('fivethirtyeight' )
val_transform=val_augmentation()
pred=nn.Softmax(dim=1)
 test_dataset=TestDataset(val_transform)
 test_dataloader=torch.utils.data.DataLoader(test_dataset,
shuffle=False,
pin_memory=True,
batch_size=16,
num_workers=4)
 submission_data=[]
with torch.no_grad() :
for(image_id,img)in test_dataloader:
img=img.to(device)
 yout=torch.zeros(img.shape[0], 5 ).to(device)
 for model in models:
yout+=model(img)
 yout/=5
yout=pred(yout)
 ypred=torch.argmax(yout, dim=1 ).cpu().numpy()
for i in range(img.shape[0]):
submission_data.append({
'image_id': image_id[i],
'label': ypred[i]
} )<save_to_csv>
train = pd.read_csv('/kaggle/input/titanic/train.csv')
 test = pd.read_csv('/kaggle/input/titanic/test.csv' )
submission_df=pd.DataFrame.from_dict(submission_data)
 submission_df.to_csv('submission.csv', index=False )<install_modules>
train.loc[train['PassengerId'] == 631, 'Age'] = 48
train.loc[train['PassengerId'] == 69, ['SibSp', 'Parch']] = [0,0]
test.loc[test['PassengerId'] == 1106, ['SibSp', 'Parch']] = [0,0]
! pip install.. /input/tta-torch<install_modules>
train[["Sex", "Survived"]].groupby(['Sex'], as_index=False ).mean().sort_values(by='Survived', ascending=False )
! pip install.. /input/timm-package/timm-0.1.26-py3-none-any.whl<import_modules>
def detect_outliers(df,n,features):

 outlier_indices = []
for col in features:
Q1 = np.percentile(df[col], 25)
 Q3 = np.percentile(df[col],75)
 IQR = Q3 - Q1
outlier_step = 1.5 * IQR
outlier_list_col = df[(df[col] < Q1 - outlier_step)|(df[col] > Q3 + outlier_step)].index
outlier_indices.extend(outlier_list_col)
 outlier_indices = Counter(outlier_indices)
 multiple_outliers = list(k for k, v in outlier_indices.items() if v > n)
 return multiple_outliers
Outliers_to_drop = detect_outliers(train,2,["Age","SibSp","Parch","Fare"])
 train.loc[Outliers_to_drop]
import torch
import torchvision
import skimage.io as io
import os
import numpy as np
from PIL import Image
import torch.nn as nn
import pandas as pd
import pickle
import pytorch_lightning as pl
import timm
import ttach as tta<feature_engineering>
class CassavaNet(nn.Module):
def __init__(self):
super().__init__()
backbone = timm.create_model(TIMM_MODEL, pretrained=True)
 n_features = backbone.fc.in_features
self.backbone = nn.Sequential(*backbone.children())[:-2]
self.classifier = nn.Linear(n_features, 5)
 self.pool = nn.AdaptiveAvgPool2d(( 1, 1))
def forward_features(self, x):
x = self.backbone(x)
 return x
def forward(self, x):
feats = self.forward_features(x)
 x = self.pool(feats ).view(x.size(0), -1)
 x = self.classifier(x)
 return x
def load_checkpoint(filepath):
checkpoint = torch.load(filepath)
 model = checkpoint['model']
model.load_state_dict(checkpoint['state_dict'])
 for parameter in model.parameters() :
parameter.requires_grad = False
model.eval()
return model
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
 print(device )<find_best_params>
df = pd.concat(( train.loc[:,'Pclass':'Embarked'], test.loc[:,'Pclass':'Embarked'])).reset_index(drop=True)
model_0 = load_checkpoint('.. /input/cassavamodels2/ckpt_resnet50-512-0.pth')
 model_0 = model_0.cuda()
model_1 = load_checkpoint('.. /input/cassavamodels2/ckpt_resnet50-512-0-snap.pth')
 model_1 = model_1.cuda()
model_2 = load_checkpoint('.. /input/cassavamodels2/ckpt_resnet50-512-0.pth')
 model_2 = model_2.cuda()<choose_model_class>
%pip install autoviz
AV = AutoViz_Class()
report_2 = AV.AutoViz("/kaggle/input/titanic/train.csv" )
tta_transforms = tta.Compose(
 [
tta.HorizontalFlip() ,
tta.Rotate90(angles=[0, 180]),
tta.Multiply(factors=[0.95, 1, 1.05])
 ] 
)
 merge_mode = 'mean'
tta_model_0 = tta.ClassificationTTAWrapper(model_0, tta_transforms, merge_mode = merge_mode)
 tta_model_1 = tta.ClassificationTTAWrapper(model_1, tta_transforms, merge_mode = merge_mode)
 tta_model_2 = tta.ClassificationTTAWrapper(model_2, tta_transforms, merge_mode = merge_mode )<categorify>
def basic_details(df):
b = pd.DataFrame()
b['Missing value, %'] = round(df.isnull().sum() /df.shape[0]*100)
 b['N unique value'] = df.nunique()
b['dtype'] = df.dtypes
return b
basic_details(df )
database_base_path = '.. /input/cassava-leaf-disease-classification/'
n_classes = 5
files_path = f'{database_base_path}test_images/'
test_size = len(os.listdir(files_path))
test_preds = np.zeros(( test_size, n_classes))
loader = torchvision.transforms.Compose([
torchvision.transforms.Resize(512),
torchvision.transforms.CenterCrop(448),
torchvision.transforms.ToTensor() ,
torchvision.transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
 ])
 image_names = os.listdir(files_path)
 def image_loader(image_name):
image = Image.open(image_name)
 image = loader(image ).float()
image = image.unsqueeze(0)
 return image.cuda()
def ensembled_prediction(model_0, model_1, model_2, files_path):
labels = []
for img in os.listdir(files_path):
image = image_loader(files_path + img)
 model_0.eval()
model_1.eval()
model_2.eval()
with torch.no_grad() :
pred_0 = model_0(image.cuda())
 pred_1 = model_1(image.cuda())
 pred_2 = model_2(image.cuda())
 pred = pred_0 + pred_1 + pred_2
label = torch.argmax(pred)
 labels.append(label.item())
 return np.array(labels )<predict_on_test>
df['Title'] = df.Name.str.extract('([A-Za-z]+)\.', expand=False)
 df['Title'] = df['Title'].replace(['Lady', 'Countess','Capt', 'Col', 'Don', 'Dr', 'Major', 'Rev', 'Sir', 'Jonkheer', 'Dona'], 'Rare')
 df['Title'] = df['Title'].replace('Mlle', 'Miss')
 df['Title'] = df['Title'].replace('Ms', 'Miss')
 df['Title'] = df['Title'].replace('Mme', 'Mrs')
 title_mapping = {"Mr": 1, "Miss": 2, "Mrs": 3, "Master": 4, "Rare": 5}
df['Title'] = df['Title'].map(title_mapping)
 df['Title'] = df['Title'].fillna(0)
 df = df.drop(['Name'], axis=1)
 df["Sex"][df["Sex"] == "male"] = 0
df["Sex"][df["Sex"] == "female"] = 1
df["Sex"] = df["Sex"].astype(int)
 df['Age'] = df.groupby('Pclass')['Age'].transform(lambda x: x.fillna(x.median()))
df["Age"] = df["Age"].astype(int)
 df['Age_cat'] = pd.qcut(df['Age'],q=[0,.16,.33,.49,.66,.83, 1], labels=False, precision=1)
 tickets = []
for i in list(df.Ticket):
if not i.isdigit() :
tickets.append(i.replace(".","" ).replace("/","" ).strip().split(" ")[0])
 else:
tickets.append("x")
 df["Ticket"] = tickets
df = pd.get_dummies(df, columns= ["Ticket"], prefix = "T")
 df['Fare'] = df.groupby("Pclass")['Fare'].transform(lambda x: x.fillna(x.median()))
df['Zero_Fare'] = df['Fare'].map(lambda x: 1 if x == 0 else(0))
def fare_category(fr):
if fr <= 7.91:
return 1
elif fr <= 14.454 and fr > 7.91:
return 2
elif fr <= 31 and fr > 14.454:
return 3
return 4
df['Fare_cat'] = df['Fare'].apply(fare_category)
 df['Cabin'] = df['Cabin'].fillna('U')
 df['Cabin'] = df['Cabin'].map(lambda x: re.compile("([a-zA-Z]+)" ).search(x ).group())
 cabin_category = {'A':9, 'B':8, 'C':7, 'D':6, 'E':5, 'F':4, 'G':3, 'T':2, 'U':1}
df['Cabin'] = df['Cabin'].map(cabin_category)
 df["Embarked"] = df["Embarked"].fillna("C")
 df["Embarked"][df["Embarked"] == "S"] = 1
df["Embarked"][df["Embarked"] == "C"] = 2
df["Embarked"][df["Embarked"] == "Q"] = 3
df["Embarked"] = df["Embarked"].astype(int)
 df['FamilySize'] = df['SibSp'] + df['Parch'] + 1
df['FamilySize_cat'] = df['FamilySize'].map(lambda x: 1 if x == 1
else(2 if 5 > x >= 2
else(3 if 8 > x >= 5
else 4)
))
df['Alone'] = [1 if i == 1 else 0 for i in df['FamilySize']]
dummy_col=['Title', 'Sex', 'Age_cat', 'SibSp', 'Parch', 'Fare_cat', 'Cabin', 'Embarked', 'Pclass', 'FamilySize_cat']
dummy = pd.get_dummies(df[dummy_col], columns=dummy_col, drop_first=False)
 df = pd.concat([dummy, df], axis = 1)
 df['FareCat_Sex'] = df['Fare_cat']*df['Sex']
df['Pcl_Sex'] = df['Pclass']*df['Sex']
df['Pcl_Title'] = df['Pclass']*df['Title']
df['Age_cat_Sex'] = df['Age_cat']*df['Sex']
df['Age_cat_Pclass'] = df['Age_cat']*df['Pclass']
df['Title_Sex'] = df['Title']*df['Sex']
df['Age_Fare'] = df['Age_cat']*df['Fare_cat']
df['SmallF'] = df['FamilySize'].map(lambda s: 1 if s == 2 else 0)
 df['MedF'] = df['FamilySize'].map(lambda s: 1 if 3 <= s <= 4 else 0)
 df['LargeF'] = df['FamilySize'].map(lambda s: 1 if s >= 5 else 0)
 df['Senior'] = df['Age'].map(lambda s:1 if s>70 else 0)
test_preds = ensembled_prediction(tta_model_0, tta_model_1, tta_model_2, files_path)
 test_preds<save_to_csv>
def descrictive_stat_feat(df):
df = pd.DataFrame(df)
 dcol= [c for c in df.columns if df[c].nunique() >=10]
d_median = df[dcol].median(axis=0)
 d_mean = df[dcol].mean(axis=0)
 q1 = df[dcol].apply(np.float32 ).quantile(0.25)
 q3 = df[dcol].apply(np.float32 ).quantile(0.75)
 for c in dcol:
df[c+str('_median_range')] =(df[c].astype(np.float32 ).values > d_median[c] ).astype(np.int8)
 df[c+str('_mean_range')] =(df[c].astype(np.float32 ).values > d_mean[c] ).astype(np.int8)
 df[c+str('_q1')] =(df[c].astype(np.float32 ).values < q1[c] ).astype(np.int8)
 df[c+str('_q3')] =(df[c].astype(np.float32 ).values > q3[c] ).astype(np.int8)
 return df
df = descrictive_stat_feat(df )
submission = pd.DataFrame({'image_id': image_names, 'label': test_preds})
 submission.to_csv('submission.csv', index=False)
 display(submission.head() )<import_modules>
def basic_details(df):
b = pd.DataFrame()
b['Missing value'] = df.isnull().sum()
b['N unique value'] = df.nunique()
b['dtype'] = df.dtypes
return b
basic_details(df )
print(os.listdir(".. /input")) 
<load_from_csv>
X_train = df[:train.shape[0]]
X_test_fin = df[train.shape[0]:]
y = train.Survived
X_train['Y'] = y
df = X_train
df.head(20)
 X = df.drop('Y', axis=1)
 y = df.Y
df_train = pd.read_csv(".. /input/histopathologic-cancer-detection/train_labels.csv")
 id_label_map = {k:v for k,v in zip(df_train.id.values, df_train.label.values)}
df_train.head()<define_variables>
from keras.models import Sequential
from keras.layers import Dense, Activation, Dropout
import keras
from keras.optimizers import SGD
import graphviz
import eli5
from eli5.sklearn import PermutationImportance
def get_id_from_file_path(file_path):
return file_path.split(os.path.sep)[-1].replace('.tif', '' )<define_variables>
x_train, x_valid, y_train, y_valid = train_test_split(X, y, test_size=0.3, random_state=10 )
labeled_files = glob('.. /input/histopathologic-cancer-detection/train/*.tif')
 test_files = glob('.. /input/histopathologic-cancer-detection/test/*.tif' )<split>
sq = Sequential()
sq.add(Dense(units = 9, kernel_initializer = 'uniform', activation = 'relu', input_dim = 134))
sq.add(Dense(units = 9, kernel_initializer = 'uniform', activation = 'relu'))
sq.add(Dense(units = 5, kernel_initializer = 'uniform', activation = 'relu'))
sq.add(Dense(units = 1, kernel_initializer = 'uniform', activation = 'sigmoid'))
sq.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])
 sq.fit(x_train, y_train, batch_size = 32, epochs = 200, validation_data=(x_valid, y_valid))
<choose_model_class><EOS> 
y_pred = sq.predict(X_test_fin)
 y_final =(y_pred > 0.5 ).astype(int ).reshape(X_test_fin.shape[0])
 sub = pd.DataFrame()
sub['PassengerId'] = test['PassengerId']
sub['Survived'] = y_final
sub['Survived'] = sub.apply(lambda r: leaks[int(r['PassengerId'])] if int(r['PassengerId'])in leaks else r['Survived'], axis=1)
 sub.to_csv('submission.csv', index=False)
 print("Your submission was successfully saved!")
 sub.head()