Kaggle competition: Titanic - Machine Learning from Disaster
submission.to_csv('submission.csv', index=False )<set_options>
train_data = pd.read_csv("/kaggle/input/titanic/train.csv")
 train_data_drop = pd.read_csv("/kaggle/input/titanic/train.csv")
 train_data['Died'] = 1 - train_data['Survived']
train_data.head()
%reload_ext autoreload
%autoreload 2
%matplotlib inline
%matplotlib inline
package_path = '.. /input/efficientnet-pytorch/efficientnet-pytorch/EfficientNet-PyTorch-master'
sys.path.append(package_path)
<load_pretrained>
test_data = pd.read_csv("/kaggle/input/titanic/test.csv")
 test_data.head()
md_ef = EfficientNet.from_pretrained('efficientnet-b5', num_classes=1 )<init_hyperparams>
women = train_data.loc[train_data.Sex == 'female']["Survived"]
rate_women = sum(women)/len(women)
 print("% of women survived:", rate_women)
 men = train_data.loc[train_data.Sex == 'male']["Survived"]
rate_men = sum(men)/len(men)
 print("% of men survived:", rate_men )
class ImagePreprocessor(object):
def __init__(self, root_dir: str, save_dir: str, img_size: int, tolerance: int = 10, remove_outer_pixels: float = 0.0):

 if remove_outer_pixels > 0.50:
print("ERROR: eroding more than 50% of image")
 raise InterruptedError
self.root_dir = root_dir
self.img_size = img_size
self.tolerance = tolerance
self.remove_outer_pixels = remove_outer_pixels
self.images = glob.glob(f"{self.root_dir}/*.png")+ glob.glob(
 f"{self.root_dir}/*.jpeg")+ glob.glob(f"{self.root_dir}/*.jpg")
 self.save_dir = save_dir
os.makedirs(self.save_dir, exist_ok=True)
 self.total_count = len(self.images)
 @staticmethod
def _counter(func):
def wrapper(self, *args, **kwargs):
func(self, *args, **kwargs)
 return wrapper
@staticmethod
def light_sensitivity_reducer(img: np.ndarray, alpha: int = 4, beta: int = -4, gamma: int = 128):

 return cv2.addWeighted(img, alpha, cv2.GaussianBlur(img,(0, 0), 30), beta, gamma)
 @staticmethod
def outer_pixels_remover(img: np.ndarray, scale: float):

 scale_2 = scale / 2.0
miny = int(img.shape[0]*scale_2)
 maxy = int(img.shape[0]-miny)
 minx = int(img.shape[1]*scale_2)
 maxx = int(img.shape[1]-minx)
 return img[miny:maxy, minx:maxx]
@staticmethod
def scale_image(img: np.ndarray, img_size: int):

 return cv2.resize(img,(img_size, img_size))
@staticmethod
def pitch_black_remover(img: np.ndarray, tolerance: int = 10):

 if img.ndim == 2:
img_mask = img > tolerance
return img[np.ix_(img_mask.any(1), img_mask.any(0)) ]
elif img.ndim == 3:
greyed = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
 img_mask = greyed > tolerance
img_1 = img[:, :, 0][np.ix_(img_mask.any(1), img_mask.any(0)) ]
if img_1.shape[0] == 0:
return img
img_2 = img[:, :, 1][np.ix_(img_mask.any(1), img_mask.any(0)) ]
img_3 = img[:, :, 2][np.ix_(img_mask.any(1), img_mask.any(0)) ]
return np.stack([img_1, img_2, img_3], axis=-1)
 else:
print("Image has more than 3 dimensions")
 raise InterruptedError
def replace_existing(self):
shutil.rmtree(self.root_dir)
 os.makedirs(self.root_dir)
 processed_images = [i.path for i in os.scandir(
 self.save_dir)if i.is_file() ]
processor_pool = multiprocessing.Pool(64)
 for counter, _ in enumerate(processor_pool.imap_unordered(lambda x: shutil.move(x, self.root_dir), processed_images), 1):
