Kaggle competition: Titanic - Machine Learning from Disaster
@tf.function
def get_mat(rotation, shear, height_zoom, width_zoom, height_shift, width_shift):
rotation = math.pi * rotation / 180.
 shear = math.pi * shear / 180.
 c1 = tf.math.cos(rotation)
 s1 = tf.math.sin(rotation)
 one = tf.constant([1],dtype='float32')
 zero = tf.constant([0],dtype='float32')
 rotation_matrix = tf.reshape(tf.concat([c1,s1,zero, -s1,c1,zero, zero,zero,one],axis=0),[3,3])
 c2 = tf.math.cos(shear)
 s2 = tf.math.sin(shear)
 shear_matrix = tf.reshape(tf.concat([one,s2,zero, zero,c2,zero, zero,zero,one],axis=0),[3,3])
 zoom_matrix = tf.reshape(tf.concat([one/height_zoom,zero,zero, zero,one/width_zoom,zero, zero,zero,one],axis=0),[3,3])
 shift_matrix = tf.reshape(tf.concat([one,zero,height_shift, zero,one,width_shift, zero,zero,one],axis=0),[3,3])
 return K.dot(K.dot(rotation_matrix, shear_matrix), K.dot(zoom_matrix, shift_matrix))
@tf.function
def transform(image):
DIM = IMAGE_SIZE[0]
XDIM = DIM%2
if tf.random.uniform(shape=[], minval=0, maxval=2, dtype=tf.int32, seed=SEED)== 0:
rot = 17.* tf.random.normal([1],dtype='float32')
 else:
rot = tf.constant([0],dtype='float32')
 if tf.random.uniform(shape=[], minval=0, maxval=2, dtype=tf.int32, seed=SEED)== 0:
shr = 5.5 * tf.random.normal([1],dtype='float32')
 else:
shr = tf.constant([0],dtype='float32')
 if tf.random.uniform(shape=[], minval=0, maxval=3, dtype=tf.int32, seed=SEED)== 0:
h_zoom = tf.random.normal([1],dtype='float32')/8.5
if h_zoom > 0:
h_zoom = 1.0 + h_zoom * -1
else:
h_zoom = 1.0 + h_zoom
else:
h_zoom = tf.constant([1],dtype='float32')
 if tf.random.uniform(shape=[], minval=0, maxval=3, dtype=tf.int32, seed=SEED)== 0:
w_zoom = tf.random.normal([1],dtype='float32')/8.5
if w_zoom > 0:
w_zoom = 1.0 + w_zoom * -1
else:
w_zoom = 1.0 + w_zoom
else:
w_zoom = tf.constant([1],dtype='float32')
 if tf.random.uniform(shape=[], minval=0, maxval=3, dtype=tf.int32, seed=SEED)== 0:
h_shift = 18.* tf.random.normal([1],dtype='float32')
 else:
h_shift = tf.constant([0],dtype='float32')
 if tf.random.uniform(shape=[], minval=0, maxval=3, dtype=tf.int32, seed=SEED)== 0:
w_shift = 18.* tf.random.normal([1],dtype='float32')
 else:
