Kaggle competition: Titanic - Machine Learning from Disaster
df.set_index('id', inplace=True)
 train_list = list(df_train['id'])
 val_list = list(df_val['id'])
 for image in train_list:
file_name = image + '.tif'
target = df.loc[image,'label']
if target == 0:
label = '0'
elif target == 1:
label = '1'
src = os.path.join('.. /input/train', file_name)
 dest = os.path.join(train_dir, label, file_name)
 shutil.copyfile(src, dest)
 for image in val_list:
file_name = image + '.tif'
target = df.loc[image,'label']
if target == 0:
label = '0'
elif target == 1:
label = '1'
src = os.path.join('.. /input/train', file_name)
 dest = os.path.join(val_dir, label, file_name)
 shutil.copyfile(src, dest )<define_variables>
df_train = pd.read_csv('/kaggle/input/titanic/train.csv')
 df_train.shape
IMAGE_SIZE = 96
train_path = 'base_dir/train_dir'
valid_path = 'base_dir/val_dir'
test_path = '.. /input/test'
num_train_samples = len(df_train)
 num_val_samples = len(df_val)
 train_batch_size = 32
val_batch_size = 32
train_steps = np.ceil(num_train_samples / train_batch_size)
 val_steps = np.ceil(num_val_samples / val_batch_size)
 datagen = ImageDataGenerator(rescale=1.0/255)
 train_gen = datagen.flow_from_directory(train_path,
target_size=(IMAGE_SIZE,IMAGE_SIZE),
batch_size=train_batch_size,
class_mode='categorical')
 val_gen = datagen.flow_from_directory(valid_path,
target_size=(IMAGE_SIZE,IMAGE_SIZE),
batch_size=val_batch_size,
class_mode='categorical')
 test_gen = datagen.flow_from_directory(valid_path,
target_size=(IMAGE_SIZE,IMAGE_SIZE),
batch_size=1,
class_mode='categorical',
shuffle=False )<import_modules>
df_test = pd.read_csv('/kaggle/input/titanic/test.csv')
 df_test.shape
import keras
from keras.models import Sequential
from keras.layers import Conv2D, Dropout, MaxPooling2D, Flatten, Dense
from keras.layers.normalization import BatchNormalization
from keras.layers.convolutional import SeparableConv2D
from keras.layers.core import Activation<choose_model_class>
df_sub = pd.read_csv('/kaggle/input/titanic/gender_submission.csv')
 df_sub.shape
class Net:
@staticmethod
def build(width, height, depth, classes):
model = Sequential()
inputShape =(height, width, depth)
 model.add(Conv2D(filters = 32, kernel_size =(5,5), padding="same", activation='relu', input_shape= inputShape))
model.add(Conv2D(filters = 32, kernel_size =(3,3), padding="same", activation='relu'))
model.add(Conv2D(filters = 32, kernel_size =(3,3), padding="same", activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Dropout(0.2))
model.add(Conv2D(filters = 64, kernel_size =(3,3), padding="same", activation='relu'))
model.add(Conv2D(filters = 64, kernel_size =(3,3), padding="same", activation='relu'))
model.add(Conv2D(filters = 64, kernel_size =(3,3), padding="same", activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Dropout(0.2))
model.add(Conv2D(filters = 128, kernel_size =(3,3), padding="same", activation='relu'))
model.add(Conv2D(filters = 128, kernel_size =(3,3), padding="same", activation='relu'))
model.add(Conv2D(filters = 128, kernel_size =(3,3), padding="same", activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Dropout(0.25))
model.add(Flatten())
 model.add(Dense(units = 500, activation = 'relu'))
model.add(Dropout(0.2))
model.add(Dense(classes, activation='softmax'))
model.summary()
return model<choose_model_class>
df_train.isnull().sum()
class CancerNet:
@staticmethod
def build(width, height, depth, classes):
model = Sequential()
inputShape =(height, width, depth)
 chanDim = -1
model.add(SeparableConv2D(32,(3, 3), padding="same",input_shape = inputShape))
model.add(Activation("relu"))
model.add(BatchNormalization(axis=chanDim))
model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Dropout(0.25))
model.add(SeparableConv2D(64,(3, 3), padding="same"))
model.add(Activation("relu"))
model.add(BatchNormalization(axis=chanDim))
model.add(SeparableConv2D(64,(3, 3), padding="same"))
model.add(Activation("relu"))
model.add(BatchNormalization(axis=chanDim))
model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Dropout(0.25))
model.add(SeparableConv2D(128,(3, 3), padding="same"))
model.add(Activation("relu"))
model.add(BatchNormalization(axis=chanDim))
model.add(SeparableConv2D(128,(3, 3), padding="same"))
model.add(Activation("relu"))
model.add(BatchNormalization(axis=chanDim))
model.add(SeparableConv2D(128,(3, 3), padding="same"))
model.add(Activation("relu"))
model.add(BatchNormalization(axis=chanDim))
model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Dropout(0.25))
model.add(Flatten())
 model.add(Dense(256))
