Kaggle competition: Petals to the Metal - Flower Classification on TPU
<SOS> metric: macrofscore Kaggle data source: petals-to-the-metal-flower-classification-on-tpu<data_type_conversions>
Image(filename=".. /input/images/Petals to the Metal 31.png", width=1200, height=1000 )
print(train_df.columns.to_list())
 print(test_df.columns.to_list() )<normalization>
Image(filename=".. /input/images/Petals to the Metal 32.png", width=1200, height=1000 )
minMaxScaler = MinMaxScaler()
for data in train_test_data:
data['Pclass'] = minMaxScaler.fit_transform(data[['Pclass']] )<count_values>
Image(filename=".. /input/images/Petals to the Metal 28.png", width=1200, height=1000 )
train_df['Pclass'].value_counts()<feature_engineering>
Image(filename=".. /input/images/3d_plot3.png", width=1200, height=1000 )
for data in train_test_data:
data['Title'] = data['Name'].str.extract('([a-zA-Z]+)\.', expand=False )<drop_column>
Image(filename=".. /input/images/3d_plot4.png", width=1200, height=1000 )
for data in train_test_data:
data.drop('Name', axis=1, inplace=True )<count_values>
Image(filename=".. /input/images/3d_plot5.png", width=1200, height=1000 )
print(train_df['Title'].value_counts())
 print('-'*50)
 print(test_df['Title'].value_counts() )<categorify>
Image(filename=".. /input/images/Petals to the Metal 10.png", width=1200, height=1000 )
title_mapping = {
'Mr':0,
'Miss':1,
'Mrs':2,
'Master':3,
'Dr':4, 'Rev':4, 'Major':4, 'Mlle':4, 'Col':4, 'Ms':4, 'Countess':4, 'Mme':4, 'Lady':4, 'Sir':4, 'Don':4, 'Jonkheer':4, 'Capt':4, 'Dona':4
}
for data in train_test_data:
data['Title'] = data['Title'].map(title_mapping )<count_values>
Image(filename=".. /input/images/Petals to the Metal 25.png", width=1200, height=1000 )
train_df['Title'].value_counts()<normalization>
Image(filename=".. /input/images/Petals to the Metal 26.png", width=1200, height=1000 )
minMaxScaler = MinMaxScaler()
for data in train_test_data:
data['Title'] = minMaxScaler.fit_transform(data[['Title']] )<count_values>
Image(filename=".. /input/images/Petals to the Metal 22.png", width=1200, height=1000 )
train_df['Title'].value_counts()<data_type_conversions>
Image(filename=".. /input/images/Deep Learning Adventures.png", width=1200, height=1000 )
for data in train_test_data:
data['Sex'] = data['Sex'].astype('category' ).cat.codes<count_values>
!pip install seaborn --upgrade
print("Tensorflow version " + tf.__version__ )
train_df['Sex'].value_counts()<count_missing_values>
GCS_DS_PATH = KaggleDatasets().get_gcs_path('tpu-getting-started')
 print(GCS_DS_PATH )
train_df['Age'].isnull().sum()<categorify>
IMAGE_SIZE = [512, 512]
GCS_PATH = GCS_DS_PATH + '/tfrecords-jpeg-512x512'
AUTO = tf.data.experimental.AUTOTUNE
TRAINING_FILENAMES = tf.io.gfile.glob(GCS_PATH + '/train/*.tfrec')
 VALIDATION_FILENAMES = tf.io.gfile.glob(GCS_PATH + '/val/*.tfrec')
 TEST_FILENAMES = tf.io.gfile.glob(GCS_PATH + '/test/*.tfrec')
 CLASSES = ['pink primrose', 'hard-leaved pocket orchid', 'canterbury bells', 'sweet pea', 'wild geranium', 'tiger lily', 'moon orchid', 'bird of paradise', 'monkshood', 'globe thistle',
'snapdragon', "colt's foot", 'king protea', 'spear thistle', 'yellow iris', 'globe-flower', 'purple coneflower', 'peruvian lily', 'balloon flower', 'giant white arum lily',
'fire lily', 'pincushion flower', 'fritillary', 'red ginger', 'grape hyacinth', 'corn poppy', 'prince of wales feathers', 'stemless gentian', 'artichoke', 'sweet william',
'carnation', 'garden phlox', 'love in the mist', 'cosmos', 'alpine sea holly', 'ruby-lipped cattleya', 'cape flower', 'great masterwort', 'siam tulip', 'lenten rose',
'barberton daisy', 'daffodil', 'sword lily', 'poinsettia', 'bolero deep blue', 'wallflower', 'marigold', 'buttercup', 'daisy', 'common dandelion',
'petunia', 'wild pansy', 'primula', 'sunflower', 'lilac hibiscus', 'bishop of llandaff', 'gaura', 'geranium', 'orange dahlia', 'pink-yellow dahlia',
'cautleya spicata', 'japanese anemone', 'black-eyed susan', 'silverbush', 'californian poppy', 'osteospermum', 'spring crocus', 'iris', 'windflower', 'tree poppy',
'gazania', 'azalea', 'water lily', 'rose', 'thorn apple', 'morning glory', 'passion flower', 'lotus', 'toad lily', 'anthurium',
'frangipani', 'clematis', 'hibiscus', 'columbine', 'desert-rose', 'tree mallow', 'magnolia', 'cyclamen ', 'watercress', 'canna lily',
'hippeastrum ', 'bee balm', 'pink quill', 'foxglove', 'bougainvillea', 'camellia', 'mallow', 'mexican petunia', 'bromelia', 'blanket flower',
'trumpet creeper', 'blackberry lily', 'common tulip', 'wild rose']
def decode_image(image_data):
image = tf.image.decode_jpeg(image_data, channels=3)
 image = tf.cast(image, tf.float32)/ 255.0
image = tf.reshape(image, [*IMAGE_SIZE, 3])
 return image
def read_labeled_tfrecord(example):
LABELED_TFREC_FORMAT = {
"image": tf.io.FixedLenFeature([], tf.string),
"class": tf.io.FixedLenFeature([], tf.int64),
}
example = tf.io.parse_single_example(example, LABELED_TFREC_FORMAT)
 image = decode_image(example['image'])
 label = tf.cast(example['class'], tf.int32)
 return image, label
def read_unlabeled_tfrecord(example):
UNLABELED_TFREC_FORMAT = {
"image": tf.io.FixedLenFeature([], tf.string),
"id": tf.io.FixedLenFeature([], tf.string),
}
example = tf.io.parse_single_example(example, UNLABELED_TFREC_FORMAT)
 image = decode_image(example['image'])
 idnum = example['id']
return image, idnum
def load_dataset(filenames, labeled=True, ordered=False):
ignore_order = tf.data.Options()
if not ordered:
ignore_order.experimental_deterministic = False
dataset = tf.data.TFRecordDataset(filenames, num_parallel_reads=AUTO)
 dataset = dataset.with_options(ignore_order)
 dataset = dataset.map(read_labeled_tfrecord if labeled else read_unlabeled_tfrecord, num_parallel_calls=AUTO)
 return dataset