w_shift = tf.constant([0],dtype='float32')
 m = get_mat(rot,shr,h_zoom,w_zoom,h_shift,w_shift)
 x = tf.repeat(tf.range(DIM//2,-DIM//2,-1), DIM)
 y = tf.tile(tf.range(-DIM//2,DIM//2),[DIM])
 z = tf.ones([DIM*DIM],dtype='int32')
 idx = tf.stack([x,y,z])
 idx2 = K.dot(m,tf.cast(idx,dtype='float32'))
idx2 = K.cast(idx2,dtype='int32')
 idx2 = K.clip(idx2,-DIM//2+XDIM+1,DIM//2)
 idx3 = tf.stack([DIM//2-idx2[0,], DIM//2-1+idx2[1,]])
 d = tf.gather_nd(image,tf.transpose(idx3))
return tf.reshape(d,[DIM,DIM,3] )<prepare_x_and_y>
train_data = pd.read_csv("/kaggle/input/titanic/train.csv")
 test_data = pd.read_csv("/kaggle/input/titanic/test.csv")
 train_data
@tf.function
def transform_grid_mark(image, inv_mat, image_shape):
h, w, c = image_shape
cx, cy = w//2, h//2
new_xs = tf.repeat(tf.range(-cx, cx, 1), h)
 new_ys = tf.tile(tf.range(-cy, cy, 1), [w])
 new_zs = tf.ones([h*w], dtype=tf.int32)
 old_coords = tf.matmul(inv_mat, tf.cast(tf.stack([new_xs, new_ys, new_zs]), tf.float32))
old_coords_x, old_coords_y = tf.round(old_coords[0, :] + tf.cast(w, tf.float32)//2.) , tf.round(old_coords[1, :] + tf.cast(h, tf.float32)//2.)
old_coords_x = tf.cast(old_coords_x, tf.int32)
 old_coords_y = tf.cast(old_coords_y, tf.int32)
 clip_mask_x = tf.logical_or(old_coords_x<0, old_coords_x>w-1)
 clip_mask_y = tf.logical_or(old_coords_y<0, old_coords_y>h-1)
 clip_mask = tf.logical_or(clip_mask_x, clip_mask_y)
 old_coords_x = tf.boolean_mask(old_coords_x, tf.logical_not(clip_mask))
old_coords_y = tf.boolean_mask(old_coords_y, tf.logical_not(clip_mask))
new_coords_x = tf.boolean_mask(new_xs+cx, tf.logical_not(clip_mask))
new_coords_y = tf.boolean_mask(new_ys+cy, tf.logical_not(clip_mask))
old_coords = tf.cast(tf.stack([old_coords_y, old_coords_x]), tf.int32)
 new_coords = tf.cast(tf.stack([new_coords_y, new_coords_x]), tf.int64)
 rotated_image_values = tf.gather_nd(image, tf.transpose(old_coords))
rotated_image_channel = list()
for i in range(c):
vals = rotated_image_values[:,i]
sparse_channel = tf.SparseTensor(tf.transpose(new_coords), vals, [h, w])
 rotated_image_channel.append(tf.sparse.to_dense(sparse_channel, default_value=0, validate_indices=False))
return tf.transpose(tf.stack(rotated_image_channel), [1,2,0])
 @tf.function
def random_rotate(image, angle, image_shape):
def get_rotation_mat_inv(angle):
angle = math.pi * angle / 180
cos_val = tf.math.cos(angle)
 sin_val = tf.math.sin(angle)
 one = tf.constant([1], tf.float32)
 zero = tf.constant([0], tf.float32)
 rot_mat_inv = tf.concat([cos_val, sin_val, zero,
-sin_val, cos_val, zero,
zero, zero, one], axis=0)
 rot_mat_inv = tf.reshape(rot_mat_inv, [3,3])
 return rot_mat_inv
angle = float(angle)* tf.random.normal([1],dtype='float32')
 rot_mat_inv = get_rotation_mat_inv(angle)
 return transform_grid_mark(image, rot_mat_inv, image_shape)
 @tf.function
def grid_mask() :
h = tf.constant(IMAGE_SIZE[0], dtype=tf.float32)
 w = tf.constant(IMAGE_SIZE[1], dtype=tf.float32)
 image_height, image_width =(h, w)
 d1 = 112
d2 = 352
rotate_angle = 45
ratio = 0.6
hh = tf.math.ceil(tf.math.sqrt(h*h+w*w))
hh = tf.cast(hh, tf.int32)
 hh = hh+1 if hh%2==1 else hh
d = tf.random.uniform(shape=[], minval=d1, maxval=d2, dtype=tf.int32)
 l = tf.cast(tf.cast(d,tf.float32)*ratio+0.5, tf.int32)
 st_h = tf.random.uniform(shape=[], minval=0, maxval=d, dtype=tf.int32)
 st_w = tf.random.uniform(shape=[], minval=0, maxval=d, dtype=tf.int32)
 y_ranges = tf.range(-1 * d + st_h, -1 * d + st_h + l)
 x_ranges = tf.range(-1 * d + st_w, -1 * d + st_w + l)
 for i in range(0, hh//d+1):
s1 = i * d + st_h
s2 = i * d + st_w
y_ranges = tf.concat([y_ranges, tf.range(s1,s1+l)], axis=0)
 x_ranges = tf.concat([x_ranges, tf.range(s2,s2+l)], axis=0)
 x_clip_mask = tf.logical_or(x_ranges <0 , x_ranges > hh-1)
 y_clip_mask = tf.logical_or(y_ranges <0 , y_ranges > hh-1)
 clip_mask = tf.logical_or(x_clip_mask, y_clip_mask)
 x_ranges = tf.boolean_mask(x_ranges, tf.logical_not(clip_mask))
y_ranges = tf.boolean_mask(y_ranges, tf.logical_not(clip_mask))
hh_ranges = tf.tile(tf.range(0,hh), [tf.cast(tf.reduce_sum(tf.ones_like(x_ranges)) , tf.int32)])
 x_ranges = tf.repeat(x_ranges, hh)
 y_ranges = tf.repeat(y_ranges, hh)
 y_hh_indices = tf.transpose(tf.stack([y_ranges, hh_ranges]))
x_hh_indices = tf.transpose(tf.stack([hh_ranges, x_ranges]))
y_mask_sparse = tf.SparseTensor(tf.cast(y_hh_indices, tf.int64), tf.zeros_like(y_ranges), [hh, hh])
 y_mask = tf.sparse.to_dense(y_mask_sparse, 1, False)
 x_mask_sparse = tf.SparseTensor(tf.cast(x_hh_indices, tf.int64), tf.zeros_like(x_ranges), [hh, hh])
 x_mask = tf.sparse.to_dense(x_mask_sparse, 1, False)
 mask = tf.expand_dims(tf.clip_by_value(x_mask + y_mask, 0, 1), axis=-1)
 mask = random_rotate(mask, rotate_angle, [hh, hh, 1])
 mask = tf.image.crop_to_bounding_box(mask,(hh-tf.cast(h, tf.int32)) //2,(hh-tf.cast(w, tf.int32)) //2, tf.cast(image_height, tf.int32), tf.cast(image_width, tf.int32))
return mask
@tf.function
def apply_grid_mask(image):
mask = grid_mask()
mask = tf.concat([mask, mask, mask], axis=-1)
 return image * tf.cast(mask, 'float32' )<normalization>
women = train_data.loc[train_data.Sex == 'female']["Survived"]
rate_women = sum(women)/len(women)
 print("% of women who survived:", rate_women*100)
 men = train_data.loc[train_data.Sex == 'male']["Survived"]
rate_men = sum(men)/len(men)
 print("% of men who survived", rate_men*100)
 passengers = train_data.Survived.values
rate_passengers = sum(passengers)/len(passengers)
 print("
% of all passengers who survived", rate_passengers*100)
 print("% of survivors who were women: ", 100*sum(women)/sum(passengers))
print("% of survivors who were men: ", 100*sum(men)/sum(passengers))
print("
% of passengers who were women:", 100*len(train_data.loc[train_data.Sex == 'female'])/len(train_data))
print("% of passengers who were men:",100*len(train_data.loc[train_data.Sex == 'male'])/len(train_data))
@tf.function
def image_augmentation_tta_0(iw, filename):
max_chance = 8
image, words = iw
if tf.random.uniform(shape=[], minval=0, maxval=11, dtype=tf.int32, seed=SEED)< max_chance:
image = tf.image.flip_left_right(image)
 if tf.random.uniform(shape=[], minval=0, maxval=11, dtype=tf.int32, seed=SEED)< max_chance:
image = tf.image.random_brightness(image, 0.1)
 if tf.random.uniform(shape=[], minval=0, maxval=11, dtype=tf.int32, seed=SEED)< max_chance:
image = tf.image.random_contrast(image, 0.9, 1.1)
 return(( image, words), filename)
 @tf.function
def image_augmentation_tta_1(iw, filename):
max_chance = 8
image, words = iw
if tf.random.uniform(shape=[], minval=0, maxval=11, dtype=tf.int32, seed=SEED)< max_chance:
image = tf.image.flip_left_right(image)
 if tf.random.uniform(shape=[], minval=0, maxval=11, dtype=tf.int32, seed=SEED)< max_chance:
image = tf.image.random_brightness(image, 0.1)
 if tf.random.uniform(shape=[], minval=0, maxval=11, dtype=tf.int32, seed=SEED)< max_chance:
image = tf.image.random_contrast(image, 0.9, 1.1)
 if tf.random.uniform(shape=[], minval=0, maxval=11, dtype=tf.int32, seed=SEED)< max_chance:
image = tf.image.random_saturation(image, 0.95, 1.05)
 if tf.random.uniform(shape=[], minval=0, maxval=11, dtype=tf.int32, seed=SEED)< max_chance:
image = tf.image.random_hue(image, 0.05)
 if tf.random.uniform(shape=[], minval=0, maxval=11, dtype=tf.int32, seed=SEED)< max_chance:
image = transform(image)
 return(( image, words), filename)
 @tf.function
def image_augmentation_tta_2(iw, filename):
max_chance = 8
image, words = iw
if tf.random.uniform(shape=[], minval=0, maxval=11, dtype=tf.int32, seed=SEED)< max_chance:
image = tf.image.flip_left_right(image)
 if tf.random.uniform(shape=[], minval=0, maxval=11, dtype=tf.int32, seed=SEED)< max_chance:
image = tf.image.random_brightness(image, 0.1)
 if tf.random.uniform(shape=[], minval=0, maxval=11, dtype=tf.int32, seed=SEED)< max_chance:
image = tf.image.random_contrast(image, 0.9, 1.1)
 if tf.random.uniform(shape=[], minval=0, maxval=11, dtype=tf.int32, seed=SEED)< max_chance:
image = tf.image.random_saturation(image, 0.95, 1.05)
 if tf.random.uniform(shape=[], minval=0, maxval=11, dtype=tf.int32, seed=SEED)< max_chance:
image = tf.image.random_hue(image, 0.05)
 if tf.random.uniform(shape=[], minval=0, maxval=11, dtype=tf.int32, seed=SEED)< max_chance:
image = transform(image)
 if tf.random.uniform(shape=[], minval=0, maxval=10, dtype=tf.int32, seed=SEED)< max_chance:
image = apply_grid_mask(image)
 return(( image, words), filename )<import_modules>
withsbsp=train_data.loc[train_data.Survived == 0]["SibSp"]
print("Average number of siblings/spouses for those who died: {}".format(sum(withsbsp)/len(withsbsp)))
 survivedsbsp = train_data.loc[train_data.Survived == 1]["SibSp"]
print("Average number of siblings/spouses for those who survived: {}".format(sum(survivedsbsp)/len(survivedsbsp)))
 nmen = train_data.loc[train_data.Survived == 0][train_data.Sex == 'male']["Parch"]
ymen = train_data.loc[train_data.Survived == 1][train_data.Sex == 'male']["Parch"]
print("
Average number of children/parents per man who did not survive: {}".format(sum(nmen)/len(nmen)))
 print("Average number of children/parents per man who survived: {}".format(sum(ymen)/len(ymen)))
from tensorflow.keras.models import Model, Sequential
from tensorflow.keras.layers import Input, Flatten, Dense, Dropout, AveragePooling2D, GlobalAveragePooling2D, SpatialDropout2D, BatchNormalization, Activation, Concatenate<init_hyperparams>
male=train_data.loc[train_data.Sex=='male']
nmen = train_data.loc[train_data.Survived == 0][train_data.Sex == 'male'][train_data.Parch >=1]
ymen = train_data.loc[train_data.Survived == 1][train_data.Sex == 'male'][train_data.Parch >=1]
mediann=np.median(nmen.Parch)
 mediany=np.median(ymen.Parch)
 print("
Average number of children/Fathers per man who did not survive: {}
The median is {}" 
.format(sum(nmen.Parch)/len(nmen),mediann))
print("Average number of children/Fathers per man who survived: {}
The median is {}" 
.format(sum(ymen.Parch)/len(ymen),mediany))
print("% of men with children/parents: ", 100*(len(nmen)+len(ymen)) /len(male))
class EpochCallback(tf.keras.callbacks.Callback):
def on_epoch_begin(self, epoch, logs=None):
global current_epoch
global chance
current_epoch = epoch
if current_epoch < 2:
chance = 0
elif current_epoch < 9:
chance = current_epoch - 1
else:
chance = 8
print(f'Epoch
print(datetime.now() )<choose_model_class>
train_data1 = train_data.drop(['Name', 'Ticket','Cabin','Embarked','PassengerId'], axis = 1)
 train_data1
X_test = test_data.drop(['Name', 'Ticket','Cabin','Embarked','PassengerId'], axis = 1)
 train_data1 = pd.get_dummies(train_data1)
 train_data1 = train_data1.drop(['Sex_male'],axis=1)
 X_test=pd.get_dummies(X_test)
 X_test= X_test.drop(['Sex_male'],axis=1)
es_val_acc = tf.keras.callbacks.EarlyStopping(
 monitor='val_sparse_categorical_accuracy', min_delta=0.001, patience=5, verbose=1, mode='auto',
baseline=None, restore_best_weights=True 
)
 es_val_loss = tf.keras.callbacks.EarlyStopping(
 monitor='val_loss', min_delta=0.001, patience=5, verbose=1, mode='auto',
baseline=None, restore_best_weights=True 
)
 es_acc = tf.keras.callbacks.EarlyStopping(
 monitor='sparse_categorical_accuracy', min_delta=0.001, patience=5, verbose=1, mode='auto',
baseline=None, restore_best_weights=False 
)
 es_loss = tf.keras.callbacks.EarlyStopping(
 monitor='loss', min_delta=0.001, patience=5, verbose=1, mode='auto',
baseline=None, restore_best_weights=False 
)
 epoch_cb = EpochCallback()<choose_model_class>
c1a = int(( train_data1.loc[train_data1.Pclass ==1])['Age'].mean())
 c2a = int(( train_data1.loc[train_data1.Pclass ==2])['Age'].mean())
 c3a = int(( train_data1.loc[train_data1.Pclass ==3])['Age'].mean())
 print(c1a,c2a,c3a)
 a =train_data1.loc[train_data.Pclass==1].fillna({'Age':c1a})
 train_data1.loc[train_data.Pclass==1]=a
a2=X_test.loc[X_test.Pclass==1].fillna({'Age':c1a})
 X_test.loc[X_test.Pclass==1] =a2
b=train_data1.loc[train_data.Pclass==2].fillna({'Age':c2a})
 train_data1.loc[train_data.Pclass==2]=b
b2=X_test.loc[X_test.Pclass==2].fillna({'Age':c2a})
 X_test.loc[X_test.Pclass==2] =b2
c=train_data1.loc[train_data.Pclass==3].fillna({'Age':c3a})
 train_data1.loc[train_data.Pclass==3]=c
c2=X_test.loc[X_test.Pclass==3].fillna({'Age':c3a})
 X_test.loc[X_test.Pclass==3] =c2
X_test['Fare'] = X_test['Fare'].fillna(20)
with strategy.scope() :
efn7 = efn.EfficientNetB7(weights='noisy-student', include_top=False, input_shape=(IMAGE_SIZE[0], IMAGE_SIZE[1], 3))
for layer in efn7.layers:
layer.trainable = True
model_image = Sequential([
efn7,
GlobalAveragePooling2D(name='efficientnet-b7_gap'),
], name='b7-image')
 model_words = Sequential([
Input(( 6633,), name='mlp-words_input'),
Dense(331, name='mlp-words_dense_1'),
BatchNormalization(name='mlp-words_bn_1'),
Activation('relu', name='mlp-words_act_1'),
Dense(110, name='mlp-words_dense_2'),
BatchNormalization(name='mlp-words_bn_2'),
Activation('relu', name='mlp-words_act_2'),
], name='mlp-words')
 concatenate = Concatenate(name='concatenate' )([model_image.output, model_words.output])
 output = Dense(len(CLASSES), activation='softmax', name='output' )(concatenate)
 model = Model(inputs=[model_image.input, model_words.input], outputs=output)
 model.compile(optimizer=tfa.optimizers.LAMB(0.01), loss='sparse_categorical_crossentropy', metrics=['sparse_categorical_accuracy'])
 model.summary() 
<train_model>
X_train = train_data1.drop(columns=["Survived"])
 y_train = train_data1.Survived
params = {'n_estimators': [1,200],
'max_features': [1,6],
'max_depth': [1,None],
'min_samples_split': [2,10],
'min_samples_leaf': [1,10],
'criterion':['gini','entropy']
}
forest = RandomForestClassifier()
forest_cv = RandomizedSearchCV(forest, params, cv=5)
 forest_cv.fit(X_train,y_train)
 print("Tuned Decision Tree Parameters: {}".format(forest_cv.best_params_))
print("Best score is {}".format(forest_cv.best_score_))
<init_hyperparams><EOS> 
y_pred = forest_cv.predict(X_test)
 output = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': y_pred})
 output.to_csv('my_submission.csv', index=False)
 print("Your submission was successfully saved!" )