model.add(Activation("relu"))
model.add(BatchNormalization())
 model.add(Dropout(0.2))
model.add(Dense(classes))
model.add(Activation("softmax"))
model.summary()
return model<choose_model_class>
women = df_train.loc[df_train.Sex == 'female']["Survived"]
rate_women = sum(women)/len(women)
 print("% of women who survived:", rate_women )
model = Net.build(width = 96, height = 96, depth = 3, classes = 2)
 model.compile(optimizer = Adam(lr=0.0001), loss = 'binary_crossentropy', metrics=['accuracy'] )<load_pretrained>
men = df_train.loc[df_train.Sex == 'male']["Survived"]
rate_men = sum(men)/len(men)
 print("% of men who survived:", rate_men )
<train_model>
df_train = df_train.fillna(-999)
 df_test = df_test.fillna(-999 )
filepath = "checkpoint.h5"
checkpoint = ModelCheckpoint(filepath, monitor='val_acc', verbose = 1,
save_best_only = True, mode = 'max')
 reduce_lr = ReduceLROnPlateau(monitor='val_acc', factor = 0.5, patience = 2, verbose = 1, mode = 'max', min_lr = 0.00001)
 callbacks_list = [checkpoint, reduce_lr]
history = model.fit_generator(train_gen, steps_per_epoch = train_steps,
validation_data = val_gen,
validation_steps = val_steps,
epochs = 11,
verbose = 1,
callbacks = callbacks_list )<train_model>
X = df_train.drop(["Survived"], axis=1)
 y = df_train["Survived"]
X_train, X_test, y_train, y_test = train_test_split(X,y, train_size=0.75, random_state=42 )
model.load_weights('checkpoint.h5')
 val_loss, val_acc = \
model.evaluate_generator(test_gen, steps=len(df_val))
print('val_loss:', val_loss)
 print('val_acc:', val_acc )<predict_on_test>
predictions = model.predict_generator(test_gen, steps=len(df_val), verbose=1 )<prepare_output>
cat_features_indices = np.where(X.dtypes != float)[0]
model = CatBoostClassifier(
 eval_metric='Accuracy',
loss_function='Logloss',
use_best_model=True,
random_seed=42,
logging_level='Silent' 
)
 model.fit(X_train,y_train,cat_features=cat_features_indices,eval_set=(X_test,y_test), plot=True )
df_preds = pd.DataFrame(predictions, columns=['no_tumor', 'has_tumor'])
 df_preds.head()<prepare_x_and_y>
cv_params = model.get_params()
cv_params.update({
'loss_function': 'Logloss'
})
 cv_data = cv(
 Pool(X, y, cat_features=cat_features_indices),
cv_params,
plot=True
)
y_true = test_gen.classes
y_pred = df_preds['has_tumor']<compute_test_metric>
print('Best validation accuracy score: {:.2f}±{:.2f} on step {}'.format(
 np.max(cv_data['test-Accuracy-mean']),
cv_data['test-Accuracy-std'][np.argmax(cv_data['test-Accuracy-mean'])],
np.argmax(cv_data['test-Accuracy-mean'])
))
print('ROC AUC Score = ',roc_auc_score(y_true, y_pred))<compute_test_metric>
print('Precise validation accuracy score: {}'.format(np.max(cv_data['test-Accuracy-mean'])) )
fpr_keras, tpr_keras, thresholds_keras = roc_curve(y_true, y_pred)
 auc_keras = auc(fpr_keras, tpr_keras )<compute_train_metric>
train_pool = Pool(X_train, y_train, cat_features=cat_features_indices)
 validate_pool = Pool(X_test, y_test, cat_features=cat_features_indices )
report = classification_report(y_true, y_pred_binary, target_names = ['no_tumor', 'has_tumor'])
 print(report )<define_variables>
feature_importances = model.get_feature_importance(train_pool)
 feature_names = X_train.columns
for score, name in sorted(zip(feature_importances, feature_names), reverse=True):
print('{}: {}'.format(name, score))
test_path ='test_dir'
test_gen = datagen.flow_from_directory(test_path,
target_size=(IMAGE_SIZE,IMAGE_SIZE),
batch_size=1,
class_mode='categorical',
shuffle=False )<predict_on_test>
eval_metrics = model.eval_metrics(validate_pool, ['AUC'], plot=True )
num_test_images = 57458
predictions = model.predict_generator(test_gen, steps=num_test_images, verbose=1 )<train_model>
if predictions.shape[0] == num_test_images:
print('All Predictions Done!')
 else:
print('Error!' )<prepare_output>
predictions = model.predict(df_test)
 predictions_probs = model.predict_proba(df_test)
 print(predictions[:10])
 print(predictions_probs[:10] )
<feature_engineering><EOS> 
output = pd.DataFrame({'PassengerId': df_test.PassengerId, 'Survived': predictions})
 output.to_csv('my_submission.csv', index=False)
 print("Submission was successfully saved!" )