Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

inference code update & Readme enhancements #5

Merged
merged 1 commit into from
Apr 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions Project_One-Tests/test_ml_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
from inference_code.model_class import MyModel
from sklearn.metrics.regression import r2_score


class Super_secret_data:
[x] = {""}
[y] = {""}

class TestMLMetrics(object):
"""
testing of the model
Expand All @@ -14,16 +19,24 @@ def setUp(self):

def test_r2_within_business_value(self):
m = MyModel()
print("initing Model")
m.init()

# Add code to load your super secret cross validation stuff
# use secrets encoded in a variable group available in the ADO Pipeline
# cuz scientists can't access that and game the system.
super_secret_data = None

y_predicted = m.predict(super_secret_data[x])
x1 = '{"age": 37, "hours-per-week": 40.0, "sex": "Female", "occupation" : "Exec-managerial"}'
y1 = [200000, 190000]
# We will now make the prediction and since we have a single sample, will create 2 points by adding and subtracting 10
# just for creating passable data to r2_score - note that this is a meamingless exercise just to demonsrtate the process
# In real case, you will have more meaningful tests and asserts

y_predicted = m.predict(x1)[0][0]
ypred = [y_predicted+10, y_predicted-10]
print (ypred)
r2score = r2_score(y1, ypred)

r2_score(super_secret_data[y], y_predicted)
assert(-2 < r2score < 2 )

assert(r2_score > 2 < r2_score)

7 changes: 6 additions & 1 deletion Project_One/inference_code/model_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def init(self):
root_path = "./assets/"
with open(root_path + "x_scaler.pkl", "rb") as xfile:
self.x_scaler = pickle.load(xfile)
print (self.x_scaler)
with open(root_path + "y_scaler.pkl", "rb") as yfile:
self.y_scaler = pickle.load(yfile)
with open(root_path + "model.pkl", "rb") as mfile:
Expand All @@ -29,10 +30,14 @@ def predict(self, input_package):
"""
input_package: json formatted string of the form
{"age": integer, "hours-per-week" : double, "sex" : string, "occupation" string}

returns json formatted string of the form: {"estimated_wages" : float}
"""
#input_package = '{"age": 37, "hours-per-week" : 40.0, "sex" : "Female", "occupation" : "Exec-managerial"}'


x = transform_input(input_package)
print (x)
x = self.x_scaler.transform(x)
y = self.model.predict(x)
y = self.y_scaler.inverse_transform(y)
Expand Down
4 changes: 3 additions & 1 deletion Project_One/inference_code/utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ def transform_input(input_package):
input_package: raw json input package as agreed upon
returns: numpy array of correct format without pre-processing
"""
print ("loading json")
d = json.loads(input_package)
print(d)
# Add extra processing for some reason.
x = np.array([d["age"], d["hours-per-week"]]).transpose()
return x
return x.reshape(-1,2)

def transform_output(y):
"""
Expand Down
Loading