Kaggle competition: Titanic - Machine Learning from Disaster
im = Image.open(train_df["path"][0] )<import_modules>
train_data = pd.read_csv("/kaggle/input/titanic/train.csv")
 train_data
import torch
import torch.nn.functional as F
import torchvision
import torchvision.transforms as transforms
from torch.utils.data import Dataset, DataLoader
from torch.utils.data.dataset import Subset
from sklearn.model_selection import KFold
import matplotlib.image as img<categorify>
t = train_data.loc[:, ['Cabin', 'Survived', 'PassengerId']]
t['initial'] = t['Cabin'].str[0]
t.groupby(['initial', 'Survived'] ).agg('count' )
class CassavaDataset(Dataset):
def __init__(self, dataframe, transform=None):
super().__init__()
self.df = dataframe
self.transform = transform
def __len__(self):
return len(self.df["path"])
 def __getitem__(self, index):
path = self.df["path"][index]
label = self.df["label"][index]
with open(path, "rb")as f:
image = Image.open(f)
 image = image.convert("RGB")
 if self.transform is not None:
image = self.transform(image)
 return image, label<import_modules>
train_data = train_data.loc[:,['PassengerId', 'Survived', 'Pclass', 'Sex', 'Age', 'Parch', 'SibSp', 'Cabin']]
train_data
import random<normalization>
train_data.loc[:, ['Parch', 'Survived', 'PassengerId']].groupby(['Parch', 'Survived'] ).agg('count' )
image_size = 512
mean = [0.485, 0.456, 0.406]
std = [0.229, 0.224, 0.225]
train_transform = transforms.Compose(
 [
transforms.RandomHorizontalFlip(p=0.5),
transforms.RandomVerticalFlip(p=0.5),
transforms.RandomResizedCrop(image_size),
make_mask_image(p=0.5, mask_size=50),
transforms.ToTensor() ,
transforms.Normalize(mean=mean, std=std),
] 
)
 valid_transform = transforms.Compose(
 [
transforms.Resize(( image_size, image_size)) ,
transforms.ToTensor() ,
transforms.Normalize(mean=mean, std=std),
]
)<create_dataframe>
t = train_data.loc[:, ['Age', 'Survived', 'PassengerId']]
t['age_group'] = t['Age'] // 10
t.groupby(['age_group', 'Survived'] ).agg('count' )
dataset = CassavaDataset(train_df, train_transform )<load_from_disk>
train_data.loc[:, ['SibSp', 'Survived', 'PassengerId']].groupby(['SibSp', 'Survived'] ).agg('count' )
path = '.. /input/cassava-leaf-disease-classification/label_num_to_disease_map.json'
with open(path, mode = 'r')as f:
label_to_name = json.load(f )<normalization>
sex_age_ave = train_data.loc[:, ['Sex', 'Age']].groupby(['Sex'] ).agg({'Age':'mean'})
 male_age_ave = sex_age_ave.loc['male'].values[0]
female_age_ave = sex_age_ave.loc['female'].values[0]
print(male_age_ave, female_age_ave )
class Unnormalize(object):
def __init__(self, mean, std):
self.mean = mean
self.std = std
def __call__(self, tensor):
for t, m, s in zip(tensor, self.mean, self.std):
t.mul_(s ).add_(m)
 return tensor<normalization>
def create_age_group(df):
df.loc[(df['Age'] >= 0)&(df['Age'] < 15), ['age_group']] = 0
df.loc[(df['Age'] >= 15)&(df['Age'] < 25), ['age_group']] = 1
df.loc[(df['Age'] >= 25)&(df['Age'] < 65), ['age_group']] = 2
df.loc[(df['Age'] >= 65), ['age_group']] = 3
unnorm = Unnormalize(mean, std )<load_pretrained>
def create_sibsp_group(df):
df.loc[(df['SibSp'] >= 0)&(df['SibSp'] <= 2), ['sibsp_group']] = 0
df.loc[(df['SibSp'] > 2), ['sibsp_group']] = 1
loader = DataLoader(dataset, 16, shuffle = True)
 display_batch(next(iter(loader)) )<import_modules>
