Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<find_best_params>
train_df = pd.read_csv('/kaggle/input/titanic/train.csv')
 test_df = pd.read_csv('/kaggle/input/titanic/test.csv')
 print(train_df.shape)
 print(test_df.shape )
model.eval()
preds = []
tk0 = tqdm(test_loader)
 with torch.no_grad() :
for i, im in enumerate(tk0):
inputs = im["image"].to(device)
 im = inputs.flip(2)
 outputs = model(im)
 im = inputs.flip(3)
 outputs =(0.25*outputs + 0.25*model(im))
outputs =(outputs + 0.5*model(inputs))
preds.extend(F.softmax(outputs, 1 ).cpu().numpy())
 preds = np.array(preds)
 labels = preds.argmax(1)
 new_preds = np.zeros(( len(preds),))
new_preds[labels != 0] = preds[labels != 0, 1:].sum(1)
 new_preds[labels == 0] = 1 - preds[labels == 0, 0]
test_df['Id'] = test_df['ImageFileName'].apply(lambda x: x.split(os.sep)[-1])
 test_df['Label'] = new_preds
test_df = test_df.drop('ImageFileName', axis=1)
 test_df.to_csv('submission_eb0.csv', index=False)
 print(test_df.head() )<load_from_csv>
train_df['Dependent'] = train_df.Parch + train_df.SibSp
test_df['Dependent'] = test_df.Parch + test_df.SibSp
train_df = train_df.replace(['female','male'],[20,10])
 test_df = test_df.replace(['female','male'],[20,10])
 test_PassengerId = test_df.PassengerId
train_df.drop(columns=['Ticket', 'Fare', 'Cabin', 'Embarked','Name','Parch','SibSp','PassengerId'], inplace=True)
 test_df.drop(columns=['Ticket', 'Fare', 'Cabin', 'Embarked','Name','Parch','SibSp','PassengerId'], inplace=True )
train = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')
 test = pd.read_csv('/kaggle/input/digit-recognizer/test.csv' )<import_modules>
train_label = train_df.Survived.to_numpy()
train_label = train_label.reshape(train_label.shape[0],1)
 train_df.drop(columns=['Survived'], inplace=True )
from sklearn.model_selection import train_test_split<import_modules>
train_set = train_df.to_numpy()
test_set = test_df.to_numpy()
print("Training data Size : ", train_set.shape)
 print("Test data Size : ", test_set.shape )
from sklearn.model_selection import train_test_split<prepare_x_and_y>
K.set_image_data_format('channels_last' )
X = train.drop('label',axis=1)
 y = train['label']<feature_engineering>
def FCModel(input_shape):
X_input = Input(input_shape)
 X = X_input
X = Dense(32, activation='relu', kernel_initializer=initializers.glorot_uniform(seed=0), kernel_regularizer=l2(0.001), bias_regularizer=l2(0.001))(X)
 X = Dense(16, activation='relu', kernel_initializer=initializers.glorot_uniform(seed=0), kernel_regularizer=l2(0.001), bias_regularizer=l2(0.001))(X)
 X = Dense(8, activation='relu', kernel_initializer=initializers.glorot_uniform(seed=0))(X)
 X = Dense(4, activation='relu', kernel_initializer=initializers.glorot_uniform(seed=0))(X)
 X = Dense(4, activation='relu', kernel_initializer=initializers.glorot_uniform(seed=0))(X)
 X = Dense(2, activation='softmax', name='fc7' )(X)
 model = Model(inputs = X_input, outputs = X, name='FCModel')
 return model
X = X/255
test = test/255<import_modules>
MyModel = FCModel(train_set.shape[1:])
 MyModel.compile(optimizer = 'adam', loss = "sparse_categorical_crossentropy", metrics = ["accuracy"])
 taining_result = MyModel.fit(x = train_set*0.01, y = train_label, epochs = 150, validation_split= 0.1, batch_size = 20 )
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Dropout, Dense, Flatten
from tensorflow.keras.callbacks import EarlyStopping<choose_model_class>
MyModel.save('TitanicPredict_SimpleNeuralNetwork.h5' )
early_stop = EarlyStopping(monitor='accuracy',mode='max',min_delta=0.005,verbose=7,patience=5 )<choose_model_class>
predictions = np.argmax(MyModel.predict(test_set*0.01), axis = -1 )
model = Sequential()
model.add(Conv2D(64, 3, activation='relu', input_shape=(28, 28, 1)))
 model.add(MaxPooling2D(( 2, 2)))
 model.add(Dropout(0.5))
model.add(Conv2D(32, 3, activation='relu'))
model.add(MaxPooling2D(( 2, 2)))
 model.add(Dropout(0.5))
model.add(Flatten())
 model.add(Dense(128,activation='relu'))
model.add(Dense(10,activation='softmax'))
model.compile(optimizer='adam',loss='sparse_categorical_crossentropy',metrics=['accuracy'] )<train_model>
myTitanicPreiction_df = pd.DataFrame()
myTitanicPreiction_df['PassengerId'] = test_PassengerId
myTitanicPreiction_df['Survived'] = pd.DataFrame(predictions)
 myTitanicPreiction_df.Survived.value_counts()
<predict_on_test><EOS> 
myTitanicPreiction_df.to_csv('MyPrediction_submission.csv', index=False )