for data in train_test_data:
data['Age'].fillna(train_df.groupby('Title')['Age'].transform('median'), inplace=True )<count_missing_values>
GCS_DS_PATH_EXT = KaggleDatasets().get_gcs_path('tf-flower-photo-tfrec')
 GCS_PATH_SELECT_EXT = {
192: '/tfrecords-jpeg-192x192',
224: '/tfrecords-jpeg-224x224',
331: '/tfrecords-jpeg-331x331',
512: '/tfrecords-jpeg-512x512'
}
GCS_PATH_EXT = GCS_PATH_SELECT_EXT[IMAGE_SIZE[0]]
IMAGENET_FILES = tf.io.gfile.glob(GCS_DS_PATH_EXT + '/imagenet' + GCS_PATH_EXT + '/*.tfrec')
 INATURELIST_FILES = tf.io.gfile.glob(GCS_DS_PATH_EXT + '/inaturalist' + GCS_PATH_EXT + '/*.tfrec')
 OPENIMAGE_FILES = tf.io.gfile.glob(GCS_DS_PATH_EXT + '/openimage' + GCS_PATH_EXT + '/*.tfrec')
 OXFORD_FILES = tf.io.gfile.glob(GCS_DS_PATH_EXT + '/oxford_102' + GCS_PATH_EXT + '/*.tfrec')
 TENSORFLOW_FILES = tf.io.gfile.glob(GCS_DS_PATH_EXT + '/tf_flowers' + GCS_PATH_EXT + '/*.tfrec')
 ADDITIONAL_TRAINING_FILENAMES = IMAGENET_FILES + INATURELIST_FILES + OPENIMAGE_FILES + OXFORD_FILES + TENSORFLOW_FILES
TRAINING_FILENAMES = TRAINING_FILENAMES + ADDITIONAL_TRAINING_FILENAMES
train_df['Age'].isnull().sum()<feature_engineering>
SEED = 2020
def random_blockout(img, sl=0.1, sh=0.2, rl=0.4):
p=random.random()
if p>=0.25:
w, h, c = IMAGE_SIZE[0], IMAGE_SIZE[1], 3
origin_area = tf.cast(h*w, tf.float32)
 e_size_l = tf.cast(tf.round(tf.sqrt(origin_area * sl * rl)) , tf.int32)
 e_size_h = tf.cast(tf.round(tf.sqrt(origin_area * sh / rl)) , tf.int32)
 e_height_h = tf.minimum(e_size_h, h)
 e_width_h = tf.minimum(e_size_h, w)
 erase_height = tf.random.uniform(shape=[], minval=e_size_l, maxval=e_height_h, dtype=tf.int32)
 erase_width = tf.random.uniform(shape=[], minval=e_size_l, maxval=e_width_h, dtype=tf.int32)
 erase_area = tf.zeros(shape=[erase_height, erase_width, c])
 erase_area = tf.cast(erase_area, tf.uint8)
 pad_h = h - erase_height
pad_top = tf.random.uniform(shape=[], minval=0, maxval=pad_h, dtype=tf.int32)
 pad_bottom = pad_h - pad_top
pad_w = w - erase_width
pad_left = tf.random.uniform(shape=[], minval=0, maxval=pad_w, dtype=tf.int32)
 pad_right = pad_w - pad_left
erase_mask = tf.pad([erase_area], [[0,0],[pad_top, pad_bottom], [pad_left, pad_right], [0,0]], constant_values=1)
 erase_mask = tf.squeeze(erase_mask, axis=0)
 erased_img = tf.multiply(tf.cast(img,tf.float32), tf.cast(erase_mask, tf.float32))
return tf.cast(erased_img, img.dtype)
 else:
return tf.cast(img, img.dtype)
 def data_augment_v2(image, label):
flag = random.randint(1,3)
 coef_1 = random.randint(70, 90)* 0.01
coef_2 = random.randint(70, 90)* 0.01
if flag == 1:
image = tf.image.random_flip_left_right(image, seed=SEED)
 elif flag == 2:
image = tf.image.random_flip_up_down(image, seed=SEED)
 else:
image = tf.image.random_crop(image, [int(IMAGE_SIZE[0]*coef_1), int(IMAGE_SIZE[0]*coef_2), 3],seed=SEED)
 image = random_blockout(image)
 return image, label
for data in train_test_data:
data['Age'] = pd.qcut(data['Age'], 9 ).cat.codes<normalization>
def data_augment(image, label):
image = tf.image.random_flip_left_right(image)
 return image, label
def get_training_dataset() :
dataset = load_dataset(TRAINING_FILENAMES, labeled=True)
 dataset = dataset.map(data_augment, num_parallel_calls=AUTO)
 dataset = dataset.repeat()
dataset = dataset.shuffle(2048)
 dataset = dataset.batch(BATCH_SIZE)
 dataset = dataset.prefetch(AUTO)
 return dataset
def get_validation_dataset(ordered=False):
dataset = load_dataset(VALIDATION_FILENAMES, labeled=True, ordered=ordered)
 dataset = dataset.batch(BATCH_SIZE)
 dataset = dataset.cache()
dataset = dataset.prefetch(AUTO)
 return dataset
def get_test_dataset(ordered=False):
dataset = load_dataset(TEST_FILENAMES, labeled=False, ordered=ordered)
 dataset = dataset.batch(BATCH_SIZE)
 dataset = dataset.prefetch(AUTO)
 return dataset
def count_data_items(filenames):
n = [int(re.compile(r"-([0-9]*)\." ).search(filename ).group(1)) for filename in filenames]
return np.sum(n)
 NUM_TRAINING_IMAGES = count_data_items(TRAINING_FILENAMES)
 NUM_VALIDATION_IMAGES = count_data_items(VALIDATION_FILENAMES)
 NUM_TEST_IMAGES = count_data_items(TEST_FILENAMES)
 print('Dataset: {} training images, {} validation images, {} unlabeled test images'.format(NUM_TRAINING_IMAGES, NUM_VALIDATION_IMAGES, NUM_TEST_IMAGES))
minMaxScaler = MinMaxScaler()
for data in train_test_data:
data['Age'] = minMaxScaler.fit_transform(data[['Age']] )<feature_engineering>
strategy.num_replicas_in_sync
for data in train_test_data:
data['FamilySize'] = data['Parch'] + data['SibSp']<drop_column>
16 * strategy.num_replicas_in_sync
for data in train_test_data:
data.drop(['SibSp', 'Parch'], axis=1, inplace=True )<feature_engineering>
BATCH_SIZE = 16 * strategy.num_replicas_in_sync
ds_train = get_training_dataset()
ds_valid = get_validation_dataset()
ds_test = get_test_dataset()
print("Training:", ds_train)
 print("Validation:", ds_valid)
 print("Test:", ds_test )
<normalization>
np.set_printoptions(threshold=15, linewidth=80)
 print("Training data shapes:")
 for image, label in ds_train.take(3):
print(image.numpy().shape, label.numpy().shape)
 print("Training data label examples:", label.numpy() )
minMaxScaler = MinMaxScaler()
for data in train_test_data:
data['FamilySize'] = minMaxScaler.fit_transform(data[['FamilySize']] )<count_values>
print("Test data shapes:")
 for image, idnum in ds_test.take(3):
