Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<prepare_x_and_y>
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
os.environ["KMP_SETTINGS"] = "false"
X, y = train.drop(columns="Chance of Admit"), train["Chance of Admit"]<import_modules>
train_file_path = ".. /input/titanic/train.csv"
X = pd.read_csv(train_file_path)
 features = ["Pclass", "Sex", "Age", "Survived"]
X = pd.get_dummies(X[features])
 X = pd.concat([X.drop('Pclass', axis=1), pd.get_dummies(X['Pclass'], prefix="Pclass")], axis=1)
 imputer = SimpleImputer()
imputed_X = pd.DataFrame(imputer.fit_transform(X))
imputed_X.columns = X.columns
X = imputed_X
learnoutput = X.Survived
learninput = X.drop("Survived", axis=1)
 test_file_path = ".. /input/titanic/test.csv"
test_data = pd.read_csv(test_file_path)
 features = ["Pclass", "Sex", "Age"]
test = pd.get_dummies(test_data[features])
 testinput = pd.concat([test.drop('Pclass', axis=1), pd.get_dummies(test['Pclass'], prefix="Pclass")], axis=1 )
from sklearn.ensemble import RandomForestRegressor<import_modules>
x_train_split, x_val_split, y_train_split, y_val_split = train_test_split(learninput, learnoutput, random_state=0 )
from sklearn.ensemble import RandomForestRegressor<train_model>
model = keras.Sequential([
layers.Dense(
 units=128,
activation = "tanh",
input_shape = [6] 
),
layers.Dropout(0.2),
layers.Dense(
 units = 256,
activation = "tanh", 
),
layers.Dropout(0.2),
layers.Dense(
 units = 512,
activation = "tanh", 
),
layers.Dropout(0.2),
layers.Dense(
 units = 1024,
activation = "tanh", 
),
layers.Dropout(0.2),
layers.Dense(
 units = 1,
activation = "sigmoid" 
)
 ] )
rf = RandomForestRegressor()
rf.fit(X, y )<predict_on_test>
history = model.fit(x_train_split, y_train_split, epochs=25, verbose=0, validation_data=(x_val_split, y_val_split))
pred = rf.predict(test )<save_to_csv>
model.evaluate(x_val_split, y_val_split )
def submit_predictions(pred):
pred = pd.Series(pred)
 serialNumber = test["Serial No."]
submission = pd.concat([serialNumber, pred], axis=1, ignore_index=True)
 submission.rename(columns={0:"Serial No.", 1:"Chance of Admit"}, inplace=True)
 submission.to_csv("submission.csv", index=False)
<predict_on_test>
predictions = model.predict(testinput)
 predictions = predictions.ravel()
predictions = predictions.round()
predictions = np.nan_to_num(predictions)
 predictions = predictions.astype(int)
 output = pd.DataFrame({'PassengerId': test_data.PassengerId, "Survived": predictions})
 output.to_csv("./file1.csv", index=False )