Kaggle competition: Titanic - Machine Learning from Disaster
learn.lr_find() 
<train_model>
t_data= pd.read_csv('/kaggle/input/titanic/train.csv',index_col='PassengerId')
 t_data.head()
lr = 1e-02
learn.fit_one_cycle(10, slice(lr)) 
<predict_on_test>
t_data.drop(columns=['Name','Ticket','Fare','Cabin'],inplace=True )
preds,_ = learn.get_preds(ds_type=DatasetType.Test )<filter>
for col in range(len(t_data.columns)) :
print(t_data[t_data.columns[col]].value_counts() )
test.has_cactus = preds.numpy() [:, 0]<save_to_csv>
t_data.isna().sum()
test.to_csv('submission.csv', index=False )<import_modules>
t_data.Age.value_counts().mode()
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import glob
import scipy
import cv2
import keras<import_modules>
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
import random<load_from_csv>
target_col="Survived"
y = t_data[target_col]
X = t_data[['Pclass','Sex','Age','SibSp','Parch','Embarked']]
X = pd.get_dummies(X)
 train_X, val_X, train_y, val_y = train_test_split(X, y)
 val_X
train_data = pd.read_csv('.. /input/train.csv' )<count_values>
cols_with_missing = [col for col in train_X.columns if train_X[col].isnull().any() ]
red_X_train=train_X.drop(columns=cols_with_missing)
 red_X_val=val_X.drop(columns=cols_with_missing )
train_data.has_cactus.value_counts()<define_search_model>
def get_accuracy(n_estimators,max_depth,train_X, val_X, train_y, val_y):
model = RandomForestClassifier(n_estimators=n_estimators, max_depth=max_depth ,random_state=1)
 model.fit(train_X,train_y)
 preds = model.predict(val_X)
 lr_accuracy = accuracy_score(val_y,preds)
 return lr_accuracy
def image_generator(batch_size = 16, all_data=True, shuffle=True, train=True, indexes=None):
while True:
if indexes is None:
if train:
if all_data:
indexes = np.arange(train_data.shape[0])
 else:
indexes = np.arange(train_data[:15000].shape[0])
 if shuffle:
np.random.shuffle(indexes)
 else:
indexes = np.arange(train_data[15000:].shape[0])
 N = int(len(indexes)/ batch_size)
 for i in range(N):
current_indexes = indexes[i*batch_size:(i+1)*batch_size]
batch_input = []
batch_output = []
for index in current_indexes:
img = mpimg.imread('.. /input/train/train/' + train_data.id[index])
 batch_input += [img]
batch_input += [img[::-1, :, :]]
batch_input += [img[:, ::-1, :]]
batch_input += [np.rot90(img)]
temp_img = np.zeros_like(img)
 temp_img[:28, :, :] = img[4:, :, :]
batch_input += [temp_img]
temp_img = np.zeros_like(img)
 temp_img[:, :28, :] = img[:, 4:, :]
batch_input += [temp_img]
temp_img = np.zeros_like(img)
 temp_img[4:, :, :] = img[:28, :, :]
batch_input += [temp_img]
temp_img = np.zeros_like(img)
 temp_img[:, 4:, :] = img[:, :28, :]
batch_input += [temp_img]
batch_input += [cv2.resize(img[2:30, 2:30, :],(32, 32)) ]
batch_input += [scipy.ndimage.interpolation.rotate(img, 10, reshape=False)]
batch_input += [scipy.ndimage.interpolation.rotate(img, 5, reshape=False)]
for _ in range(11):
batch_output += [train_data.has_cactus[index]]
batch_input = np.array(batch_input)
 batch_output = np.array(batch_output)
 yield(batch_input, batch_output.reshape(-1, 1))<choose_model_class>
accuracy=get_accuracy(200,10,red_X_train,red_X_val,train_y,val_y)
 print("Validation accurcy for Random Forest Model: {}".format(accuracy))
model = keras.models.Sequential()
model.add(keras.layers.Conv2D(64,(5, 5), input_shape=(32, 32, 3)))
 model.add(keras.layers.BatchNormalization())
 model.add(keras.layers.LeakyReLU(alpha=0.3))
model.add(keras.layers.Conv2D(64,(5, 5)))
 model.add(keras.layers.BatchNormalization())
 model.add(keras.layers.LeakyReLU(alpha=0.3))
model.add(keras.layers.Conv2D(128,(5, 5)))
 model.add(keras.layers.BatchNormalization())
 model.add(keras.layers.LeakyReLU(alpha=0.3))
model.add(keras.layers.Conv2D(128,(5, 5)))
 model.add(keras.layers.BatchNormalization())
 model.add(keras.layers.LeakyReLU(alpha=0.3))
model.add(keras.layers.Conv2D(256,(3, 3)))
 model.add(keras.layers.BatchNormalization())
 model.add(keras.layers.LeakyReLU(alpha=0.3))
model.add(keras.layers.Conv2D(256,(3, 3)))
 model.add(keras.layers.BatchNormalization())
 model.add(keras.layers.LeakyReLU(alpha=0.3))
model.add(keras.layers.Conv2D(512,(3, 3)))
 model.add(keras.layers.BatchNormalization())
 model.add(keras.layers.LeakyReLU(alpha=0.3))
model.add(keras.layers.Flatten())
 model.add(keras.layers.Dense(100))
model.add(keras.layers.BatchNormalization())
 model.add(keras.layers.LeakyReLU(alpha=0.3))