print(image.numpy().shape, idnum.numpy().shape)
 print("Test data IDs:", idnum.numpy().astype('U'))
print(train_df['Ticket'].value_counts())
 print('-'*80)
 print(train_df['Ticket'].unique().shape )<drop_column>
def batch_to_numpy_images_and_labels(data):
images, labels = data
numpy_images = images.numpy()
numpy_labels = labels.numpy()
if numpy_labels.dtype == object:
numpy_labels = [None for _ in enumerate(numpy_images)]
return numpy_images, numpy_labels
def title_from_label_and_target(label, correct_label):
if correct_label is None:
return CLASSES[label], True
correct =(label == correct_label)
 return "{} [{}{}{}]".format(CLASSES[label],
'OK' if correct else 'NO',
u"\u2192" if not correct else '',
CLASSES[correct_label] if not correct else ''), correct
def display_one_flower(image, title, subplot, red=False, titlesize=16):
plt.subplot(*subplot)
 plt.axis('off')
 plt.imshow(image)
 if len(title)> 0:
plt.title(title, fontsize=int(titlesize)if not red else int(titlesize/1.2), color='red' if red else 'black', fontdict={'verticalalignment':'center'}, pad=int(titlesize/1.5))
return(subplot[0], subplot[1], subplot[2]+1)
 def display_batch_of_images(databatch, predictions=None, display_mismatches_only=False):

 images, labels = batch_to_numpy_images_and_labels(databatch)
 if labels is None:
labels = [None for _ in enumerate(images)]
rows = int(math.sqrt(len(images)))
 cols = len(images)//rows
FIGSIZE = 13.0
SPACING = 0.1
subplot=(rows,cols,1)
 if rows < cols:
plt.figure(figsize=(FIGSIZE,FIGSIZE/cols*rows))
else:
plt.figure(figsize=(FIGSIZE/rows*cols,FIGSIZE))
for i,(image, label)in enumerate(zip(images[:rows*cols], labels[:rows*cols])) :
title = '' if label is None else CLASSES[label]
correct = True
if predictions is not None:
title, correct = title_from_label_and_target(predictions[i], label)
 dynamic_titlesize = FIGSIZE*SPACING/max(rows,cols)*40+3
if display_mismatches_only:
if predictions[i] != label:
subplot = display_one_flower(image, title, subplot, not correct, titlesize=dynamic_titlesize)
 else:
subplot = display_one_flower(image, title, subplot, not correct, titlesize=dynamic_titlesize)
 plt.tight_layout()
if label is None and predictions is None:
plt.subplots_adjust(wspace=0, hspace=0)
 else:
plt.subplots_adjust(wspace=SPACING, hspace=SPACING)
 plt.show()
