Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<install_modules>
pd.set_option('display.max_columns', 100)
 pd.set_option('display.max_rows', 10)
 pd.set_option('display.width', 1000)
 %pip install autoviz
%pip install ppscore
print("")
 for dirname, _, filenames in os.walk('/kaggle/input'):
for filename in filenames:
print(os.path.join(dirname, filename))
!pip install.. /input/faiss-163/faiss_cpu-1.6.3-cp37-cp37m-manylinux2010_x86_64.whl<load_pretrained>
df = pd.read_csv("/kaggle/input/titanic/train.csv")
 display(df.head(10))
try:
!cd.. /input; python -m cassavacodes.Cassava.retrieval.extract.run --config-file./configs/extract_r50_gq_infer.py --load-path.. /input/wwwwww/weight_epoch_13.pth --log-dir /kaggle/working/log -device 0
except:
pass<load_pretrained>
AV = AutoViz_Class()
report_2 = AV.AutoViz("/kaggle/input/titanic/train.csv" )
np.load('/kaggle/working/features/extract_r50_gq_infer/gallery_targets.npy' )<load_pretrained>
df_survivors = df[df['Survived'] == 1]
df_nonsurvivors = df[df['Survived'] == 0]
gallery_features = np.load('.. /input/gallery/gallery_features.npy')
 gallery_targets = np.load('.. /input/gallery/gallery_targets.npy')
 query_features = np.load('/kaggle/working/features/extract_r50_gq_infer/gallery_features.npy')
 query_names = np.load('/kaggle/working/features/extract_r50_gq_infer/gallery_names.npy')
 faiss.normalize_L2(gallery_features)
 faiss.normalize_L2(query_features)
<correct_missing_values>
t_stat, p_value = ztest(dist_a, dist_b)
 print("----- Z Test Results -----")
 print("T stat.= " + str(t_stat))
print("P value = " + str(p_value))
print("")
 t_stat_2, p_value_2 = stats.ttest_ind(dist_a, dist_b)
 print("----- T Test Results -----")
 print("T stat.= " + str(t_stat_2))
print("P value = " + str(p_value_2))
index = faiss.IndexFlatL2(gallery_features.shape[1])
 index.add(gallery_features)
 k=3
D, I = index.search(gallery_features, k)
 print(gallery_targets[I[-10:]])
 outliers = []
for i, indices in enumerate(I):
if gallery_targets[indices[0]] != gallery_targets[indices[1]]:
outliers.append(i)
 print(gallery_features.shape)
 gallery_features = np.delete(gallery_features , outliers, axis=0)
 gallery_targets = np.delete(gallery_targets , outliers, axis=0)
 print(gallery_features.shape )<save_to_csv>
df_survivors_sex = df_survivors['Sex'].value_counts()
df_survivors_sex = pd.DataFrame({'Sex':df_survivors_sex.index, 'count':df_survivors_sex.values})
 df_nonsurvivors_sex = df_nonsurvivors['Sex'].value_counts()
