Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<prepare_x_and_y>
import numpy as np
import pandas as pd
import os
train_images = train.iloc[:,1:]/255 - 0.5
train_labels = train.iloc[:,0]
test_images = test.iloc[:,1:]/255 - 0.5
test_id = test.iloc[:,0]
X_train=train_images.values.reshape(-1,28,28,1)
 X_test=test_images.values.reshape(-1,28,28,1)
 Y_train=to_categorical(train_labels)
 fig=plt.figure(figsize=(16, 16))
for i in range(1, 5):
img = X_train[i].squeeze()
fig.add_subplot(1, 5, i)
 plt.imshow(img,cmap=plt.cm.binary)
 plt.axis('off')
 plt.show()<categorify>
training = pd.read_csv(".. /input/train.csv");
x_test = pd.read_csv(".. /input/test.csv");
def elastic_transform(image, alpha_range, sigma, random_state=None):

 if random_state is None:
random_state = np.random.RandomState(None)
 if np.isscalar(alpha_range):
alpha = alpha_range
else:
alpha = np.random.uniform(low=alpha_range[0], high=alpha_range[1])
 shape = image.shape
dx = gaussian_filter(( random_state.rand(*shape)* 2 - 1), sigma)* alpha
dy = gaussian_filter(( random_state.rand(*shape)* 2 - 1), sigma)* alpha
x, y, z = np.meshgrid(np.arange(shape[0]), np.arange(shape[1]), np.arange(shape[2]), indexing='ij')
 indices = np.reshape(x+dx,(-1, 1)) , np.reshape(y+dy,(-1, 1)) , np.reshape(z,(-1, 1))
return map_coordinates(image, indices, order=1, mode='reflect' ).reshape(shape )<define_variables>
training['n_new']= training['Name'].apply(lambda x: x.split(',')[1].split('.')[0].strip() )
datagen = ImageDataGenerator(
 rotation_range=10,
zoom_range = 0.10,
width_shift_range=0.1,
height_shift_range=0.1
)<choose_model_class>
training['n_new'].value_counts()
nets=3
model = [0] * nets
for j in range(nets):
model[j] = Sequential()
model[j].add(Conv2D(32, kernel_size = 3, activation='relu', input_shape =(28, 28, 1)))
 model[j].add(BatchNormalization())
 model[j].add(Conv2D(32, kernel_size = 3, activation='relu'))
model[j].add(BatchNormalization())
 model[j].add(Conv2D(32, kernel_size = 5, strides=2, padding='same', activation='relu'))
model[j].add(BatchNormalization())
 model[j].add(Dropout(0.1))
model[j].add(Conv2D(64, kernel_size = 3, activation='relu'))
model[j].add(BatchNormalization())
 model[j].add(Conv2D(64, kernel_size = 3, activation='relu'))
model[j].add(BatchNormalization())
 model[j].add(Conv2D(64, kernel_size = 5, strides=2, padding='same', activation='relu'))
model[j].add(BatchNormalization())
 model[j].add(Dropout(0.15))
model[j].add(Conv2D(128, kernel_size = 4, activation='relu'))
model[j].add(BatchNormalization())
 model[j].add(Flatten())
 model[j].add(Dropout(0.2))
model[j].add(Dense(10, activation='softmax'))
model[j].compile(optimizer="adam", loss="categorical_crossentropy", metrics=["accuracy"])
 model[0].summary() 
<train_model>
training['Title'] = 0
decay = LearningRateScheduler(lambda x: 1e-3 * 0.95 ** x)
 epochs = 30
batch_size = 64
history = [0] * nets
for j in range(nets):
X_train2, X_val2, Y_train2, Y_val2 = train_test_split(X_train, Y_train, test_size = 0.1)
 history[j] = model[j].fit_generator(datagen.flow(X_train2, Y_train2, batch_size=batch_size),\
epochs = epochs,steps_per_epoch = X_train2.shape[0]//64,\
validation_data =(X_val2,Y_val2),callbacks = [decay], verbose = True)
 model_json = model[j].to_json()
