-
Notifications
You must be signed in to change notification settings - Fork 0
/
titanic.py
63 lines (47 loc) · 2.14 KB
/
titanic.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import tensorflow
import keras
import matplotlib.pyplot as plt
from keras.layers import Dense
from keras.layers import Dropout
from keras.models import Sequential
from keras.utils import to_categorical
from keras.callbacks import EarlyStopping
import pandas as pd
def show_train_history(training_data, target1, target2):
plt.plot(training_data.history[target1])
plt.plot(training_data.history[target2])
plt.title("Train History")
plt.ylabel("train")
plt.xlabel("Epoch")
plt.legend([target1, target2], loc="upper left")
plt.show()
def main():
train_df = pd.read_csv("train.csv")
# training data pre-process
train_predictors = train_df.drop(["Survived","PassengerId","Name","Ticket"], axis=1)
train_target = to_categorical(train_df[["Survived"]])
train_predictors["Sex"] = train_predictors["Sex"].map({"female":0, "male":1}).astype(int)
train_predictors = pd.get_dummies(data = train_predictors, columns = ["Embarked"])
train_predictors = pd.get_dummies(data = train_predictors, columns = ["Cabin"])
train_predictors["Age"] = train_predictors["Age"].fillna(train_predictors["Age"].mean())
train_predictors["Fare"] = train_predictors["Fare"].fillna(train_predictors["Fare"].mean())
# Set up the model
model = Sequential()
# Add the first layer
model.add(Dense(100, activation="relu", input_shape = (train_predictors.shape[1],)))
model.add(Dense(300, activation="relu"))
model.add(Dense(32, activation="relu"))
model.add(Dropout(0.2))
# output layer
model.add(Dense(2, activation="softmax"))
# Compile the model
model.compile(optimizer="adam", loss="categorical_crossentropy", metrics=["accuracy"])
#model.compile(optimizer="sdg", loss="mean_squared_error")
print("------- fit -------------")
early_stopping_monitor = EarlyStopping(patience = 10)
# Fit the model
training = model.fit(train_predictors, train_target, batch_size=100, epochs = 100, verbose=1, validation_split=0.3, callbacks=[early_stopping_monitor])
show_train_history(training, "acc", "val_acc")
if __name__ == "__main__":
# execute only if run as a script
main()