-
Notifications
You must be signed in to change notification settings - Fork 178
/
titanic.py
63 lines (53 loc) · 2.64 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
# Copyright 2018 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import argparse
import pandas as pd
import tensorflow as tf
from sklearn.model_selection import GridSearchCV
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.preprocessing import Imputer
from sklearn.externals import joblib
def train_model(titanic_data_path, model_output_path):
print('Loading the data...')
try:
with tf.gfile.Open(titanic_data_path, 'r') as data_file:
train_df = pd.read_csv(data_file)
print('Number of samples: {}'.format(train_df.shape[0]))
target_name = 'Survived'
feature_names = ['Pclass', 'Sex', 'Age', 'SibSp', 'Parch', 'Embarked']
print('Preparing the features...')
train_features = train_df[feature_names].copy()
train_features['Age'] = Imputer().fit_transform(train_features['Age'].values.reshape(-1, 1))
embarked = train_features['Embarked']
train_features['Embarked'] = embarked.fillna(embarked.mode()[0])
train_features = pd.get_dummies(train_features)
train_target = train_df[target_name]
print('Training the model...')
parameters = {'max_depth': [2, 3, 4, 5, 6, 7], 'n_estimators': [50, 100, 150, 200]}
gsc = GridSearchCV(GradientBoostingClassifier(), parameters, n_jobs=-1, cv=5)
gsc.fit(train_features, train_target)
print('Best Hyper Parameters: {}'.format(gsc.best_params_))
print('Accuracy: {}'.format(gsc.best_score_))
with tf.gfile.Open(model_output_path, 'wb') as model_file:
joblib.dump(gsc.best_estimator_, model_file, protocol=1)
except Exception as e:
print('Error: {}'.format(e))
if __name__ == '__main__':
parser = argparse.ArgumentParser('Train and save a model based on the Titanic dataset')
parser.add_argument('--titanic-data-path', required=True)
parser.add_argument('--model-output-path', required=True)
args = parser.parse_args()
data_path = args.titanic_data_path
output_path = args.model_output_path
train_model(data_path, output_path)