-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathmodel_engine.py
46 lines (34 loc) · 1.13 KB
/
model_engine.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
# model_engine.py
# Common python package imports.
import pickle
import numpy as np
import pandas as pd
from sklearn import datasets, ensemble
# Import from model_api/app/app.py.
from app.features import FEATURES
def model_pipeline(feature_names):
"""Get the data, train the model, and save it."""
X, y = get_model_data(feature_names)
model = ensemble.RandomForestRegressor(n_estimators=50)
model.fit(X, y)
pickle.dump(model, open('app/model.pkl', 'wb'))
print('Success!')
def get_model_data(feature_names):
"""Load and prepare data for modeling."""
data = datasets.load_boston()
df = build_dataframe(data)
df = clean_data(df)
# Limit the feature set for simplicity.
X, y = df[feature_names].values, df['target'].values
return X, y
def build_dataframe(data):
"""Build dataframe to facilitate cleaning."""
df = pd.DataFrame(data.data, columns=data.feature_names)
df['target'] = data.target
return df
def clean_data(df):
"""Clean data in preparation for modeling."""
df = df.loc[df['target'] != 50].copy()
return df
if __name__ == '__main__':
model_pipeline(FEATURES)