def create_parch_group(df):
df['parch_group'] = df['Parch']
import torch
import torch.nn as nn
import torch.nn.functional as F<set_options>
def create_cabin_group(df):
df.loc[(df['Cabin'].isnull())&(df['Pclass'] == 1), ['Cabin']] = 'D'
df.loc[(df['Cabin'].isnull())&(df['Pclass'] >= 2), ['Cabin']] = 'F'
df.loc[:, ['cabin_group']] = df['Cabin'].str[0]
epoch = 3
batch_size = 16
num_classes = 5
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu" )<choose_model_class>
train_data.loc[:, ['age_group', 'Survived']].groupby('age_group' ).agg({'Survived':'sum'} )
resNet = timm.create_model("resnet50", pretrained=False)
 resNet.load_state_dict(torch.load(".. /input/pretrined-models/models/models/pretrained_resNet.pth"))
resNet.fc = nn.Linear(resNet.fc.in_features, num_classes)
 resNet = resNet.to(device )<load_pretrained>
train_data.loc[:, ['parch_group', 'Survived']].groupby('parch_group' ).agg({'Survived':'sum'} )
ef_model = timm.create_model("tf_efficientnet_b2_ns", pretrained=False)
 ef_model.load_state_dict(torch.load(".. /input/pretrined-models/models/models/pretrained_ef_model.pth"))
ef_model.classifier = nn.Linear(ef_model.classifier.in_features, num_classes)
 ef_model = ef_model.to(device )<choose_model_class>
train_data = pd.get_dummies(train_data, columns=['Pclass'], prefix='P')
 train_data = pd.get_dummies(train_data, columns=['age_group'], prefix='AG')
 train_data = pd.get_dummies(train_data, columns=['Sex'], prefix='S')
 train_data = pd.get_dummies(train_data, columns=['parch_group'], prefix='PA')
 train_data = pd.get_dummies(train_data, columns=['sibsp_group'], prefix='SS')
 train_data = pd.get_dummies(train_data, columns=['cabin_group'], prefix='C')
 train_data.drop(columns=['Age'], inplace=True)
 train_data.drop(columns=['SibSp'], inplace=True)
 train_data.drop(columns=['Parch'], inplace=True)
 train_data.drop(columns=['Cabin'], inplace=True )
ef_optimizer = torch.optim.AdamW(ef_model.parameters() , lr=1e-4, weight_decay=0.0001)
 ef_scheduler = torch.optim.lr_scheduler.StepLR(ef_optimizer, step_size=2, gamma=0.1)
 resNet_optimizer = torch.optim.AdamW(resNet.parameters() , lr=1e-4, weight_decay=0.0001)
 resNet_scheduler = torch.optim.lr_scheduler.StepLR(resNet_optimizer, step_size=2, gamma=0.1)
 criterion=nn.CrossEntropyLoss()<categorify>
print(train_data.isnull().sum())
 print(len(train_data))
def calc_correction(model, df):
model.eval()
path = df["path"]
label = df["label"]
count = 0
pred_list = [0, 0, 0, 0, 0]
for i in tqdm(range(len(path))):
image_path = path[i]
image_label = label[i]
image = Image.open(image_path)
 image = valid_transform(image)
 image = image.unsqueeze(0 ).to(device)
 model = model.to(device)
 pred = model(image ).argmax(1 ).item()
pred_list[pred] += 1
if pred == image_label:
count += 1
percent = count / len(path)
 return percent, pred_list<split>
test_data = pd.read_csv("/kaggle/input/titanic/test.csv" )
def train_model(model, dataset, batch_size, optimizer, criterion, scheduler, epoch, model_title):
best_model = None
best_loss = float("inf")
 train_losses, valid_losses = [], []
kf = KFold(n_splits = 5)
 for fold,(train_index, valid_index)in enumerate(kf.split(dataset)) :
print("fold: ", fold)
 train_dataset = Subset(dataset, train_index)
 train_loader = DataLoader(train_dataset, batch_size, shuffle = True, num_workers = 4)
 valid_dataset = Subset(dataset, valid_index)
 valid_loader= DataLoader(valid_dataset, batch_size, shuffle = False)
 for epoch in range(1, epoch + 1):
epoch_start_time = time.time()
acc = []
train_loss = 0
valid_loss = 0
model.train()
for data, target in train_loader:
data = data.to(device)
 target = target.to(device)
 optimizer.zero_grad()
output = model(data)
 loss = criterion(output, target)
 loss.backward()
optimizer.step()
train_loss += loss.item() *len(data)
 train_loss = train_loss/len(train_loader.sampler)
 train_losses.append(train_loss)
 model.eval()
for data, target in valid_loader:
data = data.to(device)
 target = target.to(device)
 with torch.no_grad() :
