From 13dedec4a4e5fc863ffcf51173c9b597d3133c74 Mon Sep 17 00:00:00 2001 From: James Gan Date: Mon, 17 Jun 2019 13:38:27 -0700 Subject: [PATCH] Make it in same folder as internal repo --- .../azure-ml-datadrift.ipynb | 0 how-to-use-azureml/data-drift/readme.md | 2 +- how-to-use-azureml/data-drift/score.py | 58 +++++++++++++++++++ 3 files changed, 59 insertions(+), 1 deletion(-) rename how-to-use-azureml/data-drift/{data-drift-model => }/azure-ml-datadrift.ipynb (100%) create mode 100644 how-to-use-azureml/data-drift/score.py diff --git a/how-to-use-azureml/data-drift/data-drift-model/azure-ml-datadrift.ipynb b/how-to-use-azureml/data-drift/azure-ml-datadrift.ipynb similarity index 100% rename from how-to-use-azureml/data-drift/data-drift-model/azure-ml-datadrift.ipynb rename to how-to-use-azureml/data-drift/azure-ml-datadrift.ipynb diff --git a/how-to-use-azureml/data-drift/readme.md b/how-to-use-azureml/data-drift/readme.md index ad6f9d96..9eb416c2 100644 --- a/how-to-use-azureml/data-drift/readme.md +++ b/how-to-use-azureml/data-drift/readme.md @@ -1,3 +1,3 @@ ## Using data drift APIs -1. [Detect data drift for a model](data-drift-model): Detect data drift for a deployed model. \ No newline at end of file +1. [Detect data drift for a model](azure-ml-datadrift.ipynb): Detect data drift for a deployed model. \ No newline at end of file diff --git a/how-to-use-azureml/data-drift/score.py b/how-to-use-azureml/data-drift/score.py new file mode 100644 index 00000000..cda62f8b --- /dev/null +++ b/how-to-use-azureml/data-drift/score.py @@ -0,0 +1,58 @@ +import pickle +import json +import numpy +import azureml.train.automl +from sklearn.externals import joblib +from sklearn.linear_model import Ridge +from azureml.core.model import Model +from azureml.core.run import Run +from azureml.monitoring import ModelDataCollector +import time +import pandas as pd + + +def init(): + global model, inputs_dc, prediction_dc, feature_names, categorical_features + + print("Model is initialized" + time.strftime("%H:%M:%S")) + model_path = Model.get_model_path(model_name="driftmodel") + model = joblib.load(model_path) + + feature_names = ["usaf", "wban", "latitude", "longitude", "station_name", "p_k", + "sine_weekofyear", "cosine_weekofyear", "sine_hourofday", "cosine_hourofday", + "temperature-7"] + + categorical_features = ["usaf", "wban", "p_k", "station_name"] + + inputs_dc = ModelDataCollector(model_name="driftmodel", + identifier="inputs", + feature_names=feature_names) + + prediction_dc = ModelDataCollector("driftmodel", + identifier="predictions", + feature_names=["temperature"]) + + +def run(raw_data): + global inputs_dc, prediction_dc + + try: + data = json.loads(raw_data)["data"] + data = pd.DataFrame(data) + + # Remove the categorical features as the model expects OHE values + input_data = data.drop(categorical_features, axis=1) + + result = model.predict(input_data) + + # Collect the non-OHE dataframe + collected_df = data[feature_names] + + inputs_dc.collect(collected_df.values) + prediction_dc.collect(result) + return result.tolist() + except Exception as e: + error = str(e) + + print(error + time.strftime("%H:%M:%S")) + return error