-
Notifications
You must be signed in to change notification settings - Fork 101
/
iot_score.py
34 lines (29 loc) · 1.17 KB
/
iot_score.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
# This script generates the scoring file
# with the init and run functions needed to
# operationalize the anomaly detection sample
import pickle
import json
import pandas
from sklearn.externals import joblib
from sklearn.linear_model import Ridge
from azureml.core.model import Model
def init():
global model
# this is a different behavior than before when the code is run locally, even though the code is the same.
model_path = Model.get_model_path('model.pkl')
# deserialize the model file back into a sklearn model
model = joblib.load(model_path)
# note you can pass in multiple rows for scoring
def run(input_str):
try:
input_json = json.loads(input_str)
input_df = pandas.DataFrame([[input_json['machine']['temperature'],input_json['machine']['pressure'],input_json['ambient']['temperature'],input_json['ambient']['humidity']]])
pred = model.predict(input_df)
print("Prediction is ", pred[0])
except Exception as e:
result = str(e)
if pred[0] == 1:
input_json['anomaly']=True
else:
input_json['anomaly']=False
return [json.dumps(input_json)]