sys.stdout.write(
 f"\rMoving : {(counter/self.total_count)*100:3.2f}% \t[ {counter}/{self.total_count} ]")
 os.rmdir(self.save_dir)
 sys.stdout.write("
")
 def forward(self, image: str):

 img = cv2.imread(image)
 img = self.pitch_black_remover(img, tolerance=self.tolerance)
 img = self.scale_image(img, img_size=self.img_size)
 if self.remove_outer_pixels > 0.0:
img = self.outer_pixels_remover(img, self.remove_outer_pixels)
 cv2.imwrite(os.path.join(self.save_dir, image.split('/')[-1]), img)
 def run(self, replace: bool = False):

 for image in tqdm(self.images):
self.forward(image)
 if replace:
self.replace_existing()
def parallel_run(self, workers: int = multiprocessing.cpu_count() , replace: bool = False):

 processor_pool = multiprocessing.Pool(workers)
 for counter, _ in enumerate(processor_pool.imap_unordered(self.forward, self.images), 1):
sys.stdout.write(
 f"\rProgress : {(counter/self.total_count)*100:3.2f}% \t[ {counter}/{self.total_count} ]")
 if replace:
self.replace_existing()
sys.stdout.write("
")
 preprocessor = ImagePreprocessor(root_dir='.. /input/aptos2019-blindness-detection/test_images/',
save_dir='test_processed',
img_size=256,
tolerance=10,
remove_outer_pixels=0 
)
 preprocessor.parallel_run(multiprocessing.cpu_count() ,False )<load_from_csv>
test_data["Fare"] = test_data["Fare"].fillna(test_data["Fare"].median())
 train_data['FareGroup'] = pd.cut(train_data['Fare'],4)
 print(train_data[['FareGroup', 'Survived']].groupby('FareGroup', as_index=False ).mean().sort_values('Survived', ascending=False))
def group_fare(fare):
if fare <= 128: return 0
if fare > 128 and fare <= 256: return 1
if fare > 256 and fare <= 384: return 2
if fare > 384 : return 3
for i, row in train_data.iterrows() :
train_data.at[i,'Fare Group'] = int(group_fare(row["Fare"]))
for i, row in test_data.iterrows() :
test_data.at[i,'Fare Group'] = int(group_fare(row["Fare"]))
train_data.head()
def get_df() :
base_image_dir = os.path.join('.. ', 'input/aptos2019-blindness-detection/')
 train_dir = os.path.join(base_image_dir,'train_images/')
 df = pd.read_csv(os.path.join(base_image_dir, 'train.csv'))
df['path'] = df['id_code'].map(lambda x: os.path.join(train_dir,'{}.png'.format(x)))
 df = df.drop(columns=['id_code'])
 df = df.sample(frac=1 ).reset_index(drop=True)
 test_df = pd.read_csv('.. /input/aptos2019-blindness-detection/sample_submission.csv')
 return df, test_df
df, test_df = get_df()<feature_engineering>
y = train_data["Survived"]
features = ["Pclass", "Sex", "SibSp", "Parch", "Age","Embarked","Fare Group"]
X = pd.get_dummies(train_data[features])
 X_test = pd.get_dummies(test_data[features])
 imp = SimpleImputer()
imp_X = imp.fit_transform(X)
 imp_X_test = imp.transform(X_test)
 model1 = RandomForestClassifier(n_estimators = 100, max_depth = 5, random_state = 1)
 model1.fit(imp_X,y)
 predictions1 = model1.predict(imp_X_test)
 model1_preds = cross_val_predict(model1, imp_X, y)
 model1_acc = accuracy_score(y, model1_preds)
 print("Random Forest Accuracy:", model1_acc )
bs = 64
sz = 224
tfms = get_transforms(do_flip=True,flip_vert=True )<compute_test_metric>
model2 = GradientBoostingClassifier(random_state=42)
 model2.fit(imp_X,y)
 predictions2 = model2.predict(imp_X_test)
 model2_preds = cross_val_predict(model2, imp_X, y)
 model2_acc = accuracy_score(y, model2_preds)
 print("Gradient Booster Accuracy:", model2_acc )
def qk(y_pred, y):
return torch.tensor(cohen_kappa_score(torch.round(y_pred), y, weights='quadratic'), device='cuda:0' )<load_pretrained>
model3 = XGBClassifier(max_depth=3, n_estimators=1000, learning_rate=0.05)
 model3.fit(imp_X, y)
 predictions3 = model3.predict(imp_X_test)
 model3_preds = cross_val_predict(model3, imp_X, y)
 model3_acc = accuracy_score(y, model3_preds)
 print("XGBoost Accuracy:", model3_acc )
learn = Learner(data,
md_ef,
metrics = [qk],
model_dir="models")
 learn.data.add_test(ImageList.from_df(test_df,
'test_processed/',
folder='',
suffix='.png', 
))
learn.load('abcdef')
 pass<categorify>
model4 = SVC(random_state = 1)
 model4.fit(imp_X, y)
 predictions4 = model4.predict(imp_X_test)
 model4_preds = cross_val_predict(model4, imp_X, y)
 model4_acc = accuracy_score(y, model4_preds)
 print("SVC Accuracy:", model4_acc )
learn.to_fp32()
pass<compute_test_metric>
output = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': predictions1})
 output.to_csv('my_submission.csv', index=False)
 print("Your submission was succesfully saved!" )