with open("model"+str(j)+".json", "w")as json_file:
json_file.write(model_json)
 model[j].save_weights("modelw"+str(j)+".h5")
 print("Saved model to disk")
<save_to_csv>
training.loc[training["n_new"] == 'Mr', 'Title'] = 1
training.loc[training["n_new"] == 'Miss', 'Title'] = 4
training.loc[training["n_new"] == 'Mrs', 'Title'] = 5
training.loc[training["n_new"] == 'Master', 'Title'] = 3
training.loc[training["n_new"] == 'Dr', 'Title'] = 2
predictions = np.zeros(( X_test.shape[0],10))
for j in range(nets):
predictions = predictions + model[j].predict(X_test)
 test_labels = np.argmax(predictions, axis=1)
 submission = pd.DataFrame(test_labels)
 submission.index = test_id
submission.columns = ["label"]
submission.to_csv("submission.csv" )<set_options>
print("The number of traning examples(data points)= %i " % training.shape[0])
 print("The number of features we have = %i " % training.shape[1] )
%reload_ext autoreload
%autoreload 2
%matplotlib inline<import_modules>
unique, count= np.unique(training["Survived"], return_counts=True)
 print("The number of occurances of each class in the dataset = %s " % dict(zip(unique, count)) , "
" )
from fastai.vision import *
from fastai.metrics import error_rate
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from tqdm import tqdm<define_variables>
training.isna().sum()
PATH = ".. /input/Kannada-MNIST/"<load_from_csv>
C = training.Cabin[training.Cabin.isna() ]
C_not = training.Cabin[training.Cabin.notna() ]
C.values[:] = 0
C_not.values[:] = 1
train = pd.read_csv(PATH+"train.csv")
 train.head()<load_from_csv>
cabine_not = pd.concat([C, C_not] ).sort_index()
test = pd.read_csv(PATH+"test.csv")
 test.head()<load_from_csv>
np.random.seed(0)
 training['sp'] = training.SibSp + training['Parch']
training['cabine_n'] = cabine_not
training.cabine_n = training.cabine_n.astype(int)
 training.drop(["n_new", "Name" ,"Embarked", "PassengerId","Ticket","Cabin"], inplace = True, axis = 1 )
sample = pd.read_csv(PATH+"sample_submission.csv" )<prepare_x_and_y>
x_train = training
repCol9 = {1 : 3 , 2 : 2 , 3 : 1 }
x_train['IsAlone'] = 1
x_train['IsAlone'].loc[x_train['sp'] > 0] = 0
x_train.replace({'Pclass': repCol9} , inplace = True)
 x_train = pd.get_dummies(x_train)
 y_train = x_train["Survived"]
x_train = x_train.drop(['Survived'], axis = 1)
 print(y_train.shape)
 y_train.head(20)
 x_train.head()
image = train.iloc[:,1:]
label = train.iloc[:,0:1]<split>
imp = IterativeImputer(max_iter=10, random_state=0)
 x = imp.fit_transform(x_train)
 x_train = pd.DataFrame(x, columns = x_train.columns)
 x_train[[ 'Pclass', 'SibSp', 'Parch', 'Title', 'sp',
'cabine_n', 'IsAlone', 'Sex_female', 'Sex_male']] = x_train[[ 'Pclass', 'SibSp', 'Parch', 'Title', 'sp',
'cabine_n', 'IsAlone', 'Sex_female', 'Sex_male']].astype(int )
test_image = test.iloc[:,1:]<filter>
z = np.abs(stats.zscore(x_train))
zee =(np.where(z > 3)) [1]
print("number of data examples greater than 3 standard deviations = %i " % len(zee))
test_id = test.iloc[:,0:1] 
<import_modules>
x_test['n_new']= x_test['Name'].apply(lambda x: x.split(',')[1].split('.')[0].strip() )
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
from PIL import Image<feature_engineering>
x_test['Title'] = 0
x_test.loc[x_test["n_new"] == 'Mr', 'Title'] = 1
x_test.loc[x_test["n_new"] == 'Miss', 'Title'] = 4
x_test.loc[x_test["n_new"] == 'Mrs', 'Title'] = 5
x_test.loc[x_test["n_new"] == 'Master', "Title"] = 3
x_test.loc[x_test["n_new"] == 'Dr', 'Title'] = 2
train['file_name'] = np.NaN<train_model>
C = x_test.Cabin[x_test.Cabin.isna() ]
C_not = x_test.Cabin[x_test.Cabin.notna() ]
C.values[:] = 0
C_not.values[:] = 1
x_test['sp'] = x_test.SibSp + x_test['Parch']
x_test['cabine_n'] = cabine_not
x_test.cabine_n = x_test.cabine_n.astype(int)
 x_test['IsAlone'] = 1
x_test['IsAlone'].loc[x_test['sp'] > 0] = 0
x_test.drop(["Name", "Embarked", "Ticket", "n_new", "Cabin"], inplace = True, axis = 1)
 x_test.replace({'Pclass': repCol9} , inplace = True)
 x_test = pd.get_dummies(x_test)
 x_test.head()
for i in tqdm(range(int(len(image)))) :
code = label.iloc[i,:].astype('int')[0]
serial = i
save_path = f'train/{serial}_train_{code}.png'
train.iloc[i,785] = f'{serial}_train_{code}.png'
temp_image = Image.fromarray(image.iloc[i,:].values.astype('uint8' ).reshape(28,28))
temp_image.save(save_path)
<feature_engineering>
fact = y_train[y_train == 0].count() / y_train[y_train == 1].count()
class_weight = {1: fact, 0: 1.}
test['file_name'] = np.NaN<feature_engineering>
InputDimension = 11
print(y_train.shape)
 model = Sequential()
model.add(Dense(25,input_dim=InputDimension, activation='relu'))
model.add(Dense(50, activation='relu'))
model.add(Dense(100, activation='relu'))
model.add(Dense(100, activation='relu'))
model.add(Dense(100, activation='relu'))
model.add(Dense(100, activation='relu'))
model.add(Dense(2, activation='softmax'))
earlystopping = callbacks.EarlyStopping(monitor='val_loss', min_delta=0, patience=30, verbose=0, mode='min')
 optimizer = optimizers.Adam(lr=0.001, decay=0.0001)
 model.compile(loss='categorical_crossentropy', optimizer=optimizer, metrics=['acc'])
 history = model.fit(x_train, pd.get_dummies(y_train), epochs=2000, batch_size=60, validation_split=0.2, verbose=1, callbacks=[earlystopping], class_weight = class_weight )
for i in tqdm(range(int(len(test_image)))) :
serial = i
save_path = f'test/{serial}_test.png'
test.iloc[i,785] = f'{serial}_test.png'
temp_image = Image.fromarray(test_image.iloc[i,:].values.astype('uint8' ).reshape(28,28))
temp_image.save(save_path )<prepare_output>
id = x_test['PassengerId']
x_test.drop(['PassengerId'], inplace = True, axis = 1)
 imp = IterativeImputer(max_iter=10, random_state=0)
 x = imp.fit_transform(x_test)
 x_test = pd.DataFrame(x, columns = x_train.columns)
 x_test[[ 'Pclass', 'SibSp', 'Parch', 'Title', 'sp',
'cabine_n', 'IsAlone', 'Sex_female', 'Sex_male']] = x_test[['Pclass', 'SibSp', 'Parch', 'Title', 'sp',
'cabine_n', 'IsAlone', 'Sex_female', 'Sex_male']].astype(int )
train_dict = {'name':train['file_name'] , 'label': train['label']}
df = pd.DataFrame(train_dict)
 df.head()<prepare_output>
predictions = model.predict(x_test )
test_dict = {'name':test['file_name']}
df_test = pd.DataFrame(test_dict)
 df_test.head()<feature_engineering>
predictions = np.argmax(predictions, axis = 1 )
tfms = get_transforms(do_flip=False )<categorify>
id.reset_index(drop=True, inplace=True)
 out = pd.DataFrame({'PassengerId': id, "Survived": predictions})
 out.to_csv('titanic-predictions.csv', index = False)
 out.head(100 )