output = model(data)
 pred =(output.argmax(1)== target)
 acc.append(sum(pred)/len(pred))
loss = criterion(output, target)
 valid_loss += loss.item() *len(data)
 if valid_loss < best_loss:
best_loss = valid_loss
best_model = model
scheduler.step()
collection = sum(acc)/len(acc)
 valid_loss = valid_loss/len(valid_loader.sampler)
 valid_losses.append(valid_loss)
 print('Time: {:.3f}\t Epoch: {} \tTraining Loss: {:.3f} \tValidation Loss: {:.3f} \t Acc: {:.2f}' 
.format(time.time() - epoch_start_time, epoch, train_loss, valid_loss, collection))
num_collection = []
torch.save(best_model.state_dict() , model_title)
 return model, train_losses, valid_losses<train_model>
test_data = test_data.loc[:,['PassengerId', 'Pclass', 'Sex', 'Age','Parch', 'SibSp', 'Cabin']]
train_models(resNet, ef_model )<load_pretrained>
test_data = pd.get_dummies(test_data, columns=['Pclass'], prefix='P')
 test_data = pd.get_dummies(test_data, columns=['age_group'], prefix='AG')
 test_data = pd.get_dummies(test_data, columns=['Sex'], prefix='S')
 test_data = pd.get_dummies(test_data, columns=['parch_group'], prefix='PA')
 test_data = pd.get_dummies(test_data, columns=['sibsp_group'], prefix='SS')
 test_data = pd.get_dummies(test_data, columns=['cabin_group'], prefix='C')
 test_data.drop(columns=['Age'], inplace=True)
 test_data.drop(columns=['SibSp'], inplace=True)
 test_data.drop(columns=['Parch'], inplace=True)
 test_data.drop(columns=['Cabin'], inplace=True )
ef_model.load_state_dict(torch.load(".. /input/models/ef_model.pth", map_location = device))
resNet.load_state_dict(torch.load(".. /input/models/res_model.pth", map_location = device))<choose_model_class>
print(test_data.isnull().sum())
 print(len(test_data))
class CassaveClassifier(nn.Module):
def __init__(self, model, ef_model):
super().__init__()
self.model = model
self.ef_model = ef_model
def forward(self, x):
x1 = self.model(x)
 x2 = self.ef_model(x)
 return(0.5 * x1 + 0.5 * x2)
 def test(self, x, rate):
x1 = self.model(x)
 x2 = self.ef_model(x)
 p = rate * x1 +(1 - rate)* x2
return p<choose_model_class>
classifier = CassaveClassifier(resNet, ef_model)
 classifier = classifier.to(device )<define_search_space>
y = train_data["Survived"]
features = ['P_1','P_2','P_3','AG_0.0','AG_1.0','AG_2.0','AG_3.0','S_0', 'S_1']
X = train_data.loc[:, features]
X_train, X_test, y_train, y_test = train_test_split(X, y)
def test_rate() :
for rate in range(1, 10):
classifier.eval()
path = valid_df["path"]
label = valid_df["label"]
count = 0
pred_list = [0, 0, 0, 0, 0]
for i in tqdm(range(len(path))):
image_path = path[i]
image_label = label[i]
image = Image.open(image_path)
 image = valid_transform(image)
 image = image.unsqueeze(0 ).to(device)
 pred = classifier.test(image, rate/10 ).argmax(1 ).item()
pred_list[pred] += 1
if pred == image_label:
count += 1
percent = count / len(path)
 print("rate: ", rate/10)
 print("percent: ", percent )<define_variables>
ss = StandardScaler()
ss.fit_transform(X_train)
 ss.transform(X_test )
path = ".. /input/cassava-leaf-disease-classification/test_images/"<define_variables>
model_lr = LogisticRegression(solver='liblinear', max_iter=1000)
 model_lr.fit(X_train, y_train)
 predictions_lr = model_lr.predict(X_test )
image_path = []
image_id = []
for i in os.listdir(path):
image_id.append(str(i))
image_path.append(path + str(i))<categorify>
score_train = model_lr.score(X_train, y_train)
 score_test = model_lr.score(X_test, y_test)
 print(score_train, score_test )
pred = []
for path in image_path:
image = Image.open(path)
 image = valid_transform(image)
 image = image.unsqueeze(0 ).to(device)
 predict = resNet(image ).argmax(1 ).item()
pred.append(predict )<create_dataframe>
thresholds = model_lr.decision_function(X_train)
 fpr, tpr, thresholds = roc_curve(y_train, thresholds )
