Skip to content

Commit

Permalink
Merge pull request #5 from drcrook1/prk
Browse files Browse the repository at this point in the history
inference code update & Readme enhancements
  • Loading branch information
pkarb committed Apr 18, 2019
2 parents ce7d1cb + e8d7866 commit 2d31b38
Show file tree
Hide file tree
Showing 5 changed files with 232 additions and 181 deletions.
21 changes: 17 additions & 4 deletions Project_One-Tests/test_ml_metrics.py
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
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
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

0 comments on commit 2d31b38

Please sign in to comment.