def display_training_curves(training, validation, title, subplot):
if subplot%10==1:
plt.subplots(figsize=(10,10), facecolor='
plt.tight_layout()
ax = plt.subplot(subplot)
 ax.set_facecolor('
ax.plot(training)
 ax.plot(validation)
 ax.set_title('model '+ title)
 ax.set_ylabel(title)
 ax.set_xlabel('epoch')
 ax.legend(['train', 'valid.'])
 def display_training_curves_v2(training, validation, learning_rate_list, title, subplot):
if subplot%10==1:
plt.subplots(figsize=(10,10), facecolor='
plt.tight_layout()
ax = plt.subplot(subplot)
 ax.set_facecolor('
ax.plot(training)
 ax.plot(validation)
 ax.set_title('model '+ title)
 ax.set_ylabel(title, color='b')
 ax.set_xlabel('epoch')
 ax.legend(['train', 'valid.', 'learning rate'])
 ax2 = ax.twinx()
ax2.plot(learning_rate_list, 'g-')
 ax2.set_ylabel('learning rate', color='g' )
for data in train_test_data:
data.drop('Ticket', axis=1, inplace=True )<count_missing_values>
ds_iter = iter(ds_train.unbatch().batch(20))
print(train_df['Fare'].isnull().sum())
 print(test_df['Fare'].isnull().sum() )<categorify>
one_batch = next(ds_iter)
 display_batch_of_images(one_batch )
for data in train_test_data:
data['Fare'].fillna(train_df.groupby(['Embarked', 'Pclass'])['Fare'].transform('median'), inplace=True )<count_missing_values>
[*IMAGE_SIZE, 3]
test_df['Fare'].isnull().sum()<data_type_conversions>
early_stopping = tf.keras.callbacks.EarlyStopping(monitor='val_loss', patience=3 )
for data in train_test_data:
data['Fare'] = pd.qcut(data['Fare'], 10 ).astype('category' ).cat.codes<count_values>
class LearningRateTracking(tf.keras.callbacks.Callback):
def on_epoch_end(self, epoch, logs=None):
keys = list(logs.keys())
 print("End epoch {} of training; got log keys: {}".format(epoch, keys))
train_df['Fare'].value_counts()<normalization>
use_efficientnet = False
if use_efficientnet:
!pip install -q efficientnet
minMaxScaler = MinMaxScaler()
for data in train_test_data:
data['Fare'] = minMaxScaler.fit_transform(data[['Fare']] )<count_values>
weight_per_class = True
if weight_per_class:
gc.enable()
def get_training_dataset_raw() :
dataset = load_dataset(TRAINING_FILENAMES, labeled = True, ordered = False)
 return dataset
raw_training_dataset = get_training_dataset_raw()
label_counter = Counter()
for images, labels in raw_training_dataset:
label_counter.update([labels.numpy() ])
 del raw_training_dataset
TARGET_NUM_PER_CLASS = 122
def get_weight_for_class(class_id):
counting = label_counter[class_id]
weight = TARGET_NUM_PER_CLASS / counting
return weight
weight_per_class = {class_id: get_weight_for_class(class_id)for class_id in range(104)}
train_df['Fare'].value_counts()<count_values>
using_ensemble_models = False
print(train_df['Cabin'].value_counts())
 print('-'*80)
 print(train_df['Cabin'].unique().shape)
 print('-'*80)
 print(train_df['Cabin'].str[:1].value_counts() )<count_values>
if not using_ensemble_models:
with strategy.scope() :
pretrained_model = tf.keras.applications.MobileNetV2(
 include_top=False ,
weights='imagenet',
input_shape=[*IMAGE_SIZE, 3] 
)
 pretrained_model.trainable = True
model = tf.keras.Sequential([
pretrained_model,
tf.keras.layers.GlobalAveragePooling2D() ,
tf.keras.layers.Dense(len(CLASSES), activation='softmax')
 ] )
print(test_df['Cabin'].str[:1].value_counts() )<feature_engineering>
if not using_ensemble_models:
model.compile(
 optimizer='nadam',
loss = 'sparse_categorical_crossentropy',
metrics=['sparse_categorical_accuracy'],
)
for data in train_test_data:
data['Cabin'] = data['Cabin'].str[:1]<categorify>
if not using_ensemble_models:
EPOCHS = 30
BATCH_SIZE = 16 * strategy.num_replicas_in_sync
STEPS_PER_EPOCH = NUM_TRAINING_IMAGES // BATCH_SIZE
cabin_mapping={"A":1, "B":2, "C":3, "D":4, "E":5, "F":6, "G":7, "T":8}
for data in train_test_data:
data['Cabin'] = data['Cabin'].map(cabin_mapping )<count_values>
if not using_ensemble_models:
def exponential_lr(epoch,
start_lr = 0.00001, min_lr = 0.00001, max_lr = 0.00005 * strategy.num_replicas_in_sync,
rampup_epochs = 5, sustain_epochs = 0,
exp_decay = 0.75):
def lr(epoch, start_lr, min_lr, max_lr, rampup_epochs, sustain_epochs, exp_decay):
if epoch < rampup_epochs:
lr =(( max_lr - start_lr)/
rampup_epochs * epoch + start_lr)
 elif epoch < rampup_epochs + sustain_epochs:
lr = max_lr
else:
lr =(( max_lr - min_lr)*
exp_decay**(epoch - rampup_epochs - sustain_epochs)+
min_lr)
 return lr
return lr(epoch,
start_lr,
min_lr,
max_lr,
rampup_epochs,
sustain_epochs,
exp_decay)
 lr_callback = tf.keras.callbacks.LearningRateScheduler(exponential_lr, verbose=True)
 rng = [i for i in range(EPOCHS)]
y = [exponential_lr(x)for x in rng]
plt.plot(rng, y)
 print("Learning rate schedule: {:.3g} to {:.3g} to {:.3g}".format(y[0], max(y), y[-1]))
print(train_df['Cabin'].value_counts())
 print('-'*80)
 print(train_df.groupby(['Pclass'])['Cabin'].median() )<categorify>
if not using_ensemble_models:
history = model.fit(
 ds_train,
validation_data=ds_valid,
epochs=EPOCHS,
steps_per_epoch=STEPS_PER_EPOCH,
callbacks=[lr_callback, checkpoint],
class_weight = weight_per_class
)
for data in train_test_data:
data['Cabin'].fillna(data.groupby(['Pclass'])['Cabin'].transform('median'), inplace=True )<count_missing_values>
if not using_ensemble_models:
model.load_weights(checkpoint_filepath )
print(train_df['Cabin'].isnull().sum())
 print('-'*80)
 print(train_df['Cabin'].value_counts() )<normalization>
print(checkpoint_filepath)
 tflite_model_name = checkpoint_filepath.replace('.h5', '.tflite')
 tflite_model_name
minMaxScaler = MinMaxScaler()
for data in train_test_data:
data['Cabin'] = minMaxScaler.fit_transform(data[['Cabin']] )<count_missing_values>
def get_pretrained_model(model_name, image_dataset_weights, trainable=True):
pretrained_model= model_name(
 include_top=False ,
weights=image_dataset_weights,
input_shape=[*IMAGE_SIZE, 3] 
)
 pretrained_model.trainable = trainable
model = tf.keras.Sequential([
pretrained_model,
tf.keras.layers.GlobalAveragePooling2D() ,
tf.keras.layers.Dense(len(CLASSES), activation='softmax')
 ])
 return model
print(train_df['Embarked'].isnull().sum())
 print(test_df['Embarked'].isnull().sum() )<data_type_conversions>
if using_ensemble_models:
with strategy.scope() :
model_EB7 = get_pretrained_model(EfficientNetB7, 'noisy-student', trainable=True)
 model_EB7.load_weights('.. /input/models/Petals_to_the_Metal-70K_images-trainable_True-EfficientNetB7.h5' )
for data in train_test_data:
data['Embarked'] = data['Embarked'].fillna('S' )<data_type_conversions>
if using_ensemble_models:
with strategy.scope() :
model_D201 = get_pretrained_model(tf.keras.applications.DenseNet201, 'imagenet', trainable=True)
 model_D201.load_weights('.. /input/models/Petals_to_the_Metal-70K_images-trainable_True-DenseNet201.h5' )
for data in train_test_data:
data['Embarked'] = data['Embarked'].astype('category' ).cat.codes<normalization>
from sklearn.metrics import f1_score, precision_score, recall_score, confusion_matrix
minMaxScaler = MinMaxScaler()
for data in train_test_data:
data['Embarked'] = minMaxScaler.fit_transform(data[['Embarked']] )<prepare_x_and_y>
if using_ensemble_models:
cmdataset = get_validation_dataset(ordered=True)
 images_ds = cmdataset.map(lambda image, label: image)
 labels_ds = cmdataset.map(lambda image, label: label ).unbatch()
cm_correct_labels = next(iter(labels_ds.batch(NUM_VALIDATION_IMAGES)) ).numpy()
m1 = model_EB7.predict(images_ds)
 m2 = model_D201.predict(images_ds)
 scores = []
for alpha in np.linspace(0,1,100):
cm_probabilities = alpha*m1+(1-alpha)*m2
cm_predictions = np.argmax(cm_probabilities, axis=-1)
 scores.append(f1_score(cm_correct_labels, cm_predictions, labels=range(len(CLASSES)) , average='macro'))
print("Correct labels: ", cm_correct_labels.shape, cm_correct_labels)
 print("Predicted labels: ", cm_predictions.shape, cm_predictions)
 plt.plot(scores)
 best_alpha = np.argmax(scores)/100
cm_probabilities = best_alpha*m1+(1-best_alpha)*m2
cm_predictions = np.argmax(cm_probabilities, axis=-1)
y_train_s = train_df['Survived']
x_train_df = train_df.drop('Survived', axis=1 )<split>
if using_ensemble_models:
print(best_alpha, max(scores))
x_train, x_test, y_train, y_test = train_test_split(x_train_df, y_train_s, test_size=0.2, random_state=10 )<compute_train_metric>
if using_ensemble_models:
test_ds = get_test_dataset(ordered=True)
 print('Computing predictions...')
 test_images_ds = test_ds.map(lambda image, idnum: image)
 probabilities1 = model_EB7.predict(test_images_ds)
 probabilities2 = model_D201.predict(test_images_ds)
 probabilities = best_alpha * probabilities1 +(1 - best_alpha)* probabilities2
predictions = np.argmax(probabilities, axis=-1)
 print(predictions)
 print('Generating submission.csv file...')
 test_ids_ds = test_ds.map(lambda image, idnum: idnum ).unbatch()
test_ids = next(iter(test_ids_ds.batch(NUM_TEST_IMAGES)) ).numpy().astype('U')
 np.savetxt(
 'submission.csv',
np.rec.fromarrays([test_ids, predictions]),
fmt=['%s', '%d'],
delimiter=',',
header='id,label',
comments='', 
)
 !head submission.csv
def cross_val_score_result(estimator, x, y, scoring, cv):
clf_scores = cross_val_score(estimator, x, y, scoring=scoring, cv=cv)
 clf_scores_mean = np.round(np.mean(clf_scores), 4)
 return clf_scores_mean<choose_model_class>
cmdataset = get_validation_dataset(ordered=True)
 images_ds = cmdataset.map(lambda image, label: image)
 labels_ds = cmdataset.map(lambda image, label: label ).unbatch()
cm_correct_labels = next(iter(labels_ds.batch(NUM_VALIDATION_IMAGES)) ).numpy()
if using_ensemble_models:
print('using_ensemble_models')
 probabilities1 = model_EB7.predict(images_ds)
 probabilities2 = model_D201.predict(images_ds)
 cm_probabilities = best_alpha * probabilities1 +(1 - best_alpha)* probabilities2
else:
cm_probabilities = model.predict(images_ds)
 cm_predictions = np.argmax(cm_probabilities, axis=-1)
 labels = range(len(CLASSES))
cmat = confusion_matrix(
 cm_correct_labels,
cm_predictions,
labels=labels, 
)
 cmat =(cmat.T / cmat.sum(axis=1)).T
classifiers = [
DecisionTreeClassifier() ,
RandomForestClassifier() ,
GradientBoostingClassifier() ,
KNeighborsClassifier() ,
SVC() ,
LGBMClassifier() ,
XGBClassifier() ,
AdaBoostClassifier()
]<find_best_params>
score = f1_score(
 cm_correct_labels,
cm_predictions,
labels=labels,
average='macro', 
)
 precision = precision_score(
 cm_correct_labels,
cm_predictions,
labels=labels,
average='macro', 
)
 recall = recall_score(
 cm_correct_labels,
cm_predictions,
labels=labels,
average='macro', 
)
 display_confusion_matrix(cmat, score, precision, recall )
best_clf_score = 0
best_clf = None
clf_name = []
clf_mean_score = []
for clf in classifiers:
current_clf_score = cross_val_score_result(clf, x_train, y_train, 'accuracy', 10)
 clf_name.append(clf.__class__.__name__)
 clf_mean_score.append(current_clf_score)
 if current_clf_score > best_clf_score:
best_clf_score = current_clf_score
best_clf = clf<train_on_grid>
model_performance_report = pd.DataFrame(columns=['model-family', 'model', 'epochs', 'arg min loss', 'arg max accuracy',
'min loss', 'max accuracy', 'f1', 'precision', 'recall'])
 model_performance_report.loc[len(model_performance_report)]={ 'model-family': 'VGG',
'model':'VGG16',
'epochs':12,
'arg min loss':11,
'arg max accuracy':11,
'min loss':3.47,
'max accuracy':0.23,
'f1':0.123,
'precision':0.146,
'recall':0.226}
model_performance_report.loc[len(model_performance_report)]={ 'model-family': 'DenseNet',
'model':'DenseNet201',
'epochs':12,
'arg min loss':11,
'arg max accuracy':10,
'min loss':1.31,
'max accuracy':0.74,
'f1':0.643,
'precision':0.761,
'recall':0.599}
model_performance_report.loc[len(model_performance_report)]={ 'model-family':'InceptionResNet',
'model':'InceptionResNetV2',
'epochs':12,
'arg min loss':11,
'arg max accuracy':11,
'min loss':1.57,
'max accuracy':0.66,
'f1':0.513,
'precision':0.640,
'recall':0.480}
model_performance_report.loc[len(model_performance_report)]={ 'model-family':'Inception',
'model':'InceptionV3',
'epochs':12,
'arg min loss':11,
'arg max accuracy':11,
'min loss':1.48,
'max accuracy':0.69,
'f1':0.581,
'precision':0.728,
'recall':0.538}
model_performance_report.loc[len(model_performance_report)]={ 'model-family':'MobileNet',
'model':'MobileNet',
'epochs':12,
'arg min loss':11,
'arg max accuracy':10,
'min loss':1.11,
'max accuracy':0.76,
'f1':0.717,
'precision':0.798,
'recall':0.679}
model_performance_report.loc[len(model_performance_report)]={ 'model-family':'MobileNet',
'model':'MobileNetV2',
'epochs':12,
'arg min loss':11,
'arg max accuracy':11,
'min loss':1.26,
'max accuracy':0.72,
'f1':0.650,
'precision':0.763,
'recall':0.606}
model_performance_report.loc[len(model_performance_report)]={ 'model-family':'NASNetMobile',
'model':'NASNetMobile',
'epochs':12,
'arg min loss':11,
'arg max accuracy':11,
'min loss':2.69,
'max accuracy':0.38,
'f1':0.224,
'precision':0.401,
'recall':0.203}
model_performance_report.loc[len(model_performance_report)]={ 'model-family':'ResNet',
'model':'ResNet50',
'epochs':12,
'arg min loss':11,
'arg max accuracy':11,
'min loss':3.85,
'max accuracy':0.12,
'f1':0.017,
'precision':0.035,
'recall':0.025}
model_performance_report.loc[len(model_performance_report)]={ 'model-family':'ResNet',
'model':'R101V2',
'epochs':12,
'arg min loss':11,
'arg max accuracy':9,
'min loss':0.87,
'max accuracy':0.83,
'f1':0.775,
'precision':0.842,
'recall':0.741}
model_performance_report.loc[len(model_performance_report)]={ 'model-family':'VGG',
'model':'VGG19',
'epochs':12,
'arg min loss':11,
'arg max accuracy':11,
'min loss':3.58,
'max accuracy':0.21,
'f1':0.031,
'precision':0.036,
'recall':0.048}
model_performance_report.loc[len(model_performance_report)]={ 'model-family':'Xception',
'model':'Xception',
'epochs':12,
'arg min loss':11,
'arg max accuracy':11,
'min loss':1.43,
'max accuracy':0.71,
'f1':0.575,
'precision':0.712,
'recall':0.536}
model_performance_report.loc[len(model_performance_report)]={ 'model-family':'ResNet',
'model':'R2 30e',
'epochs':30,
'arg min loss':29,
'arg max accuracy':28,
'min loss':0.83,
'max accuracy':0.83,
'f1':0.788,
'precision':0.863,
'recall':0.753}
model_performance_report.loc[len(model_performance_report)]={ 'model-family':'ResNet',
'model':'R101V2 1,2,3+OF',
'epochs':30,
'arg min loss':26,
'arg max accuracy':27,
'min loss':0.52,
'max accuracy':0.88,
'f1':0.864,
'precision':0.916,
'recall':0.842}
model_performance_report.loc[len(model_performance_report)]={ 'model-family':'DenseNet',
'model':'D 1,2',
'epochs':30,
'arg min loss':29,
'arg max accuracy':29,
'min loss':0.92,
'max accuracy':0.81,
'f1':0.767,
'precision':0.833,
'recall':0.732}
model_performance_report.loc[len(model_performance_report)]={ 'model-family':'DenseNet',
'model':'D201 1,2,4',
'epochs':30,
'arg min loss':29,
'arg max accuracy':27,
'min loss':0.92,
'max accuracy':0.82,
'f1':0.772,
'precision':0.846,
'recall':0.734}
model_performance_report.loc[len(model_performance_report)]={ 'model-family':'ResNet',
'model':'R101V2 1,2,4',
'epochs':30,
'arg min loss':29,
'arg max accuracy':28,
'min loss':0.66,
'max accuracy':0.85,
'f1':0.829,
'precision':0.870,
'recall':0.802}
model_performance_report.loc[len(model_performance_report)]={ 'model-family':'ResNet',
'model':'R101V2 1,2,4,5',
'epochs':30,
'arg min loss':29,
'arg max accuracy':23,
'min loss':0.66,
'max accuracy':0.86,
'f1':0.829,
'precision':0.883,
'recall':0.802}
model_performance_report.loc[len(model_performance_report)]={ 'model-family':'DenseNet',
'model':'D 1,8',
'epochs':30,
'arg min loss':26,
'arg max accuracy':28,
'min loss':0.23,
'max accuracy':0.95,
'f1':0.945,
'precision':0.950,
'recall':0.946}
model_performance_report.loc[len(model_performance_report)]={ 'model-family':'ResNet',
'model':'R101V2 1,8',
'epochs':30,
'arg min loss':10,
'arg max accuracy':16,
'min loss':0.36,
'max accuracy':0.92,
'f1':0.909,
'precision':0.913,
'recall':0.911}
model_performance_report.loc[len(model_performance_report)]={ 'model-family':'ResNet',
'model':'D 1,2,8',
'epochs':30,
'arg min loss':10,
'arg max accuracy':11,
'min loss':0.21,
'max accuracy':0.95,
'f1':0.953,
'precision':0.960,
'recall':0.950}
model_performance_report.loc[len(model_performance_report)]={ 'model-family':'EfficientNet',
'model':'EB7 1,2,9,10',
'epochs':30,
'arg min loss':29,
'arg max accuracy':27,
'min loss':0.73,
'max accuracy':0.84,
'f1':0.779,
'precision':0.839,
'recall':0.755}
model_performance_report.loc[len(model_performance_report)]={ 'model-family':'EfficientNet',
'model':'EB7 +11',
'epochs':30,
'arg min loss':29,
'arg max accuracy':28,
'min loss':1.0,
'max accuracy':0.81,
'f1':0.775,
'precision':0.769,
'recall':0.821}
model_performance_report.loc[len(model_performance_report)]={ 'model-family':'EfficientNet',
'model':'EB7 1,2,8,9,10,11',
'epochs':30,
'arg min loss':15,
'arg max accuracy':18,
'min loss':0.25,
'max accuracy':0.96,
'f1':0.955,
'precision':0.950,
'recall':0.964}
model_performance_report.loc[len(model_performance_report)]={ 'model-family':'ResNet',
'model':'D 1,2,8,11',
'epochs':30,
'arg min loss':24,
'arg max accuracy':23,
'min loss':0.22,
'max accuracy':0.95,
'f1':0.956,
'precision':0.957,
'recall':0.958}
model_performance_report.loc[len(model_performance_report)]={ 'model-family':'Ensemble',
'model':'Ensemble EB7+D201',
'epochs':30,
'arg min loss':24,
'arg max accuracy':23,
'min loss':0.22,
'max accuracy':0.95,
'f1':0.962,
'precision':0.960,
'recall':0.966}
extra_columns = ['total params', 'trainable params', 'non-trainable params','training time per epoch(sec)']
model_performance_report[extra_columns] = pd.DataFrame([[np.nan, np.nan, np.nan, np.nan]], index=model_performance_report.index)
 model_performance_report.loc[len(model_performance_report)]={ 'model-family':'DenseNet',
'model':'D 1,2,6',
'total params':18_521_768,
'trainable params':199_784,
'non-trainable params':18_321_984,
'training time per epoch(sec)':114,
'epochs':30,
'arg min loss':29,
'arg max accuracy':29,
'min loss':0.71,
'max accuracy':0.85,
'f1':0.826,
'precision':0.791,
'recall':0.890}
model_performance_report.loc[len(model_performance_report)]={ 'model-family':'DenseNet',
'model':'D 1,2,6,12',
'total params':18_521_768,
'trainable params':199_784,
'non-trainable params':18_321_984,
'training time per epoch(sec)':114,
'epochs':30,
'arg min loss':29,
'arg max accuracy':29,
'min loss':0.71,
'max accuracy':0.85,
'f1':0.826,
'precision':0.791,
'recall':0.890}
model_performance_report.loc[len(model_performance_report)]={ 'model-family':'DenseNet',
'model':'D 1,2,6,8',
'total params':18_521_768,
'trainable params':18_292_712,
'non-trainable params':229_056,
'training time per epoch(sec)':274,
'epochs':30,
'arg min loss':26,
'arg max accuracy':28,
'min loss':0.22,
'max accuracy':0.96,
'f1':0.948,
'precision':0.942,
'recall':0.957}
model_performance_report.loc[len(model_performance_report)]={ 'model-family':'DenseNet',
'model':'D 1,2,6,8,12',
'total params':18_521_768,
'trainable params':18_292_712,
'non-trainable params':229_056,
'training time per epoch(sec)':274,
'epochs':30,
'arg min loss':26,
'arg max accuracy':28,
'min loss':0.22,
'max accuracy':0.96,
'f1':0.948,
'precision':0.942,
'recall':0.957}
model_performance_report.loc[len(model_performance_report)]={ 'model-family':'EfficientNet',
'model':'EB7 1,2,6,8,9,10,11',
'total params':64_364_024,
'trainable params':64_053_304,
'non-trainable params':310_720,
'training time per epoch(sec)':511,
'epochs':30,
'arg min loss':20,
'arg max accuracy':28,
'min loss':0.24,
'max accuracy':0.96,
'f1':0.956,
'precision':0.949,
'recall':0.967}
model_performance_report.loc[len(model_performance_report)]={ 'model-family':'Ensemble',
'model':'Ensemble 6,12 EB7+D201',
'total params':82_885_792,
'trainable params':82_346_016,
'non-trainable params':539_776,
'training time per epoch(sec)':785,
'epochs':30,
'arg min loss':20,
'arg max accuracy':28,
'min loss':0.24,
'max accuracy':0.96,
'f1':0.962,
'precision':0.956,
'recall':0.971}
model_performance_report.loc[len(model_performance_report)]={ 'model-family':'MobileNet',
'model':'MobileNetV2 1,2,6',
'total params':2_391_208,
'trainable params':133_224,
'non-trainable params':2_257_984,
'training time per epoch(sec)':79,
'epochs':30,
'arg min loss':29,
'arg max accuracy':26,
'min loss':0.83,
'max accuracy':0.8,
'f1':0.781,
'precision':0.752,
'recall':0.850}
model_performance_report.loc[len(model_performance_report)]={ 'model-family':'MobileNet',
'model':'MobileNetV2 1,2,6,8',
'total params':2_391_208,
'trainable params':2_357_096,
'non-trainable params':34_112,
'training time per epoch(sec)':102,
'epochs':30,
'arg min loss':24,
'arg max accuracy':27,
'min loss':0.27,
'max accuracy':0.95,
'f1':0.936,
'precision':0.929,
'recall':0.951}
lgbm_clf = LGBMClassifier()
grid_param = {
'learning_rate':[0.005, 0.01, 0.015, 0.02],
'n_estimators':[100, 150, 200],
'bossting_type':['rf', 'gbdt', 'dart', 'goss'],
'max_depth':[10, 15, 20]
}
lgbm_grid = GridSearchCV(lgbm_clf, grid_param, cv=10)
 lgbm_grid.fit(x_train, y_train )<find_best_params>
model_performance_report = model_performance_report.sort_values(by='max accuracy')
 model_performance_report
print('best_param:', lgbm_grid.best_params_)
 print('best_score:{:.4f}'.format(lgbm_grid.best_score_))<predict_on_test>
dataset = get_validation_dataset()
dataset = dataset.unbatch().batch(20)
 batch = iter(dataset )
test_pred = lgbm_grid.best_estimator_.predict(test_df)
 submission = pd.DataFrame({
'PassengerId': test_df_PId,
'Survived': test_pred
} )<save_to_csv>
images, labels = next(batch )
submission.to_csv('submission_test.csv', index=False )<load_from_csv>
if using_ensemble_models:
probabilities1 = model_EB7.predict(images)
 probabilities2 = model_D201.predict(images)
 probabilities = best_alpha * probabilities1 +(1 - best_alpha)* probabilities2
else:
probabilities = model.predict(images )
check_submission = pd.read_csv('submission_test.csv')
 check_submission<import_modules>
predictions = np.argmax(probabilities, axis=-1)
 display_batch_of_images(( images, labels), predictions )
random.seed(42)
<set_options>
mismatches = sum(cm_predictions!=cm_correct_labels)
 print('Number of mismatches on validation data: {} out of {} or({:.2%})'.format(mismatches, NUM_VALIDATION_IMAGES, mismatches/NUM_VALIDATION_IMAGES))
warnings.filterwarnings("ignore" )<compute_test_metric>
cmdataset = get_validation_dataset(ordered=True)
 images_ds = cmdataset.map(lambda image, label: image)
 labels_ds = cmdataset.map(lambda image, label: label ).unbatch()
cm_correct_labels = next(iter(labels_ds.batch(NUM_VALIDATION_IMAGES)) ).numpy()
mismatches_images, mismatches_predictions, mismatches_labels = [], [], []
mismatches_dataset = tf.data.Dataset.from_tensors([])
 val_batch = iter(cmdataset.unbatch().batch(1))
for image_index in range(NUM_VALIDATION_IMAGES):
batch = next(val_batch)
 if cm_predictions[image_index] != cm_correct_labels[image_index]:
print('Predicted vs Correct labels: {}, {}'.format(cm_predictions[image_index], cm_correct_labels[image_index]))
def prim_set(names):
def pDiv(left, right):
try:
return left / right
except ZeroDivisionError:
return 1
def pPow(left, right):
try:
return abs(left)** min(float(right),8)
 except ZeroDivisionError:
return 1
except OverflowError:
return 1
def pSqrt(inp):
return math.sqrt(abs(inp))
def abs_(inp):
return abs(inp)
 pset = gp.PrimitiveSet("MAIN", len(names))
pset.addPrimitive(pDiv, 2)
 pset.addPrimitive(pPow, 2)
 pset.addPrimitive(pSqrt, 1)
 pset.addPrimitive(abs_, 1)
 pset.addPrimitive(operator.add, 2)
 pset.addPrimitive(operator.sub, 2)
 pset.addPrimitive(operator.mul, 2)
 pset.addPrimitive(operator.neg, 1)
 pset.addPrimitive(math.floor, 1)
 pset.addPrimitive(math.tanh, 1)
 pset.addPrimitive(math.sin, 1)
 pset.addPrimitive(math.cos, 1)
 pset.addPrimitive(max, 2)
 pset.addPrimitive(min, 2)
 for i, a in enumerate(pset.arguments):
new_name = names[i]
pset.arguments[i] = new_name
pset.mapping[new_name] = pset.mapping[a]
pset.mapping[new_name].value = new_name
del pset.mapping[a]
return pset<feature_engineering>
dataset = get_validation_dataset()
dataset = dataset.unbatch().batch(20)
 batch = iter(dataset)
 images, labels = next(batch )
def get_title(name):
title_search = re.search('([A-Za-z]+)\.', name)
 if title_search:
return title_search.group(1)
 return ""
def PrepData(data):
data['IsNull'] = data.isnull().sum(axis=1)
 data['Ticket'] = data['Ticket'].str.lower().replace('\W', '')
 data.Sex.fillna(0, inplace=True)
 data.loc[data.Sex != 'male', 'Sex'] = 1
data.loc[data.Sex == 'male', 'Sex'] = 0
data['NameLen'] = data['Name'].apply(len)
 bin_num = 4
data['NameLen'] = pd.qcut(data['NameLen'], bin_num,labels=list(range(bin_num)) ).astype(float)
 data['Has_Cabin'] = data["Cabin"].apply(lambda x: 0 if type(x)== float else 1)
 data['FamilySize'] = data['SibSp'] + data['Parch'] + 1
data['isFamily'] = 1
data.loc[data['isFamily'] == 1, 'notAlone'] = 0
data['Title'] = data['Name'].apply(get_title)
 mapping = {'Mlle': 'Rare', 'Major': 'Mr', 'Col': 'Mr', 'Sir': 'Rare', 'Rev': 'Mr',
'Don': 'Mr', 'Mme': 'Rare', 'Jonkheer': 'Mr', 'Lady': 'Mrs',
'Capt': 'Mr', 'Countess': 'Rare', 'Ms': 'Miss', 'Dona': 'Rare'}
data.replace({'Title': mapping}, inplace=True)
 title_mapping = {"Mr": 0, "Miss": 1, "Mrs": 2, "Master": 3, "Rare": 4}
data['Title'] = data['Title'].map(title_mapping)
 data['Title'] = data['Title'].fillna(0)
 data['Embarked'].fillna(method='backfill', inplace=True)
 data['Embarked'] = data['Embarked'].map({'C': 1, 'Q': 2, 'S': 0} ).astype(int)
 data['Fare'] = data['Fare'].fillna(train['Fare'].median())
 data.loc[ data['Fare'] <= 7.91, 'Fare'] = 0
data.loc[(data['Fare'] > 7.91)&(data['Fare'] <= 14.454), 'Fare'] = 1
data.loc[(data['Fare'] > 14.454)&(data['Fare'] <= 31), 'Fare'] = 2
data.loc[ data['Fare'] > 31, 'Fare'] = 3
data['Fare'] = data['Fare'].astype(int)
 data.Cabin.fillna('0', inplace=True)
 data.loc[data.Cabin.str[0] == 'A', 'Cabin'] = 1
data.loc[data.Cabin.str[0] == 'B', 'Cabin'] = 2
data.loc[data.Cabin.str[0] == 'C', 'Cabin'] = 3
data.loc[data.Cabin.str[0] == 'D', 'Cabin'] = 4
data.loc[data.Cabin.str[0] == 'E', 'Cabin'] = 5
data.loc[data.Cabin.str[0] == 'F', 'Cabin'] = 6
data.loc[data.Cabin.str[0] == 'G', 'Cabin'] = 7
data.loc[data.Cabin.str[0] == 'T', 'Cabin'] = 8
data['Cabin'] = data['Cabin'].astype(int)
 grouped = data.groupby(['Sex','Pclass', 'Title'])
 data['Age'] = grouped['Age'].apply(lambda x: x.fillna(x.median()))
data['Age'] = data['Age'].astype(int)
 boy =(data['Name'].str.contains('Master')) |(( data['Sex']==0)&(data['Age']<13))
female = data['Sex']==1
boy_or_female = boy | female
n_ticket = data[boy_or_female].groupby('Ticket' ).Survived.count()
tick_surv = data[boy_or_female].groupby('Ticket' ).Survived.mean()
data['Boy'] =(data['Name'].str.contains('Master')) |(( data['Sex']==0)&(data['Age']<13))
data['NTicket'] = data['Ticket'].replace(n_ticket)
 data.loc[~data.Ticket.isin(n_ticket.index),'NTicket']=0
data['TicketSurv'] = data['Ticket'].replace(tick_surv)
 data.loc[~data.Ticket.isin(tick_surv.index),'TicketSurv']=0
data['TicketSurv'].fillna(0, inplace=True)
 data.loc[ data['Age'] <= 16, 'Age'] = 5
data.loc[(data['Age'] > 16)&(data['Age'] <= 32), 'Age'] = 1
data.loc[(data['Age'] > 32)&(data['Age'] <= 48), 'Age'] = 2
data.loc[(data['Age'] > 48)&(data['Age'] <= 64), 'Age'] = 3
data.loc[ data['Age'] > 64, 'Age'] = 4
data['manual_tree'] = 0
data.loc[boy_or_female, 'manual_tree'] = 1
data.loc[(data['Sex'] == 1)& 
(data['Pclass'] == 3)& 
(data['Embarked'] == 0)& 
(data['Fare'] > 0), 'manual_tree'] = 0
data.loc[(data['Sex'] == 0)& 
(data['Title'] == 3), 'manual_tree'] = 1
tfidf_vec = TfidfVectorizer(max_features=15, token_pattern="\w+")
 svd = TruncatedSVD(n_components=10)
 tfidf_array = svd.fit_transform(tfidf_vec.fit_transform(data["Name"]))
for i in range(tfidf_array.shape[1]):
data.insert(len(data.columns), column = 'Name_' + str(i), value = tfidf_array [:,i])
 tfidf_vec = TfidfVectorizer(max_features=5, analyzer="char")
 svd = TruncatedSVD(n_components=3)
 tfidf_array = svd.fit_transform(tfidf_vec.fit_transform(data["Ticket"]))
for i in range(tfidf_array.shape[1]):
data.insert(len(data.columns), column = 'Ticket_' + str(i), value = tfidf_array [:,i])
 data['Ticket'] = data['Ticket'].str.extract('(\d+)', expand=False ).fillna(0 ).astype(float)
 data['Ticket'] = np.round(np.log1p(data['Ticket'])*10)
 data.drop(['Name'],1,inplace=True)
 return data.astype(float )<load_from_csv>
for i in range(3):
display_batch_of_images(( images, labels), predictions, display_mismatches_only=True)
 images, labels = next(batch )
train = pd.read_csv(".. /input/titanic/train.csv", dtype={"Age": np.float64}, index_col='PassengerId')
 test = pd.read_csv(".. /input/titanic/test.csv", dtype={"Age": np.float64}, index_col='PassengerId' )<load_from_csv>
one_batch = next(ds_iter)
 display_batch_of_images(one_batch )
test_data_with_labels = pd.read_csv('.. /input/titanic-test-data/titanic.csv')
 test_data = pd.read_csv('.. /input/titanic/test.csv')
 for i, name in enumerate(test_data_with_labels['name']):
if '"' in name:
test_data_with_labels['name'][i] = re.sub('"', '', name)
 for i, name in enumerate(test_data['Name']):
if '"' in name:
test_data['Name'][i] = re.sub('"', '', name)
 survived = []
for name in test_data['Name']:
survived.append(int(test_data_with_labels.loc[test_data_with_labels['name'] == name]['survived'].values[-1]))
test['Survived'] = survived<data_type_conversions>
using_tta = False
tta_iterations = 3
df = pd.concat(( train,test),0)
 target = train['Survived'].astype(float)
 df = PrepData(df)
 df['Ticket'] = df['Ticket'].astype(int ).astype('category')
 col_to_use = ['Embarked', 'Fare', 'Parch', 'Pclass', 'Sex', 'Age',
'NameLen', 'Has_Cabin', 'Cabin', 'FamilySize', 'isFamily',
'Title', 'TicketSurv', 'NTicket', 'Boy', 'manual_tree',
'Ticket_0', 'Ticket_1', 'Ticket_2',
'Name_0', 'Name_1', 'Name_2', 'Name_3', 'Name_4',
'Name_5', 'Name_6', 'Name_7', 'Name_8', 'Name_9']
df = pd.get_dummies(df[col_to_use])
 df[col_to_use] += 0.0
mungedtrain = df[:train.shape[0]].copy()
mungedtest = df[train.shape[0]:].copy()
mytrain = mungedtrain.values.tolist()
mytest = mungedtest.values.tolist()<define_search_space>
if using_tta:
def get_test_dataset(ordered=False):
dataset = load_dataset(TEST_FILENAMES, labeled=False, ordered=ordered)
 dataset = dataset.map(data_augment, num_parallel_calls=AUTO)
 dataset = dataset.batch(BATCH_SIZE)
 dataset = dataset.prefetch(AUTO)
 return dataset
GPhof = []
for n in [5,7,14,21,28,42,57]:
hof, Tbox = mydeap(mungedtrain, target, seed=n, mxvl=7, ngen=121)
 GPhof.append(hof )<drop_column>
def predict_tta(model, tta_iterations):
probs = []
for i in range(tta_iterations):
print('TTA iteration ', i)
 test_ds = get_test_dataset(ordered=True)
 test_images_ds = test_ds.map(lambda image, idnum: image)
 if using_ensemble_models:
print('using_ensemble_models')
 probabilities1 = model_EB7.predict(test_images_ds)
 probabilities2 = model_D201.predict(test_images_ds)
 probabilities = best_alpha * probabilities1 +(1 - best_alpha)* probabilities2
probs.append(probabilities)
 else:
probs.append(model.predict(test_images_ds,verbose=0))
return probs
test = test.reset_index()<compute_test_metric>
test_ds = get_test_dataset(ordered=True)
 test_images_ds = test_ds.map(lambda image, idnum: image)
 if using_tta:
print('Computing predictions using TTA...')
 probabilities = np.mean(predict_tta(model, tta_iterations), axis=0)
 else:
print('Computing predictions...')
 probabilities = model.predict(test_images_ds)
 predictions = np.argmax(probabilities, axis=-1)
 print(predictions )
<save_to_csv><EOS> 
print('using_ensemble_models:', using_ensemble_models)
 print('Generating submission.csv file...')
 test_ids_ds = test_ds.map(lambda image, idnum: idnum ).unbatch()
test_ids = next(iter(test_ids_ds.batch(NUM_TEST_IMAGES)) ).numpy().astype('U')
 np.savetxt(
 'submission.csv',
np.rec.fromarrays([test_ids, predictions]),
fmt=['%s', '%d'],
delimiter=',',
header='id,label',
comments='', 
)
 !head submission.csv