df_nonsurvivors_sex = pd.DataFrame({'Sex':df_nonsurvivors_sex.index, 'count':df_nonsurvivors_sex.values})
 pie_survivors_sex = go.Pie(
 labels = df_survivors_sex['Sex'],
values = df_survivors_sex['count'],
domain=dict(x=[0, 0.5]),
name='Survivors',
hole = 0.5,
marker = dict(colors=['violet', 'cornflowerblue'], line=dict(color=' 
)
 pie_nonsurvivors_sex = go.Pie(
 labels = df_nonsurvivors_sex['Sex'],
values = df_nonsurvivors_sex['count'],
domain=dict(x=[0.5, 1.0]),
name='non-Survivors',
hole = 0.5,
marker = dict(colors=['cornflowerblue', 'violet'], line=dict(color=' 
)
 data = [pie_survivors_sex, pie_nonsurvivors_sex]
layout = go.Layout(
 paper_bgcolor='rgba(0,0,0,0)',
plot_bgcolor='rgba(0,0,0,0)',
title='"Sex" percentage from Survivors vs non-Survivors',
annotations=[dict(text='Survivors', x=0.18, y=0.5, font_size=15, showarrow=False),
dict(text='Non-Survivors', x=0.85, y=0.5, font_size=15, showarrow=False)] 
)
 fig = go.Figure(data=data, layout=layout)
 fig.show()
index = faiss.IndexFlatL2(gallery_features.shape[1])
 index.add(gallery_features)
 k=1
D, I = index.search(query_features, k)
 f = open('submission.csv', 'w')
 f.write('image_id,label
')
 for fn, i in zip(query_names, I):
fn = fn.split('/')[-1]
pred = gallery_targets[i][0]
f.write(fn + ',' + str(pred)+ '
')
 f.close()<load_from_csv>
df_survivors_pclass = df_survivors['Pclass'].value_counts()
df_survivors_pclass = pd.DataFrame({'Pclass':df_survivors_pclass.index, 'count':df_survivors_pclass.values})
 df_nonsurvivors_pclass = df_nonsurvivors['Pclass'].value_counts()
df_nonsurvivors_pclass = pd.DataFrame({'Pclass':df_nonsurvivors_pclass.index, 'count':df_nonsurvivors_pclass.values})
 pie_survivors_pclass = go.Pie(
 labels = df_survivors_pclass['Pclass'],
values = df_survivors_pclass['count'],
domain=dict(x=[0, 0.5]),
name='Survivors',
hole = 0.5,
marker = dict(colors=[' 
)
 pie_nonsurvivors_pclass = go.Pie(
 labels = df_nonsurvivors_pclass['Pclass'],
values = df_nonsurvivors_pclass['count'],
domain=dict(x=[0.5, 1.0]),
name='non-Survivors',
hole = 0.5,
marker = dict(colors=[' 
)
 data = [pie_survivors_pclass, pie_nonsurvivors_pclass]
layout = go.Layout(
 paper_bgcolor='rgba(0,0,0,0)',
plot_bgcolor='rgba(0,0,0,0)',
title='"Pclass" percentage from Survivors vs non-Survivors',
annotations=[dict(text='Survivors', x=0.18, y=0.5, font_size=15, showarrow=False),
dict(text='Non-Survivors', x=0.85, y=0.5, font_size=15, showarrow=False)] 
)
 fig = go.Figure(data=data, layout=layout)
 fig.show()
f = open('submission.csv', 'r')
 print(f.readlines())
 f.close()<define_variables>
t_stat_3, p_value_3 = ztest(dist_c, dist_d)
 print("----- Z Test Results -----")
 print("T stat.= " + str(t_stat_3))
print("P value = " + str(p_value_3))
print("")
 t_stat_4, p_value_4 = stats.ttest_ind(dist_c, dist_d)
 print("----- T Test Results -----")
 print("T stat.= " + str(t_stat_4))
print("P value = " + str(p_value_4))
package_paths = [
'.. /input/pytorch-image-models/pytorch-image-models-master',
'.. /input/image-fmix/FMix-master'
]
for pth in package_paths:
sys.path.append(pth)
<import_modules>
import collections
import matplotlib.pyplot as plt
import seaborn as sns
from scipy import stats
from imblearn.combine import SMOTEENN
from imblearn.over_sampling import SMOTE
from imblearn.under_sampling import RandomUnderSampler
from category_encoders import OneHotEncoder, TargetEncoder, LeaveOneOutEncoder
from sklearn.pipeline import Pipeline
from sklearn.compose import ColumnTransformer
from sklearn.experimental import enable_iterative_imputer
from sklearn.impute import SimpleImputer, IterativeImputer
from sklearn.model_selection import StratifiedKFold, StratifiedShuffleSplit, RandomizedSearchCV, train_test_split
from sklearn.feature_selection import SelectPercentile, SelectFromModel
from sklearn.preprocessing import StandardScaler, MinMaxScaler, PolynomialFeatures
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score, log_loss, precision_recall_curve, average_precision_score, roc_curve, roc_auc_score
from sklearn.neighbors import KNeighborsClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier, StackingClassifier, VotingClassifier
from sklearn.svm import SVC
from sklearn.decomposition import PCA
from sklearn.manifold import TSNE
from xgboost import XGBClassifier, plot_importance as plot_importance_xgb
from lightgbm import LGBMClassifier, plot_importance as plot_importance_lgbm
from glob import glob
from sklearn.model_selection import GroupKFold, StratifiedKFold
import cv2
from skimage import io
import torch
from torch import nn
import os
from datetime import datetime
import time
import random
import cv2
import torchvision
from torchvision import transforms
import pandas as pd
import numpy as np
from tqdm import tqdm
import matplotlib.pyplot as plt
from torch.utils.data import Dataset,DataLoader
from torch.utils.data.sampler import SequentialSampler, RandomSampler
from torch.cuda.amp import autocast, GradScaler
from torch.nn.modules.loss import _WeightedLoss
import torch.nn.functional as F
import timm
import sklearn
import warnings
import joblib
from sklearn.metrics import roc_auc_score, log_loss
from sklearn import metrics
import warnings
import cv2
import pydicom
from scipy.ndimage.interpolation import zoom
from torchvision.datasets import ImageFolder<init_hyperparams>
df['AgeCat'] = ''
df['AgeCat'].loc[(df['Age'] < 18)] = 'young'
df['AgeCat'].loc[(df['Age'] >= 18)&(df['Age'] < 56)] = 'mature'
df['AgeCat'].loc[(df['Age'] >= 56)] = 'senior'
df['FamilySize'] = ''
df['FamilySize'].loc[(df['SibSp'] <= 2)] = 'small'
df['FamilySize'].loc[(df['SibSp'] > 2)&(df['SibSp'] <= 5)] = 'medium'
df['FamilySize'].loc[(df['SibSp'] > 5)] = 'large'
df['IsAlone'] = ''
df['IsAlone'].loc[(( df['SibSp'] + df['Parch'])> 0)] = 'no'
df['IsAlone'].loc[(( df['SibSp'] + df['Parch'])== 0)] = 'yes'
df['SexCat'] = ''
df['SexCat'].loc[(df['Sex'] == 'male')&(df['Age'] <= 21)] = 'youngmale'
df['SexCat'].loc[(df['Sex'] == 'male')&(( df['Age'] > 21)&(df['Age'])< 50)] = 'maturemale'
df['SexCat'].loc[(df['Sex'] == 'male')&(df['Age'] > 50)] = 'seniormale'
df['SexCat'].loc[(df['Sex'] == 'female')&(df['Age'] <= 21)] = 'youngfemale'
df['SexCat'].loc[(df['Sex'] == 'female')&(( df['Age'] > 21)&(df['Age'])< 50)] = 'maturefemale'
df['SexCat'].loc[(df['Sex'] == 'female')&(df['Age'] > 50)] = 'seniorfemale'
df['Title'] = df['Name'].str.split(', ', expand=True)[1].str.split('.', expand=True)[0]
df['Is_Married'] = 0
df['Is_Married'].loc[df['Title'] == 'Mrs'] = 1
df['Title'] = df['Title'].replace(['Miss', 'Mrs','Ms', 'Mlle', 'Lady', 'Mme', 'the Countess', 'Dona'], 'Miss/Mrs/Ms')
 df['Title'] = df['Title'].replace(['Dr', 'Col', 'Major', 'Jonkheer', 'Capt', 'Sir', 'Don', 'Rev'], 'Dr/Military/Noble/Clergy')
 df['Ticket_Frequency'] = df.groupby('Ticket')['Ticket'].transform('count')
 df.head(10 )
CFG = {
'fold_num': 5,
'seed': 719,
'model_arch': 'tf_efficientnet_b4_ns',
'img_size': 512,
'epochs': 10,
'train_bs': 16,
'valid_bs': 32,
'T_0': 10,
'lr': 1e-4,
'min_lr': 1e-6,
'weight_decay':1e-6,
'num_workers': 4,
'accum_iter': 2,
'verbose_step': 1,
'device': 'cuda:0'
}<normalization>
def get_feature_names(df):
target = df['Survived']
df.drop(['PassengerId', 'Survived', 'Ticket', 'Name', 'Cabin'], axis=1, inplace=True)
 categorical_df = df.select_dtypes(include=['object'])
 numeric_df = df.select_dtypes(exclude=['object'])
 categorical_columns = list(categorical_df.columns)
 numeric_columns = list(numeric_df.columns)
 print("Categorical columns:
", categorical_columns)
 print("
Numeric columns:
", numeric_columns)
 return target, categorical_columns, numeric_columns
target, categorical_columns, numeric_columns = get_feature_names(df )
HorizontalFlip, VerticalFlip, IAAPerspective, ShiftScaleRotate, CLAHE, RandomRotate90,
Transpose, ShiftScaleRotate, Blur, OpticalDistortion, GridDistortion, HueSaturationValue,
IAAAdditiveGaussianNoise, GaussNoise, MotionBlur, MedianBlur, IAAPiecewiseAffine, RandomResizedCrop,
IAASharpen, IAAEmboss, RandomBrightnessContrast, Flip, OneOf, Compose, Normalize, Cutout, CoarseDropout, ShiftScaleRotate, CenterCrop, Resize 
)
 def get_valid_transforms() :
return Compose([
CenterCrop(CFG['img_size'], CFG['img_size'], p=1.) ,
Resize(CFG['img_size'], CFG['img_size']),
Normalize(mean=[0.430, 0.497, 0.314], std=[0.211, 0.217, 0.193], max_pixel_value=255.0, p=1.0),
ToTensorV2(p=1.0),
], p=1.)<load_from_csv>
def balancingClassesRus(x_train, y_train):
rus = RandomUnderSampler(random_state=7)
 features_balanced, target_balanced = rus.fit_resample(x_train, y_train)
 print("Count for each class value after RandomUnderSampler:", collections.Counter(target_balanced))
return features_balanced, target_balanced
def balancingClassesSmoteenn(x_train, y_train):
smn = SMOTEENN(random_state=7)
 features_balanced, target_balanced = smn.fit_resample(x_train, y_train)
 print("Count for each class value after SMOTEEN:", collections.Counter(target_balanced))
return features_balanced, target_balanced
def balancingClassesSmote(x_train, y_train):
sm = SMOTE(random_state=7)
 features_balanced, target_balanced = sm.fit_resample(x_train, y_train)
 print("Count for each class value after SMOTE:", collections.Counter(target_balanced))
return features_balanced, target_balanced
class test_dataset(Dataset):
def __init__(self,transforms=None):
self.transforms = transforms
self.image_name = pd.read_csv('.. /input/cassava-leaf-disease-classification/sample_submission.csv')['image_id']
def __len__(self):
return len(self.image_name)
 def __getitem__(self,index):
img_name = self.image_name[index]
img = get_img('.. /input/cassava-leaf-disease-classification/test_images/{}'.format(img_name))
if self.transforms:
img = self.transforms(image=img)['image']
return index,img, img_name 
<choose_model_class>
def testSetResultsClassifier(classifier, x_test, y_test):
results = []
predictions = classifier.predict(x_test)
 predicted_probas_class1 = classifier.predict_proba(x_test)[:, 1]
roc_auc = roc_auc_score(y_test, predicted_probas_class1)
 avg_precision = average_precision_score(y_test, predicted_probas_class1)
 accuracy = accuracy_score(y_test, predictions)
 precision = precision_score(y_test, predictions)
 recall = recall_score(y_test, predictions)
 f1 = f1_score(y_test, predictions)
 results.append(accuracy)
 results.append(precision)
 results.append(recall)
 results.append(f1)
 results.append(avg_precision)
 results.append(roc_auc)
 print("
")
 print("Accuracy:", round(results[0], 3))
print("Precision:", round(results[1], 3))
print("Recall:", round(results[2], 3))
print("F1-Score:", round(results[3], 3))
print("Average Precision(Precision/Recall AUC):", round(results[4], 3))
print("ROC_AUC:", round(results[5], 3))
return results
class CassvaImgClassifier(nn.Module):
def __init__(self, model_arch, n_class, pretrained=False):
super().__init__()
self.model = timm.create_model(model_arch, pretrained=pretrained)
 n_features = self.model.classifier.in_features
self.model.classifier = nn.Linear(n_features, n_class)

def forward(self, x):
x = self.model(x)
 return x<load_pretrained>
def defineBestModelPipeline(df, target, numeric_columns, categorical_columns):
x_train, x_test, y_train, y_test = train_test_split(df, target, test_size=0.10, random_state=42)
 y_train = y_train.to_numpy()
y_test = y_test.to_numpy()
numeric_transformer_1 = Pipeline(steps=[('imp', IterativeImputer(max_iter=30, random_state=42)) , 
('scaler', MinMaxScaler())])
 numeric_transformer_2 = Pipeline(steps=[('imp', SimpleImputer(strategy='mean')) , 
('scaler', StandardScaler())])
 categorical_transformer_1 = Pipeline(steps=[('frequent', SimpleImputer(strategy='most_frequent')) , 
('onehot', OneHotEncoder(use_cat_names=True)) ])
 categorical_transformer_2 = Pipeline(steps=[('frequent', SimpleImputer(strategy='most_frequent')) , 
('targetencoder', TargetEncoder(min_samples_leaf=3, smoothing=3)) ])
 data_transformations_1 = ColumnTransformer(transformers=[('num', numeric_transformer_1, numeric_columns), 
('cat', categorical_transformer_1, categorical_columns)])
 data_transformations_2 = ColumnTransformer(transformers=[('num', numeric_transformer_1, numeric_columns), 
('cat', categorical_transformer_2, categorical_columns)])
 data_transformations_3 = ColumnTransformer(transformers=[('num', numeric_transformer_2, numeric_columns), 
('cat', categorical_transformer_1, categorical_columns)])
 data_transformations_4 = ColumnTransformer(transformers=[('num', numeric_transformer_2, numeric_columns), 
('cat', categorical_transformer_2, categorical_columns)])
 pipe = Pipeline(steps=[('data_transformations', data_transformations_1), 
('feature_transf', PCA()), 
('clf', SVC())])
 params_grid = [
{'data_transformations': [data_transformations_2, data_transformations_4],
'feature_transf': [SelectPercentile(percentile=75),
SelectFromModel(DecisionTreeClassifier() , threshold=-np.inf, max_features=x_train.shape[1]*0.75),
PCA(n_components=round(x_train.shape[1]*0.75)) ,
PCA(0.75)],
'clf': [KNeighborsClassifier() ],
'clf__n_neighbors': stats.randint(1, 30),
'clf__metric': ['minkowski', 'euclidean']},
{'data_transformations': [data_transformations_1, data_transformations_3],
'feature_transf': [None],
'clf': [LogisticRegression() ],
'clf__penalty': ['l1', 'l2'],
'clf__C': stats.uniform(0.01, 10)},
{'data_transformations': [data_transformations_1, data_transformations_3],
'feature_transf': [None],
'clf': [SVC() ],
'clf__C': stats.uniform(0.1, 10),
'clf__gamma': stats.uniform(0.1, 10),
'clf__kernel': ['linear', 'poly', 'rbf']},
{'data_transformations': [data_transformations_2, data_transformations_4],
'feature_transf': [None],
'clf': [DecisionTreeClassifier() ],
'clf__criterion': ['gini', 'entropy'],
'clf__max_features': [None, "sqrt", "log2"],
'clf__max_depth': [None, stats.randint(1, 7)]},
{'data_transformations': [data_transformations_2, data_transformations_4],
'feature_transf': [None],
'clf': [RandomForestClassifier() ],
'clf__n_estimators': stats.randint(10, 175),
'clf__max_features': [None, "auto", "log2"],
'clf__max_depth': [None, stats.randint(1, 5)],
'clf__random_state': stats.randint(1, 49)},
{'data_transformations': [data_transformations_2, data_transformations_4],
'feature_transf': [None],
'clf': [LGBMClassifier() ],
'clf__n_estimators': stats.randint(1, 100),
'clf__learning_rate': stats.uniform(0.01, 0.7),
'clf__max_depth': [None, stats.randint(1, 6)]},
{'data_transformations': [data_transformations_2, data_transformations_4],
'feature_transf': [None],
'clf': [XGBClassifier() ],
'clf__n_estimators': stats.randint(5, 125),
'clf__eta': stats.uniform(0.01, 1),
'clf__max_depth': [None, stats.randint(1, 6)],
'clf__gamma': stats.uniform(0.01, 1)},
]
metrics = ['accuracy', 'precision', 'recall', 'f1', 'average_precision', 'roc_auc']
cross_validator = StratifiedShuffleSplit(n_splits=10, train_size=0.8, test_size=0.2, random_state=49)
 best_model_pipeline = RandomizedSearchCV(estimator=pipe, param_distributions=params_grid,
n_iter=50, scoring=metrics, refit='roc_auc',
n_jobs=-1, cv=cross_validator, random_state=7)
 best_model_pipeline.fit(x_train, y_train)
 print("
", best_model_pipeline.best_estimator_[0])
 print("
", best_model_pipeline.best_estimator_[1])
 print("
", best_model_pipeline.best_estimator_[2])
 print("
", best_model_pipeline.best_score_)
 return x_train, x_test, y_train, y_test, best_model_pipeline
if __name__ == '__main__':
test_transforms = get_valid_transforms()
test_ds = test_dataset(test_transforms)
 test_dataloader = torch.utils.data.DataLoader(
 test_ds,
batch_size=1,
num_workers=0,
shuffle=False,
pin_memory=False, 
)
 device = torch.device('cuda:0')
 model = CassvaImgClassifier(model_arch=CFG['model_arch'],n_class=5 ).to(device)
 model.load_state_dict(torch.load('.. /input/pytorch-efficientnet-baseline-train-amp-aug/tf_efficientnet_b4_ns_fold_4_4'))
submission = pd.DataFrame(columns=('image_id','label'))
for i, img ,img_name in test_dataloader:
model.eval()
img = img.to(device)
 index = i.item()
label = torch.argmax(model(img)).item()
submission = submission.append([{'image_id':img_name[0],'label':label}],ignore_index=True)
 submission.to_csv('submission.csv',index=False )<import_modules>
x_test = best_model_pipeline.best_estimator_[0].transform(x_test)
 precision, recall, thresholds_prc = precision_recall_curve(y_test, best_model_pipeline.best_estimator_[2].predict_proba(x_test)[:, 1])
 closest_to_025_prc = np.argmin(np.abs(thresholds_prc - 0.25))
closest_to_default_prc = np.argmin(np.abs(thresholds_prc - 0.5))
closest_to_075_prc = np.argmin(np.abs(thresholds_prc - 0.75))
plt.plot(precision, recall, label="Results, Pecision Recall Curve")
 plt.plot(precision[closest_to_025_prc], recall[closest_to_025_prc], 'v', c='k',
markersize=10, label="Threshold 0.25", fillstyle="none", mew=2)
 plt.plot(precision[closest_to_default_prc], recall[closest_to_default_prc], 's', c='k',
markersize=10, label="Default threshold(0.5)", fillstyle="none", mew=2)
 plt.plot(precision[closest_to_075_prc], recall[closest_to_075_prc], '^', c='k',
markersize=10, label="Threshold 0.75", fillstyle="none", mew=2)
 plt.title("Precision-Recall Curve for the best model found")
 plt.xlabel("Precision: TP /(TP + FP)")
 plt.ylabel("Recall: TP /(TP + FN)")
 plt.legend(loc="best" )
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import plotly.express as px
import os
import cv2
from PIL import Image
import keras
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
import tensorflow as tf
from tensorflow.keras import models, layers
from tensorflow.keras.preprocessing import image
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.callbacks import ModelCheckpoint, EarlyStopping, ReduceLROnPlateau
from tensorflow.keras.applications import EfficientNetB0, Xception
from tensorflow.keras.optimizers import Adam<define_variables>
fpr, tpr, thresholds_roc = roc_curve(y_test, best_model_pipeline.best_estimator_[2].predict_proba(x_test)[:, 1])
 closest_to_025_roc = np.argmin(np.abs(thresholds_roc - 0.25))
closest_to_default_roc = np.argmin(np.abs(thresholds_roc - 0.5))
closest_to_075_roc = np.argmin(np.abs(thresholds_roc - 0.75))
plt.plot(fpr, tpr, label="Results, ROC Curve")
 plt.plot(fpr[closest_to_025_roc], tpr[closest_to_025_roc], 'v', c='k',
markersize=10, label="Threshold 0.25", fillstyle="none", mew=2)
 plt.plot(fpr[closest_to_default_roc], tpr[closest_to_default_roc], 's', c='k',
markersize=10, label="Default threshold(0.5)", fillstyle="none", mew=2)
 plt.plot(fpr[closest_to_075_roc], tpr[closest_to_075_roc], '^', c='k',
markersize=10, label="Threshold 0.75", fillstyle="none", mew=2)
 plt.title("ROC Curve for the best model found")
 plt.xlabel("FPR: FP /(FP + TN)")
 plt.ylabel("TPR(Recall): TP /(TP + FN)")
 plt.legend(loc="best" )
input_dir = ".. /input/cassava-leaf-disease-classification"
train_images_path = os.path.join(input_dir,"train_images")
 test_images_path = os.path.join(input_dir,'test_images' )<load_from_csv>
df_results = pd.DataFrame(best_model_pipeline.cv_results_)
 display(df_results )
train = pd.read_csv('.. /input/cassava-leaf-disease-classification/train.csv')
 train.head(10 )<categorify>
feature_names_in_order = numeric_columns + categorical_columns
print(feature_names_in_order )
image_list = train['image_id'].to_list()
label_list = train['label'].to_list()<load_pretrained>
Features chosen by Feature Selector:", best_model_pipeline.best_estimator_.named_steps['feature_transf'].get_support())
with open(".. /input/cassava-leaf-disease-classification/label_num_to_disease_map.json")as f:
class_mapping = json.load(f)
 class_mapping2 ={int(k):v for k,v in class_mapping.items() }
class_mapping2<define_variables>
df_test = pd.read_csv("/kaggle/input/titanic/test.csv")
 df_test['AgeCat'] = ''
df_test['AgeCat'].loc[(df_test['Age'] < 18)] = 'young'
df_test['AgeCat'].loc[(df_test['Age'] >= 18)&(df_test['Age'] < 56)] = 'mature'
df_test['AgeCat'].loc[(df_test['Age'] >= 56)] = 'senior'
df_test['FamilySize'] = ''
df_test['FamilySize'].loc[(df_test['SibSp'] <= 2)] = 'small'
df_test['FamilySize'].loc[(df_test['SibSp'] > 2)&(df_test['SibSp'] <= 5)] = 'medium'
df_test['FamilySize'].loc[(df_test['SibSp'] > 5)] = 'large'
df_test['IsAlone'] = ''
df_test['IsAlone'].loc[(( df_test['SibSp'] + df_test['Parch'])> 0)] = 'no'
df_test['IsAlone'].loc[(( df_test['SibSp'] + df_test['Parch'])== 0)] = 'yes'
df_test['SexCat'] = ''
df_test['SexCat'].loc[(df_test['Sex'] == 'male')&(df_test['Age'] <= 21)] = 'youngmale'
df_test['SexCat'].loc[(df_test['Sex'] == 'male')&(( df_test['Age'] > 21)&(df_test['Age'])< 50)] = 'maturemale'
df_test['SexCat'].loc[(df_test['Sex'] == 'male')&(df_test['Age'] > 50)] = 'seniormale'
df_test['SexCat'].loc[(df_test['Sex'] == 'female')&(df_test['Age'] <= 21)] = 'youngfemale'
df_test['SexCat'].loc[(df_test['Sex'] == 'female')&(( df_test['Age'] > 21)&(df_test['Age'])< 50)] = 'maturefemale'
df_test['SexCat'].loc[(df_test['Sex'] == 'female')&(df_test['Age'] > 50)] = 'seniorfemale'
df_test['Title'] = df_test['Name'].str.split(', ', expand=True)[1].str.split('.', expand=True)[0]
df_test['Is_Married'] = 0
df_test['Is_Married'].loc[df['Title'] == 'Mrs'] = 1
df_test['Title'] = df_test['Title'].replace(['Miss', 'Mrs','Ms', 'Mlle', 'Lady', 'Mme', 'the Countess', 'Dona'], 'Miss/Mrs/Ms')
 df_test['Title'] = df_test['Title'].replace(['Dr', 'Col', 'Major', 'Jonkheer', 'Capt', 'Sir', 'Don', 'Rev'], 'Dr/Military/Noble/Clergy')
 df_test['Ticket_Frequency'] = df_test.groupby('Ticket')['Ticket'].transform('count')
 df_test.drop(['PassengerId', 'Name', 'Ticket', 'Cabin'], axis=1, inplace=True )
BATCH_SIZE =8
STEPS_PER_EPOCH = len(train)*0.8 / BATCH_SIZE
VALIDATION_STEPS = len(train)*0.2 / BATCH_SIZE
EPOCHS = 20
TARGET_SIZE = 350<data_type_conversions>
test_predictions = best_model_pipeline.predict(df_test)
 print(test_predictions )
train.label = train.label.astype('str')
 train_datagen = ImageDataGenerator(validation_split = 0.2,
rotation_range = 45,
zoom_range = 0.3,
horizontal_flip = True,
vertical_flip = True,
fill_mode = 'nearest',
shear_range = 0.1,
height_shift_range = 0.1,
width_shift_range = 0.1,
featurewise_center = True,
featurewise_std_normalization = True)
 train_generator = train_datagen.flow_from_dataframe(train,
directory = os.path.join('.. /input/cassava-leaf-disease-classification/train_images'),
subset = "training",
x_col = "image_id",
y_col = "label",
target_size =(TARGET_SIZE, TARGET_SIZE),
batch_size = BATCH_SIZE,
class_mode = "sparse",
shuffle= True)
 validation_datagen = ImageDataGenerator(validation_split = 0.2)
 validation_generator = validation_datagen.flow_from_dataframe(train,
directory = os.path.join('.. /input/cassava-leaf-disease-classification/train_images'),
subset = "validation",
x_col = "image_id",
y_col = "label",
target_size =(TARGET_SIZE, TARGET_SIZE),
batch_size = BATCH_SIZE,
class_mode = "sparse" )<train_model>
df_submission = pd.read_csv("/kaggle/input/titanic/test.csv")
 df_submission['Survived'] = test_predictions
df_submission.drop(df_submission.columns.difference(['PassengerId', 'Survived']), axis=1, inplace=True)
 df_submission.head(10 )
img_path = os.path.join('.. /input/cassava-leaf-disease-classification/train_images/1003442061.jpg')
 img = image.load_img(img_path, target_size =(TARGET_SIZE, TARGET_SIZE))
img_tensor = image.img_to_array(img)
 img_tensor = np.expand_dims(img_tensor, axis = 0)
 img_tensor /= 255.
 plt.imshow(img_tensor[0])
 plt.show()<prepare_x_and_y>
df_submission.count()
generator = train_datagen.flow_from_dataframe(train.iloc[17:18],
directory = os.path.join('.. /input/cassava-leaf-disease-classification/train_images'),
x_col = "image_id",
y_col = "label",
target_size =(TARGET_SIZE, TARGET_SIZE),
batch_size = BATCH_SIZE,
class_mode = "sparse")
 aug_images = [generator[0][0][0]/255 for i in range(10)]
fig, axes = plt.subplots(2, 5, figsize =(20, 10))
axes = axes.flatten()
for img, ax in zip(aug_images, axes):
ax.imshow(img)
 plt.tight_layout()
plt.show()<choose_model_class>
df_submission.to_csv('submission.csv', index=False )
def create_model() :
conv_base = Xception(include_top=False, input_tensor=None,
pooling=None, input_shape=(TARGET_SIZE, TARGET_SIZE, 3), classifier_activation='softmax')
 model = conv_base.output
model = layers.GlobalAveragePooling2D()(model)
 model = layers.Dense(5, activation = "softmax" )(model)
 model = models.Model(conv_base.input, model)
 model.compile(optimizer = Adam(lr = 0.001),
loss = "sparse_categorical_crossentropy",
metrics = ["acc"])
 return model<load_pretrained>
def definePCAPipeline(numeric_columns):
numeric_transformer = Pipeline(steps=[('imp', SimpleImputer(strategy='mean')) , 
('scaler', StandardScaler())])
 data_transformations = ColumnTransformer(transformers=[('num', numeric_transformer, numeric_columns)])
 pca_pipeline = Pipeline(steps=[('data_transformations', data_transformations), 
('feature_transf', PCA(n_components=2, whiten=True)) ])
 return pca_pipeline
model = keras.models.load_model('.. /input/xception-best-weights/Xception_best_weights.h5' )<load_from_csv>
pca_df = pd.DataFrame(pca_arr, columns=["PC1", "PC2"])
 pca_df['Survived'] = target
pca_df.head(10 )
submission_file = pd.read_csv(os.path.join('.. /input/cassava-leaf-disease-classification/sample_submission.csv'))
submission_file<predict_on_test>
transformed_df = pca_pipeline[0].transform(df)
 tsne = TSNE(n_components=2, random_state=42)
 tsne_arr = tsne.fit_transform(transformed_df )
preds = []
for image_id in submission_file.image_id:
image = Image.open(os.path.join(f'.. /input/cassava-leaf-disease-classification/test_images/{image_id}'))
image = image.resize(( TARGET_SIZE, TARGET_SIZE))
image = np.expand_dims(image, axis = 0)
 preds.append(np.argmax(model.predict(image)))
 submission_file['label'] = preds
submission_file<save_to_csv>
tsne_df = pd.DataFrame(tsne_arr, columns=["tsne_dim1", "tsne_dim2"])
 tsne_df['Survived'] = target
tsne_df.head(10 )
submission_file.to_csv('submission.csv', index = False )<set_options>
def findOptimalNClustersKMeans(transformed_df):
range_n_clusters = [2, 3, 4, 5, 6, 7, 8, 9, 10]
silhouette_scores = []
for n_clusters in range_n_clusters:
kmeans = KMeans(n_clusters=n_clusters, random_state=7)
 cluster_labels = kmeans.fit_predict(transformed_df)
 silhouette_avg = silhouette_score(transformed_df, cluster_labels)
 print("K-Means: for n_clusters =", n_clusters, ", the average silhouette_score is", silhouette_avg)
 silhouette_scores.append(silhouette_avg)
 return range_n_clusters, silhouette_scores
range_n_clusters, silhouette_scores = findOptimalNClustersKMeans(transformed_df )
warnings.simplefilter("ignore")
<train_model>
kmeans = KMeans(n_clusters=2, random_state=7)
 kmeans_defined_clusters = kmeans.fit_predict(transformed_df)
 pca_df['KMeans_Defined_Clusters'] = ''
pca_df['KMeans_Defined_Clusters'] = kmeans_defined_clusters
pca_df['KMeans_Defined_Clusters'] = pca_df['KMeans_Defined_Clusters'].astype(str)
 pca_df.head(10 )
print('Train images: %d' %len(os.listdir(
 os.path.join(WORK_DIR, "train_images"))))<load_from_disk>
pca_cluster_centers = pca_pipeline[1].transform(kmeans.cluster_centers_)
 centroids_df = pd.DataFrame(pca_cluster_centers, columns=["X_coord", "Y_coord"])
 centroids_df['cluster_centroid'] = ""
centroids_df['cluster_centroid'][0] = 'centroid, cluster 0'
centroids_df['cluster_centroid'][1] = 'centroid, cluster 1'
centroids_df.head(10 )
with open(os.path.join(WORK_DIR, "label_num_to_disease_map.json")) as file:
print(json.dumps(json.loads(file.read()), indent=4))<load_from_csv>
def findOptimalNClustersAC(transformed_df):
range_n_clusters = [2, 3, 4, 5, 6, 7, 8, 9, 10]
silhouette_scores = []
for n_clusters in range_n_clusters:
ac = AgglomerativeClustering(n_clusters=n_clusters)
 cluster_labels = ac.fit_predict(transformed_df)
 silhouette_avg = silhouette_score(transformed_df, cluster_labels)
 print("Agglomerative Clustering: for n_clusters =", n_clusters, ", the average silhouette_score is", silhouette_avg)
 silhouette_scores.append(silhouette_avg)
 return range_n_clusters, silhouette_scores
range_n_clusters_ac, silhouette_scores_ac = findOptimalNClustersAC(transformed_df )
train_labels = pd.read_csv(os.path.join(WORK_DIR, "train.csv"))
train_labels.head()<define_variables>
ac = AgglomerativeClustering(n_clusters=2)
 ac_defined_clusters = ac.fit_predict(transformed_df)
 pca_df['AgglomerativeClustering_Defined_Clusters'] = ''
pca_df['AgglomerativeClustering_Defined_Clusters'] = ac_defined_clusters
pca_df['AgglomerativeClustering_Defined_Clusters'] = pca_df['AgglomerativeClustering_Defined_Clusters'].astype(str)
 pca_df.head(10 )
BATCH_SIZE = 8
STEPS_PER_EPOCH = len(train_labels)*0.7 / BATCH_SIZE
VALIDATION_STEPS = len(train_labels)*0.3 / BATCH_SIZE
EPOCHS = 20
TARGET_SIZE = 512<data_type_conversions>
dbscan = DBSCAN(min_samples=20, eps=2.4)
 dbscan_defined_clusters = dbscan.fit_predict(transformed_df)
 silhouette_dbscan = silhouette_score(transformed_df, dbscan_defined_clusters)
 print("DBSCAN: for eps = 2.4 and min_samples = 20, the average silhouette_score is", silhouette_dbscan )
train_labels.label = train_labels.label.astype('str')
 train_datagen = ImageDataGenerator(featurewise_center=True,
featurewise_std_normalization=True,
validation_split = 0.3,
preprocessing_function = None,
rotation_range = 45,
zoom_range = 0.3,
horizontal_flip = True,
vertical_flip = True,
fill_mode = 'nearest',
shear_range = 0.1,
height_shift_range = 0.1,
width_shift_range = 0.1 
)
 train_generator = train_datagen.flow_from_dataframe(train_labels,
directory = os.path.join(WORK_DIR, "train_images"),
subset = "training",
x_col = "image_id",
y_col = "label",
target_size =(TARGET_SIZE, TARGET_SIZE),
batch_size = BATCH_SIZE,
class_mode = "sparse")
 validation_datagen = ImageDataGenerator(validation_split = 0.3)
 validation_generator = validation_datagen.flow_from_dataframe(train_labels,
directory = os.path.join(WORK_DIR, "train_images"),
subset = "validation",
x_col = "image_id",
y_col = "label",
target_size =(TARGET_SIZE, TARGET_SIZE),
batch_size = BATCH_SIZE,
class_mode = "sparse" )<prepare_x_and_y>
pca_df['DBSCAN_Defined_Clusters'] = ''
pca_df['DBSCAN_Defined_Clusters'] = dbscan_defined_clusters
pca_df['DBSCAN_Defined_Clusters'] = pca_df['DBSCAN_Defined_Clusters'].astype(str)
 pca_df.head(10 )