mirror of
https://github.com/Azure/MachineLearningNotebooks.git
synced 2025-12-20 01:27:06 -05:00
29 lines
917 B
Python
29 lines
917 B
Python
import pickle
|
|
import json
|
|
import numpy
|
|
from sklearn.externals import joblib
|
|
from sklearn.linear_model import Ridge
|
|
from azureml.core.model import Model
|
|
|
|
|
|
def init():
|
|
global model
|
|
# note here "sklearn_regression_model.pkl" is the name of the model registered under
|
|
# 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('sklearn_regression_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(raw_data):
|
|
try:
|
|
data = json.loads(raw_data)['data']
|
|
data = numpy.array(data)
|
|
result = model.predict(data)
|
|
# you can return any data type as long as it is JSON-serializable
|
|
return result.tolist()
|
|
except Exception as e:
|
|
error = str(e)
|
|
return error
|