-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrain.py
224 lines (173 loc) · 7.45 KB
/
train.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import re
import keras
import matplotlib.pyplot as plt
import mlflow
import numpy as np
import pandas as pd
from keras import backend as K
from keras.layers import Dense, Dropout
from keras.models import Sequential
from keras.utils import np_utils
from sklearn.feature_selection import SelectKBest, f_classif
from sklearn.model_selection import StratifiedKFold
from sklearn.preprocessing import StandardScaler
from numpy.random import RandomState
# PassengerId -- A numerical id assigned to each passenger.
# Survived -- Whether the passenger survived (1), or didn't (0). We'll be making predictions for this column.
# Pclass -- The class the passenger was in -- first class (1), second class (2), or third class (3).
# Name -- the name of the passenger.
# Sex -- The gender of the passenger -- male or female.
# Age -- The age of the passenger. Fractional.
# SibSp -- The number of siblings and spouses the passenger had on board.
# Parch -- The number of parents and children the passenger had on board.
# Ticket -- The ticket number of the passenger.
# Fare -- How much the passenger paid for the ticker.
# Cabin -- Which cabin the passenger was in.
# Embarked -- Where the passenger boarded the Titanic.
seed = 42
np.random.seed(seed)
train = pd.read_csv("/titanic-train/train.csv")
test = pd.read_csv("/titanic-test/test.csv")
all_data = pd.concat([train, test])
class LogMetric(keras.callbacks.Callback):
def on_epoch_end(self, epoch, logs=None):
mlflow.log_metric("train_loss", logs["loss"])
mlflow.log_metric("train_acc", logs["accuracy"])
class DataDigest:
def __init__(self):
self.ages = None
self.fares = None
self.titles = None
self.cabins = None
self.families = None
self.tickets = None
def get_title(name):
if pd.isnull(name):
return "Null"
title_search = re.search(' ([A-Za-z]+)\.', name)
if title_search:
return title_search.group(1).lower()
else:
return "None"
def get_family(row):
last_name = row["Name"].split(",")[0]
if last_name:
family_size = 1 + row["Parch"] + row["SibSp"]
if family_size > 3:
return "{0}_{1}".format(last_name.lower(), family_size)
else:
return "nofamily"
else:
return "unknown"
def get_index(item, index):
if pd.isnull(item):
return -1
try:
return index.get_loc(item)
except KeyError:
return -1
def data_prepare(data, digest):
genders = {"male": 1, "female": 0}
data['SexF'] = data['Sex'].apply(lambda x: genders.get(x))
gender_dummies = pd.get_dummies(data["Sex"], prefix="SexD", dummy_na=False)
data = pd.concat([data, gender_dummies], axis=1)
data['AgeF'] = data.apply(lambda x: digest.ages[x['Sex']] if pd.isnull(x["Age"]) else x["Age"], axis=1)
data["FareF"] = data.apply(lambda x: digest.fares[x["Pclass"]] if pd.isnull(x["Fare"]) else x["Fare"], axis=1)
embarkments = {"U": 0, "S": 1, "C": 2, "Q": 3}
data["EmbarkedF"] = data["Embarked"].fillna("U").apply(lambda x: embarkments.get(x))
embarkment_dummies = pd.get_dummies(data["Embarked"], prefix="EmbarkedD", dummy_na=False)
data = pd.concat([data, embarkment_dummies], axis=1)
data["RelativesF"] = data["Parch"] + data["SibSp"]
data["SingleF"] = data["RelativesF"].apply(lambda x: 1 if x == 0 else 0)
decks = {"U": 0, "A": 1, "B": 2, "C": 3, "D": 4, "E": 5, "F": 6, "G": 7, "T": 8}
data["DeckF"] = data["Cabin"].fillna("U").apply(lambda x: decks.get(x[0], -1))
deck_dummies = pd.get_dummies(data["Cabin"].fillna("U").apply(lambda x: x[0]), prefix="DeckD", dummy_na=False)
data = pd.concat([data, deck_dummies], axis=1)
title_dummies = pd.get_dummies(data["Name"].apply(lambda x: get_title(x)), prefix="TitleD", dummy_na=False)
data = pd.concat([data, title_dummies], axis=1)
data["CabinF"] = data["Cabin"].fillna("unknown").apply(lambda x: get_index(x, digest.cabins))
data["TitleF"] = data["Name"].apply(lambda x: get_index(get_title(x), digest.titles))
data["TicketF"] = data["Ticket"].apply(lambda x: get_index(x, digest.tickets))
data["FamilyF"] = data.apply(lambda x: get_index(get_family(x), digest.families), axis=1)
return data
data_digest = DataDigest()
data_digest.ages = all_data.groupby("Sex")["Age"].median()
data_digest.fares = all_data.groupby("Pclass")["Fare"].median()
titles_trn = pd.Index(train["Name"].apply(get_title).unique())
titles_tst = pd.Index(test["Name"].apply(get_title).unique())
data_digest.titles = titles_tst
families_trn = pd.Index(train.apply(get_family, axis=1).unique())
families_tst = pd.Index(test.apply(get_family, axis=1).unique())
data_digest.families = families_tst
cabins_trn = pd.Index(train["Cabin"].fillna("unknown").unique())
cabins_tst = pd.Index(test["Cabin"].fillna("unknown").unique())
data_digest.cabins = cabins_tst
tickets_trn = pd.Index(train["Ticket"].fillna("unknown").unique())
tickets_tst = pd.Index(test["Ticket"].fillna("unknown").unique())
data_digest.tickets = tickets_tst
# List of feature for selection
predictors = ["Pclass",
"AgeF",
"TitleF",
"TitleD_mr", "TitleD_mrs", "TitleD_miss", "TitleD_master",
"TitleD_col",
"CabinF",
"DeckF",
"DeckD_U",
"DeckD_A",
"DeckD_B", "DeckD_C", "DeckD_D", "DeckD_E", "DeckD_F",
"FamilyF",
"TicketF",
"SexF",
"SexD_male", "SexD_female",
"EmbarkedF",
"EmbarkedD_S", "EmbarkedD_C",
"EmbarkedD_Q",
"FareF",
"SibSp",
"Parch",
"RelativesF",
'SingleF',
]
pre_train = data_prepare(train, data_digest)
pre_test = data_prepare(test, data_digest)
pre_all = pd.concat([pre_train, pre_test])
scaler = StandardScaler()
scaler.fit(pre_all[predictors])
train_data_scaled = scaler.transform(pre_train[predictors])
test_data_scaled = scaler.transform(pre_test[predictors])
selector = SelectKBest(f_classif, k=5)
selector.fit(pre_train[predictors], pre_train["Survived"])
scores = -np.log10(selector.pvalues_)
# Show predictors
plt.bar(range(len(predictors)), scores)
plt.xticks(range(len(predictors)), predictors, rotation='vertical')
plt.show()
X = pre_train[predictors].to_numpy()
Y = np_utils.to_categorical(pre_train['Survived'])
Y = Y[:, 1]
kfold = StratifiedKFold(n_splits=5, shuffle=True, random_state=seed).split(X, Y)
cv = []
i = len(predictors)
index = test["PassengerId"].to_numpy()
def load_model_and_fit(train_x, train_y):
model = Sequential()
model.add(Dense(32, input_dim=i, activation='sigmoid'))
model.add(Dropout(0.5))
model.add(Dense(16, activation='sigmoid'))
model.add(Dropout(0.5))
model.add(Dense(4, activation='sigmoid'))
model.add(Dropout(0.5))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='rmsprop', metrics=['accuracy'])
model.fit(train_x, train_y, epochs=60, batch_size=32, verbose=0, callbacks=[LogMetric()])
return model
# For testing
for train, test in kfold:
model = load_model_and_fit(X[train], Y[train])
scores = model.evaluate(X[test], Y[test], verbose=0)
print("%s: %.2f%%" % (model.metrics_names[1], scores[1] * 100))
cv.append(scores[1] * 100)
print("%.2f%% (+/- %.2f%%)" % (np.mean(cv), np.std(cv)))
model.save("/model/model.h5")
#source: https://raw.githubusercontent.com/evg-dev/titanic-keras-nn/master/titanic.py