sub = pd.DataFrame({"image_id": image_id, "label": pred} )<save_to_csv>
print(precision_score(y_test, predictions_lr))
print(recall_score(y_test, predictions_lr))
sub.to_csv("submission.csv", index=False )<categorify>
model_knn = KNeighborsClassifier(n_neighbors = 10, p = 1)
 model_knn.fit(X_train, y_train)
 predictions_knn = model_knn.predict(X_test )
warnings.filterwarnings(action="ignore")
 class CategoricalFeatures:
def __init__(self,df,categorical_features, encoding_type,handle_na=False):
self.df = df
self.cat_feats = categorical_features
self.enc_type = encoding_type
self.handle_na = handle_na
self.label_encoders = dict()
self.binary_encoders = dict()
self.ohe = None
if self.handle_na:
for c in self.cat_feats:
self.df.loc[:, c] = self.df.loc[:,c].astype(str ).fillna("-9999999")
 self.output_df = self.df.copy(deep=True)
 def _label_encoding(self):
for c in self.cat_feats:
lbl = preprocessing.LabelEncoder()
lbl.fit(self.df[c].values)
 self.output_df.loc[:,c]=lbl.transform(self.df[c].values)
 self.label_encoders[c]=lbl
return self.output_df
def _label_binarization(self):
for c in self.cat_feats:
lbl = preprocessing.LabelBinarizer()
lbl.fit(self.df[c].values)
 val = lbl.transform(self.df[c].values)
 self.output_df = self.output_df.drop(c,axis=1)
 for j in range(val.shape[1]):
new_col_name = c + f"__bin_{j}"
self.output_df[new_col_name] = val[:,j]
self.binary_encoders[c]=lbl
return self.output_df
def _one_hot(self):
ohe = preprocessing.OneHotEncoder()
ohe.fit(self.df[self.cat_feats].values)
 return ohe.transform(self.df[self.cat_feats].values)
 def fit_transform(self):
if self.enc_type=="label":
return self._label_encoding()
elif self.enc_type=="binary":
return self._label_binarization()
elif self.enc_type=="ohe":
return self._one_hot()
else:
raise Exception("Encoding type not understood")
 def transform(self, dataframe):
if self.handle_na:
for c in self.cat_feats:
dataframe.loc[:,c] = dataframe.loc[:,c].astype(str ).fillna("-99999999")
 if self.enc_type=="label":
for c, lbl in self.label_encoders.items() :
dataframe.loc[:,c] = lbl.transform(dataframe[c].values)
 return dataframe
elif self.enc_type =="binary":
for c, lbl in self.binary_encoders.items() :
val = lbl.transform(dataframe[c].values)
 dataframe = dataframe.drop(c, axis=1)
 for j in range(val.shape[1]):
new_col_name = c + f"__bin_{j}"
dataframe[new_col_name] = val[:, j]
return dataframe
elif self.enc_type == "ohe":
return self.ohe(dataframe[self.cat_feats].values)
 else:
raise Exception("Encoding type not understood")
 if __name__=="__main__":
df = pd.read_csv(".. /input/cat-in-the-dat-ii/train.csv")
 df_test = pd.read_csv(".. /input/cat-in-the-dat-ii/test.csv")
 sample = pd.read_csv(".. /input/cat-in-the-dat-ii/sample_submission.csv")
 train_len = len(df)
 df_test["target"] = -1
full_data = pd.concat([df, df_test])
 cols = [c for c in df.columns if c not in["id","target"]]
cat_feats = CategoricalFeatures(full_data,
categorical_features=cols,
encoding_type="ohe",
handle_na=True)
 full_data_transformed = cat_feats.fit_transform()
X = full_data_transformed[:train_len,:]
X_test = full_data_transformed[train_len:,:]
clf = linear_model.LogisticRegression()
clf.fit(X, df.target.values)
 preds = clf.predict_proba(X_test)[:,1]
print(preds)
<save_to_csv>
probabilities = model_knn.predict_proba(X_train)
 fpr, tpr, threshold = roc_curve(y_train, probabilities[:, 1] )
sample.loc[:,"target"] = preds
sample.to_csv("submission_csv", index=False)
<save_to_csv>
score_train = model_knn.score(X_train, y_train)
 score_test = model_knn.score(X_test, y_test)
 print(score_train, score_test )
print(sample.to_csv )<load_from_csv>
model = model_knn
X_test = test_data.loc[:, features]
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!" )