model.add(keras.layers.Dense(1, activation='sigmoid'))<choose_model_class>
my_imputer = SimpleImputer()
imputed_X_train = pd.DataFrame(my_imputer.fit_transform(train_X))
imputed_X_valid = pd.DataFrame(my_imputer.transform(val_X))
imputed_X_train.columns =train_X.columns
imputed_X_valid.columns = val_X.columns
opt = keras.optimizers.Adam(0.0001)
 model.compile(optimizer=opt, loss='binary_crossentropy', metrics=['accuracy'] )<train_model>
accuracy=get_accuracy(1000,10,imputed_X_train,imputed_X_valid,train_y,val_y)
 print("Validation accurcy for Random Forest Model: {}".format(accuracy))
model.fit_generator(image_generator() , steps_per_epoch= train_data.shape[0] / 16, epochs=30 )<find_best_params>
max_accur=.5
max_dep=0
best_tree_size=0
for maxDepth in range(1,11):
for i in range(10,101,10):
accuracy=get_accuracy(i,maxDepth,imputed_X_train,imputed_X_valid,train_y,val_y)
 if accuracy>max_accur:
max_accur=accuracy
max_dep=maxDepth
best_tree_size=i
print("max accuracy = {} max depth={} best tree size={}".format(max_accur,max_dep,best_tree_size))
keras.backend.eval(model.optimizer.lr.assign(0.00001))<train_model>
max_accur=.5
max_dep=0
best_tree_size=0
for maxDepth in range(1,11):
for i in range(10,101,10):
accuracy=get_accuracy(i,maxDepth,red_X_train,red_X_val,train_y,val_y)
 if accuracy>max_accur:
max_accur=accuracy
max_dep=maxDepth
best_tree_size=i
print("max accuracy = {} max depth={} best tree size={}".format(max_accur,max_dep,best_tree_size))
model.fit_generator(image_generator() , steps_per_epoch= train_data.shape[0] / 16, epochs=15 )<load_pretrained>
pd.get_dummies(df, prefix=['col1', 'col2'] )
indexes = np.arange(train_data.shape[0])
 N = int(len(indexes)/ 64)
 batch_size = 64
wrong_ind = []
for i in range(N):
current_indexes = indexes[i*64:(i+1)*64]
batch_input = []
batch_output = []
for index in current_indexes:
img = mpimg.imread('.. /input/train/train/' + train_data.id[index])
 batch_input += [img]
batch_output.append(train_data.has_cactus[index])
 batch_input = np.array(batch_input)
 model_pred = model.predict_classes(batch_input)
 for j in range(len(batch_output)) :
if model_pred[j] != batch_output[j]:
wrong_ind.append(i*batch_size+j )<load_pretrained>
accuracy=get_accuracy(60,4,red_X_train,red_X_val,train_y,val_y)
 accuracy
indexes = np.arange(train_data.shape[0])
 N = int(len(indexes)/ 64)
 batch_size = 64
wrong_ind = []
for i in range(N):
current_indexes = indexes[i*64:(i+1)*64]
batch_input = []
batch_output = []
for index in current_indexes:
img = mpimg.imread('.. /input/train/train/' + train_data.id[index])
 batch_input += [img[::-1, :, :]]
batch_output.append(train_data.has_cactus[index])
 batch_input = np.array(batch_input)
 model_pred = model.predict_classes(batch_input)
 for j in range(len(batch_output)) :
if model_pred[j] != batch_output[j]:
wrong_ind.append(i*batch_size+j )<load_pretrained>
test_data= pd.read_csv('/kaggle/input/titanic/test.csv')
 test_data.info()
indexes = np.arange(train_data.shape[0])
 N = int(len(indexes)/ 64)
 batch_size = 64
wrong_ind = []
for i in range(N):
current_indexes = indexes[i*64:(i+1)*64]
batch_input = []
batch_output = []
for index in current_indexes:
img = mpimg.imread('.. /input/train/train/' + train_data.id[index])
 batch_input += [img[:, ::-1, :]]
batch_output.append(train_data.has_cactus[index])
 batch_input = np.array(batch_input)
 model_pred = model.predict_classes(batch_input)
 for j in range(len(batch_output)) :
if model_pred[j] != batch_output[j]:
wrong_ind.append(i*batch_size+j )<define_variables>
model = RandomForestClassifier(n_estimators=60, max_depth=10 ,random_state=1)
 model.fit(imputed_X_train,train_y)
 preds = model.predict(imputed_X_valid)
 model_accuracy = accuracy_score(val_y,preds)
 print("Accarany = {}:".format(model_accuracy))
test_files = os.listdir('.. /input/test/test/' )<predict_on_test>
test=test_data[['Pclass','Sex','Age','SibSp','Parch','Embarked']]
final_X_test = pd.get_dummies(test)
 X_test.info()
batch = 40
all_out = []
for i in range(int(4000/batch)) :
images = []
for j in range(batch):
img = mpimg.imread('.. /input/test/test/'+test_files[i*batch + j])
 images += [img]
out = model.predict(np.array(images))
all_out += [out]<create_dataframe>
final_X_test = pd.DataFrame(my_imputer.transform(final_X_test))
sub_file = pd.DataFrame(data = {'id': test_files, 'has_cactus': all_out.reshape(-1 ).tolist() } )<save_to_csv>
predictions = model.predict(final_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!")