mirror of
https://github.com/Azure/MachineLearningNotebooks.git
synced 2025-12-20 09:37:04 -05:00
Compare commits
20 Commits
update-spa
...
azureml-sd
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
aebe34b4e8 | ||
|
|
c7e1241e20 | ||
|
|
6529298c24 | ||
|
|
e2dddfde85 | ||
|
|
36d96f96ec | ||
|
|
7ebcfea5a3 | ||
|
|
b20bfed33a | ||
|
|
a66a92e338 | ||
|
|
c56c2c3525 | ||
|
|
4cac072fa4 | ||
|
|
aeab6b3e28 | ||
|
|
015e261f29 | ||
|
|
d2a423dde9 | ||
|
|
3ecbfd6532 | ||
|
|
02ecb2d755 | ||
|
|
122df6e846 | ||
|
|
7d6a0a2051 | ||
|
|
6cc8af80a2 | ||
|
|
f61898f718 | ||
|
|
5cb465171e |
@@ -103,7 +103,7 @@
|
|||||||
"source": [
|
"source": [
|
||||||
"import azureml.core\n",
|
"import azureml.core\n",
|
||||||
"\n",
|
"\n",
|
||||||
"print(\"This notebook was created using version 1.32.0 of the Azure ML SDK\")\n",
|
"print(\"This notebook was created using version 1.36.0 of the Azure ML SDK\")\n",
|
||||||
"print(\"You are currently using version\", azureml.core.VERSION, \"of the Azure ML SDK\")"
|
"print(\"You are currently using version\", azureml.core.VERSION, \"of the Azure ML SDK\")"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -6,4 +6,4 @@ dependencies:
|
|||||||
- fairlearn>=0.6.2
|
- fairlearn>=0.6.2
|
||||||
- joblib
|
- joblib
|
||||||
- liac-arff
|
- liac-arff
|
||||||
- raiwidgets~=0.7.0
|
- raiwidgets~=0.13.0
|
||||||
|
|||||||
@@ -6,4 +6,4 @@ dependencies:
|
|||||||
- fairlearn>=0.6.2
|
- fairlearn>=0.6.2
|
||||||
- joblib
|
- joblib
|
||||||
- liac-arff
|
- liac-arff
|
||||||
- raiwidgets~=0.7.0
|
- raiwidgets~=0.13.0
|
||||||
|
|||||||
@@ -18,11 +18,13 @@ dependencies:
|
|||||||
- holidays==0.9.11
|
- holidays==0.9.11
|
||||||
- pytorch::pytorch=1.4.0
|
- pytorch::pytorch=1.4.0
|
||||||
- cudatoolkit=10.1.243
|
- cudatoolkit=10.1.243
|
||||||
|
- tornado==6.1.0
|
||||||
|
|
||||||
- pip:
|
- pip:
|
||||||
# Required packages for AzureML execution, history, and data preparation.
|
# Required packages for AzureML execution, history, and data preparation.
|
||||||
- azureml-widgets~=1.32.0
|
- azureml-widgets~=1.36.0
|
||||||
- pytorch-transformers==1.0.0
|
- pytorch-transformers==1.0.0
|
||||||
- spacy==2.1.8
|
- spacy==2.1.8
|
||||||
- https://aka.ms/automl-resources/packages/en_core_web_sm-2.1.0.tar.gz
|
- https://aka.ms/automl-resources/packages/en_core_web_sm-2.1.0.tar.gz
|
||||||
- -r https://automlresources-prod.azureedge.net/validated-requirements/1.32.0/validated_win32_requirements.txt [--no-deps]
|
- -r https://automlsdkdataresources.blob.core.windows.net/validated-requirements/1.36.0/validated_win32_requirements.txt [--no-deps]
|
||||||
|
- arch==4.14
|
||||||
|
|||||||
@@ -18,11 +18,13 @@ dependencies:
|
|||||||
- holidays==0.9.11
|
- holidays==0.9.11
|
||||||
- pytorch::pytorch=1.4.0
|
- pytorch::pytorch=1.4.0
|
||||||
- cudatoolkit=10.1.243
|
- cudatoolkit=10.1.243
|
||||||
|
- tornado==6.1.0
|
||||||
|
|
||||||
- pip:
|
- pip:
|
||||||
# Required packages for AzureML execution, history, and data preparation.
|
# Required packages for AzureML execution, history, and data preparation.
|
||||||
- azureml-widgets~=1.32.0
|
- azureml-widgets~=1.36.0
|
||||||
- pytorch-transformers==1.0.0
|
- pytorch-transformers==1.0.0
|
||||||
- spacy==2.1.8
|
- spacy==2.1.8
|
||||||
- https://aka.ms/automl-resources/packages/en_core_web_sm-2.1.0.tar.gz
|
- https://aka.ms/automl-resources/packages/en_core_web_sm-2.1.0.tar.gz
|
||||||
- -r https://automlresources-prod.azureedge.net/validated-requirements/1.32.0/validated_linux_requirements.txt [--no-deps]
|
- -r https://automlsdkdataresources.blob.core.windows.net/validated-requirements/1.36.0/validated_linux_requirements.txt [--no-deps]
|
||||||
|
- arch==4.14
|
||||||
|
|||||||
@@ -19,11 +19,13 @@ dependencies:
|
|||||||
- holidays==0.9.11
|
- holidays==0.9.11
|
||||||
- pytorch::pytorch=1.4.0
|
- pytorch::pytorch=1.4.0
|
||||||
- cudatoolkit=9.0
|
- cudatoolkit=9.0
|
||||||
|
- tornado==6.1.0
|
||||||
|
|
||||||
- pip:
|
- pip:
|
||||||
# Required packages for AzureML execution, history, and data preparation.
|
# Required packages for AzureML execution, history, and data preparation.
|
||||||
- azureml-widgets~=1.32.0
|
- azureml-widgets~=1.36.0
|
||||||
- pytorch-transformers==1.0.0
|
- pytorch-transformers==1.0.0
|
||||||
- spacy==2.1.8
|
- spacy==2.1.8
|
||||||
- https://aka.ms/automl-resources/packages/en_core_web_sm-2.1.0.tar.gz
|
- https://aka.ms/automl-resources/packages/en_core_web_sm-2.1.0.tar.gz
|
||||||
- -r https://automlresources-prod.azureedge.net/validated-requirements/1.32.0/validated_darwin_requirements.txt [--no-deps]
|
- -r https://automlsdkdataresources.blob.core.windows.net/validated-requirements/1.36.0/validated_darwin_requirements.txt [--no-deps]
|
||||||
|
- arch==4.14
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ import platform
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
import conda
|
import conda
|
||||||
except:
|
except Exception:
|
||||||
print('Failed to import conda.')
|
print('Failed to import conda.')
|
||||||
print('This setup is usually run from the base conda environment.')
|
print('This setup is usually run from the base conda environment.')
|
||||||
print('You can activate the base environment using the command "conda activate base"')
|
print('You can activate the base environment using the command "conda activate base"')
|
||||||
|
|||||||
@@ -86,7 +86,6 @@
|
|||||||
"import azureml.core\n",
|
"import azureml.core\n",
|
||||||
"from azureml.core.experiment import Experiment\n",
|
"from azureml.core.experiment import Experiment\n",
|
||||||
"from azureml.core.workspace import Workspace\n",
|
"from azureml.core.workspace import Workspace\n",
|
||||||
"from azureml.automl.core.featurization import FeaturizationConfig\n",
|
|
||||||
"from azureml.core.dataset import Dataset\n",
|
"from azureml.core.dataset import Dataset\n",
|
||||||
"from azureml.train.automl import AutoMLConfig\n",
|
"from azureml.train.automl import AutoMLConfig\n",
|
||||||
"from azureml.interpret import ExplanationClient"
|
"from azureml.interpret import ExplanationClient"
|
||||||
@@ -105,7 +104,7 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"print(\"This notebook was created using version 1.32.0 of the Azure ML SDK\")\n",
|
"print(\"This notebook was created using version 1.36.0 of the Azure ML SDK\")\n",
|
||||||
"print(\"You are currently using version\", azureml.core.VERSION, \"of the Azure ML SDK\")"
|
"print(\"You are currently using version\", azureml.core.VERSION, \"of the Azure ML SDK\")"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
@@ -599,27 +598,21 @@
|
|||||||
"from azureml.automl.core.onnx_convert import OnnxConvertConstants\n",
|
"from azureml.automl.core.onnx_convert import OnnxConvertConstants\n",
|
||||||
"from azureml.train.automl import constants\n",
|
"from azureml.train.automl import constants\n",
|
||||||
"\n",
|
"\n",
|
||||||
"if sys.version_info < OnnxConvertConstants.OnnxIncompatiblePythonVersion:\n",
|
|
||||||
" python_version_compatible = True\n",
|
|
||||||
"else:\n",
|
|
||||||
" python_version_compatible = False\n",
|
|
||||||
"\n",
|
|
||||||
"import onnxruntime\n",
|
|
||||||
"from azureml.automl.runtime.onnx_convert import OnnxInferenceHelper\n",
|
"from azureml.automl.runtime.onnx_convert import OnnxInferenceHelper\n",
|
||||||
"\n",
|
"\n",
|
||||||
"def get_onnx_res(run):\n",
|
"def get_onnx_res(run):\n",
|
||||||
" res_path = 'onnx_resource.json'\n",
|
" res_path = 'onnx_resource.json'\n",
|
||||||
" run.download_file(name=constants.MODEL_RESOURCE_PATH_ONNX, output_file_path=res_path)\n",
|
" run.download_file(name=constants.MODEL_RESOURCE_PATH_ONNX, output_file_path=res_path)\n",
|
||||||
" with open(res_path) as f:\n",
|
" with open(res_path) as f:\n",
|
||||||
" onnx_res = json.load(f)\n",
|
" result = json.load(f)\n",
|
||||||
" return onnx_res\n",
|
" return result\n",
|
||||||
"\n",
|
"\n",
|
||||||
"if python_version_compatible:\n",
|
"if sys.version_info < OnnxConvertConstants.OnnxIncompatiblePythonVersion:\n",
|
||||||
" test_df = test_dataset.to_pandas_dataframe()\n",
|
" test_df = test_dataset.to_pandas_dataframe()\n",
|
||||||
" mdl_bytes = onnx_mdl.SerializeToString()\n",
|
" mdl_bytes = onnx_mdl.SerializeToString()\n",
|
||||||
" onnx_res = get_onnx_res(best_run)\n",
|
" onnx_result = get_onnx_res(best_run)\n",
|
||||||
"\n",
|
"\n",
|
||||||
" onnxrt_helper = OnnxInferenceHelper(mdl_bytes, onnx_res)\n",
|
" onnxrt_helper = OnnxInferenceHelper(mdl_bytes, onnx_result)\n",
|
||||||
" pred_onnx, pred_prob_onnx = onnxrt_helper.predict(test_df)\n",
|
" pred_onnx, pred_prob_onnx = onnxrt_helper.predict(test_df)\n",
|
||||||
"\n",
|
"\n",
|
||||||
" print(pred_onnx)\n",
|
" print(pred_onnx)\n",
|
||||||
@@ -708,14 +701,12 @@
|
|||||||
"source": [
|
"source": [
|
||||||
"from azureml.core.model import InferenceConfig\n",
|
"from azureml.core.model import InferenceConfig\n",
|
||||||
"from azureml.core.webservice import AciWebservice\n",
|
"from azureml.core.webservice import AciWebservice\n",
|
||||||
"from azureml.core.webservice import Webservice\n",
|
|
||||||
"from azureml.core.model import Model\n",
|
"from azureml.core.model import Model\n",
|
||||||
"from azureml.core.environment import Environment\n",
|
|
||||||
"\n",
|
"\n",
|
||||||
"inference_config = InferenceConfig(entry_script=script_file_name)\n",
|
"inference_config = InferenceConfig(environment = best_run.get_environment(), entry_script=script_file_name)\n",
|
||||||
"\n",
|
"\n",
|
||||||
"aciconfig = AciWebservice.deploy_configuration(cpu_cores = 1, \n",
|
"aciconfig = AciWebservice.deploy_configuration(cpu_cores = 2, \n",
|
||||||
" memory_gb = 1, \n",
|
" memory_gb = 2, \n",
|
||||||
" tags = {'area': \"bmData\", 'type': \"automl_classification\"}, \n",
|
" tags = {'area': \"bmData\", 'type': \"automl_classification\"}, \n",
|
||||||
" description = 'sample service for Automl Classification')\n",
|
" description = 'sample service for Automl Classification')\n",
|
||||||
"\n",
|
"\n",
|
||||||
@@ -792,7 +783,6 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"import json\n",
|
|
||||||
"import requests\n",
|
"import requests\n",
|
||||||
"\n",
|
"\n",
|
||||||
"X_test_json = X_test.to_json(orient='records')\n",
|
"X_test_json = X_test.to_json(orient='records')\n",
|
||||||
@@ -832,7 +822,6 @@
|
|||||||
"source": [
|
"source": [
|
||||||
"%matplotlib notebook\n",
|
"%matplotlib notebook\n",
|
||||||
"from sklearn.metrics import confusion_matrix\n",
|
"from sklearn.metrics import confusion_matrix\n",
|
||||||
"import numpy as np\n",
|
|
||||||
"import itertools\n",
|
"import itertools\n",
|
||||||
"\n",
|
"\n",
|
||||||
"cf =confusion_matrix(actual,y_pred)\n",
|
"cf =confusion_matrix(actual,y_pred)\n",
|
||||||
|
|||||||
@@ -93,7 +93,7 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"print(\"This notebook was created using version 1.32.0 of the Azure ML SDK\")\n",
|
"print(\"This notebook was created using version 1.36.0 of the Azure ML SDK\")\n",
|
||||||
"print(\"You are currently using version\", azureml.core.VERSION, \"of the Azure ML SDK\")"
|
"print(\"You are currently using version\", azureml.core.VERSION, \"of the Azure ML SDK\")"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
@@ -215,7 +215,7 @@
|
|||||||
"source": [
|
"source": [
|
||||||
"automl_settings = {\n",
|
"automl_settings = {\n",
|
||||||
" \"n_cross_validations\": 3,\n",
|
" \"n_cross_validations\": 3,\n",
|
||||||
" \"primary_metric\": 'average_precision_score_weighted',\n",
|
" \"primary_metric\": 'AUC_weighted',\n",
|
||||||
" \"enable_early_stopping\": True,\n",
|
" \"enable_early_stopping\": True,\n",
|
||||||
" \"max_concurrent_iterations\": 2, # This is a limit for testing purpose, please increase it as per cluster size\n",
|
" \"max_concurrent_iterations\": 2, # This is a limit for testing purpose, please increase it as per cluster size\n",
|
||||||
" \"experiment_timeout_hours\": 0.25, # This is a time limit for testing purposes, remove it for real use cases, this will drastically limit ablity to find the best model possible\n",
|
" \"experiment_timeout_hours\": 0.25, # This is a time limit for testing purposes, remove it for real use cases, this will drastically limit ablity to find the best model possible\n",
|
||||||
|
|||||||
@@ -96,7 +96,7 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"print(\"This notebook was created using version 1.32.0 of the Azure ML SDK\")\n",
|
"print(\"This notebook was created using version 1.36.0 of the Azure ML SDK\")\n",
|
||||||
"print(\"You are currently using version\", azureml.core.VERSION, \"of the Azure ML SDK\")"
|
"print(\"You are currently using version\", azureml.core.VERSION, \"of the Azure ML SDK\")"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
@@ -284,7 +284,7 @@
|
|||||||
"source": [
|
"source": [
|
||||||
"automl_settings = {\n",
|
"automl_settings = {\n",
|
||||||
" \"experiment_timeout_minutes\": 30,\n",
|
" \"experiment_timeout_minutes\": 30,\n",
|
||||||
" \"primary_metric\": 'accuracy',\n",
|
" \"primary_metric\": 'AUC_weighted',\n",
|
||||||
" \"max_concurrent_iterations\": num_nodes, \n",
|
" \"max_concurrent_iterations\": num_nodes, \n",
|
||||||
" \"max_cores_per_iteration\": -1,\n",
|
" \"max_cores_per_iteration\": -1,\n",
|
||||||
" \"enable_dnn\": True,\n",
|
" \"enable_dnn\": True,\n",
|
||||||
|
|||||||
@@ -81,7 +81,7 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"print(\"This notebook was created using version 1.32.0 of the Azure ML SDK\")\n",
|
"print(\"This notebook was created using version 1.36.0 of the Azure ML SDK\")\n",
|
||||||
"print(\"You are currently using version\", azureml.core.VERSION, \"of the Azure ML SDK\")"
|
"print(\"You are currently using version\", azureml.core.VERSION, \"of the Azure ML SDK\")"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
@@ -348,7 +348,7 @@
|
|||||||
" \"iteration_timeout_minutes\": 10,\n",
|
" \"iteration_timeout_minutes\": 10,\n",
|
||||||
" \"experiment_timeout_hours\": 0.25,\n",
|
" \"experiment_timeout_hours\": 0.25,\n",
|
||||||
" \"n_cross_validations\": 3,\n",
|
" \"n_cross_validations\": 3,\n",
|
||||||
" \"primary_metric\": 'r2_score',\n",
|
" \"primary_metric\": 'normalized_root_mean_squared_error',\n",
|
||||||
" \"max_concurrent_iterations\": 3,\n",
|
" \"max_concurrent_iterations\": 3,\n",
|
||||||
" \"max_cores_per_iteration\": -1,\n",
|
" \"max_cores_per_iteration\": -1,\n",
|
||||||
" \"verbosity\": logging.INFO,\n",
|
" \"verbosity\": logging.INFO,\n",
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ try:
|
|||||||
model = Model(ws, args.model_name)
|
model = Model(ws, args.model_name)
|
||||||
last_train_time = model.created_time
|
last_train_time = model.created_time
|
||||||
print("Model was last trained on {0}.".format(last_train_time))
|
print("Model was last trained on {0}.".format(last_train_time))
|
||||||
except Exception as e:
|
except Exception:
|
||||||
print("Could not get last model train time.")
|
print("Could not get last model train time.")
|
||||||
last_train_time = datetime.min.replace(tzinfo=pytz.UTC)
|
last_train_time = datetime.min.replace(tzinfo=pytz.UTC)
|
||||||
|
|
||||||
|
|||||||
@@ -92,7 +92,7 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"print(\"This notebook was created using version 1.32.0 of the Azure ML SDK\")\n",
|
"print(\"This notebook was created using version 1.36.0 of the Azure ML SDK\")\n",
|
||||||
"print(\"You are currently using version\", azureml.core.VERSION, \"of the Azure ML SDK\")"
|
"print(\"You are currently using version\", azureml.core.VERSION, \"of the Azure ML SDK\")"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -91,7 +91,7 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"print(\"This notebook was created using version 1.32.0 of the Azure ML SDK\")\n",
|
"print(\"This notebook was created using version 1.36.0 of the Azure ML SDK\")\n",
|
||||||
"print(\"You are currently using version\", azureml.core.VERSION, \"of the Azure ML SDK\")"
|
"print(\"You are currently using version\", azureml.core.VERSION, \"of the Azure ML SDK\")"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -113,7 +113,7 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"print(\"This notebook was created using version 1.32.0 of the Azure ML SDK\")\n",
|
"print(\"This notebook was created using version 1.36.0 of the Azure ML SDK\")\n",
|
||||||
"print(\"You are currently using version\", azureml.core.VERSION, \"of the Azure ML SDK\")"
|
"print(\"You are currently using version\", azureml.core.VERSION, \"of the Azure ML SDK\")"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
@@ -139,18 +139,18 @@
|
|||||||
"ws = Workspace.from_config()\n",
|
"ws = Workspace.from_config()\n",
|
||||||
"\n",
|
"\n",
|
||||||
"# choose a name for the run history container in the workspace\n",
|
"# choose a name for the run history container in the workspace\n",
|
||||||
"experiment_name = 'beer-remote-cpu'\n",
|
"experiment_name = \"beer-remote-cpu\"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"experiment = Experiment(ws, experiment_name)\n",
|
"experiment = Experiment(ws, experiment_name)\n",
|
||||||
"\n",
|
"\n",
|
||||||
"output = {}\n",
|
"output = {}\n",
|
||||||
"output['Subscription ID'] = ws.subscription_id\n",
|
"output[\"Subscription ID\"] = ws.subscription_id\n",
|
||||||
"output['Workspace'] = ws.name\n",
|
"output[\"Workspace\"] = ws.name\n",
|
||||||
"output['Resource Group'] = ws.resource_group\n",
|
"output[\"Resource Group\"] = ws.resource_group\n",
|
||||||
"output['Location'] = ws.location\n",
|
"output[\"Location\"] = ws.location\n",
|
||||||
"output['Run History Name'] = experiment_name\n",
|
"output[\"Run History Name\"] = experiment_name\n",
|
||||||
"pd.set_option('display.max_colwidth', -1)\n",
|
"pd.set_option(\"display.max_colwidth\", -1)\n",
|
||||||
"outputDf = pd.DataFrame(data = output, index = [''])\n",
|
"outputDf = pd.DataFrame(data=output, index=[\"\"])\n",
|
||||||
"outputDf.T"
|
"outputDf.T"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
@@ -185,10 +185,11 @@
|
|||||||
"# Verify that cluster does not exist already\n",
|
"# Verify that cluster does not exist already\n",
|
||||||
"try:\n",
|
"try:\n",
|
||||||
" compute_target = ComputeTarget(workspace=ws, name=cpu_cluster_name)\n",
|
" compute_target = ComputeTarget(workspace=ws, name=cpu_cluster_name)\n",
|
||||||
" print('Found existing cluster, use it.')\n",
|
" print(\"Found existing cluster, use it.\")\n",
|
||||||
"except ComputeTargetException:\n",
|
"except ComputeTargetException:\n",
|
||||||
" compute_config = AmlCompute.provisioning_configuration(vm_size='STANDARD_DS12_V2',\n",
|
" compute_config = AmlCompute.provisioning_configuration(\n",
|
||||||
" max_nodes=4)\n",
|
" vm_size=\"STANDARD_DS12_V2\", max_nodes=4\n",
|
||||||
|
" )\n",
|
||||||
" compute_target = ComputeTarget.create(ws, cpu_cluster_name, compute_config)\n",
|
" compute_target = ComputeTarget.create(ws, cpu_cluster_name, compute_config)\n",
|
||||||
"\n",
|
"\n",
|
||||||
"compute_target.wait_for_completion(show_output=True)"
|
"compute_target.wait_for_completion(show_output=True)"
|
||||||
@@ -245,17 +246,21 @@
|
|||||||
"plt.tight_layout()\n",
|
"plt.tight_layout()\n",
|
||||||
"\n",
|
"\n",
|
||||||
"plt.subplot(2, 1, 1)\n",
|
"plt.subplot(2, 1, 1)\n",
|
||||||
"plt.title('Beer Production By Year')\n",
|
"plt.title(\"Beer Production By Year\")\n",
|
||||||
"df = pd.read_csv(\"Beer_no_valid_split_train.csv\", parse_dates=True, index_col= 'DATE').drop(columns='grain')\n",
|
"df = pd.read_csv(\n",
|
||||||
"test_df = pd.read_csv(\"Beer_no_valid_split_test.csv\", parse_dates=True, index_col= 'DATE').drop(columns='grain')\n",
|
" \"Beer_no_valid_split_train.csv\", parse_dates=True, index_col=\"DATE\"\n",
|
||||||
|
").drop(columns=\"grain\")\n",
|
||||||
|
"test_df = pd.read_csv(\n",
|
||||||
|
" \"Beer_no_valid_split_test.csv\", parse_dates=True, index_col=\"DATE\"\n",
|
||||||
|
").drop(columns=\"grain\")\n",
|
||||||
"plt.plot(df)\n",
|
"plt.plot(df)\n",
|
||||||
"\n",
|
"\n",
|
||||||
"plt.subplot(2, 1, 2)\n",
|
"plt.subplot(2, 1, 2)\n",
|
||||||
"plt.title('Beer Production By Month')\n",
|
"plt.title(\"Beer Production By Month\")\n",
|
||||||
"groups = df.groupby(df.index.month)\n",
|
"groups = df.groupby(df.index.month)\n",
|
||||||
"months = concat([DataFrame(x[1].values) for x in groups], axis=1)\n",
|
"months = concat([DataFrame(x[1].values) for x in groups], axis=1)\n",
|
||||||
"months = DataFrame(months)\n",
|
"months = DataFrame(months)\n",
|
||||||
"months.columns = range(1,13)\n",
|
"months.columns = range(1, 13)\n",
|
||||||
"months.boxplot()\n",
|
"months.boxplot()\n",
|
||||||
"\n",
|
"\n",
|
||||||
"plt.show()"
|
"plt.show()"
|
||||||
@@ -270,10 +275,10 @@
|
|||||||
},
|
},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"target_column_name = 'BeerProduction'\n",
|
"target_column_name = \"BeerProduction\"\n",
|
||||||
"time_column_name = 'DATE'\n",
|
"time_column_name = \"DATE\"\n",
|
||||||
"time_series_id_column_names = []\n",
|
"time_series_id_column_names = []\n",
|
||||||
"freq = 'M' #Monthly data"
|
"freq = \"M\" # Monthly data"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -301,14 +306,36 @@
|
|||||||
"test_df.to_csv(\"test.csv\")\n",
|
"test_df.to_csv(\"test.csv\")\n",
|
||||||
"\n",
|
"\n",
|
||||||
"datastore = ws.get_default_datastore()\n",
|
"datastore = ws.get_default_datastore()\n",
|
||||||
"datastore.upload_files(files = ['./train.csv'], target_path = 'beer-dataset/tabular/', overwrite = True,show_progress = True)\n",
|
"datastore.upload_files(\n",
|
||||||
"datastore.upload_files(files = ['./valid.csv'], target_path = 'beer-dataset/tabular/', overwrite = True,show_progress = True)\n",
|
" files=[\"./train.csv\"],\n",
|
||||||
"datastore.upload_files(files = ['./test.csv'], target_path = 'beer-dataset/tabular/', overwrite = True,show_progress = True)\n",
|
" target_path=\"beer-dataset/tabular/\",\n",
|
||||||
|
" overwrite=True,\n",
|
||||||
|
" show_progress=True,\n",
|
||||||
|
")\n",
|
||||||
|
"datastore.upload_files(\n",
|
||||||
|
" files=[\"./valid.csv\"],\n",
|
||||||
|
" target_path=\"beer-dataset/tabular/\",\n",
|
||||||
|
" overwrite=True,\n",
|
||||||
|
" show_progress=True,\n",
|
||||||
|
")\n",
|
||||||
|
"datastore.upload_files(\n",
|
||||||
|
" files=[\"./test.csv\"],\n",
|
||||||
|
" target_path=\"beer-dataset/tabular/\",\n",
|
||||||
|
" overwrite=True,\n",
|
||||||
|
" show_progress=True,\n",
|
||||||
|
")\n",
|
||||||
"\n",
|
"\n",
|
||||||
"from azureml.core import Dataset\n",
|
"from azureml.core import Dataset\n",
|
||||||
"train_dataset = Dataset.Tabular.from_delimited_files(path = [(datastore, 'beer-dataset/tabular/train.csv')])\n",
|
"\n",
|
||||||
"valid_dataset = Dataset.Tabular.from_delimited_files(path = [(datastore, 'beer-dataset/tabular/valid.csv')])\n",
|
"train_dataset = Dataset.Tabular.from_delimited_files(\n",
|
||||||
"test_dataset = Dataset.Tabular.from_delimited_files(path = [(datastore, 'beer-dataset/tabular/test.csv')])"
|
" path=[(datastore, \"beer-dataset/tabular/train.csv\")]\n",
|
||||||
|
")\n",
|
||||||
|
"valid_dataset = Dataset.Tabular.from_delimited_files(\n",
|
||||||
|
" path=[(datastore, \"beer-dataset/tabular/valid.csv\")]\n",
|
||||||
|
")\n",
|
||||||
|
"test_dataset = Dataset.Tabular.from_delimited_files(\n",
|
||||||
|
" path=[(datastore, \"beer-dataset/tabular/test.csv\")]\n",
|
||||||
|
")"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -366,24 +393,29 @@
|
|||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"from azureml.automl.core.forecasting_parameters import ForecastingParameters\n",
|
"from azureml.automl.core.forecasting_parameters import ForecastingParameters\n",
|
||||||
|
"\n",
|
||||||
"forecasting_parameters = ForecastingParameters(\n",
|
"forecasting_parameters = ForecastingParameters(\n",
|
||||||
" time_column_name=time_column_name,\n",
|
" time_column_name=time_column_name,\n",
|
||||||
" forecast_horizon=forecast_horizon,\n",
|
" forecast_horizon=forecast_horizon,\n",
|
||||||
" freq='MS' # Set the forecast frequency to be monthly (start of the month)\n",
|
" freq=\"MS\", # Set the forecast frequency to be monthly (start of the month)\n",
|
||||||
")\n",
|
")\n",
|
||||||
"\n",
|
"\n",
|
||||||
"automl_config = AutoMLConfig(task='forecasting', \n",
|
"# We will disable the enable_early_stopping flag to ensure the DNN model is recommended for demonstration purpose.\n",
|
||||||
" primary_metric='normalized_root_mean_squared_error',\n",
|
"automl_config = AutoMLConfig(\n",
|
||||||
" experiment_timeout_hours = 1,\n",
|
" task=\"forecasting\",\n",
|
||||||
|
" primary_metric=\"normalized_root_mean_squared_error\",\n",
|
||||||
|
" experiment_timeout_hours=1,\n",
|
||||||
" training_data=train_dataset,\n",
|
" training_data=train_dataset,\n",
|
||||||
" label_column_name=target_column_name,\n",
|
" label_column_name=target_column_name,\n",
|
||||||
" validation_data=valid_dataset, \n",
|
" validation_data=valid_dataset,\n",
|
||||||
" verbosity=logging.INFO,\n",
|
" verbosity=logging.INFO,\n",
|
||||||
" compute_target=compute_target,\n",
|
" compute_target=compute_target,\n",
|
||||||
" max_concurrent_iterations=4,\n",
|
" max_concurrent_iterations=4,\n",
|
||||||
" max_cores_per_iteration=-1,\n",
|
" max_cores_per_iteration=-1,\n",
|
||||||
" enable_dnn=True,\n",
|
" enable_dnn=True,\n",
|
||||||
" forecasting_parameters=forecasting_parameters)"
|
" enable_early_stopping=False,\n",
|
||||||
|
" forecasting_parameters=forecasting_parameters,\n",
|
||||||
|
")"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -405,7 +437,7 @@
|
|||||||
},
|
},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"remote_run = experiment.submit(automl_config, show_output= True)"
|
"remote_run = experiment.submit(automl_config, show_output=True)"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -453,6 +485,7 @@
|
|||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"from helper import get_result_df\n",
|
"from helper import get_result_df\n",
|
||||||
|
"\n",
|
||||||
"summary_df = get_result_df(remote_run)\n",
|
"summary_df = get_result_df(remote_run)\n",
|
||||||
"summary_df"
|
"summary_df"
|
||||||
]
|
]
|
||||||
@@ -468,11 +501,12 @@
|
|||||||
"source": [
|
"source": [
|
||||||
"from azureml.core.run import Run\n",
|
"from azureml.core.run import Run\n",
|
||||||
"from azureml.widgets import RunDetails\n",
|
"from azureml.widgets import RunDetails\n",
|
||||||
"forecast_model = 'TCNForecaster'\n",
|
"\n",
|
||||||
"if not forecast_model in summary_df['run_id']:\n",
|
"forecast_model = \"TCNForecaster\"\n",
|
||||||
" forecast_model = 'ForecastTCN'\n",
|
"if not forecast_model in summary_df[\"run_id\"]:\n",
|
||||||
" \n",
|
" forecast_model = \"ForecastTCN\"\n",
|
||||||
"best_dnn_run_id = summary_df['run_id'][forecast_model]\n",
|
"\n",
|
||||||
|
"best_dnn_run_id = summary_df[\"run_id\"][forecast_model]\n",
|
||||||
"best_dnn_run = Run(experiment, best_dnn_run_id)"
|
"best_dnn_run = Run(experiment, best_dnn_run_id)"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
@@ -486,7 +520,7 @@
|
|||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"best_dnn_run.parent\n",
|
"best_dnn_run.parent\n",
|
||||||
"RunDetails(best_dnn_run.parent).show() "
|
"RunDetails(best_dnn_run.parent).show()"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -499,7 +533,7 @@
|
|||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"best_dnn_run\n",
|
"best_dnn_run\n",
|
||||||
"RunDetails(best_dnn_run).show() "
|
"RunDetails(best_dnn_run).show()"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -534,7 +568,10 @@
|
|||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"from azureml.core import Dataset\n",
|
"from azureml.core import Dataset\n",
|
||||||
"test_dataset = Dataset.Tabular.from_delimited_files(path = [(datastore, 'beer-dataset/tabular/test.csv')])\n",
|
"\n",
|
||||||
|
"test_dataset = Dataset.Tabular.from_delimited_files(\n",
|
||||||
|
" path=[(datastore, \"beer-dataset/tabular/test.csv\")]\n",
|
||||||
|
")\n",
|
||||||
"# preview the first 3 rows of the dataset\n",
|
"# preview the first 3 rows of the dataset\n",
|
||||||
"test_dataset.take(5).to_pandas_dataframe()"
|
"test_dataset.take(5).to_pandas_dataframe()"
|
||||||
]
|
]
|
||||||
@@ -545,7 +582,7 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"compute_target = ws.compute_targets['beer-cluster']\n",
|
"compute_target = ws.compute_targets[\"beer-cluster\"]\n",
|
||||||
"test_experiment = Experiment(ws, experiment_name + \"_test\")"
|
"test_experiment = Experiment(ws, experiment_name + \"_test\")"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
@@ -561,9 +598,9 @@
|
|||||||
"import os\n",
|
"import os\n",
|
||||||
"import shutil\n",
|
"import shutil\n",
|
||||||
"\n",
|
"\n",
|
||||||
"script_folder = os.path.join(os.getcwd(), 'inference')\n",
|
"script_folder = os.path.join(os.getcwd(), \"inference\")\n",
|
||||||
"os.makedirs(script_folder, exist_ok=True)\n",
|
"os.makedirs(script_folder, exist_ok=True)\n",
|
||||||
"shutil.copy('infer.py', script_folder)"
|
"shutil.copy(\"infer.py\", script_folder)"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -574,8 +611,18 @@
|
|||||||
"source": [
|
"source": [
|
||||||
"from helper import run_inference\n",
|
"from helper import run_inference\n",
|
||||||
"\n",
|
"\n",
|
||||||
"test_run = run_inference(test_experiment, compute_target, script_folder, best_dnn_run, test_dataset, valid_dataset, forecast_horizon,\n",
|
"test_run = run_inference(\n",
|
||||||
" target_column_name, time_column_name, freq)"
|
" test_experiment,\n",
|
||||||
|
" compute_target,\n",
|
||||||
|
" script_folder,\n",
|
||||||
|
" best_dnn_run,\n",
|
||||||
|
" test_dataset,\n",
|
||||||
|
" valid_dataset,\n",
|
||||||
|
" forecast_horizon,\n",
|
||||||
|
" target_column_name,\n",
|
||||||
|
" time_column_name,\n",
|
||||||
|
" freq,\n",
|
||||||
|
")"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -595,8 +642,19 @@
|
|||||||
"source": [
|
"source": [
|
||||||
"from helper import run_multiple_inferences\n",
|
"from helper import run_multiple_inferences\n",
|
||||||
"\n",
|
"\n",
|
||||||
"summary_df = run_multiple_inferences(summary_df, experiment, test_experiment, compute_target, script_folder, test_dataset, \n",
|
"summary_df = run_multiple_inferences(\n",
|
||||||
" valid_dataset, forecast_horizon, target_column_name, time_column_name, freq)"
|
" summary_df,\n",
|
||||||
|
" experiment,\n",
|
||||||
|
" test_experiment,\n",
|
||||||
|
" compute_target,\n",
|
||||||
|
" script_folder,\n",
|
||||||
|
" test_dataset,\n",
|
||||||
|
" valid_dataset,\n",
|
||||||
|
" forecast_horizon,\n",
|
||||||
|
" target_column_name,\n",
|
||||||
|
" time_column_name,\n",
|
||||||
|
" freq,\n",
|
||||||
|
")"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -616,7 +674,7 @@
|
|||||||
" test_run = Run(test_experiment, test_run_id)\n",
|
" test_run = Run(test_experiment, test_run_id)\n",
|
||||||
" test_run.wait_for_completion()\n",
|
" test_run.wait_for_completion()\n",
|
||||||
" test_score = test_run.get_metrics()[run_summary.primary_metric]\n",
|
" test_score = test_run.get_metrics()[run_summary.primary_metric]\n",
|
||||||
" summary_df.loc[summary_df.run_id == run_id, 'Test Score'] = test_score\n",
|
" summary_df.loc[summary_df.run_id == run_id, \"Test Score\"] = test_score\n",
|
||||||
" print(\"Test Score: \", test_score)"
|
" print(\"Test Score: \", test_score)"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -6,120 +6,158 @@ from azureml.core.run import Run
|
|||||||
from azureml.automl.core.shared import constants
|
from azureml.automl.core.shared import constants
|
||||||
|
|
||||||
|
|
||||||
def split_fraction_by_grain(df, fraction, time_column_name,
|
def split_fraction_by_grain(df, fraction, time_column_name, grain_column_names=None):
|
||||||
grain_column_names=None):
|
|
||||||
if not grain_column_names:
|
if not grain_column_names:
|
||||||
df['tmp_grain_column'] = 'grain'
|
df["tmp_grain_column"] = "grain"
|
||||||
grain_column_names = ['tmp_grain_column']
|
grain_column_names = ["tmp_grain_column"]
|
||||||
|
|
||||||
"""Group df by grain and split on last n rows for each group."""
|
"""Group df by grain and split on last n rows for each group."""
|
||||||
df_grouped = (df.sort_values(time_column_name)
|
df_grouped = df.sort_values(time_column_name).groupby(
|
||||||
.groupby(grain_column_names, group_keys=False))
|
grain_column_names, group_keys=False
|
||||||
|
)
|
||||||
|
|
||||||
df_head = df_grouped.apply(lambda dfg: dfg.iloc[:-int(len(dfg) *
|
df_head = df_grouped.apply(
|
||||||
fraction)] if fraction > 0 else dfg)
|
lambda dfg: dfg.iloc[: -int(len(dfg) * fraction)] if fraction > 0 else dfg
|
||||||
|
)
|
||||||
|
|
||||||
df_tail = df_grouped.apply(lambda dfg: dfg.iloc[-int(len(dfg) *
|
df_tail = df_grouped.apply(
|
||||||
fraction):] if fraction > 0 else dfg[:0])
|
lambda dfg: dfg.iloc[-int(len(dfg) * fraction) :] if fraction > 0 else dfg[:0]
|
||||||
|
)
|
||||||
|
|
||||||
if 'tmp_grain_column' in grain_column_names:
|
if "tmp_grain_column" in grain_column_names:
|
||||||
for df2 in (df, df_head, df_tail):
|
for df2 in (df, df_head, df_tail):
|
||||||
df2.drop('tmp_grain_column', axis=1, inplace=True)
|
df2.drop("tmp_grain_column", axis=1, inplace=True)
|
||||||
|
|
||||||
grain_column_names.remove('tmp_grain_column')
|
grain_column_names.remove("tmp_grain_column")
|
||||||
|
|
||||||
return df_head, df_tail
|
return df_head, df_tail
|
||||||
|
|
||||||
|
|
||||||
def split_full_for_forecasting(df, time_column_name,
|
def split_full_for_forecasting(
|
||||||
grain_column_names=None, test_split=0.2):
|
df, time_column_name, grain_column_names=None, test_split=0.2
|
||||||
|
):
|
||||||
index_name = df.index.name
|
index_name = df.index.name
|
||||||
|
|
||||||
# Assumes that there isn't already a column called tmpindex
|
# Assumes that there isn't already a column called tmpindex
|
||||||
|
|
||||||
df['tmpindex'] = df.index
|
df["tmpindex"] = df.index
|
||||||
|
|
||||||
train_df, test_df = split_fraction_by_grain(
|
train_df, test_df = split_fraction_by_grain(
|
||||||
df, test_split, time_column_name, grain_column_names)
|
df, test_split, time_column_name, grain_column_names
|
||||||
|
)
|
||||||
|
|
||||||
train_df = train_df.set_index('tmpindex')
|
train_df = train_df.set_index("tmpindex")
|
||||||
train_df.index.name = index_name
|
train_df.index.name = index_name
|
||||||
|
|
||||||
test_df = test_df.set_index('tmpindex')
|
test_df = test_df.set_index("tmpindex")
|
||||||
test_df.index.name = index_name
|
test_df.index.name = index_name
|
||||||
|
|
||||||
df.drop('tmpindex', axis=1, inplace=True)
|
df.drop("tmpindex", axis=1, inplace=True)
|
||||||
|
|
||||||
return train_df, test_df
|
return train_df, test_df
|
||||||
|
|
||||||
|
|
||||||
def get_result_df(remote_run):
|
def get_result_df(remote_run):
|
||||||
children = list(remote_run.get_children(recursive=True))
|
children = list(remote_run.get_children(recursive=True))
|
||||||
summary_df = pd.DataFrame(index=['run_id', 'run_algorithm',
|
summary_df = pd.DataFrame(
|
||||||
'primary_metric', 'Score'])
|
index=["run_id", "run_algorithm", "primary_metric", "Score"]
|
||||||
|
)
|
||||||
goal_minimize = False
|
goal_minimize = False
|
||||||
for run in children:
|
for run in children:
|
||||||
if run.get_status().lower() == constants.RunState.COMPLETE_RUN \
|
if (
|
||||||
and 'run_algorithm' in run.properties and 'score' in run.properties:
|
run.get_status().lower() == constants.RunState.COMPLETE_RUN
|
||||||
|
and "run_algorithm" in run.properties
|
||||||
|
and "score" in run.properties
|
||||||
|
):
|
||||||
# We only count in the completed child runs.
|
# We only count in the completed child runs.
|
||||||
summary_df[run.id] = [run.id, run.properties['run_algorithm'],
|
summary_df[run.id] = [
|
||||||
run.properties['primary_metric'],
|
run.id,
|
||||||
float(run.properties['score'])]
|
run.properties["run_algorithm"],
|
||||||
if ('goal' in run.properties):
|
run.properties["primary_metric"],
|
||||||
goal_minimize = run.properties['goal'].split('_')[-1] == 'min'
|
float(run.properties["score"]),
|
||||||
|
]
|
||||||
|
if "goal" in run.properties:
|
||||||
|
goal_minimize = run.properties["goal"].split("_")[-1] == "min"
|
||||||
|
|
||||||
summary_df = summary_df.T.sort_values(
|
summary_df = summary_df.T.sort_values(
|
||||||
'Score',
|
"Score", ascending=goal_minimize
|
||||||
ascending=goal_minimize).drop_duplicates(['run_algorithm'])
|
).drop_duplicates(["run_algorithm"])
|
||||||
summary_df = summary_df.set_index('run_algorithm')
|
summary_df = summary_df.set_index("run_algorithm")
|
||||||
return summary_df
|
return summary_df
|
||||||
|
|
||||||
|
|
||||||
def run_inference(test_experiment, compute_target, script_folder, train_run,
|
def run_inference(
|
||||||
test_dataset, lookback_dataset, max_horizon,
|
test_experiment,
|
||||||
target_column_name, time_column_name, freq):
|
compute_target,
|
||||||
model_base_name = 'model.pkl'
|
script_folder,
|
||||||
if 'model_data_location' in train_run.properties:
|
train_run,
|
||||||
model_location = train_run.properties['model_data_location']
|
test_dataset,
|
||||||
_, model_base_name = model_location.rsplit('/', 1)
|
lookback_dataset,
|
||||||
train_run.download_file('outputs/{}'.format(model_base_name), 'inference/{}'.format(model_base_name))
|
max_horizon,
|
||||||
train_run.download_file('outputs/conda_env_v_1_0_0.yml', 'inference/condafile.yml')
|
target_column_name,
|
||||||
|
time_column_name,
|
||||||
|
freq,
|
||||||
|
):
|
||||||
|
model_base_name = "model.pkl"
|
||||||
|
if "model_data_location" in train_run.properties:
|
||||||
|
model_location = train_run.properties["model_data_location"]
|
||||||
|
_, model_base_name = model_location.rsplit("/", 1)
|
||||||
|
train_run.download_file(
|
||||||
|
"outputs/{}".format(model_base_name), "inference/{}".format(model_base_name)
|
||||||
|
)
|
||||||
|
train_run.download_file("outputs/conda_env_v_1_0_0.yml", "inference/condafile.yml")
|
||||||
|
|
||||||
inference_env = Environment("myenv")
|
inference_env = Environment("myenv")
|
||||||
inference_env.docker.enabled = True
|
inference_env.docker.enabled = True
|
||||||
inference_env.python.conda_dependencies = CondaDependencies(
|
inference_env.python.conda_dependencies = CondaDependencies(
|
||||||
conda_dependencies_file_path='inference/condafile.yml')
|
conda_dependencies_file_path="inference/condafile.yml"
|
||||||
|
)
|
||||||
|
|
||||||
est = Estimator(source_directory=script_folder,
|
est = Estimator(
|
||||||
entry_script='infer.py',
|
source_directory=script_folder,
|
||||||
|
entry_script="infer.py",
|
||||||
script_params={
|
script_params={
|
||||||
'--max_horizon': max_horizon,
|
"--max_horizon": max_horizon,
|
||||||
'--target_column_name': target_column_name,
|
"--target_column_name": target_column_name,
|
||||||
'--time_column_name': time_column_name,
|
"--time_column_name": time_column_name,
|
||||||
'--frequency': freq,
|
"--frequency": freq,
|
||||||
'--model_path': model_base_name
|
"--model_path": model_base_name,
|
||||||
},
|
},
|
||||||
inputs=[test_dataset.as_named_input('test_data'),
|
inputs=[
|
||||||
lookback_dataset.as_named_input('lookback_data')],
|
test_dataset.as_named_input("test_data"),
|
||||||
|
lookback_dataset.as_named_input("lookback_data"),
|
||||||
|
],
|
||||||
compute_target=compute_target,
|
compute_target=compute_target,
|
||||||
environment_definition=inference_env)
|
environment_definition=inference_env,
|
||||||
|
)
|
||||||
|
|
||||||
run = test_experiment.submit(
|
run = test_experiment.submit(
|
||||||
est, tags={
|
est,
|
||||||
'training_run_id': train_run.id,
|
tags={
|
||||||
'run_algorithm': train_run.properties['run_algorithm'],
|
"training_run_id": train_run.id,
|
||||||
'valid_score': train_run.properties['score'],
|
"run_algorithm": train_run.properties["run_algorithm"],
|
||||||
'primary_metric': train_run.properties['primary_metric']
|
"valid_score": train_run.properties["score"],
|
||||||
})
|
"primary_metric": train_run.properties["primary_metric"],
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
run.log("run_algorithm", run.tags['run_algorithm'])
|
run.log("run_algorithm", run.tags["run_algorithm"])
|
||||||
return run
|
return run
|
||||||
|
|
||||||
|
|
||||||
def run_multiple_inferences(summary_df, train_experiment, test_experiment,
|
def run_multiple_inferences(
|
||||||
compute_target, script_folder, test_dataset,
|
summary_df,
|
||||||
lookback_dataset, max_horizon, target_column_name,
|
train_experiment,
|
||||||
time_column_name, freq):
|
test_experiment,
|
||||||
|
compute_target,
|
||||||
|
script_folder,
|
||||||
|
test_dataset,
|
||||||
|
lookback_dataset,
|
||||||
|
max_horizon,
|
||||||
|
target_column_name,
|
||||||
|
time_column_name,
|
||||||
|
freq,
|
||||||
|
):
|
||||||
for run_name, run_summary in summary_df.iterrows():
|
for run_name, run_summary in summary_df.iterrows():
|
||||||
print(run_name)
|
print(run_name)
|
||||||
print(run_summary)
|
print(run_summary)
|
||||||
@@ -127,12 +165,19 @@ def run_multiple_inferences(summary_df, train_experiment, test_experiment,
|
|||||||
train_run = Run(train_experiment, run_id)
|
train_run = Run(train_experiment, run_id)
|
||||||
|
|
||||||
test_run = run_inference(
|
test_run = run_inference(
|
||||||
test_experiment, compute_target, script_folder, train_run,
|
test_experiment,
|
||||||
test_dataset, lookback_dataset, max_horizon, target_column_name,
|
compute_target,
|
||||||
time_column_name, freq)
|
script_folder,
|
||||||
|
train_run,
|
||||||
|
test_dataset,
|
||||||
|
lookback_dataset,
|
||||||
|
max_horizon,
|
||||||
|
target_column_name,
|
||||||
|
time_column_name,
|
||||||
|
freq,
|
||||||
|
)
|
||||||
|
|
||||||
print(test_run)
|
print(test_run)
|
||||||
summary_df.loc[summary_df.run_id == run_id,
|
summary_df.loc[summary_df.run_id == run_id, "test_run_id"] = test_run.id
|
||||||
'test_run_id'] = test_run.id
|
|
||||||
|
|
||||||
return summary_df
|
return summary_df
|
||||||
|
|||||||
@@ -19,9 +19,14 @@ except ImportError:
|
|||||||
_torch_present = False
|
_torch_present = False
|
||||||
|
|
||||||
|
|
||||||
def align_outputs(y_predicted, X_trans, X_test, y_test,
|
def align_outputs(
|
||||||
predicted_column_name='predicted',
|
y_predicted,
|
||||||
horizon_colname='horizon_origin'):
|
X_trans,
|
||||||
|
X_test,
|
||||||
|
y_test,
|
||||||
|
predicted_column_name="predicted",
|
||||||
|
horizon_colname="horizon_origin",
|
||||||
|
):
|
||||||
"""
|
"""
|
||||||
Demonstrates how to get the output aligned to the inputs
|
Demonstrates how to get the output aligned to the inputs
|
||||||
using pandas indexes. Helps understand what happened if
|
using pandas indexes. Helps understand what happened if
|
||||||
@@ -33,9 +38,13 @@ def align_outputs(y_predicted, X_trans, X_test, y_test,
|
|||||||
* model was asked to predict past max_horizon -> increase max horizon
|
* model was asked to predict past max_horizon -> increase max horizon
|
||||||
* data at start of X_test was needed for lags -> provide previous periods
|
* data at start of X_test was needed for lags -> provide previous periods
|
||||||
"""
|
"""
|
||||||
if (horizon_colname in X_trans):
|
if horizon_colname in X_trans:
|
||||||
df_fcst = pd.DataFrame({predicted_column_name: y_predicted,
|
df_fcst = pd.DataFrame(
|
||||||
horizon_colname: X_trans[horizon_colname]})
|
{
|
||||||
|
predicted_column_name: y_predicted,
|
||||||
|
horizon_colname: X_trans[horizon_colname],
|
||||||
|
}
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
df_fcst = pd.DataFrame({predicted_column_name: y_predicted})
|
df_fcst = pd.DataFrame({predicted_column_name: y_predicted})
|
||||||
|
|
||||||
@@ -48,20 +57,21 @@ def align_outputs(y_predicted, X_trans, X_test, y_test,
|
|||||||
|
|
||||||
# X_test_full's index does not include origin, so reset for merge
|
# X_test_full's index does not include origin, so reset for merge
|
||||||
df_fcst.reset_index(inplace=True)
|
df_fcst.reset_index(inplace=True)
|
||||||
X_test_full = X_test_full.reset_index().drop(columns='index')
|
X_test_full = X_test_full.reset_index().drop(columns="index")
|
||||||
together = df_fcst.merge(X_test_full, how='right')
|
together = df_fcst.merge(X_test_full, how="right")
|
||||||
|
|
||||||
# drop rows where prediction or actuals are nan
|
# drop rows where prediction or actuals are nan
|
||||||
# happens because of missing actuals
|
# happens because of missing actuals
|
||||||
# or at edges of time due to lags/rolling windows
|
# or at edges of time due to lags/rolling windows
|
||||||
clean = together[together[[target_column_name,
|
clean = together[
|
||||||
predicted_column_name]].notnull().all(axis=1)]
|
together[[target_column_name, predicted_column_name]].notnull().all(axis=1)
|
||||||
return (clean)
|
]
|
||||||
|
return clean
|
||||||
|
|
||||||
|
|
||||||
def do_rolling_forecast_with_lookback(fitted_model, X_test, y_test,
|
def do_rolling_forecast_with_lookback(
|
||||||
max_horizon, X_lookback, y_lookback,
|
fitted_model, X_test, y_test, max_horizon, X_lookback, y_lookback, freq="D"
|
||||||
freq='D'):
|
):
|
||||||
"""
|
"""
|
||||||
Produce forecasts on a rolling origin over the given test set.
|
Produce forecasts on a rolling origin over the given test set.
|
||||||
|
|
||||||
@@ -83,22 +93,28 @@ def do_rolling_forecast_with_lookback(fitted_model, X_test, y_test,
|
|||||||
horizon_time = origin_time + max_horizon * to_offset(freq)
|
horizon_time = origin_time + max_horizon * to_offset(freq)
|
||||||
|
|
||||||
# Extract test data from an expanding window up-to the horizon
|
# Extract test data from an expanding window up-to the horizon
|
||||||
expand_wind = (X[time_column_name] < horizon_time)
|
expand_wind = X[time_column_name] < horizon_time
|
||||||
X_test_expand = X[expand_wind]
|
X_test_expand = X[expand_wind]
|
||||||
y_query_expand = np.zeros(len(X_test_expand)).astype(np.float)
|
y_query_expand = np.zeros(len(X_test_expand)).astype(np.float)
|
||||||
y_query_expand.fill(np.NaN)
|
y_query_expand.fill(np.NaN)
|
||||||
|
|
||||||
if origin_time != X[time_column_name].min():
|
if origin_time != X[time_column_name].min():
|
||||||
# Set the context by including actuals up-to the origin time
|
# Set the context by including actuals up-to the origin time
|
||||||
test_context_expand_wind = (X[time_column_name] < origin_time)
|
test_context_expand_wind = X[time_column_name] < origin_time
|
||||||
context_expand_wind = (X_test_expand[time_column_name] < origin_time)
|
context_expand_wind = X_test_expand[time_column_name] < origin_time
|
||||||
y_query_expand[context_expand_wind] = y[test_context_expand_wind]
|
y_query_expand[context_expand_wind] = y[test_context_expand_wind]
|
||||||
|
|
||||||
# Print some debug info
|
# Print some debug info
|
||||||
print("Horizon_time:", horizon_time,
|
print(
|
||||||
" origin_time: ", origin_time,
|
"Horizon_time:",
|
||||||
" max_horizon: ", max_horizon,
|
horizon_time,
|
||||||
" freq: ", freq)
|
" origin_time: ",
|
||||||
|
origin_time,
|
||||||
|
" max_horizon: ",
|
||||||
|
max_horizon,
|
||||||
|
" freq: ",
|
||||||
|
freq,
|
||||||
|
)
|
||||||
print("expand_wind: ", expand_wind)
|
print("expand_wind: ", expand_wind)
|
||||||
print("y_query_expand")
|
print("y_query_expand")
|
||||||
print(y_query_expand)
|
print(y_query_expand)
|
||||||
@@ -124,9 +140,14 @@ def do_rolling_forecast_with_lookback(fitted_model, X_test, y_test,
|
|||||||
trans_tindex = X_trans.index.get_level_values(time_column_name)
|
trans_tindex = X_trans.index.get_level_values(time_column_name)
|
||||||
trans_roll_wind = (trans_tindex >= origin_time) & (trans_tindex < horizon_time)
|
trans_roll_wind = (trans_tindex >= origin_time) & (trans_tindex < horizon_time)
|
||||||
test_roll_wind = expand_wind & (X[time_column_name] >= origin_time)
|
test_roll_wind = expand_wind & (X[time_column_name] >= origin_time)
|
||||||
df_list.append(align_outputs(
|
df_list.append(
|
||||||
y_fcst[trans_roll_wind], X_trans[trans_roll_wind],
|
align_outputs(
|
||||||
X[test_roll_wind], y[test_roll_wind]))
|
y_fcst[trans_roll_wind],
|
||||||
|
X_trans[trans_roll_wind],
|
||||||
|
X[test_roll_wind],
|
||||||
|
y[test_roll_wind],
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
# Advance the origin time
|
# Advance the origin time
|
||||||
origin_time = horizon_time
|
origin_time = horizon_time
|
||||||
@@ -134,7 +155,7 @@ def do_rolling_forecast_with_lookback(fitted_model, X_test, y_test,
|
|||||||
return pd.concat(df_list, ignore_index=True)
|
return pd.concat(df_list, ignore_index=True)
|
||||||
|
|
||||||
|
|
||||||
def do_rolling_forecast(fitted_model, X_test, y_test, max_horizon, freq='D'):
|
def do_rolling_forecast(fitted_model, X_test, y_test, max_horizon, freq="D"):
|
||||||
"""
|
"""
|
||||||
Produce forecasts on a rolling origin over the given test set.
|
Produce forecasts on a rolling origin over the given test set.
|
||||||
|
|
||||||
@@ -153,23 +174,28 @@ def do_rolling_forecast(fitted_model, X_test, y_test, max_horizon, freq='D'):
|
|||||||
horizon_time = origin_time + max_horizon * to_offset(freq)
|
horizon_time = origin_time + max_horizon * to_offset(freq)
|
||||||
|
|
||||||
# Extract test data from an expanding window up-to the horizon
|
# Extract test data from an expanding window up-to the horizon
|
||||||
expand_wind = (X_test[time_column_name] < horizon_time)
|
expand_wind = X_test[time_column_name] < horizon_time
|
||||||
X_test_expand = X_test[expand_wind]
|
X_test_expand = X_test[expand_wind]
|
||||||
y_query_expand = np.zeros(len(X_test_expand)).astype(np.float)
|
y_query_expand = np.zeros(len(X_test_expand)).astype(np.float)
|
||||||
y_query_expand.fill(np.NaN)
|
y_query_expand.fill(np.NaN)
|
||||||
|
|
||||||
if origin_time != X_test[time_column_name].min():
|
if origin_time != X_test[time_column_name].min():
|
||||||
# Set the context by including actuals up-to the origin time
|
# Set the context by including actuals up-to the origin time
|
||||||
test_context_expand_wind = (X_test[time_column_name] < origin_time)
|
test_context_expand_wind = X_test[time_column_name] < origin_time
|
||||||
context_expand_wind = (X_test_expand[time_column_name] < origin_time)
|
context_expand_wind = X_test_expand[time_column_name] < origin_time
|
||||||
y_query_expand[context_expand_wind] = y_test[
|
y_query_expand[context_expand_wind] = y_test[test_context_expand_wind]
|
||||||
test_context_expand_wind]
|
|
||||||
|
|
||||||
# Print some debug info
|
# Print some debug info
|
||||||
print("Horizon_time:", horizon_time,
|
print(
|
||||||
" origin_time: ", origin_time,
|
"Horizon_time:",
|
||||||
" max_horizon: ", max_horizon,
|
horizon_time,
|
||||||
" freq: ", freq)
|
" origin_time: ",
|
||||||
|
origin_time,
|
||||||
|
" max_horizon: ",
|
||||||
|
max_horizon,
|
||||||
|
" freq: ",
|
||||||
|
freq,
|
||||||
|
)
|
||||||
print("expand_wind: ", expand_wind)
|
print("expand_wind: ", expand_wind)
|
||||||
print("y_query_expand")
|
print("y_query_expand")
|
||||||
print(y_query_expand)
|
print(y_query_expand)
|
||||||
@@ -193,10 +219,14 @@ def do_rolling_forecast(fitted_model, X_test, y_test, max_horizon, freq='D'):
|
|||||||
trans_tindex = X_trans.index.get_level_values(time_column_name)
|
trans_tindex = X_trans.index.get_level_values(time_column_name)
|
||||||
trans_roll_wind = (trans_tindex >= origin_time) & (trans_tindex < horizon_time)
|
trans_roll_wind = (trans_tindex >= origin_time) & (trans_tindex < horizon_time)
|
||||||
test_roll_wind = expand_wind & (X_test[time_column_name] >= origin_time)
|
test_roll_wind = expand_wind & (X_test[time_column_name] >= origin_time)
|
||||||
df_list.append(align_outputs(y_fcst[trans_roll_wind],
|
df_list.append(
|
||||||
|
align_outputs(
|
||||||
|
y_fcst[trans_roll_wind],
|
||||||
X_trans[trans_roll_wind],
|
X_trans[trans_roll_wind],
|
||||||
X_test[test_roll_wind],
|
X_test[test_roll_wind],
|
||||||
y_test[test_roll_wind]))
|
y_test[test_roll_wind],
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
# Advance the origin time
|
# Advance the origin time
|
||||||
origin_time = horizon_time
|
origin_time = horizon_time
|
||||||
@@ -230,20 +260,31 @@ def map_location_cuda(storage, loc):
|
|||||||
|
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
'--max_horizon', type=int, dest='max_horizon',
|
"--max_horizon",
|
||||||
default=10, help='Max Horizon for forecasting')
|
type=int,
|
||||||
|
dest="max_horizon",
|
||||||
|
default=10,
|
||||||
|
help="Max Horizon for forecasting",
|
||||||
|
)
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
'--target_column_name', type=str, dest='target_column_name',
|
"--target_column_name",
|
||||||
help='Target Column Name')
|
type=str,
|
||||||
|
dest="target_column_name",
|
||||||
|
help="Target Column Name",
|
||||||
|
)
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
'--time_column_name', type=str, dest='time_column_name',
|
"--time_column_name", type=str, dest="time_column_name", help="Time Column Name"
|
||||||
help='Time Column Name')
|
)
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
'--frequency', type=str, dest='freq',
|
"--frequency", type=str, dest="freq", help="Frequency of prediction"
|
||||||
help='Frequency of prediction')
|
)
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
'--model_path', type=str, dest='model_path',
|
"--model_path",
|
||||||
default='model.pkl', help='Filename of model to be loaded')
|
type=str,
|
||||||
|
dest="model_path",
|
||||||
|
default="model.pkl",
|
||||||
|
help="Filename of model to be loaded",
|
||||||
|
)
|
||||||
|
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
max_horizon = args.max_horizon
|
max_horizon = args.max_horizon
|
||||||
@@ -252,7 +293,7 @@ time_column_name = args.time_column_name
|
|||||||
freq = args.freq
|
freq = args.freq
|
||||||
model_path = args.model_path
|
model_path = args.model_path
|
||||||
|
|
||||||
print('args passed are: ')
|
print("args passed are: ")
|
||||||
print(max_horizon)
|
print(max_horizon)
|
||||||
print(target_column_name)
|
print(target_column_name)
|
||||||
print(time_column_name)
|
print(time_column_name)
|
||||||
@@ -261,39 +302,41 @@ print(model_path)
|
|||||||
|
|
||||||
run = Run.get_context()
|
run = Run.get_context()
|
||||||
# get input dataset by name
|
# get input dataset by name
|
||||||
test_dataset = run.input_datasets['test_data']
|
test_dataset = run.input_datasets["test_data"]
|
||||||
lookback_dataset = run.input_datasets['lookback_data']
|
lookback_dataset = run.input_datasets["lookback_data"]
|
||||||
|
|
||||||
grain_column_names = []
|
grain_column_names = []
|
||||||
|
|
||||||
df = test_dataset.to_pandas_dataframe()
|
df = test_dataset.to_pandas_dataframe()
|
||||||
|
|
||||||
print('Read df')
|
print("Read df")
|
||||||
print(df)
|
print(df)
|
||||||
|
|
||||||
X_test_df = test_dataset.drop_columns(columns=[target_column_name])
|
X_test_df = test_dataset.drop_columns(columns=[target_column_name])
|
||||||
y_test_df = test_dataset.with_timestamp_columns(
|
y_test_df = test_dataset.with_timestamp_columns(None).keep_columns(
|
||||||
None).keep_columns(columns=[target_column_name])
|
columns=[target_column_name]
|
||||||
|
)
|
||||||
|
|
||||||
X_lookback_df = lookback_dataset.drop_columns(columns=[target_column_name])
|
X_lookback_df = lookback_dataset.drop_columns(columns=[target_column_name])
|
||||||
y_lookback_df = lookback_dataset.with_timestamp_columns(
|
y_lookback_df = lookback_dataset.with_timestamp_columns(None).keep_columns(
|
||||||
None).keep_columns(columns=[target_column_name])
|
columns=[target_column_name]
|
||||||
|
)
|
||||||
|
|
||||||
_, ext = os.path.splitext(model_path)
|
_, ext = os.path.splitext(model_path)
|
||||||
if ext == '.pt':
|
if ext == ".pt":
|
||||||
# Load the fc-tcn torch model.
|
# Load the fc-tcn torch model.
|
||||||
assert _torch_present
|
assert _torch_present
|
||||||
if torch.cuda.is_available():
|
if torch.cuda.is_available():
|
||||||
map_location = map_location_cuda
|
map_location = map_location_cuda
|
||||||
else:
|
else:
|
||||||
map_location = 'cpu'
|
map_location = "cpu"
|
||||||
with open(model_path, 'rb') as fh:
|
with open(model_path, "rb") as fh:
|
||||||
fitted_model = torch.load(fh, map_location=map_location)
|
fitted_model = torch.load(fh, map_location=map_location)
|
||||||
else:
|
else:
|
||||||
# Load the sklearn pipeline.
|
# Load the sklearn pipeline.
|
||||||
fitted_model = joblib.load(model_path)
|
fitted_model = joblib.load(model_path)
|
||||||
|
|
||||||
if hasattr(fitted_model, 'get_lookback'):
|
if hasattr(fitted_model, "get_lookback"):
|
||||||
lookback = fitted_model.get_lookback()
|
lookback = fitted_model.get_lookback()
|
||||||
df_all = do_rolling_forecast_with_lookback(
|
df_all = do_rolling_forecast_with_lookback(
|
||||||
fitted_model,
|
fitted_model,
|
||||||
@@ -302,26 +345,28 @@ if hasattr(fitted_model, 'get_lookback'):
|
|||||||
max_horizon,
|
max_horizon,
|
||||||
X_lookback_df.to_pandas_dataframe()[-lookback:],
|
X_lookback_df.to_pandas_dataframe()[-lookback:],
|
||||||
y_lookback_df.to_pandas_dataframe().values.T[0][-lookback:],
|
y_lookback_df.to_pandas_dataframe().values.T[0][-lookback:],
|
||||||
freq)
|
freq,
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
df_all = do_rolling_forecast(
|
df_all = do_rolling_forecast(
|
||||||
fitted_model,
|
fitted_model,
|
||||||
X_test_df.to_pandas_dataframe(),
|
X_test_df.to_pandas_dataframe(),
|
||||||
y_test_df.to_pandas_dataframe().values.T[0],
|
y_test_df.to_pandas_dataframe().values.T[0],
|
||||||
max_horizon,
|
max_horizon,
|
||||||
freq)
|
freq,
|
||||||
|
)
|
||||||
|
|
||||||
print(df_all)
|
print(df_all)
|
||||||
|
|
||||||
print("target values:::")
|
print("target values:::")
|
||||||
print(df_all[target_column_name])
|
print(df_all[target_column_name])
|
||||||
print("predicted values:::")
|
print("predicted values:::")
|
||||||
print(df_all['predicted'])
|
print(df_all["predicted"])
|
||||||
|
|
||||||
# Use the AutoML scoring module
|
# Use the AutoML scoring module
|
||||||
regression_metrics = list(constants.REGRESSION_SCALAR_SET)
|
regression_metrics = list(constants.REGRESSION_SCALAR_SET)
|
||||||
y_test = np.array(df_all[target_column_name])
|
y_test = np.array(df_all[target_column_name])
|
||||||
y_pred = np.array(df_all['predicted'])
|
y_pred = np.array(df_all["predicted"])
|
||||||
scores = scoring.score_regression(y_test, y_pred, regression_metrics)
|
scores = scoring.score_regression(y_test, y_pred, regression_metrics)
|
||||||
|
|
||||||
print("scores:")
|
print("scores:")
|
||||||
@@ -331,12 +376,11 @@ for key, value in scores.items():
|
|||||||
run.log(key, value)
|
run.log(key, value)
|
||||||
|
|
||||||
print("Simple forecasting model")
|
print("Simple forecasting model")
|
||||||
rmse = np.sqrt(mean_squared_error(
|
rmse = np.sqrt(mean_squared_error(df_all[target_column_name], df_all["predicted"]))
|
||||||
df_all[target_column_name], df_all['predicted']))
|
|
||||||
print("[Test Data] \nRoot Mean squared error: %.2f" % rmse)
|
print("[Test Data] \nRoot Mean squared error: %.2f" % rmse)
|
||||||
mae = mean_absolute_error(df_all[target_column_name], df_all['predicted'])
|
mae = mean_absolute_error(df_all[target_column_name], df_all["predicted"])
|
||||||
print('mean_absolute_error score: %.2f' % mae)
|
print("mean_absolute_error score: %.2f" % mae)
|
||||||
print('MAPE: %.2f' % MAPE(df_all[target_column_name], df_all['predicted']))
|
print("MAPE: %.2f" % MAPE(df_all[target_column_name], df_all["predicted"]))
|
||||||
|
|
||||||
run.log('rmse', rmse)
|
run.log("rmse", rmse)
|
||||||
run.log('mae', mae)
|
run.log("mae", mae)
|
||||||
|
|||||||
@@ -71,7 +71,8 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"from azureml.core import Workspace, Experiment, Dataset\n",
|
"from azureml.core import Workspace, Experiment, Dataset\n",
|
||||||
"from azureml.train.automl import AutoMLConfig\n",
|
"from azureml.train.automl import AutoMLConfig\n",
|
||||||
"from datetime import datetime"
|
"from datetime import datetime\n",
|
||||||
|
"from azureml.automl.core.featurization import FeaturizationConfig"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -87,7 +88,7 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"print(\"This notebook was created using version 1.32.0 of the Azure ML SDK\")\n",
|
"print(\"This notebook was created using version 1.36.0 of the Azure ML SDK\")\n",
|
||||||
"print(\"You are currently using version\", azureml.core.VERSION, \"of the Azure ML SDK\")"
|
"print(\"You are currently using version\", azureml.core.VERSION, \"of the Azure ML SDK\")"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
@@ -107,19 +108,19 @@
|
|||||||
"ws = Workspace.from_config()\n",
|
"ws = Workspace.from_config()\n",
|
||||||
"\n",
|
"\n",
|
||||||
"# choose a name for the run history container in the workspace\n",
|
"# choose a name for the run history container in the workspace\n",
|
||||||
"experiment_name = 'automl-bikeshareforecasting'\n",
|
"experiment_name = \"automl-bikeshareforecasting\"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"experiment = Experiment(ws, experiment_name)\n",
|
"experiment = Experiment(ws, experiment_name)\n",
|
||||||
"\n",
|
"\n",
|
||||||
"output = {}\n",
|
"output = {}\n",
|
||||||
"output['Subscription ID'] = ws.subscription_id\n",
|
"output[\"Subscription ID\"] = ws.subscription_id\n",
|
||||||
"output['Workspace'] = ws.name\n",
|
"output[\"Workspace\"] = ws.name\n",
|
||||||
"output['SKU'] = ws.sku\n",
|
"output[\"SKU\"] = ws.sku\n",
|
||||||
"output['Resource Group'] = ws.resource_group\n",
|
"output[\"Resource Group\"] = ws.resource_group\n",
|
||||||
"output['Location'] = ws.location\n",
|
"output[\"Location\"] = ws.location\n",
|
||||||
"output['Run History Name'] = experiment_name\n",
|
"output[\"Run History Name\"] = experiment_name\n",
|
||||||
"pd.set_option('display.max_colwidth', -1)\n",
|
"pd.set_option(\"display.max_colwidth\", -1)\n",
|
||||||
"outputDf = pd.DataFrame(data = output, index = [''])\n",
|
"outputDf = pd.DataFrame(data=output, index=[\"\"])\n",
|
||||||
"outputDf.T"
|
"outputDf.T"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
@@ -152,10 +153,11 @@
|
|||||||
"# Verify that cluster does not exist already\n",
|
"# Verify that cluster does not exist already\n",
|
||||||
"try:\n",
|
"try:\n",
|
||||||
" compute_target = ComputeTarget(workspace=ws, name=amlcompute_cluster_name)\n",
|
" compute_target = ComputeTarget(workspace=ws, name=amlcompute_cluster_name)\n",
|
||||||
" print('Found existing cluster, use it.')\n",
|
" print(\"Found existing cluster, use it.\")\n",
|
||||||
"except ComputeTargetException:\n",
|
"except ComputeTargetException:\n",
|
||||||
" compute_config = AmlCompute.provisioning_configuration(vm_size='STANDARD_DS12_V2',\n",
|
" compute_config = AmlCompute.provisioning_configuration(\n",
|
||||||
" max_nodes=4)\n",
|
" vm_size=\"STANDARD_DS12_V2\", max_nodes=4\n",
|
||||||
|
" )\n",
|
||||||
" compute_target = ComputeTarget.create(ws, amlcompute_cluster_name, compute_config)\n",
|
" compute_target = ComputeTarget.create(ws, amlcompute_cluster_name, compute_config)\n",
|
||||||
"\n",
|
"\n",
|
||||||
"compute_target.wait_for_completion(show_output=True)"
|
"compute_target.wait_for_completion(show_output=True)"
|
||||||
@@ -177,7 +179,9 @@
|
|||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"datastore = ws.get_default_datastore()\n",
|
"datastore = ws.get_default_datastore()\n",
|
||||||
"datastore.upload_files(files = ['./bike-no.csv'], target_path = 'dataset/', overwrite = True,show_progress = True)"
|
"datastore.upload_files(\n",
|
||||||
|
" files=[\"./bike-no.csv\"], target_path=\"dataset/\", overwrite=True, show_progress=True\n",
|
||||||
|
")"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -197,8 +201,8 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"target_column_name = 'cnt'\n",
|
"target_column_name = \"cnt\"\n",
|
||||||
"time_column_name = 'date'"
|
"time_column_name = \"date\""
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -207,10 +211,12 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"dataset = Dataset.Tabular.from_delimited_files(path = [(datastore, 'dataset/bike-no.csv')]).with_timestamp_columns(fine_grain_timestamp=time_column_name) \n",
|
"dataset = Dataset.Tabular.from_delimited_files(\n",
|
||||||
|
" path=[(datastore, \"dataset/bike-no.csv\")]\n",
|
||||||
|
").with_timestamp_columns(fine_grain_timestamp=time_column_name)\n",
|
||||||
"\n",
|
"\n",
|
||||||
"# Drop the columns 'casual' and 'registered' as these columns are a breakdown of the total and therefore a leak.\n",
|
"# Drop the columns 'casual' and 'registered' as these columns are a breakdown of the total and therefore a leak.\n",
|
||||||
"dataset = dataset.drop_columns(columns=['casual', 'registered'])\n",
|
"dataset = dataset.drop_columns(columns=[\"casual\", \"registered\"])\n",
|
||||||
"\n",
|
"\n",
|
||||||
"dataset.take(5).to_pandas_dataframe().reset_index(drop=True)"
|
"dataset.take(5).to_pandas_dataframe().reset_index(drop=True)"
|
||||||
]
|
]
|
||||||
@@ -303,6 +309,25 @@
|
|||||||
"forecast_horizon = 14"
|
"forecast_horizon = 14"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"### Convert prediction type to integer\n",
|
||||||
|
"The featurization configuration can be used to change the default prediction type from decimal numbers to integer. This customization can be used in the scenario when the target column is expected to contain whole values as the number of rented bikes per day."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"featurization_config = FeaturizationConfig()\n",
|
||||||
|
"# Force the target column, to be integer type.\n",
|
||||||
|
"featurization_config.add_prediction_transform_type(\"Integer\")"
|
||||||
|
]
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
@@ -317,27 +342,31 @@
|
|||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"from azureml.automl.core.forecasting_parameters import ForecastingParameters\n",
|
"from azureml.automl.core.forecasting_parameters import ForecastingParameters\n",
|
||||||
|
"\n",
|
||||||
"forecasting_parameters = ForecastingParameters(\n",
|
"forecasting_parameters = ForecastingParameters(\n",
|
||||||
" time_column_name=time_column_name,\n",
|
" time_column_name=time_column_name,\n",
|
||||||
" forecast_horizon=forecast_horizon,\n",
|
" forecast_horizon=forecast_horizon,\n",
|
||||||
" country_or_region_for_holidays='US', # set country_or_region will trigger holiday featurizer\n",
|
" country_or_region_for_holidays=\"US\", # set country_or_region will trigger holiday featurizer\n",
|
||||||
" target_lags='auto', # use heuristic based lag setting\n",
|
" target_lags=\"auto\", # use heuristic based lag setting\n",
|
||||||
" freq='D' # Set the forecast frequency to be daily\n",
|
" freq=\"D\", # Set the forecast frequency to be daily\n",
|
||||||
")\n",
|
")\n",
|
||||||
"\n",
|
"\n",
|
||||||
"automl_config = AutoMLConfig(task='forecasting', \n",
|
"automl_config = AutoMLConfig(\n",
|
||||||
" primary_metric='normalized_root_mean_squared_error',\n",
|
" task=\"forecasting\",\n",
|
||||||
" blocked_models = ['ExtremeRandomTrees'], \n",
|
" primary_metric=\"normalized_root_mean_squared_error\",\n",
|
||||||
|
" featurization=featurization_config,\n",
|
||||||
|
" blocked_models=[\"ExtremeRandomTrees\"],\n",
|
||||||
" experiment_timeout_hours=0.3,\n",
|
" experiment_timeout_hours=0.3,\n",
|
||||||
" training_data=train,\n",
|
" training_data=train,\n",
|
||||||
" label_column_name=target_column_name,\n",
|
" label_column_name=target_column_name,\n",
|
||||||
" compute_target=compute_target,\n",
|
" compute_target=compute_target,\n",
|
||||||
" enable_early_stopping=True,\n",
|
" enable_early_stopping=True,\n",
|
||||||
" n_cross_validations=3, \n",
|
" n_cross_validations=3,\n",
|
||||||
" max_concurrent_iterations=4,\n",
|
" max_concurrent_iterations=4,\n",
|
||||||
" max_cores_per_iteration=-1,\n",
|
" max_cores_per_iteration=-1,\n",
|
||||||
" verbosity=logging.INFO,\n",
|
" verbosity=logging.INFO,\n",
|
||||||
" forecasting_parameters=forecasting_parameters)"
|
" forecasting_parameters=forecasting_parameters,\n",
|
||||||
|
")"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -398,7 +427,7 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"fitted_model.named_steps['timeseriestransformer'].get_engineered_feature_names()"
|
"fitted_model.named_steps[\"timeseriestransformer\"].get_engineered_feature_names()"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -423,7 +452,9 @@
|
|||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"# Get the featurization summary as a list of JSON\n",
|
"# Get the featurization summary as a list of JSON\n",
|
||||||
"featurization_summary = fitted_model.named_steps['timeseriestransformer'].get_featurization_summary()\n",
|
"featurization_summary = fitted_model.named_steps[\n",
|
||||||
|
" \"timeseriestransformer\"\n",
|
||||||
|
"].get_featurization_summary()\n",
|
||||||
"# View the featurization summary as a pandas dataframe\n",
|
"# View the featurization summary as a pandas dataframe\n",
|
||||||
"pd.DataFrame.from_records(featurization_summary)"
|
"pd.DataFrame.from_records(featurization_summary)"
|
||||||
]
|
]
|
||||||
@@ -470,9 +501,9 @@
|
|||||||
"import os\n",
|
"import os\n",
|
||||||
"import shutil\n",
|
"import shutil\n",
|
||||||
"\n",
|
"\n",
|
||||||
"script_folder = os.path.join(os.getcwd(), 'forecast')\n",
|
"script_folder = os.path.join(os.getcwd(), \"forecast\")\n",
|
||||||
"os.makedirs(script_folder, exist_ok=True)\n",
|
"os.makedirs(script_folder, exist_ok=True)\n",
|
||||||
"shutil.copy('forecasting_script.py', script_folder)"
|
"shutil.copy(\"forecasting_script.py\", script_folder)"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -490,7 +521,9 @@
|
|||||||
"source": [
|
"source": [
|
||||||
"from run_forecast import run_rolling_forecast\n",
|
"from run_forecast import run_rolling_forecast\n",
|
||||||
"\n",
|
"\n",
|
||||||
"remote_run = run_rolling_forecast(test_experiment, compute_target, best_run, test, target_column_name)\n",
|
"remote_run = run_rolling_forecast(\n",
|
||||||
|
" test_experiment, compute_target, best_run, test, target_column_name\n",
|
||||||
|
")\n",
|
||||||
"remote_run"
|
"remote_run"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
@@ -507,7 +540,7 @@
|
|||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"### Download the prediction result for metrics calcuation\n",
|
"### Download the prediction result for metrics calculation\n",
|
||||||
"The test data with predictions are saved in artifact outputs/predictions.csv. You can download it and calculation some error metrics for the forecasts and vizualize the predictions vs. the actuals."
|
"The test data with predictions are saved in artifact outputs/predictions.csv. You can download it and calculation some error metrics for the forecasts and vizualize the predictions vs. the actuals."
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
@@ -517,8 +550,8 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"remote_run.download_file('outputs/predictions.csv', 'predictions.csv')\n",
|
"remote_run.download_file(\"outputs/predictions.csv\", \"predictions.csv\")\n",
|
||||||
"df_all = pd.read_csv('predictions.csv')"
|
"df_all = pd.read_csv(\"predictions.csv\")"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -535,18 +568,23 @@
|
|||||||
"# use automl metrics module\n",
|
"# use automl metrics module\n",
|
||||||
"scores = scoring.score_regression(\n",
|
"scores = scoring.score_regression(\n",
|
||||||
" y_test=df_all[target_column_name],\n",
|
" y_test=df_all[target_column_name],\n",
|
||||||
" y_pred=df_all['predicted'],\n",
|
" y_pred=df_all[\"predicted\"],\n",
|
||||||
" metrics=list(constants.Metric.SCALAR_REGRESSION_SET))\n",
|
" metrics=list(constants.Metric.SCALAR_REGRESSION_SET),\n",
|
||||||
|
")\n",
|
||||||
"\n",
|
"\n",
|
||||||
"print(\"[Test data scores]\\n\")\n",
|
"print(\"[Test data scores]\\n\")\n",
|
||||||
"for key, value in scores.items(): \n",
|
"for key, value in scores.items():\n",
|
||||||
" print('{}: {:.3f}'.format(key, value))\n",
|
" print(\"{}: {:.3f}\".format(key, value))\n",
|
||||||
" \n",
|
"\n",
|
||||||
"# Plot outputs\n",
|
"# Plot outputs\n",
|
||||||
"%matplotlib inline\n",
|
"%matplotlib inline\n",
|
||||||
"test_pred = plt.scatter(df_all[target_column_name], df_all['predicted'], color='b')\n",
|
"test_pred = plt.scatter(df_all[target_column_name], df_all[\"predicted\"], color=\"b\")\n",
|
||||||
"test_test = plt.scatter(df_all[target_column_name], df_all[target_column_name], color='g')\n",
|
"test_test = plt.scatter(\n",
|
||||||
"plt.legend((test_pred, test_test), ('prediction', 'truth'), loc='upper left', fontsize=8)\n",
|
" df_all[target_column_name], df_all[target_column_name], color=\"g\"\n",
|
||||||
|
")\n",
|
||||||
|
"plt.legend(\n",
|
||||||
|
" (test_pred, test_test), (\"prediction\", \"truth\"), loc=\"upper left\", fontsize=8\n",
|
||||||
|
")\n",
|
||||||
"plt.show()"
|
"plt.show()"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
@@ -567,10 +605,18 @@
|
|||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"from metrics_helper import MAPE, APE\n",
|
"from metrics_helper import MAPE, APE\n",
|
||||||
"df_all.groupby('horizon_origin').apply(\n",
|
"\n",
|
||||||
" lambda df: pd.Series({'MAPE': MAPE(df[target_column_name], df['predicted']),\n",
|
"df_all.groupby(\"horizon_origin\").apply(\n",
|
||||||
" 'RMSE': np.sqrt(mean_squared_error(df[target_column_name], df['predicted'])),\n",
|
" lambda df: pd.Series(\n",
|
||||||
" 'MAE': mean_absolute_error(df[target_column_name], df['predicted'])}))"
|
" {\n",
|
||||||
|
" \"MAPE\": MAPE(df[target_column_name], df[\"predicted\"]),\n",
|
||||||
|
" \"RMSE\": np.sqrt(\n",
|
||||||
|
" mean_squared_error(df[target_column_name], df[\"predicted\"])\n",
|
||||||
|
" ),\n",
|
||||||
|
" \"MAE\": mean_absolute_error(df[target_column_name], df[\"predicted\"]),\n",
|
||||||
|
" }\n",
|
||||||
|
" )\n",
|
||||||
|
")"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -586,15 +632,18 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"df_all_APE = df_all.assign(APE=APE(df_all[target_column_name], df_all['predicted']))\n",
|
"df_all_APE = df_all.assign(APE=APE(df_all[target_column_name], df_all[\"predicted\"]))\n",
|
||||||
"APEs = [df_all_APE[df_all['horizon_origin'] == h].APE.values for h in range(1, forecast_horizon + 1)]\n",
|
"APEs = [\n",
|
||||||
|
" df_all_APE[df_all[\"horizon_origin\"] == h].APE.values\n",
|
||||||
|
" for h in range(1, forecast_horizon + 1)\n",
|
||||||
|
"]\n",
|
||||||
"\n",
|
"\n",
|
||||||
"%matplotlib inline\n",
|
"%matplotlib inline\n",
|
||||||
"plt.boxplot(APEs)\n",
|
"plt.boxplot(APEs)\n",
|
||||||
"plt.yscale('log')\n",
|
"plt.yscale(\"log\")\n",
|
||||||
"plt.xlabel('horizon')\n",
|
"plt.xlabel(\"horizon\")\n",
|
||||||
"plt.ylabel('APE (%)')\n",
|
"plt.ylabel(\"APE (%)\")\n",
|
||||||
"plt.title('Absolute Percentage Errors by Forecast Horizon')\n",
|
"plt.title(\"Absolute Percentage Errors by Forecast Horizon\")\n",
|
||||||
"\n",
|
"\n",
|
||||||
"plt.show()"
|
"plt.show()"
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -4,11 +4,14 @@ from sklearn.externals import joblib
|
|||||||
|
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
'--target_column_name', type=str, dest='target_column_name',
|
"--target_column_name",
|
||||||
help='Target Column Name')
|
type=str,
|
||||||
|
dest="target_column_name",
|
||||||
|
help="Target Column Name",
|
||||||
|
)
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
'--test_dataset', type=str, dest='test_dataset',
|
"--test_dataset", type=str, dest="test_dataset", help="Test Dataset"
|
||||||
help='Test Dataset')
|
)
|
||||||
|
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
target_column_name = args.target_column_name
|
target_column_name = args.target_column_name
|
||||||
@@ -20,19 +23,30 @@ ws = run.experiment.workspace
|
|||||||
# get the input dataset by id
|
# get the input dataset by id
|
||||||
test_dataset = Dataset.get_by_id(ws, id=test_dataset_id)
|
test_dataset = Dataset.get_by_id(ws, id=test_dataset_id)
|
||||||
|
|
||||||
X_test_df = test_dataset.drop_columns(columns=[target_column_name]).to_pandas_dataframe().reset_index(drop=True)
|
X_test_df = (
|
||||||
y_test_df = test_dataset.with_timestamp_columns(None).keep_columns(columns=[target_column_name]).to_pandas_dataframe()
|
test_dataset.drop_columns(columns=[target_column_name])
|
||||||
|
.to_pandas_dataframe()
|
||||||
|
.reset_index(drop=True)
|
||||||
|
)
|
||||||
|
y_test_df = (
|
||||||
|
test_dataset.with_timestamp_columns(None)
|
||||||
|
.keep_columns(columns=[target_column_name])
|
||||||
|
.to_pandas_dataframe()
|
||||||
|
)
|
||||||
|
|
||||||
fitted_model = joblib.load('model.pkl')
|
fitted_model = joblib.load("model.pkl")
|
||||||
|
|
||||||
y_pred, X_trans = fitted_model.rolling_evaluation(X_test_df, y_test_df.values)
|
y_pred, X_trans = fitted_model.rolling_evaluation(X_test_df, y_test_df.values)
|
||||||
|
|
||||||
# Add predictions, actuals, and horizon relative to rolling origin to the test feature data
|
# Add predictions, actuals, and horizon relative to rolling origin to the test feature data
|
||||||
assign_dict = {'horizon_origin': X_trans['horizon_origin'].values, 'predicted': y_pred,
|
assign_dict = {
|
||||||
target_column_name: y_test_df[target_column_name].values}
|
"horizon_origin": X_trans["horizon_origin"].values,
|
||||||
|
"predicted": y_pred,
|
||||||
|
target_column_name: y_test_df[target_column_name].values,
|
||||||
|
}
|
||||||
df_all = X_test_df.assign(**assign_dict)
|
df_all = X_test_df.assign(**assign_dict)
|
||||||
|
|
||||||
file_name = 'outputs/predictions.csv'
|
file_name = "outputs/predictions.csv"
|
||||||
export_csv = df_all.to_csv(file_name, header=True)
|
export_csv = df_all.to_csv(file_name, header=True)
|
||||||
|
|
||||||
# Upload the predictions into artifacts
|
# Upload the predictions into artifacts
|
||||||
|
|||||||
@@ -1,32 +1,40 @@
|
|||||||
from azureml.core import ScriptRunConfig
|
from azureml.core import ScriptRunConfig
|
||||||
|
|
||||||
|
|
||||||
def run_rolling_forecast(test_experiment, compute_target, train_run,
|
def run_rolling_forecast(
|
||||||
test_dataset, target_column_name,
|
test_experiment,
|
||||||
inference_folder='./forecast'):
|
compute_target,
|
||||||
train_run.download_file('outputs/model.pkl',
|
train_run,
|
||||||
inference_folder + '/model.pkl')
|
test_dataset,
|
||||||
|
target_column_name,
|
||||||
|
inference_folder="./forecast",
|
||||||
|
):
|
||||||
|
train_run.download_file("outputs/model.pkl", inference_folder + "/model.pkl")
|
||||||
|
|
||||||
inference_env = train_run.get_environment()
|
inference_env = train_run.get_environment()
|
||||||
|
|
||||||
config = ScriptRunConfig(source_directory=inference_folder,
|
config = ScriptRunConfig(
|
||||||
script='forecasting_script.py',
|
source_directory=inference_folder,
|
||||||
arguments=['--target_column_name',
|
script="forecasting_script.py",
|
||||||
|
arguments=[
|
||||||
|
"--target_column_name",
|
||||||
target_column_name,
|
target_column_name,
|
||||||
'--test_dataset',
|
"--test_dataset",
|
||||||
test_dataset.as_named_input(test_dataset.name)],
|
test_dataset.as_named_input(test_dataset.name),
|
||||||
|
],
|
||||||
compute_target=compute_target,
|
compute_target=compute_target,
|
||||||
environment=inference_env)
|
environment=inference_env,
|
||||||
|
)
|
||||||
|
|
||||||
run = test_experiment.submit(config,
|
run = test_experiment.submit(
|
||||||
tags={'training_run_id':
|
config,
|
||||||
train_run.id,
|
tags={
|
||||||
'run_algorithm':
|
"training_run_id": train_run.id,
|
||||||
train_run.properties['run_algorithm'],
|
"run_algorithm": train_run.properties["run_algorithm"],
|
||||||
'valid_score':
|
"valid_score": train_run.properties["score"],
|
||||||
train_run.properties['score'],
|
"primary_metric": train_run.properties["primary_metric"],
|
||||||
'primary_metric':
|
},
|
||||||
train_run.properties['primary_metric']})
|
)
|
||||||
|
|
||||||
run.log("run_algorithm", run.tags['run_algorithm'])
|
run.log("run_algorithm", run.tags["run_algorithm"])
|
||||||
return run
|
return run
|
||||||
|
|||||||
@@ -99,7 +99,7 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"print(\"This notebook was created using version 1.32.0 of the Azure ML SDK\")\n",
|
"print(\"This notebook was created using version 1.36.0 of the Azure ML SDK\")\n",
|
||||||
"print(\"You are currently using version\", azureml.core.VERSION, \"of the Azure ML SDK\")"
|
"print(\"You are currently using version\", azureml.core.VERSION, \"of the Azure ML SDK\")"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
@@ -119,7 +119,7 @@
|
|||||||
"ws = Workspace.from_config()\n",
|
"ws = Workspace.from_config()\n",
|
||||||
"\n",
|
"\n",
|
||||||
"# choose a name for the run history container in the workspace\n",
|
"# choose a name for the run history container in the workspace\n",
|
||||||
"experiment_name = 'automl-forecasting-energydemand'\n",
|
"experiment_name = \"automl-forecasting-energydemand\"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"# # project folder\n",
|
"# # project folder\n",
|
||||||
"# project_folder = './sample_projects/automl-forecasting-energy-demand'\n",
|
"# project_folder = './sample_projects/automl-forecasting-energy-demand'\n",
|
||||||
@@ -127,13 +127,13 @@
|
|||||||
"experiment = Experiment(ws, experiment_name)\n",
|
"experiment = Experiment(ws, experiment_name)\n",
|
||||||
"\n",
|
"\n",
|
||||||
"output = {}\n",
|
"output = {}\n",
|
||||||
"output['Subscription ID'] = ws.subscription_id\n",
|
"output[\"Subscription ID\"] = ws.subscription_id\n",
|
||||||
"output['Workspace'] = ws.name\n",
|
"output[\"Workspace\"] = ws.name\n",
|
||||||
"output['Resource Group'] = ws.resource_group\n",
|
"output[\"Resource Group\"] = ws.resource_group\n",
|
||||||
"output['Location'] = ws.location\n",
|
"output[\"Location\"] = ws.location\n",
|
||||||
"output['Run History Name'] = experiment_name\n",
|
"output[\"Run History Name\"] = experiment_name\n",
|
||||||
"pd.set_option('display.max_colwidth', -1)\n",
|
"pd.set_option(\"display.max_colwidth\", -1)\n",
|
||||||
"outputDf = pd.DataFrame(data = output, index = [''])\n",
|
"outputDf = pd.DataFrame(data=output, index=[\"\"])\n",
|
||||||
"outputDf.T"
|
"outputDf.T"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
@@ -166,10 +166,11 @@
|
|||||||
"# Verify that cluster does not exist already\n",
|
"# Verify that cluster does not exist already\n",
|
||||||
"try:\n",
|
"try:\n",
|
||||||
" compute_target = ComputeTarget(workspace=ws, name=amlcompute_cluster_name)\n",
|
" compute_target = ComputeTarget(workspace=ws, name=amlcompute_cluster_name)\n",
|
||||||
" print('Found existing cluster, use it.')\n",
|
" print(\"Found existing cluster, use it.\")\n",
|
||||||
"except ComputeTargetException:\n",
|
"except ComputeTargetException:\n",
|
||||||
" compute_config = AmlCompute.provisioning_configuration(vm_size='STANDARD_DS12_V2',\n",
|
" compute_config = AmlCompute.provisioning_configuration(\n",
|
||||||
" max_nodes=6)\n",
|
" vm_size=\"STANDARD_DS12_V2\", max_nodes=6\n",
|
||||||
|
" )\n",
|
||||||
" compute_target = ComputeTarget.create(ws, amlcompute_cluster_name, compute_config)\n",
|
" compute_target = ComputeTarget.create(ws, amlcompute_cluster_name, compute_config)\n",
|
||||||
"\n",
|
"\n",
|
||||||
"compute_target.wait_for_completion(show_output=True)"
|
"compute_target.wait_for_completion(show_output=True)"
|
||||||
@@ -204,8 +205,8 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"target_column_name = 'demand'\n",
|
"target_column_name = \"demand\"\n",
|
||||||
"time_column_name = 'timeStamp'"
|
"time_column_name = \"timeStamp\""
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -214,7 +215,9 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"dataset = Dataset.Tabular.from_delimited_files(path = \"https://automlsamplenotebookdata.blob.core.windows.net/automl-sample-notebook-data/nyc_energy.csv\").with_timestamp_columns(fine_grain_timestamp=time_column_name) \n",
|
"dataset = Dataset.Tabular.from_delimited_files(\n",
|
||||||
|
" path=\"https://automlsamplenotebookdata.blob.core.windows.net/automl-sample-notebook-data/nyc_energy.csv\"\n",
|
||||||
|
").with_timestamp_columns(fine_grain_timestamp=time_column_name)\n",
|
||||||
"dataset.take(5).to_pandas_dataframe().reset_index(drop=True)"
|
"dataset.take(5).to_pandas_dataframe().reset_index(drop=True)"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
@@ -343,23 +346,26 @@
|
|||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"from azureml.automl.core.forecasting_parameters import ForecastingParameters\n",
|
"from azureml.automl.core.forecasting_parameters import ForecastingParameters\n",
|
||||||
|
"\n",
|
||||||
"forecasting_parameters = ForecastingParameters(\n",
|
"forecasting_parameters = ForecastingParameters(\n",
|
||||||
" time_column_name=time_column_name,\n",
|
" time_column_name=time_column_name,\n",
|
||||||
" forecast_horizon=forecast_horizon,\n",
|
" forecast_horizon=forecast_horizon,\n",
|
||||||
" freq='H' # Set the forecast frequency to be hourly\n",
|
" freq=\"H\", # Set the forecast frequency to be hourly\n",
|
||||||
")\n",
|
")\n",
|
||||||
"\n",
|
"\n",
|
||||||
"automl_config = AutoMLConfig(task='forecasting', \n",
|
"automl_config = AutoMLConfig(\n",
|
||||||
" primary_metric='normalized_root_mean_squared_error',\n",
|
" task=\"forecasting\",\n",
|
||||||
" blocked_models = ['ExtremeRandomTrees', 'AutoArima', 'Prophet'], \n",
|
" primary_metric=\"normalized_root_mean_squared_error\",\n",
|
||||||
|
" blocked_models=[\"ExtremeRandomTrees\", \"AutoArima\", \"Prophet\"],\n",
|
||||||
" experiment_timeout_hours=0.3,\n",
|
" experiment_timeout_hours=0.3,\n",
|
||||||
" training_data=train,\n",
|
" training_data=train,\n",
|
||||||
" label_column_name=target_column_name,\n",
|
" label_column_name=target_column_name,\n",
|
||||||
" compute_target=compute_target,\n",
|
" compute_target=compute_target,\n",
|
||||||
" enable_early_stopping=True,\n",
|
" enable_early_stopping=True,\n",
|
||||||
" n_cross_validations=3, \n",
|
" n_cross_validations=3,\n",
|
||||||
" verbosity=logging.INFO,\n",
|
" verbosity=logging.INFO,\n",
|
||||||
" forecasting_parameters=forecasting_parameters)"
|
" forecasting_parameters=forecasting_parameters,\n",
|
||||||
|
")"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -420,7 +426,7 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"fitted_model.named_steps['timeseriestransformer'].get_engineered_feature_names()"
|
"fitted_model.named_steps[\"timeseriestransformer\"].get_engineered_feature_names()"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -444,7 +450,9 @@
|
|||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"# Get the featurization summary as a list of JSON\n",
|
"# Get the featurization summary as a list of JSON\n",
|
||||||
"featurization_summary = fitted_model.named_steps['timeseriestransformer'].get_featurization_summary()\n",
|
"featurization_summary = fitted_model.named_steps[\n",
|
||||||
|
" \"timeseriestransformer\"\n",
|
||||||
|
"].get_featurization_summary()\n",
|
||||||
"# View the featurization summary as a pandas dataframe\n",
|
"# View the featurization summary as a pandas dataframe\n",
|
||||||
"pd.DataFrame.from_records(featurization_summary)"
|
"pd.DataFrame.from_records(featurization_summary)"
|
||||||
]
|
]
|
||||||
@@ -484,15 +492,18 @@
|
|||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"from run_forecast import run_remote_inference\n",
|
"from run_forecast import run_remote_inference\n",
|
||||||
"remote_run_infer = run_remote_inference(test_experiment=test_experiment,\n",
|
"\n",
|
||||||
|
"remote_run_infer = run_remote_inference(\n",
|
||||||
|
" test_experiment=test_experiment,\n",
|
||||||
" compute_target=compute_target,\n",
|
" compute_target=compute_target,\n",
|
||||||
" train_run=best_run,\n",
|
" train_run=best_run,\n",
|
||||||
" test_dataset=test,\n",
|
" test_dataset=test,\n",
|
||||||
" target_column_name=target_column_name)\n",
|
" target_column_name=target_column_name,\n",
|
||||||
|
")\n",
|
||||||
"remote_run_infer.wait_for_completion(show_output=False)\n",
|
"remote_run_infer.wait_for_completion(show_output=False)\n",
|
||||||
"\n",
|
"\n",
|
||||||
"# download the inference output file to the local machine\n",
|
"# download the inference output file to the local machine\n",
|
||||||
"remote_run_infer.download_file('outputs/predictions.csv', 'predictions.csv')"
|
"remote_run_infer.download_file(\"outputs/predictions.csv\", \"predictions.csv\")"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -510,7 +521,7 @@
|
|||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"# load forecast data frame\n",
|
"# load forecast data frame\n",
|
||||||
"fcst_df = pd.read_csv('predictions.csv', parse_dates=[time_column_name])\n",
|
"fcst_df = pd.read_csv(\"predictions.csv\", parse_dates=[time_column_name])\n",
|
||||||
"fcst_df.head()"
|
"fcst_df.head()"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
@@ -527,18 +538,23 @@
|
|||||||
"# use automl metrics module\n",
|
"# use automl metrics module\n",
|
||||||
"scores = scoring.score_regression(\n",
|
"scores = scoring.score_regression(\n",
|
||||||
" y_test=fcst_df[target_column_name],\n",
|
" y_test=fcst_df[target_column_name],\n",
|
||||||
" y_pred=fcst_df['predicted'],\n",
|
" y_pred=fcst_df[\"predicted\"],\n",
|
||||||
" metrics=list(constants.Metric.SCALAR_REGRESSION_SET))\n",
|
" metrics=list(constants.Metric.SCALAR_REGRESSION_SET),\n",
|
||||||
|
")\n",
|
||||||
"\n",
|
"\n",
|
||||||
"print(\"[Test data scores]\\n\")\n",
|
"print(\"[Test data scores]\\n\")\n",
|
||||||
"for key, value in scores.items(): \n",
|
"for key, value in scores.items():\n",
|
||||||
" print('{}: {:.3f}'.format(key, value))\n",
|
" print(\"{}: {:.3f}\".format(key, value))\n",
|
||||||
" \n",
|
"\n",
|
||||||
"# Plot outputs\n",
|
"# Plot outputs\n",
|
||||||
"%matplotlib inline\n",
|
"%matplotlib inline\n",
|
||||||
"test_pred = plt.scatter(fcst_df[target_column_name], fcst_df['predicted'], color='b')\n",
|
"test_pred = plt.scatter(fcst_df[target_column_name], fcst_df[\"predicted\"], color=\"b\")\n",
|
||||||
"test_test = plt.scatter(fcst_df[target_column_name], fcst_df[target_column_name], color='g')\n",
|
"test_test = plt.scatter(\n",
|
||||||
"plt.legend((test_pred, test_test), ('prediction', 'truth'), loc='upper left', fontsize=8)\n",
|
" fcst_df[target_column_name], fcst_df[target_column_name], color=\"g\"\n",
|
||||||
|
")\n",
|
||||||
|
"plt.legend(\n",
|
||||||
|
" (test_pred, test_test), (\"prediction\", \"truth\"), loc=\"upper left\", fontsize=8\n",
|
||||||
|
")\n",
|
||||||
"plt.show()"
|
"plt.show()"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
@@ -567,21 +583,33 @@
|
|||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"advanced_forecasting_parameters = ForecastingParameters(\n",
|
"advanced_forecasting_parameters = ForecastingParameters(\n",
|
||||||
" time_column_name=time_column_name, forecast_horizon=forecast_horizon,\n",
|
" time_column_name=time_column_name,\n",
|
||||||
" target_lags=12, target_rolling_window_size=4\n",
|
" forecast_horizon=forecast_horizon,\n",
|
||||||
|
" target_lags=12,\n",
|
||||||
|
" target_rolling_window_size=4,\n",
|
||||||
")\n",
|
")\n",
|
||||||
"\n",
|
"\n",
|
||||||
"automl_config = AutoMLConfig(task='forecasting', \n",
|
"automl_config = AutoMLConfig(\n",
|
||||||
" primary_metric='normalized_root_mean_squared_error',\n",
|
" task=\"forecasting\",\n",
|
||||||
" blocked_models = ['ElasticNet','ExtremeRandomTrees','GradientBoosting','XGBoostRegressor','ExtremeRandomTrees', 'AutoArima', 'Prophet'], #These models are blocked for tutorial purposes, remove this for real use cases. \n",
|
" primary_metric=\"normalized_root_mean_squared_error\",\n",
|
||||||
|
" blocked_models=[\n",
|
||||||
|
" \"ElasticNet\",\n",
|
||||||
|
" \"ExtremeRandomTrees\",\n",
|
||||||
|
" \"GradientBoosting\",\n",
|
||||||
|
" \"XGBoostRegressor\",\n",
|
||||||
|
" \"ExtremeRandomTrees\",\n",
|
||||||
|
" \"AutoArima\",\n",
|
||||||
|
" \"Prophet\",\n",
|
||||||
|
" ], # These models are blocked for tutorial purposes, remove this for real use cases.\n",
|
||||||
" experiment_timeout_hours=0.3,\n",
|
" experiment_timeout_hours=0.3,\n",
|
||||||
" training_data=train,\n",
|
" training_data=train,\n",
|
||||||
" label_column_name=target_column_name,\n",
|
" label_column_name=target_column_name,\n",
|
||||||
" compute_target=compute_target,\n",
|
" compute_target=compute_target,\n",
|
||||||
" enable_early_stopping = True,\n",
|
" enable_early_stopping=True,\n",
|
||||||
" n_cross_validations=3, \n",
|
" n_cross_validations=3,\n",
|
||||||
" verbosity=logging.INFO,\n",
|
" verbosity=logging.INFO,\n",
|
||||||
" forecasting_parameters=advanced_forecasting_parameters)"
|
" forecasting_parameters=advanced_forecasting_parameters,\n",
|
||||||
|
")"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -640,16 +668,20 @@
|
|||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"test_experiment_advanced = Experiment(ws, experiment_name + \"_inference_advanced\")\n",
|
"test_experiment_advanced = Experiment(ws, experiment_name + \"_inference_advanced\")\n",
|
||||||
"advanced_remote_run_infer = run_remote_inference(test_experiment=test_experiment_advanced,\n",
|
"advanced_remote_run_infer = run_remote_inference(\n",
|
||||||
|
" test_experiment=test_experiment_advanced,\n",
|
||||||
" compute_target=compute_target,\n",
|
" compute_target=compute_target,\n",
|
||||||
" train_run=best_run_lags,\n",
|
" train_run=best_run_lags,\n",
|
||||||
" test_dataset=test,\n",
|
" test_dataset=test,\n",
|
||||||
" target_column_name=target_column_name,\n",
|
" target_column_name=target_column_name,\n",
|
||||||
" inference_folder='./forecast_advanced')\n",
|
" inference_folder=\"./forecast_advanced\",\n",
|
||||||
|
")\n",
|
||||||
"advanced_remote_run_infer.wait_for_completion(show_output=False)\n",
|
"advanced_remote_run_infer.wait_for_completion(show_output=False)\n",
|
||||||
"\n",
|
"\n",
|
||||||
"# download the inference output file to the local machine\n",
|
"# download the inference output file to the local machine\n",
|
||||||
"advanced_remote_run_infer.download_file('outputs/predictions.csv', 'predictions_advanced.csv')"
|
"advanced_remote_run_infer.download_file(\n",
|
||||||
|
" \"outputs/predictions.csv\", \"predictions_advanced.csv\"\n",
|
||||||
|
")"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -658,7 +690,7 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"fcst_adv_df = pd.read_csv('predictions_advanced.csv', parse_dates=[time_column_name])\n",
|
"fcst_adv_df = pd.read_csv(\"predictions_advanced.csv\", parse_dates=[time_column_name])\n",
|
||||||
"fcst_adv_df.head()"
|
"fcst_adv_df.head()"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
@@ -675,18 +707,25 @@
|
|||||||
"# use automl metrics module\n",
|
"# use automl metrics module\n",
|
||||||
"scores = scoring.score_regression(\n",
|
"scores = scoring.score_regression(\n",
|
||||||
" y_test=fcst_adv_df[target_column_name],\n",
|
" y_test=fcst_adv_df[target_column_name],\n",
|
||||||
" y_pred=fcst_adv_df['predicted'],\n",
|
" y_pred=fcst_adv_df[\"predicted\"],\n",
|
||||||
" metrics=list(constants.Metric.SCALAR_REGRESSION_SET))\n",
|
" metrics=list(constants.Metric.SCALAR_REGRESSION_SET),\n",
|
||||||
|
")\n",
|
||||||
"\n",
|
"\n",
|
||||||
"print(\"[Test data scores]\\n\")\n",
|
"print(\"[Test data scores]\\n\")\n",
|
||||||
"for key, value in scores.items(): \n",
|
"for key, value in scores.items():\n",
|
||||||
" print('{}: {:.3f}'.format(key, value))\n",
|
" print(\"{}: {:.3f}\".format(key, value))\n",
|
||||||
" \n",
|
"\n",
|
||||||
"# Plot outputs\n",
|
"# Plot outputs\n",
|
||||||
"%matplotlib inline\n",
|
"%matplotlib inline\n",
|
||||||
"test_pred = plt.scatter(fcst_adv_df[target_column_name], fcst_adv_df['predicted'], color='b')\n",
|
"test_pred = plt.scatter(\n",
|
||||||
"test_test = plt.scatter(fcst_adv_df[target_column_name], fcst_adv_df[target_column_name], color='g')\n",
|
" fcst_adv_df[target_column_name], fcst_adv_df[\"predicted\"], color=\"b\"\n",
|
||||||
"plt.legend((test_pred, test_test), ('prediction', 'truth'), loc='upper left', fontsize=8)\n",
|
")\n",
|
||||||
|
"test_test = plt.scatter(\n",
|
||||||
|
" fcst_adv_df[target_column_name], fcst_adv_df[target_column_name], color=\"g\"\n",
|
||||||
|
")\n",
|
||||||
|
"plt.legend(\n",
|
||||||
|
" (test_pred, test_test), (\"prediction\", \"truth\"), loc=\"upper left\", fontsize=8\n",
|
||||||
|
")\n",
|
||||||
"plt.show()"
|
"plt.show()"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,62 +5,20 @@ compute instance.
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
import pandas as pd
|
|
||||||
import numpy as np
|
|
||||||
from azureml.core import Dataset, Run
|
from azureml.core import Dataset, Run
|
||||||
from azureml.automl.core.shared.constants import TimeSeriesInternal
|
|
||||||
from sklearn.externals import joblib
|
from sklearn.externals import joblib
|
||||||
from pandas.tseries.frequencies import to_offset
|
from pandas.tseries.frequencies import to_offset
|
||||||
|
|
||||||
|
|
||||||
def align_outputs(y_predicted, X_trans, X_test, y_test, target_column_name,
|
|
||||||
predicted_column_name='predicted',
|
|
||||||
horizon_colname='horizon_origin'):
|
|
||||||
"""
|
|
||||||
Demonstrates how to get the output aligned to the inputs
|
|
||||||
using pandas indexes. Helps understand what happened if
|
|
||||||
the output's shape differs from the input shape, or if
|
|
||||||
the data got re-sorted by time and grain during forecasting.
|
|
||||||
|
|
||||||
Typical causes of misalignment are:
|
|
||||||
* we predicted some periods that were missing in actuals -> drop from eval
|
|
||||||
* model was asked to predict past max_horizon -> increase max horizon
|
|
||||||
* data at start of X_test was needed for lags -> provide previous periods
|
|
||||||
"""
|
|
||||||
|
|
||||||
if (horizon_colname in X_trans):
|
|
||||||
df_fcst = pd.DataFrame({predicted_column_name: y_predicted,
|
|
||||||
horizon_colname: X_trans[horizon_colname]})
|
|
||||||
else:
|
|
||||||
df_fcst = pd.DataFrame({predicted_column_name: y_predicted})
|
|
||||||
|
|
||||||
# y and X outputs are aligned by forecast() function contract
|
|
||||||
df_fcst.index = X_trans.index
|
|
||||||
|
|
||||||
# align original X_test to y_test
|
|
||||||
X_test_full = X_test.copy()
|
|
||||||
X_test_full[target_column_name] = y_test
|
|
||||||
|
|
||||||
# X_test_full's index does not include origin, so reset for merge
|
|
||||||
df_fcst.reset_index(inplace=True)
|
|
||||||
X_test_full = X_test_full.reset_index().drop(columns='index')
|
|
||||||
together = df_fcst.merge(X_test_full, how='right')
|
|
||||||
|
|
||||||
# drop rows where prediction or actuals are nan
|
|
||||||
# happens because of missing actuals
|
|
||||||
# or at edges of time due to lags/rolling windows
|
|
||||||
clean = together[together[[target_column_name,
|
|
||||||
predicted_column_name]].notnull().all(axis=1)]
|
|
||||||
return(clean)
|
|
||||||
|
|
||||||
|
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
'--target_column_name', type=str, dest='target_column_name',
|
"--target_column_name",
|
||||||
help='Target Column Name')
|
type=str,
|
||||||
|
dest="target_column_name",
|
||||||
|
help="Target Column Name",
|
||||||
|
)
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
'--test_dataset', type=str, dest='test_dataset',
|
"--test_dataset", type=str, dest="test_dataset", help="Test Dataset"
|
||||||
help='Test Dataset')
|
)
|
||||||
|
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
target_column_name = args.target_column_name
|
target_column_name = args.target_column_name
|
||||||
@@ -76,14 +34,28 @@ X_test = test_dataset.to_pandas_dataframe().reset_index(drop=True)
|
|||||||
y_test = X_test.pop(target_column_name).values
|
y_test = X_test.pop(target_column_name).values
|
||||||
|
|
||||||
# generate forecast
|
# generate forecast
|
||||||
fitted_model = joblib.load('model.pkl')
|
fitted_model = joblib.load("model.pkl")
|
||||||
y_predictions, X_trans = fitted_model.forecast(X_test)
|
# We have default quantiles values set as below(95th percentile)
|
||||||
|
quantiles = [0.025, 0.5, 0.975]
|
||||||
|
predicted_column_name = "predicted"
|
||||||
|
PI = "prediction_interval"
|
||||||
|
fitted_model.quantiles = quantiles
|
||||||
|
pred_quantiles = fitted_model.forecast_quantiles(X_test)
|
||||||
|
pred_quantiles[PI] = pred_quantiles[[min(quantiles), max(quantiles)]].apply(
|
||||||
|
lambda x: "[{}, {}]".format(x[0], x[1]), axis=1
|
||||||
|
)
|
||||||
|
X_test[target_column_name] = y_test
|
||||||
|
X_test[PI] = pred_quantiles[PI]
|
||||||
|
X_test[predicted_column_name] = pred_quantiles[0.5]
|
||||||
|
# drop rows where prediction or actuals are nan
|
||||||
|
# happens because of missing actuals
|
||||||
|
# or at edges of time due to lags/rolling windows
|
||||||
|
clean = X_test[
|
||||||
|
X_test[[target_column_name, predicted_column_name]].notnull().all(axis=1)
|
||||||
|
]
|
||||||
|
|
||||||
# align output
|
file_name = "outputs/predictions.csv"
|
||||||
df_all = align_outputs(y_predictions, X_trans, X_test, y_test, target_column_name)
|
export_csv = clean.to_csv(file_name, header=True, index=False) # added Index
|
||||||
|
|
||||||
file_name = 'outputs/predictions.csv'
|
|
||||||
export_csv = df_all.to_csv(file_name, header=True, index=False) # added Index
|
|
||||||
|
|
||||||
# Upload the predictions into artifacts
|
# Upload the predictions into artifacts
|
||||||
run.upload_file(name=file_name, path_or_stream=file_name)
|
run.upload_file(name=file_name, path_or_stream=file_name)
|
||||||
|
|||||||
@@ -3,36 +3,47 @@ import shutil
|
|||||||
from azureml.core import ScriptRunConfig
|
from azureml.core import ScriptRunConfig
|
||||||
|
|
||||||
|
|
||||||
def run_remote_inference(test_experiment, compute_target, train_run,
|
def run_remote_inference(
|
||||||
test_dataset, target_column_name, inference_folder='./forecast'):
|
test_experiment,
|
||||||
|
compute_target,
|
||||||
|
train_run,
|
||||||
|
test_dataset,
|
||||||
|
target_column_name,
|
||||||
|
inference_folder="./forecast",
|
||||||
|
):
|
||||||
# Create local directory to copy the model.pkl and forecsting_script.py files into.
|
# Create local directory to copy the model.pkl and forecsting_script.py files into.
|
||||||
# These files will be uploaded to and executed on the compute instance.
|
# These files will be uploaded to and executed on the compute instance.
|
||||||
os.makedirs(inference_folder, exist_ok=True)
|
os.makedirs(inference_folder, exist_ok=True)
|
||||||
shutil.copy('forecasting_script.py', inference_folder)
|
shutil.copy("forecasting_script.py", inference_folder)
|
||||||
|
|
||||||
train_run.download_file('outputs/model.pkl',
|
train_run.download_file(
|
||||||
os.path.join(inference_folder, 'model.pkl'))
|
"outputs/model.pkl", os.path.join(inference_folder, "model.pkl")
|
||||||
|
)
|
||||||
|
|
||||||
inference_env = train_run.get_environment()
|
inference_env = train_run.get_environment()
|
||||||
|
|
||||||
config = ScriptRunConfig(source_directory=inference_folder,
|
config = ScriptRunConfig(
|
||||||
script='forecasting_script.py',
|
source_directory=inference_folder,
|
||||||
arguments=['--target_column_name',
|
script="forecasting_script.py",
|
||||||
|
arguments=[
|
||||||
|
"--target_column_name",
|
||||||
target_column_name,
|
target_column_name,
|
||||||
'--test_dataset',
|
"--test_dataset",
|
||||||
test_dataset.as_named_input(test_dataset.name)],
|
test_dataset.as_named_input(test_dataset.name),
|
||||||
|
],
|
||||||
compute_target=compute_target,
|
compute_target=compute_target,
|
||||||
environment=inference_env)
|
environment=inference_env,
|
||||||
|
)
|
||||||
|
|
||||||
run = test_experiment.submit(config,
|
run = test_experiment.submit(
|
||||||
tags={'training_run_id':
|
config,
|
||||||
train_run.id,
|
tags={
|
||||||
'run_algorithm':
|
"training_run_id": train_run.id,
|
||||||
train_run.properties['run_algorithm'],
|
"run_algorithm": train_run.properties["run_algorithm"],
|
||||||
'valid_score':
|
"valid_score": train_run.properties["score"],
|
||||||
train_run.properties['score'],
|
"primary_metric": train_run.properties["primary_metric"],
|
||||||
'primary_metric':
|
},
|
||||||
train_run.properties['primary_metric']})
|
)
|
||||||
|
|
||||||
run.log("run_algorithm", run.tags['run_algorithm'])
|
run.log("run_algorithm", run.tags["run_algorithm"])
|
||||||
return run
|
return run
|
||||||
|
|||||||
@@ -94,7 +94,7 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"print(\"This notebook was created using version 1.32.0 of the Azure ML SDK\")\n",
|
"print(\"This notebook was created using version 1.36.0 of the Azure ML SDK\")\n",
|
||||||
"print(\"You are currently using version\", azureml.core.VERSION, \"of the Azure ML SDK\")"
|
"print(\"You are currently using version\", azureml.core.VERSION, \"of the Azure ML SDK\")"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
@@ -111,19 +111,19 @@
|
|||||||
"ws = Workspace.from_config()\n",
|
"ws = Workspace.from_config()\n",
|
||||||
"\n",
|
"\n",
|
||||||
"# choose a name for the run history container in the workspace\n",
|
"# choose a name for the run history container in the workspace\n",
|
||||||
"experiment_name = 'automl-forecast-function-demo'\n",
|
"experiment_name = \"automl-forecast-function-demo\"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"experiment = Experiment(ws, experiment_name)\n",
|
"experiment = Experiment(ws, experiment_name)\n",
|
||||||
"\n",
|
"\n",
|
||||||
"output = {}\n",
|
"output = {}\n",
|
||||||
"output['Subscription ID'] = ws.subscription_id\n",
|
"output[\"Subscription ID\"] = ws.subscription_id\n",
|
||||||
"output['Workspace'] = ws.name\n",
|
"output[\"Workspace\"] = ws.name\n",
|
||||||
"output['SKU'] = ws.sku\n",
|
"output[\"SKU\"] = ws.sku\n",
|
||||||
"output['Resource Group'] = ws.resource_group\n",
|
"output[\"Resource Group\"] = ws.resource_group\n",
|
||||||
"output['Location'] = ws.location\n",
|
"output[\"Location\"] = ws.location\n",
|
||||||
"output['Run History Name'] = experiment_name\n",
|
"output[\"Run History Name\"] = experiment_name\n",
|
||||||
"pd.set_option('display.max_colwidth', -1)\n",
|
"pd.set_option(\"display.max_colwidth\", -1)\n",
|
||||||
"outputDf = pd.DataFrame(data = output, index = [''])\n",
|
"outputDf = pd.DataFrame(data=output, index=[\"\"])\n",
|
||||||
"outputDf.T"
|
"outputDf.T"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
@@ -141,17 +141,20 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"TIME_COLUMN_NAME = 'date'\n",
|
"TIME_COLUMN_NAME = \"date\"\n",
|
||||||
"TIME_SERIES_ID_COLUMN_NAME = 'time_series_id'\n",
|
"TIME_SERIES_ID_COLUMN_NAME = \"time_series_id\"\n",
|
||||||
"TARGET_COLUMN_NAME = 'y'\n",
|
"TARGET_COLUMN_NAME = \"y\"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"def get_timeseries(train_len: int,\n",
|
"\n",
|
||||||
|
"def get_timeseries(\n",
|
||||||
|
" train_len: int,\n",
|
||||||
" test_len: int,\n",
|
" test_len: int,\n",
|
||||||
" time_column_name: str,\n",
|
" time_column_name: str,\n",
|
||||||
" target_column_name: str,\n",
|
" target_column_name: str,\n",
|
||||||
" time_series_id_column_name: str,\n",
|
" time_series_id_column_name: str,\n",
|
||||||
" time_series_number: int = 1,\n",
|
" time_series_number: int = 1,\n",
|
||||||
" freq: str = 'H'):\n",
|
" freq: str = \"H\",\n",
|
||||||
|
"):\n",
|
||||||
" \"\"\"\n",
|
" \"\"\"\n",
|
||||||
" Return the time series of designed length.\n",
|
" Return the time series of designed length.\n",
|
||||||
"\n",
|
"\n",
|
||||||
@@ -174,14 +177,18 @@
|
|||||||
" data_test = [] # type: List[pd.DataFrame]\n",
|
" data_test = [] # type: List[pd.DataFrame]\n",
|
||||||
" data_length = train_len + test_len\n",
|
" data_length = train_len + test_len\n",
|
||||||
" for i in range(time_series_number):\n",
|
" for i in range(time_series_number):\n",
|
||||||
" X = pd.DataFrame({\n",
|
" X = pd.DataFrame(\n",
|
||||||
" time_column_name: pd.date_range(start='2000-01-01',\n",
|
" {\n",
|
||||||
" periods=data_length,\n",
|
" time_column_name: pd.date_range(\n",
|
||||||
" freq=freq),\n",
|
" start=\"2000-01-01\", periods=data_length, freq=freq\n",
|
||||||
" target_column_name: np.arange(data_length).astype(float) + np.random.rand(data_length) + i*5,\n",
|
" ),\n",
|
||||||
" 'ext_predictor': np.asarray(range(42, 42 + data_length)),\n",
|
" target_column_name: np.arange(data_length).astype(float)\n",
|
||||||
" time_series_id_column_name: np.repeat('ts{}'.format(i), data_length)\n",
|
" + np.random.rand(data_length)\n",
|
||||||
" })\n",
|
" + i * 5,\n",
|
||||||
|
" \"ext_predictor\": np.asarray(range(42, 42 + data_length)),\n",
|
||||||
|
" time_series_id_column_name: np.repeat(\"ts{}\".format(i), data_length),\n",
|
||||||
|
" }\n",
|
||||||
|
" )\n",
|
||||||
" data_train.append(X[:train_len])\n",
|
" data_train.append(X[:train_len])\n",
|
||||||
" data_test.append(X[train_len:])\n",
|
" data_test.append(X[train_len:])\n",
|
||||||
" X_train = pd.concat(data_train)\n",
|
" X_train = pd.concat(data_train)\n",
|
||||||
@@ -190,14 +197,17 @@
|
|||||||
" y_test = X_test.pop(target_column_name).values\n",
|
" y_test = X_test.pop(target_column_name).values\n",
|
||||||
" return X_train, y_train, X_test, y_test\n",
|
" return X_train, y_train, X_test, y_test\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
"\n",
|
||||||
"n_test_periods = 6\n",
|
"n_test_periods = 6\n",
|
||||||
"n_train_periods = 30\n",
|
"n_train_periods = 30\n",
|
||||||
"X_train, y_train, X_test, y_test = get_timeseries(train_len=n_train_periods,\n",
|
"X_train, y_train, X_test, y_test = get_timeseries(\n",
|
||||||
|
" train_len=n_train_periods,\n",
|
||||||
" test_len=n_test_periods,\n",
|
" test_len=n_test_periods,\n",
|
||||||
" time_column_name=TIME_COLUMN_NAME,\n",
|
" time_column_name=TIME_COLUMN_NAME,\n",
|
||||||
" target_column_name=TARGET_COLUMN_NAME,\n",
|
" target_column_name=TARGET_COLUMN_NAME,\n",
|
||||||
" time_series_id_column_name=TIME_SERIES_ID_COLUMN_NAME,\n",
|
" time_series_id_column_name=TIME_SERIES_ID_COLUMN_NAME,\n",
|
||||||
" time_series_number=2)"
|
" time_series_number=2,\n",
|
||||||
|
")"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -224,11 +234,12 @@
|
|||||||
"source": [
|
"source": [
|
||||||
"# plot the example time series\n",
|
"# plot the example time series\n",
|
||||||
"import matplotlib.pyplot as plt\n",
|
"import matplotlib.pyplot as plt\n",
|
||||||
|
"\n",
|
||||||
"whole_data = X_train.copy()\n",
|
"whole_data = X_train.copy()\n",
|
||||||
"target_label = 'y'\n",
|
"target_label = \"y\"\n",
|
||||||
"whole_data[target_label] = y_train\n",
|
"whole_data[target_label] = y_train\n",
|
||||||
"for g in whole_data.groupby('time_series_id'): \n",
|
"for g in whole_data.groupby(\"time_series_id\"):\n",
|
||||||
" plt.plot(g[1]['date'].values, g[1]['y'].values, label=g[0])\n",
|
" plt.plot(g[1][\"date\"].values, g[1][\"y\"].values, label=g[0])\n",
|
||||||
"plt.legend()\n",
|
"plt.legend()\n",
|
||||||
"plt.show()"
|
"plt.show()"
|
||||||
]
|
]
|
||||||
@@ -250,12 +261,12 @@
|
|||||||
"# We need to save thw artificial data and then upload them to default workspace datastore.\n",
|
"# We need to save thw artificial data and then upload them to default workspace datastore.\n",
|
||||||
"DATA_PATH = \"fc_fn_data\"\n",
|
"DATA_PATH = \"fc_fn_data\"\n",
|
||||||
"DATA_PATH_X = \"{}/data_train.csv\".format(DATA_PATH)\n",
|
"DATA_PATH_X = \"{}/data_train.csv\".format(DATA_PATH)\n",
|
||||||
"if not os.path.isdir('data'):\n",
|
"if not os.path.isdir(\"data\"):\n",
|
||||||
" os.mkdir('data')\n",
|
" os.mkdir(\"data\")\n",
|
||||||
"pd.DataFrame(whole_data).to_csv(\"data/data_train.csv\", index=False)\n",
|
"pd.DataFrame(whole_data).to_csv(\"data/data_train.csv\", index=False)\n",
|
||||||
"# Upload saved data to the default data store.\n",
|
"# Upload saved data to the default data store.\n",
|
||||||
"ds = ws.get_default_datastore()\n",
|
"ds = ws.get_default_datastore()\n",
|
||||||
"ds.upload(src_dir='./data', target_path=DATA_PATH, overwrite=True, show_progress=True)\n",
|
"ds.upload(src_dir=\"./data\", target_path=DATA_PATH, overwrite=True, show_progress=True)\n",
|
||||||
"train_data = Dataset.Tabular.from_delimited_files(path=ds.path(DATA_PATH_X))"
|
"train_data = Dataset.Tabular.from_delimited_files(path=ds.path(DATA_PATH_X))"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
@@ -283,10 +294,11 @@
|
|||||||
"# Verify that cluster does not exist already\n",
|
"# Verify that cluster does not exist already\n",
|
||||||
"try:\n",
|
"try:\n",
|
||||||
" compute_target = ComputeTarget(workspace=ws, name=amlcompute_cluster_name)\n",
|
" compute_target = ComputeTarget(workspace=ws, name=amlcompute_cluster_name)\n",
|
||||||
" print('Found existing cluster, use it.')\n",
|
" print(\"Found existing cluster, use it.\")\n",
|
||||||
"except ComputeTargetException:\n",
|
"except ComputeTargetException:\n",
|
||||||
" compute_config = AmlCompute.provisioning_configuration(vm_size='STANDARD_DS12_V2',\n",
|
" compute_config = AmlCompute.provisioning_configuration(\n",
|
||||||
" max_nodes=6)\n",
|
" vm_size=\"STANDARD_DS12_V2\", max_nodes=6\n",
|
||||||
|
" )\n",
|
||||||
" compute_target = ComputeTarget.create(ws, amlcompute_cluster_name, compute_config)\n",
|
" compute_target = ComputeTarget.create(ws, amlcompute_cluster_name, compute_config)\n",
|
||||||
"\n",
|
"\n",
|
||||||
"compute_target.wait_for_completion(show_output=True)"
|
"compute_target.wait_for_completion(show_output=True)"
|
||||||
@@ -315,14 +327,15 @@
|
|||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"from azureml.automl.core.forecasting_parameters import ForecastingParameters\n",
|
"from azureml.automl.core.forecasting_parameters import ForecastingParameters\n",
|
||||||
"lags = [1,2,3]\n",
|
"\n",
|
||||||
|
"lags = [1, 2, 3]\n",
|
||||||
"forecast_horizon = n_test_periods\n",
|
"forecast_horizon = n_test_periods\n",
|
||||||
"forecasting_parameters = ForecastingParameters(\n",
|
"forecasting_parameters = ForecastingParameters(\n",
|
||||||
" time_column_name=TIME_COLUMN_NAME,\n",
|
" time_column_name=TIME_COLUMN_NAME,\n",
|
||||||
" forecast_horizon=forecast_horizon,\n",
|
" forecast_horizon=forecast_horizon,\n",
|
||||||
" time_series_id_column_names=[ TIME_SERIES_ID_COLUMN_NAME ],\n",
|
" time_series_id_column_names=[TIME_SERIES_ID_COLUMN_NAME],\n",
|
||||||
" target_lags=lags,\n",
|
" target_lags=lags,\n",
|
||||||
" freq='H' # Set the forecast frequency to be hourly\n",
|
" freq=\"H\", # Set the forecast frequency to be hourly\n",
|
||||||
")"
|
")"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
@@ -344,19 +357,21 @@
|
|||||||
"from azureml.train.automl import AutoMLConfig\n",
|
"from azureml.train.automl import AutoMLConfig\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"automl_config = AutoMLConfig(task='forecasting',\n",
|
"automl_config = AutoMLConfig(\n",
|
||||||
" debug_log='automl_forecasting_function.log',\n",
|
" task=\"forecasting\",\n",
|
||||||
" primary_metric='normalized_root_mean_squared_error',\n",
|
" debug_log=\"automl_forecasting_function.log\",\n",
|
||||||
|
" primary_metric=\"normalized_root_mean_squared_error\",\n",
|
||||||
" experiment_timeout_hours=0.25,\n",
|
" experiment_timeout_hours=0.25,\n",
|
||||||
" enable_early_stopping=True,\n",
|
" enable_early_stopping=True,\n",
|
||||||
" training_data=train_data,\n",
|
" training_data=train_data,\n",
|
||||||
" compute_target=compute_target,\n",
|
" compute_target=compute_target,\n",
|
||||||
" n_cross_validations=3,\n",
|
" n_cross_validations=3,\n",
|
||||||
" verbosity = logging.INFO,\n",
|
" verbosity=logging.INFO,\n",
|
||||||
" max_concurrent_iterations=4,\n",
|
" max_concurrent_iterations=4,\n",
|
||||||
" max_cores_per_iteration=-1,\n",
|
" max_cores_per_iteration=-1,\n",
|
||||||
" label_column_name=target_label,\n",
|
" label_column_name=target_label,\n",
|
||||||
" forecasting_parameters=forecasting_parameters)\n",
|
" forecasting_parameters=forecasting_parameters,\n",
|
||||||
|
")\n",
|
||||||
"\n",
|
"\n",
|
||||||
"remote_run = experiment.submit(automl_config, show_output=False)"
|
"remote_run = experiment.submit(automl_config, show_output=False)"
|
||||||
]
|
]
|
||||||
@@ -481,12 +496,12 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"# specify which quantiles you would like \n",
|
"# specify which quantiles you would like\n",
|
||||||
"fitted_model.quantiles = [0.01, 0.5, 0.95]\n",
|
"fitted_model.quantiles = [0.01, 0.5, 0.95]\n",
|
||||||
"# use forecast_quantiles function, not the forecast() one\n",
|
"# use forecast_quantiles function, not the forecast() one\n",
|
||||||
"y_pred_quantiles = fitted_model.forecast_quantiles(X_test)\n",
|
"y_pred_quantiles = fitted_model.forecast_quantiles(X_test)\n",
|
||||||
"\n",
|
"\n",
|
||||||
"# quantile forecasts returned in a Dataframe along with the time and time series id columns \n",
|
"# quantile forecasts returned in a Dataframe along with the time and time series id columns\n",
|
||||||
"y_pred_quantiles"
|
"y_pred_quantiles"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
@@ -534,14 +549,16 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"# generate the same kind of test data we trained on, \n",
|
"# generate the same kind of test data we trained on,\n",
|
||||||
"# but now make the train set much longer, so that the test set will be in the future\n",
|
"# but now make the train set much longer, so that the test set will be in the future\n",
|
||||||
"X_context, y_context, X_away, y_away = get_timeseries(train_len=42, # train data was 30 steps long\n",
|
"X_context, y_context, X_away, y_away = get_timeseries(\n",
|
||||||
|
" train_len=42, # train data was 30 steps long\n",
|
||||||
" test_len=4,\n",
|
" test_len=4,\n",
|
||||||
" time_column_name=TIME_COLUMN_NAME,\n",
|
" time_column_name=TIME_COLUMN_NAME,\n",
|
||||||
" target_column_name=TARGET_COLUMN_NAME,\n",
|
" target_column_name=TARGET_COLUMN_NAME,\n",
|
||||||
" time_series_id_column_name=TIME_SERIES_ID_COLUMN_NAME,\n",
|
" time_series_id_column_name=TIME_SERIES_ID_COLUMN_NAME,\n",
|
||||||
" time_series_number=2)\n",
|
" time_series_number=2,\n",
|
||||||
|
")\n",
|
||||||
"\n",
|
"\n",
|
||||||
"# end of the data we trained on\n",
|
"# end of the data we trained on\n",
|
||||||
"print(X_train.groupby(TIME_SERIES_ID_COLUMN_NAME)[TIME_COLUMN_NAME].max())\n",
|
"print(X_train.groupby(TIME_SERIES_ID_COLUMN_NAME)[TIME_COLUMN_NAME].max())\n",
|
||||||
@@ -562,7 +579,7 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"try: \n",
|
"try:\n",
|
||||||
" y_pred_away, xy_away = fitted_model.forecast(X_away)\n",
|
" y_pred_away, xy_away = fitted_model.forecast(X_away)\n",
|
||||||
" xy_away\n",
|
" xy_away\n",
|
||||||
"except Exception as e:\n",
|
"except Exception as e:\n",
|
||||||
@@ -584,7 +601,9 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"def make_forecasting_query(fulldata, time_column_name, target_column_name, forecast_origin, horizon, lookback):\n",
|
"def make_forecasting_query(\n",
|
||||||
|
" fulldata, time_column_name, target_column_name, forecast_origin, horizon, lookback\n",
|
||||||
|
"):\n",
|
||||||
"\n",
|
"\n",
|
||||||
" \"\"\"\n",
|
" \"\"\"\n",
|
||||||
" This function will take the full dataset, and create the query\n",
|
" This function will take the full dataset, and create the query\n",
|
||||||
@@ -592,24 +611,24 @@
|
|||||||
" forward for the next `horizon` horizons. Context from previous\n",
|
" forward for the next `horizon` horizons. Context from previous\n",
|
||||||
" `lookback` periods will be included.\n",
|
" `lookback` periods will be included.\n",
|
||||||
"\n",
|
"\n",
|
||||||
" \n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
" fulldata: pandas.DataFrame a time series dataset. Needs to contain X and y.\n",
|
" fulldata: pandas.DataFrame a time series dataset. Needs to contain X and y.\n",
|
||||||
" time_column_name: string which column (must be in fulldata) is the time axis\n",
|
" time_column_name: string which column (must be in fulldata) is the time axis\n",
|
||||||
" target_column_name: string which column (must be in fulldata) is to be forecast\n",
|
" target_column_name: string which column (must be in fulldata) is to be forecast\n",
|
||||||
" forecast_origin: datetime type the last time we (pretend to) have target values \n",
|
" forecast_origin: datetime type the last time we (pretend to) have target values\n",
|
||||||
" horizon: timedelta how far forward, in time units (not periods)\n",
|
" horizon: timedelta how far forward, in time units (not periods)\n",
|
||||||
" lookback: timedelta how far back does the model look?\n",
|
" lookback: timedelta how far back does the model look\n",
|
||||||
"\n",
|
"\n",
|
||||||
" Example:\n",
|
" Example:\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
" ```\n",
|
" ```\n",
|
||||||
"\n",
|
"\n",
|
||||||
" forecast_origin = pd.to_datetime('2012-09-01') + pd.DateOffset(days=5) # forecast 5 days after end of training\n",
|
" forecast_origin = pd.to_datetime(\"2012-09-01\") + pd.DateOffset(days=5) # forecast 5 days after end of training\n",
|
||||||
" print(forecast_origin)\n",
|
" print(forecast_origin)\n",
|
||||||
"\n",
|
"\n",
|
||||||
" X_query, y_query = make_forecasting_query(data, \n",
|
" X_query, y_query = make_forecasting_query(data,\n",
|
||||||
" forecast_origin = forecast_origin,\n",
|
" forecast_origin = forecast_origin,\n",
|
||||||
" horizon = pd.DateOffset(days=7), # 7 days into the future\n",
|
" horizon = pd.DateOffset(days=7), # 7 days into the future\n",
|
||||||
" lookback = pd.DateOffset(days=1), # model has lag 1 period (day)\n",
|
" lookback = pd.DateOffset(days=1), # model has lag 1 period (day)\n",
|
||||||
@@ -618,28 +637,30 @@
|
|||||||
" ```\n",
|
" ```\n",
|
||||||
" \"\"\"\n",
|
" \"\"\"\n",
|
||||||
"\n",
|
"\n",
|
||||||
" X_past = fulldata[ (fulldata[ time_column_name ] > forecast_origin - lookback) &\n",
|
" X_past = fulldata[\n",
|
||||||
" (fulldata[ time_column_name ] <= forecast_origin)\n",
|
" (fulldata[time_column_name] > forecast_origin - lookback)\n",
|
||||||
|
" & (fulldata[time_column_name] <= forecast_origin)\n",
|
||||||
" ]\n",
|
" ]\n",
|
||||||
"\n",
|
"\n",
|
||||||
" X_future = fulldata[ (fulldata[ time_column_name ] > forecast_origin) &\n",
|
" X_future = fulldata[\n",
|
||||||
" (fulldata[ time_column_name ] <= forecast_origin + horizon)\n",
|
" (fulldata[time_column_name] > forecast_origin)\n",
|
||||||
|
" & (fulldata[time_column_name] <= forecast_origin + horizon)\n",
|
||||||
" ]\n",
|
" ]\n",
|
||||||
"\n",
|
"\n",
|
||||||
" y_past = X_past.pop(target_column_name).values.astype(np.float)\n",
|
" y_past = X_past.pop(target_column_name).values.astype(np.float)\n",
|
||||||
" y_future = X_future.pop(target_column_name).values.astype(np.float)\n",
|
" y_future = X_future.pop(target_column_name).values.astype(np.float)\n",
|
||||||
"\n",
|
"\n",
|
||||||
" # Now take y_future and turn it into question marks\n",
|
" # Now take y_future and turn it into question marks\n",
|
||||||
" y_query = y_future.copy().astype(np.float) # because sometimes life hands you an int\n",
|
" y_query = y_future.copy().astype(\n",
|
||||||
|
" np.float\n",
|
||||||
|
" ) # because sometimes life hands you an int\n",
|
||||||
" y_query.fill(np.NaN)\n",
|
" y_query.fill(np.NaN)\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
|
||||||
" print(\"X_past is \" + str(X_past.shape) + \" - shaped\")\n",
|
" print(\"X_past is \" + str(X_past.shape) + \" - shaped\")\n",
|
||||||
" print(\"X_future is \" + str(X_future.shape) + \" - shaped\")\n",
|
" print(\"X_future is \" + str(X_future.shape) + \" - shaped\")\n",
|
||||||
" print(\"y_past is \" + str(y_past.shape) + \" - shaped\")\n",
|
" print(\"y_past is \" + str(y_past.shape) + \" - shaped\")\n",
|
||||||
" print(\"y_query is \" + str(y_query.shape) + \" - shaped\")\n",
|
" print(\"y_query is \" + str(y_query.shape) + \" - shaped\")\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
|
||||||
" X_pred = pd.concat([X_past, X_future])\n",
|
" X_pred = pd.concat([X_past, X_future])\n",
|
||||||
" y_pred = np.concatenate([y_past, y_query])\n",
|
" y_pred = np.concatenate([y_past, y_query])\n",
|
||||||
" return X_pred, y_pred"
|
" return X_pred, y_pred"
|
||||||
@@ -658,8 +679,16 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"print(X_context.groupby(TIME_SERIES_ID_COLUMN_NAME)[TIME_COLUMN_NAME].agg(['min','max','count']))\n",
|
"print(\n",
|
||||||
"print(X_away.groupby(TIME_SERIES_ID_COLUMN_NAME)[TIME_COLUMN_NAME].agg(['min','max','count']))\n",
|
" X_context.groupby(TIME_SERIES_ID_COLUMN_NAME)[TIME_COLUMN_NAME].agg(\n",
|
||||||
|
" [\"min\", \"max\", \"count\"]\n",
|
||||||
|
" )\n",
|
||||||
|
")\n",
|
||||||
|
"print(\n",
|
||||||
|
" X_away.groupby(TIME_SERIES_ID_COLUMN_NAME)[TIME_COLUMN_NAME].agg(\n",
|
||||||
|
" [\"min\", \"max\", \"count\"]\n",
|
||||||
|
" )\n",
|
||||||
|
")\n",
|
||||||
"X_context.tail(5)"
|
"X_context.tail(5)"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
@@ -669,11 +698,11 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"# Since the length of the lookback is 3, \n",
|
"# Since the length of the lookback is 3,\n",
|
||||||
"# we need to add 3 periods from the context to the request\n",
|
"# we need to add 3 periods from the context to the request\n",
|
||||||
"# so that the model has the data it needs\n",
|
"# so that the model has the data it needs\n",
|
||||||
"\n",
|
"\n",
|
||||||
"# Put the X and y back together for a while. \n",
|
"# Put the X and y back together for a while.\n",
|
||||||
"# They like each other and it makes them happy.\n",
|
"# They like each other and it makes them happy.\n",
|
||||||
"X_context[TARGET_COLUMN_NAME] = y_context\n",
|
"X_context[TARGET_COLUMN_NAME] = y_context\n",
|
||||||
"X_away[TARGET_COLUMN_NAME] = y_away\n",
|
"X_away[TARGET_COLUMN_NAME] = y_away\n",
|
||||||
@@ -684,7 +713,7 @@
|
|||||||
"# it is indeed the last point of the context\n",
|
"# it is indeed the last point of the context\n",
|
||||||
"assert forecast_origin == X_context[TIME_COLUMN_NAME].max()\n",
|
"assert forecast_origin == X_context[TIME_COLUMN_NAME].max()\n",
|
||||||
"print(\"Forecast origin: \" + str(forecast_origin))\n",
|
"print(\"Forecast origin: \" + str(forecast_origin))\n",
|
||||||
" \n",
|
"\n",
|
||||||
"# the model uses lags and rolling windows to look back in time\n",
|
"# the model uses lags and rolling windows to look back in time\n",
|
||||||
"n_lookback_periods = max(lags)\n",
|
"n_lookback_periods = max(lags)\n",
|
||||||
"lookback = pd.DateOffset(hours=n_lookback_periods)\n",
|
"lookback = pd.DateOffset(hours=n_lookback_periods)\n",
|
||||||
@@ -692,8 +721,9 @@
|
|||||||
"horizon = pd.DateOffset(hours=forecast_horizon)\n",
|
"horizon = pd.DateOffset(hours=forecast_horizon)\n",
|
||||||
"\n",
|
"\n",
|
||||||
"# now make the forecast query from context (refer to figure)\n",
|
"# now make the forecast query from context (refer to figure)\n",
|
||||||
"X_pred, y_pred = make_forecasting_query(fulldata, TIME_COLUMN_NAME, TARGET_COLUMN_NAME,\n",
|
"X_pred, y_pred = make_forecasting_query(\n",
|
||||||
" forecast_origin, horizon, lookback)\n",
|
" fulldata, TIME_COLUMN_NAME, TARGET_COLUMN_NAME, forecast_origin, horizon, lookback\n",
|
||||||
|
")\n",
|
||||||
"\n",
|
"\n",
|
||||||
"# show the forecast request aligned\n",
|
"# show the forecast request aligned\n",
|
||||||
"X_show = X_pred.copy()\n",
|
"X_show = X_pred.copy()\n",
|
||||||
@@ -720,7 +750,7 @@
|
|||||||
"# show the forecast aligned\n",
|
"# show the forecast aligned\n",
|
||||||
"X_show = xy_away.reset_index()\n",
|
"X_show = xy_away.reset_index()\n",
|
||||||
"# without the generated features\n",
|
"# without the generated features\n",
|
||||||
"X_show[['date', 'time_series_id', 'ext_predictor', '_automl_target_col']]\n",
|
"X_show[[\"date\", \"time_series_id\", \"ext_predictor\", \"_automl_target_col\"]]\n",
|
||||||
"# prediction is in _automl_target_col"
|
"# prediction is in _automl_target_col"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
@@ -751,12 +781,14 @@
|
|||||||
"source": [
|
"source": [
|
||||||
"# generate the same kind of test data we trained on, but with a single time-series and test period twice as long\n",
|
"# generate the same kind of test data we trained on, but with a single time-series and test period twice as long\n",
|
||||||
"# as the forecast_horizon.\n",
|
"# as the forecast_horizon.\n",
|
||||||
"_, _, X_test_long, y_test_long = get_timeseries(train_len=n_train_periods,\n",
|
"_, _, X_test_long, y_test_long = get_timeseries(\n",
|
||||||
" test_len=forecast_horizon*2,\n",
|
" train_len=n_train_periods,\n",
|
||||||
|
" test_len=forecast_horizon * 2,\n",
|
||||||
" time_column_name=TIME_COLUMN_NAME,\n",
|
" time_column_name=TIME_COLUMN_NAME,\n",
|
||||||
" target_column_name=TARGET_COLUMN_NAME,\n",
|
" target_column_name=TARGET_COLUMN_NAME,\n",
|
||||||
" time_series_id_column_name=TIME_SERIES_ID_COLUMN_NAME,\n",
|
" time_series_id_column_name=TIME_SERIES_ID_COLUMN_NAME,\n",
|
||||||
" time_series_number=1)\n",
|
" time_series_number=1,\n",
|
||||||
|
")\n",
|
||||||
"\n",
|
"\n",
|
||||||
"print(X_test_long.groupby(TIME_SERIES_ID_COLUMN_NAME)[TIME_COLUMN_NAME].min())\n",
|
"print(X_test_long.groupby(TIME_SERIES_ID_COLUMN_NAME)[TIME_COLUMN_NAME].min())\n",
|
||||||
"print(X_test_long.groupby(TIME_SERIES_ID_COLUMN_NAME)[TIME_COLUMN_NAME].max())"
|
"print(X_test_long.groupby(TIME_SERIES_ID_COLUMN_NAME)[TIME_COLUMN_NAME].max())"
|
||||||
@@ -779,9 +811,11 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"# What forecast() function does in this case is equivalent to iterating it twice over the test set as the following. \n",
|
"# What forecast() function does in this case is equivalent to iterating it twice over the test set as the following.\n",
|
||||||
"y_pred1, _ = fitted_model.forecast(X_test_long[:forecast_horizon])\n",
|
"y_pred1, _ = fitted_model.forecast(X_test_long[:forecast_horizon])\n",
|
||||||
"y_pred_all, _ = fitted_model.forecast(X_test_long, np.concatenate((y_pred1, np.full(forecast_horizon, np.nan))))\n",
|
"y_pred_all, _ = fitted_model.forecast(\n",
|
||||||
|
" X_test_long, np.concatenate((y_pred1, np.full(forecast_horizon, np.nan)))\n",
|
||||||
|
")\n",
|
||||||
"np.array_equal(y_pred_all, y_pred_long)"
|
"np.array_equal(y_pred_all, y_pred_long)"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,677 @@
|
|||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"Copyright (c) Microsoft Corporation. All rights reserved.\n",
|
||||||
|
"\n",
|
||||||
|
"Licensed under the MIT License."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
""
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Hierarchical Time Series - Automated ML\n",
|
||||||
|
"**_Generate hierarchical time series forecasts with Automated Machine Learning_**\n",
|
||||||
|
"\n",
|
||||||
|
"---"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"For this notebook we are using a synthetic dataset portraying sales data to predict the the quantity of a vartiety of product skus across several states, stores, and product categories.\n",
|
||||||
|
"\n",
|
||||||
|
"**NOTE: There are limits on how many runs we can do in parallel per workspace, and we currently recommend to set the parallelism to maximum of 320 runs per experiment per workspace. If users want to have more parallelism and increase this limit they might encounter Too Many Requests errors (HTTP 429).**"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"### Prerequisites\n",
|
||||||
|
"You'll need to create a compute Instance by following the instructions in the [EnvironmentSetup.md](../Setup_Resources/EnvironmentSetup.md)."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## 1.0 Set up workspace, datastore, experiment"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"gather": {
|
||||||
|
"logged": 1613003526897
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"import azureml.core\n",
|
||||||
|
"from azureml.core import Workspace, Datastore\n",
|
||||||
|
"import pandas as pd\n",
|
||||||
|
"\n",
|
||||||
|
"# Set up your workspace\n",
|
||||||
|
"ws = Workspace.from_config()\n",
|
||||||
|
"ws.get_details()\n",
|
||||||
|
"\n",
|
||||||
|
"# Set up your datastores\n",
|
||||||
|
"dstore = ws.get_default_datastore()\n",
|
||||||
|
"\n",
|
||||||
|
"output = {}\n",
|
||||||
|
"output[\"SDK version\"] = azureml.core.VERSION\n",
|
||||||
|
"output[\"Subscription ID\"] = ws.subscription_id\n",
|
||||||
|
"output[\"Workspace\"] = ws.name\n",
|
||||||
|
"output[\"Resource Group\"] = ws.resource_group\n",
|
||||||
|
"output[\"Location\"] = ws.location\n",
|
||||||
|
"output[\"Default datastore name\"] = dstore.name\n",
|
||||||
|
"pd.set_option(\"display.max_colwidth\", -1)\n",
|
||||||
|
"outputDf = pd.DataFrame(data=output, index=[\"\"])\n",
|
||||||
|
"outputDf.T"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"### Choose an experiment"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"gather": {
|
||||||
|
"logged": 1613003540729
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"from azureml.core import Experiment\n",
|
||||||
|
"\n",
|
||||||
|
"experiment = Experiment(ws, \"automl-hts\")\n",
|
||||||
|
"\n",
|
||||||
|
"print(\"Experiment name: \" + experiment.name)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## 2.0 Data\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {
|
||||||
|
"nteract": {
|
||||||
|
"transient": {
|
||||||
|
"deleting": false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"source": [
|
||||||
|
"### Upload local csv files to datastore\n",
|
||||||
|
"You can upload your train and inference csv files to the default datastore in your workspace. \n",
|
||||||
|
"\n",
|
||||||
|
"A Datastore is a place where data can be stored that is then made accessible to a compute either by means of mounting or copying the data to the compute target.\n",
|
||||||
|
"Please refer to [Datastore](https://docs.microsoft.com/en-us/python/api/azureml-core/azureml.core.datastore.datastore?view=azure-ml-py) documentation on how to access data from Datastore."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"datastore_path = \"hts-sample\""
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"datastore = ws.get_default_datastore()\n",
|
||||||
|
"datastore"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"gather": {
|
||||||
|
"logged": 1613005886349
|
||||||
|
},
|
||||||
|
"jupyter": {
|
||||||
|
"outputs_hidden": false,
|
||||||
|
"source_hidden": false
|
||||||
|
},
|
||||||
|
"nteract": {
|
||||||
|
"transient": {
|
||||||
|
"deleting": false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"datastore.upload(\n",
|
||||||
|
" src_dir=\"./Data/\", target_path=datastore_path, overwrite=True, show_progress=True\n",
|
||||||
|
")"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"### Create the TabularDatasets \n",
|
||||||
|
"\n",
|
||||||
|
"Datasets in Azure Machine Learning are references to specific data in a Datastore. The data can be retrieved as a [TabularDatasets](https://docs.microsoft.com/en-us/python/api/azureml-core/azureml.data.tabulardataset?view=azure-ml-py)."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"gather": {
|
||||||
|
"logged": 1613007017296
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"from azureml.core.dataset import Dataset\n",
|
||||||
|
"\n",
|
||||||
|
"train_ds = Dataset.Tabular.from_delimited_files(\n",
|
||||||
|
" path=datastore.path(\"hts-sample/hts-sample-train.csv\"), validate=False\n",
|
||||||
|
")\n",
|
||||||
|
"inference_ds = Dataset.Tabular.from_delimited_files(\n",
|
||||||
|
" path=datastore.path(\"hts-sample/hts-sample-test.csv\"), validate=False\n",
|
||||||
|
")"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"### Register the TabularDatasets to the Workspace \n",
|
||||||
|
"Finally, register the dataset to your Workspace so it can be called as an input into the training pipeline in the next notebook. We will use the inference dataset as part of the forecasting pipeline. The step need only be completed once."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"registered_train = train_ds.register(ws, \"hts-sales-train\")\n",
|
||||||
|
"registered_inference = inference_ds.register(ws, \"hts-sales-test\")"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## 3.0 Build the training pipeline\n",
|
||||||
|
"Now that the dataset, WorkSpace, and datastore are set up, we can put together a pipeline for training.\n",
|
||||||
|
"\n",
|
||||||
|
"> Note that if you have an AzureML Data Scientist role, you will not have permission to create compute resources. Talk to your workspace or IT admin to create the compute targets described in this section, if they do not already exist."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"### Choose a compute target\n",
|
||||||
|
"\n",
|
||||||
|
"You will need to create a [compute target](https://docs.microsoft.com/en-us/azure/machine-learning/how-to-set-up-training-targets#amlcompute) for your AutoML run. In this tutorial, you create AmlCompute as your training compute resource.\n",
|
||||||
|
"\n",
|
||||||
|
"\\*\\*Creation of AmlCompute takes approximately 5 minutes.**\n",
|
||||||
|
"\n",
|
||||||
|
"If the AmlCompute with that name is already in your workspace this code will skip the creation process. As with other Azure services, there are limits on certain resources (e.g. AmlCompute) associated with the Azure Machine Learning service. Please read this [article](https://docs.microsoft.com/en-us/azure/machine-learning/how-to-manage-quotas) on the default limits and how to request more quota."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"gather": {
|
||||||
|
"logged": 1613007037308
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"from azureml.core.compute import ComputeTarget, AmlCompute\n",
|
||||||
|
"\n",
|
||||||
|
"# Name your cluster\n",
|
||||||
|
"compute_name = \"hts-compute\"\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"if compute_name in ws.compute_targets:\n",
|
||||||
|
" compute_target = ws.compute_targets[compute_name]\n",
|
||||||
|
" if compute_target and type(compute_target) is AmlCompute:\n",
|
||||||
|
" print(\"Found compute target: \" + compute_name)\n",
|
||||||
|
"else:\n",
|
||||||
|
" print(\"Creating a new compute target...\")\n",
|
||||||
|
" provisioning_config = AmlCompute.provisioning_configuration(\n",
|
||||||
|
" vm_size=\"STANDARD_D16S_V3\", max_nodes=20\n",
|
||||||
|
" )\n",
|
||||||
|
" # Create the compute target\n",
|
||||||
|
" compute_target = ComputeTarget.create(ws, compute_name, provisioning_config)\n",
|
||||||
|
"\n",
|
||||||
|
" # Can poll for a minimum number of nodes and for a specific timeout.\n",
|
||||||
|
" # If no min node count is provided it will use the scale settings for the cluster\n",
|
||||||
|
" compute_target.wait_for_completion(\n",
|
||||||
|
" show_output=True, min_node_count=None, timeout_in_minutes=20\n",
|
||||||
|
" )\n",
|
||||||
|
"\n",
|
||||||
|
" # For a more detailed view of current cluster status, use the 'status' property\n",
|
||||||
|
" print(compute_target.status.serialize())"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"### Set up training parameters\n",
|
||||||
|
"\n",
|
||||||
|
"This dictionary defines the AutoML and hierarchy settings. For this forecasting task we need to define several settings inncluding the name of the time column, the maximum forecast horizon, the hierarchy definition, and the level of the hierarchy at which to train.\n",
|
||||||
|
"\n",
|
||||||
|
"| Property | Description|\n",
|
||||||
|
"| :--------------- | :------------------- |\n",
|
||||||
|
"| **task** | forecasting |\n",
|
||||||
|
"| **primary_metric** | This is the metric that you want to optimize.<br> Forecasting supports the following primary metrics <br><i>spearman_correlation</i><br><i>normalized_root_mean_squared_error</i><br><i>r2_score</i><br><i>normalized_mean_absolute_error</i> |\n",
|
||||||
|
"| **blocked_models** | Blocked models won't be used by AutoML. |\n",
|
||||||
|
"| **iteration_timeout_minutes** | Maximum amount of time in minutes that the model can train. This is optional but provides customers with greater control on exit criteria. |\n",
|
||||||
|
"| **iterations** | Number of models to train. This is optional but provides customers with greater control on exit criteria. |\n",
|
||||||
|
"| **experiment_timeout_hours** | Maximum amount of time in hours that the experiment can take before it terminates. This is optional but provides customers with greater control on exit criteria. |\n",
|
||||||
|
"| **label_column_name** | The name of the label column. |\n",
|
||||||
|
"| **forecast_horizon** | The forecast horizon is how many periods forward you would like to forecast. This integer horizon is in units of the timeseries frequency (e.g. daily, weekly). Periods are inferred from your data. |\n",
|
||||||
|
"| **n_cross_validations** | Number of cross validation splits. Rolling Origin Validation is used to split time-series in a temporally consistent way. |\n",
|
||||||
|
"| **enable_early_stopping** | Flag to enable early termination if the score is not improving in the short term. |\n",
|
||||||
|
"| **time_column_name** | The name of your time column. |\n",
|
||||||
|
"| **hierarchy_column_names** | The names of columns that define the hierarchical structure of the data from highest level to most granular. |\n",
|
||||||
|
"| **training_level** | The level of the hierarchy to be used for training models. |\n",
|
||||||
|
"| **enable_engineered_explanations** | Engineered feature explanations will be downloaded if enable_engineered_explanations flag is set to True. By default it is set to False to save storage space. |\n",
|
||||||
|
"| **time_series_id_column_name** | The column names used to uniquely identify timeseries in data that has multiple rows with the same timestamp. |\n",
|
||||||
|
"| **track_child_runs** | Flag to disable tracking of child runs. Only best run is tracked if the flag is set to False (this includes the model and metrics of the run). |\n",
|
||||||
|
"| **pipeline_fetch_max_batch_size** | Determines how many pipelines (training algorithms) to fetch at a time for training, this helps reduce throttling when training at large scale. |\n",
|
||||||
|
"| **model_explainability** | Flag to disable explaining the best automated ML model at the end of all training iterations. The default is True and will block non-explainable models which may impact the forecast accuracy. For more information, see [Interpretability: model explanations in automated machine learning](https://docs.microsoft.com/en-us/azure/machine-learning/how-to-machine-learning-interpretability-automl). |"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"gather": {
|
||||||
|
"logged": 1613007061544
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"from azureml.train.automl.runtime._hts.hts_parameters import HTSTrainParameters\n",
|
||||||
|
"\n",
|
||||||
|
"model_explainability = True\n",
|
||||||
|
"\n",
|
||||||
|
"engineered_explanations = False\n",
|
||||||
|
"# Define your hierarchy. Adjust the settings below based on your dataset.\n",
|
||||||
|
"hierarchy = [\"state\", \"store_id\", \"product_category\", \"SKU\"]\n",
|
||||||
|
"training_level = \"SKU\"\n",
|
||||||
|
"\n",
|
||||||
|
"# Set your forecast parameters. Adjust the settings below based on your dataset.\n",
|
||||||
|
"time_column_name = \"date\"\n",
|
||||||
|
"label_column_name = \"quantity\"\n",
|
||||||
|
"forecast_horizon = 7\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"automl_settings = {\n",
|
||||||
|
" \"task\": \"forecasting\",\n",
|
||||||
|
" \"primary_metric\": \"normalized_root_mean_squared_error\",\n",
|
||||||
|
" \"label_column_name\": label_column_name,\n",
|
||||||
|
" \"time_column_name\": time_column_name,\n",
|
||||||
|
" \"forecast_horizon\": forecast_horizon,\n",
|
||||||
|
" \"hierarchy_column_names\": hierarchy,\n",
|
||||||
|
" \"hierarchy_training_level\": training_level,\n",
|
||||||
|
" \"track_child_runs\": False,\n",
|
||||||
|
" \"pipeline_fetch_max_batch_size\": 15,\n",
|
||||||
|
" \"model_explainability\": model_explainability,\n",
|
||||||
|
" # The following settings are specific to this sample and should be adjusted according to your own needs.\n",
|
||||||
|
" \"iteration_timeout_minutes\": 10,\n",
|
||||||
|
" \"iterations\": 10,\n",
|
||||||
|
" \"n_cross_validations\": 2,\n",
|
||||||
|
"}\n",
|
||||||
|
"\n",
|
||||||
|
"hts_parameters = HTSTrainParameters(\n",
|
||||||
|
" automl_settings=automl_settings,\n",
|
||||||
|
" hierarchy_column_names=hierarchy,\n",
|
||||||
|
" training_level=training_level,\n",
|
||||||
|
" enable_engineered_explanations=engineered_explanations,\n",
|
||||||
|
")"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"### Set up hierarchy training pipeline"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"Parallel run step is leveraged to train the hierarchy. To configure the ParallelRunConfig you will need to determine the appropriate number of workers and nodes for your use case. The `process_count_per_node` is based off the number of cores of the compute VM. The node_count will determine the number of master nodes to use, increasing the node count will speed up the training process.\n",
|
||||||
|
"\n",
|
||||||
|
"* **experiment:** The experiment used for training.\n",
|
||||||
|
"* **train_data:** The tabular dataset to be used as input to the training run.\n",
|
||||||
|
"* **node_count:** The number of compute nodes to be used for running the user script. We recommend to start with 3 and increase the node_count if the training time is taking too long.\n",
|
||||||
|
"* **process_count_per_node:** Process count per node, we recommend 2:1 ratio for number of cores: number of processes per node. eg. If node has 16 cores then configure 8 or less process count per node or optimal performance.\n",
|
||||||
|
"* **train_pipeline_parameters:** The set of configuration parameters defined in the previous section. \n",
|
||||||
|
"\n",
|
||||||
|
"Calling this method will create a new aggregated dataset which is generated dynamically on pipeline execution."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"from azureml.contrib.automl.pipeline.steps import AutoMLPipelineBuilder\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"training_pipeline_steps = AutoMLPipelineBuilder.get_many_models_train_steps(\n",
|
||||||
|
" experiment=experiment,\n",
|
||||||
|
" train_data=registered_train,\n",
|
||||||
|
" compute_target=compute_target,\n",
|
||||||
|
" node_count=2,\n",
|
||||||
|
" process_count_per_node=8,\n",
|
||||||
|
" train_pipeline_parameters=hts_parameters,\n",
|
||||||
|
")"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"from azureml.pipeline.core import Pipeline\n",
|
||||||
|
"\n",
|
||||||
|
"training_pipeline = Pipeline(ws, steps=training_pipeline_steps)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"### Submit the pipeline to run\n",
|
||||||
|
"Next we submit our pipeline to run. The whole training pipeline takes about 1h 11m using a Standard_D12_V2 VM with our current ParallelRunConfig setting."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"training_run = experiment.submit(training_pipeline)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"training_run.wait_for_completion(show_output=False)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"Check the run status, if training_run is in completed state, continue to forecasting. If training_run is in another state, check the portal for failures."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"### [Optional] Get the explanations\n",
|
||||||
|
"First we need to download the explanations to the local disk."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"if model_explainability:\n",
|
||||||
|
" expl_output = training_run.get_pipeline_output(\"explanations\")\n",
|
||||||
|
" expl_output.download(\"training_explanations\")\n",
|
||||||
|
"else:\n",
|
||||||
|
" print(\n",
|
||||||
|
" \"Model explanations are available only if model_explainability is set to True.\"\n",
|
||||||
|
" )"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"The explanations are downloaded to the \"training_explanations/azureml\" directory."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"import os\n",
|
||||||
|
"\n",
|
||||||
|
"if model_explainability:\n",
|
||||||
|
" explanations_dirrectory = os.listdir(\n",
|
||||||
|
" os.path.join(\"training_explanations\", \"azureml\")\n",
|
||||||
|
" )\n",
|
||||||
|
" if len(explanations_dirrectory) > 1:\n",
|
||||||
|
" print(\n",
|
||||||
|
" \"Warning! The directory contains multiple explanations, only the first one will be displayed.\"\n",
|
||||||
|
" )\n",
|
||||||
|
" print(\"The explanations are located at {}.\".format(explanations_dirrectory[0]))\n",
|
||||||
|
" # Now we will list all the explanations.\n",
|
||||||
|
" explanation_path = os.path.join(\n",
|
||||||
|
" \"training_explanations\",\n",
|
||||||
|
" \"azureml\",\n",
|
||||||
|
" explanations_dirrectory[0],\n",
|
||||||
|
" \"training_explanations\",\n",
|
||||||
|
" )\n",
|
||||||
|
" print(\"Available explanations\")\n",
|
||||||
|
" print(\"==============================\")\n",
|
||||||
|
" print(\"\\n\".join(os.listdir(explanation_path)))\n",
|
||||||
|
"else:\n",
|
||||||
|
" print(\n",
|
||||||
|
" \"Model explanations are available only if model_explainability is set to True.\"\n",
|
||||||
|
" )"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"View the explanations on \"state\" level."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"from IPython.display import display\n",
|
||||||
|
"\n",
|
||||||
|
"explanation_type = \"raw\"\n",
|
||||||
|
"level = \"state\"\n",
|
||||||
|
"\n",
|
||||||
|
"if model_explainability:\n",
|
||||||
|
" display(\n",
|
||||||
|
" pd.read_csv(\n",
|
||||||
|
" os.path.join(explanation_path, \"{}_explanations_{}.csv\").format(\n",
|
||||||
|
" explanation_type, level\n",
|
||||||
|
" )\n",
|
||||||
|
" )\n",
|
||||||
|
" )"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## 5.0 Forecasting\n",
|
||||||
|
"For hierarchical forecasting we need to provide the HTSInferenceParameters object.\n",
|
||||||
|
"#### HTSInferenceParameters arguments\n",
|
||||||
|
"* **hierarchy_forecast_level:** The default level of the hierarchy to produce prediction/forecast on.\n",
|
||||||
|
"* **allocation_method:** \\[Optional] The disaggregation method to use if the hierarchy forecast level specified is below the define hierarchy training level. <br><i>(average historical proportions) 'average_historical_proportions'</i><br><i>(proportions of the historical averages) 'proportions_of_historical_average'</i>\n",
|
||||||
|
"\n",
|
||||||
|
"#### get_many_models_batch_inference_steps arguments\n",
|
||||||
|
"* **experiment:** The experiment used for inference run.\n",
|
||||||
|
"* **inference_data:** The data to use for inferencing. It should be the same schema as used for training.\n",
|
||||||
|
"* **compute_target:** The compute target that runs the inference pipeline.\n",
|
||||||
|
"* **node_count:** The number of compute nodes to be used for running the user script. We recommend to start with the number of cores per node (varies by compute sku).\n",
|
||||||
|
"* **process_count_per_node:** The number of processes per node.\n",
|
||||||
|
"* **train_run_id:** \\[Optional] The run id of the hierarchy training, by default it is the latest successful training hts run in the experiment.\n",
|
||||||
|
"* **train_experiment_name:** \\[Optional] The train experiment that contains the train pipeline. This one is only needed when the train pipeline is not in the same experiement as the inference pipeline.\n",
|
||||||
|
"* **process_count_per_node:** \\[Optional] The number of processes per node, by default it's 4."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"from azureml.train.automl.runtime._hts.hts_parameters import HTSInferenceParameters\n",
|
||||||
|
"\n",
|
||||||
|
"inference_parameters = HTSInferenceParameters(\n",
|
||||||
|
" hierarchy_forecast_level=\"store_id\", # The setting is specific to this dataset and should be changed based on your dataset.\n",
|
||||||
|
" allocation_method=\"proportions_of_historical_average\",\n",
|
||||||
|
")\n",
|
||||||
|
"\n",
|
||||||
|
"steps = AutoMLPipelineBuilder.get_many_models_batch_inference_steps(\n",
|
||||||
|
" experiment=experiment,\n",
|
||||||
|
" inference_data=registered_inference,\n",
|
||||||
|
" compute_target=compute_target,\n",
|
||||||
|
" inference_pipeline_parameters=inference_parameters,\n",
|
||||||
|
" node_count=2,\n",
|
||||||
|
" process_count_per_node=8,\n",
|
||||||
|
")"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"from azureml.pipeline.core import Pipeline\n",
|
||||||
|
"\n",
|
||||||
|
"inference_pipeline = Pipeline(ws, steps=steps)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"inference_run = experiment.submit(inference_pipeline)\n",
|
||||||
|
"inference_run.wait_for_completion(show_output=False)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Retrieve results\n",
|
||||||
|
"\n",
|
||||||
|
"Forecast results can be retrieved through the following code. The prediction results summary and the actual predictions are downloaded the \"forecast_results\" folder"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"forecasts = inference_run.get_pipeline_output(\"forecasts\")\n",
|
||||||
|
"forecasts.download(\"forecast_results\")"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Resbumit the Pipeline\n",
|
||||||
|
"\n",
|
||||||
|
"The inference pipeline can be submitted with different configurations."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"inference_run = experiment.submit(\n",
|
||||||
|
" inference_pipeline, pipeline_parameters={\"hierarchy_forecast_level\": \"state\"}\n",
|
||||||
|
")\n",
|
||||||
|
"inference_run.wait_for_completion(show_output=False)"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"authors": [
|
||||||
|
{
|
||||||
|
"name": "jialiu"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"categories": [
|
||||||
|
"how-to-use-azureml",
|
||||||
|
"automated-machine-learning"
|
||||||
|
],
|
||||||
|
"kernelspec": {
|
||||||
|
"display_name": "Python 3.6",
|
||||||
|
"language": "python",
|
||||||
|
"name": "python36"
|
||||||
|
},
|
||||||
|
"language_info": {
|
||||||
|
"codemirror_mode": {
|
||||||
|
"name": "ipython",
|
||||||
|
"version": 3
|
||||||
|
},
|
||||||
|
"file_extension": ".py",
|
||||||
|
"mimetype": "text/x-python",
|
||||||
|
"name": "python",
|
||||||
|
"nbconvert_exporter": "python",
|
||||||
|
"pygments_lexer": "ipython3",
|
||||||
|
"version": "3.6.8"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 4
|
||||||
|
}
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
name: auto-ml-forecasting-hierarchical-timeseries
|
||||||
|
dependencies:
|
||||||
|
- pip:
|
||||||
|
- azureml-sdk
|
||||||
@@ -0,0 +1,746 @@
|
|||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"Copyright (c) Microsoft Corporation. All rights reserved.\n",
|
||||||
|
"\n",
|
||||||
|
"Licensed under the MIT License."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
""
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Many Models - Automated ML\n",
|
||||||
|
"**_Generate many models time series forecasts with Automated Machine Learning_**\n",
|
||||||
|
"\n",
|
||||||
|
"---"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"For this notebook we are using a synthetic dataset portraying sales data to predict the the quantity of a vartiety of product skus across several states, stores, and product categories.\n",
|
||||||
|
"\n",
|
||||||
|
"**NOTE: There are limits on how many runs we can do in parallel per workspace, and we currently recommend to set the parallelism to maximum of 320 runs per experiment per workspace. If users want to have more parallelism and increase this limit they might encounter Too Many Requests errors (HTTP 429).**"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"### Prerequisites\n",
|
||||||
|
"You'll need to create a compute Instance by following the instructions in the [EnvironmentSetup.md](../Setup_Resources/EnvironmentSetup.md)."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## 1.0 Set up workspace, datastore, experiment"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"gather": {
|
||||||
|
"logged": 1613003526897
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"import azureml.core\n",
|
||||||
|
"from azureml.core import Workspace, Datastore\n",
|
||||||
|
"import pandas as pd\n",
|
||||||
|
"\n",
|
||||||
|
"# Set up your workspace\n",
|
||||||
|
"ws = Workspace.from_config()\n",
|
||||||
|
"ws.get_details()\n",
|
||||||
|
"\n",
|
||||||
|
"# Set up your datastores\n",
|
||||||
|
"dstore = ws.get_default_datastore()\n",
|
||||||
|
"\n",
|
||||||
|
"output = {}\n",
|
||||||
|
"output[\"SDK version\"] = azureml.core.VERSION\n",
|
||||||
|
"output[\"Subscription ID\"] = ws.subscription_id\n",
|
||||||
|
"output[\"Workspace\"] = ws.name\n",
|
||||||
|
"output[\"Resource Group\"] = ws.resource_group\n",
|
||||||
|
"output[\"Location\"] = ws.location\n",
|
||||||
|
"output[\"Default datastore name\"] = dstore.name\n",
|
||||||
|
"pd.set_option(\"display.max_colwidth\", -1)\n",
|
||||||
|
"outputDf = pd.DataFrame(data=output, index=[\"\"])\n",
|
||||||
|
"outputDf.T"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"### Choose an experiment"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"gather": {
|
||||||
|
"logged": 1613003540729
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"from azureml.core import Experiment\n",
|
||||||
|
"\n",
|
||||||
|
"experiment = Experiment(ws, \"automl-many-models\")\n",
|
||||||
|
"\n",
|
||||||
|
"print(\"Experiment name: \" + experiment.name)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## 2.0 Data\n",
|
||||||
|
"\n",
|
||||||
|
"This notebook uses simulated orange juice sales data to walk you through the process of training many models on Azure Machine Learning using Automated ML. \n",
|
||||||
|
"\n",
|
||||||
|
"The time series data used in this example was simulated based on the University of Chicago's Dominick's Finer Foods dataset which featured two years of sales of 3 different orange juice brands for individual stores. The full simulated dataset includes 3,991 stores with 3 orange juice brands each thus allowing 11,973 models to be trained to showcase the power of the many models pattern.\n",
|
||||||
|
"\n",
|
||||||
|
" \n",
|
||||||
|
"In this notebook, two datasets will be created: one with all 11,973 files and one with only 10 files that can be used to quickly test and debug. For each dataset, you'll be walked through the process of:\n",
|
||||||
|
"\n",
|
||||||
|
"1. Registering the blob container as a Datastore to the Workspace\n",
|
||||||
|
"2. Registering a tabular dataset to the Workspace"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {
|
||||||
|
"nteract": {
|
||||||
|
"transient": {
|
||||||
|
"deleting": false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"source": [
|
||||||
|
"### 2.1 Data Preparation\n",
|
||||||
|
"The OJ data is available in the public blob container. The data is split to be used for training and for inferencing. For the current dataset, the data was split on time column ('WeekStarting') before and after '1992-5-28' .\n",
|
||||||
|
"\n",
|
||||||
|
"The container has\n",
|
||||||
|
"<ol>\n",
|
||||||
|
" <li><b>'oj-data-tabular'</b> and <b>'oj-inference-tabular'</b> folders that contains training and inference data respectively for the 11,973 models. </li>\n",
|
||||||
|
" <li>It also has <b>'oj-data-small-tabular'</b> and <b>'oj-inference-small-tabular'</b> folders that has training and inference data for 10 models.</li>\n",
|
||||||
|
"</ol>\n",
|
||||||
|
"\n",
|
||||||
|
"To create the [TabularDataset](https://docs.microsoft.com/en-us/python/api/azureml-core/azureml.data.tabular_dataset.tabulardataset?view=azure-ml-py) needed for the ParallelRunStep, you first need to register the blob container to the workspace."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {
|
||||||
|
"nteract": {
|
||||||
|
"transient": {
|
||||||
|
"deleting": false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"source": [
|
||||||
|
"<b> To use your own data, put your own data in a blobstore folder. As shown it can be one file or multiple files. We can then register datastore using that blob as shown below.\n",
|
||||||
|
" \n",
|
||||||
|
"<h3> How sample data in blob store looks like</h3>\n",
|
||||||
|
"\n",
|
||||||
|
"['oj-data-tabular'](https://ms.portal.azure.com/#blade/Microsoft_Azure_Storage/ContainerMenuBlade/overview/storageAccountId/%2Fsubscriptions%2F102a16c3-37d3-48a8-9237-4c9b1e8e80e0%2FresourceGroups%2FAutoMLSampleNotebooksData%2Fproviders%2FMicrosoft.Storage%2FstorageAccounts%2Fautomlsamplenotebookdata/path/automl-sample-notebook-data/etag/%220x8D84EAA65DE50B7%22/defaultEncryptionScope/%24account-encryption-key/denyEncryptionScopeOverride//defaultId//publicAccessVal/Container)</b>\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"['oj-inference-tabular'](https://ms.portal.azure.com/#blade/Microsoft_Azure_Storage/ContainerMenuBlade/overview/storageAccountId/%2Fsubscriptions%2F102a16c3-37d3-48a8-9237-4c9b1e8e80e0%2FresourceGroups%2FAutoMLSampleNotebooksData%2Fproviders%2FMicrosoft.Storage%2FstorageAccounts%2Fautomlsamplenotebookdata/path/automl-sample-notebook-data/etag/%220x8D84EAA65DE50B7%22/defaultEncryptionScope/%24account-encryption-key/denyEncryptionScopeOverride//defaultId//publicAccessVal/Container)\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"['oj-data-small-tabular'](https://ms.portal.azure.com/#blade/Microsoft_Azure_Storage/ContainerMenuBlade/overview/storageAccountId/%2Fsubscriptions%2F102a16c3-37d3-48a8-9237-4c9b1e8e80e0%2FresourceGroups%2FAutoMLSampleNotebooksData%2Fproviders%2FMicrosoft.Storage%2FstorageAccounts%2Fautomlsamplenotebookdata/path/automl-sample-notebook-data/etag/%220x8D84EAA65DE50B7%22/defaultEncryptionScope/%24account-encryption-key/denyEncryptionScopeOverride//defaultId//publicAccessVal/Container)\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"['oj-inference-small-tabular'](https://ms.portal.azure.com/#blade/Microsoft_Azure_Storage/ContainerMenuBlade/overview/storageAccountId/%2Fsubscriptions%2F102a16c3-37d3-48a8-9237-4c9b1e8e80e0%2FresourceGroups%2FAutoMLSampleNotebooksData%2Fproviders%2FMicrosoft.Storage%2FstorageAccounts%2Fautomlsamplenotebookdata/path/automl-sample-notebook-data/etag/%220x8D84EAA65DE50B7%22/defaultEncryptionScope/%24account-encryption-key/denyEncryptionScopeOverride//defaultId//publicAccessVal/Container)\n",
|
||||||
|
""
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"#### 2.2 Register the blob container as DataStore\n",
|
||||||
|
"\n",
|
||||||
|
"A Datastore is a place where data can be stored that is then made accessible to a compute either by means of mounting or copying the data to the compute target.\n",
|
||||||
|
"\n",
|
||||||
|
"Please refer to [Datastore](https://docs.microsoft.com/en-us/python/api/azureml-core/azureml.core.datastore(class)?view=azure-ml-py) documentation on how to access data from Datastore.\n",
|
||||||
|
"\n",
|
||||||
|
"In this next step, we will be registering blob storage as datastore to the Workspace."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"from azureml.core import Datastore\n",
|
||||||
|
"\n",
|
||||||
|
"# Please change the following to point to your own blob container and pass in account_key\n",
|
||||||
|
"blob_datastore_name = \"automl_many_models\"\n",
|
||||||
|
"container_name = \"automl-sample-notebook-data\"\n",
|
||||||
|
"account_name = \"automlsamplenotebookdata\"\n",
|
||||||
|
"\n",
|
||||||
|
"oj_datastore = Datastore.register_azure_blob_container(\n",
|
||||||
|
" workspace=ws,\n",
|
||||||
|
" datastore_name=blob_datastore_name,\n",
|
||||||
|
" container_name=container_name,\n",
|
||||||
|
" account_name=account_name,\n",
|
||||||
|
" create_if_not_exists=True,\n",
|
||||||
|
")"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"#### 2.3 Using tabular datasets \n",
|
||||||
|
"\n",
|
||||||
|
"Now that the datastore is available from the Workspace, [TabularDataset](https://docs.microsoft.com/en-us/python/api/azureml-core/azureml.data.tabular_dataset.tabulardataset?view=azure-ml-py) can be created. Datasets in Azure Machine Learning are references to specific data in a Datastore. We are using TabularDataset, so that users who have their data which can be in one or many files (*.parquet or *.csv) and have not split up data according to group columns needed for training, can do so using out of box support for 'partiion_by' feature of TabularDataset shown in section 5.0 below."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"gather": {
|
||||||
|
"logged": 1613007017296
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"from azureml.core import Dataset\n",
|
||||||
|
"\n",
|
||||||
|
"ds_name_small = \"oj-data-small-tabular\"\n",
|
||||||
|
"input_ds_small = Dataset.Tabular.from_delimited_files(\n",
|
||||||
|
" path=oj_datastore.path(ds_name_small + \"/\"), validate=False\n",
|
||||||
|
")\n",
|
||||||
|
"\n",
|
||||||
|
"inference_name_small = \"oj-inference-small-tabular\"\n",
|
||||||
|
"inference_ds_small = Dataset.Tabular.from_delimited_files(\n",
|
||||||
|
" path=oj_datastore.path(inference_name_small + \"/\"), validate=False\n",
|
||||||
|
")"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## 3.0 Build the training pipeline\n",
|
||||||
|
"Now that the dataset, WorkSpace, and datastore are set up, we can put together a pipeline for training.\n",
|
||||||
|
"\n",
|
||||||
|
"> Note that if you have an AzureML Data Scientist role, you will not have permission to create compute resources. Talk to your workspace or IT admin to create the compute targets described in this section, if they do not already exist."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"### Choose a compute target\n",
|
||||||
|
"\n",
|
||||||
|
"You will need to create a [compute target](https://docs.microsoft.com/en-us/azure/machine-learning/how-to-set-up-training-targets#amlcompute) for your AutoML run. In this tutorial, you create AmlCompute as your training compute resource.\n",
|
||||||
|
"\n",
|
||||||
|
"\\*\\*Creation of AmlCompute takes approximately 5 minutes.**\n",
|
||||||
|
"\n",
|
||||||
|
"If the AmlCompute with that name is already in your workspace this code will skip the creation process. As with other Azure services, there are limits on certain resources (e.g. AmlCompute) associated with the Azure Machine Learning service. Please read this [article](https://docs.microsoft.com/en-us/azure/machine-learning/how-to-manage-quotas) on the default limits and how to request more quota."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"gather": {
|
||||||
|
"logged": 1613007037308
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"from azureml.core.compute import ComputeTarget, AmlCompute\n",
|
||||||
|
"\n",
|
||||||
|
"# Name your cluster\n",
|
||||||
|
"compute_name = \"mm-compute\"\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"if compute_name in ws.compute_targets:\n",
|
||||||
|
" compute_target = ws.compute_targets[compute_name]\n",
|
||||||
|
" if compute_target and type(compute_target) is AmlCompute:\n",
|
||||||
|
" print(\"Found compute target: \" + compute_name)\n",
|
||||||
|
"else:\n",
|
||||||
|
" print(\"Creating a new compute target...\")\n",
|
||||||
|
" provisioning_config = AmlCompute.provisioning_configuration(\n",
|
||||||
|
" vm_size=\"STANDARD_D16S_V3\", max_nodes=20\n",
|
||||||
|
" )\n",
|
||||||
|
" # Create the compute target\n",
|
||||||
|
" compute_target = ComputeTarget.create(ws, compute_name, provisioning_config)\n",
|
||||||
|
"\n",
|
||||||
|
" # Can poll for a minimum number of nodes and for a specific timeout.\n",
|
||||||
|
" # If no min node count is provided it will use the scale settings for the cluster\n",
|
||||||
|
" compute_target.wait_for_completion(\n",
|
||||||
|
" show_output=True, min_node_count=None, timeout_in_minutes=20\n",
|
||||||
|
" )\n",
|
||||||
|
"\n",
|
||||||
|
" # For a more detailed view of current cluster status, use the 'status' property\n",
|
||||||
|
" print(compute_target.status.serialize())"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"### Set up training parameters\n",
|
||||||
|
"\n",
|
||||||
|
"This dictionary defines the AutoML and many models settings. For this forecasting task we need to define several settings inncluding the name of the time column, the maximum forecast horizon, and the partition column name definition.\n",
|
||||||
|
"\n",
|
||||||
|
"| Property | Description|\n",
|
||||||
|
"| :--------------- | :------------------- |\n",
|
||||||
|
"| **task** | forecasting |\n",
|
||||||
|
"| **primary_metric** | This is the metric that you want to optimize.<br> Forecasting supports the following primary metrics <br><i>spearman_correlation</i><br><i>normalized_root_mean_squared_error</i><br><i>r2_score</i><br><i>normalized_mean_absolute_error</i> |\n",
|
||||||
|
"| **blocked_models** | Blocked models won't be used by AutoML. |\n",
|
||||||
|
"| **iteration_timeout_minutes** | Maximum amount of time in minutes that the model can train. This is optional but provides customers with greater control on exit criteria. |\n",
|
||||||
|
"| **iterations** | Number of models to train. This is optional but provides customers with greater control on exit criteria. |\n",
|
||||||
|
"| **experiment_timeout_hours** | Maximum amount of time in hours that the experiment can take before it terminates. This is optional but provides customers with greater control on exit criteria. |\n",
|
||||||
|
"| **label_column_name** | The name of the label column. |\n",
|
||||||
|
"| **forecast_horizon** | The forecast horizon is how many periods forward you would like to forecast. This integer horizon is in units of the timeseries frequency (e.g. daily, weekly). Periods are inferred from your data. |\n",
|
||||||
|
"| **n_cross_validations** | Number of cross validation splits. Rolling Origin Validation is used to split time-series in a temporally consistent way. |\n",
|
||||||
|
"| **enable_early_stopping** | Flag to enable early termination if the score is not improving in the short term. |\n",
|
||||||
|
"| **time_column_name** | The name of your time column. |\n",
|
||||||
|
"| **enable_engineered_explanations** | Engineered feature explanations will be downloaded if enable_engineered_explanations flag is set to True. By default it is set to False to save storage space. |\n",
|
||||||
|
"| **time_series_id_column_name** | The column names used to uniquely identify timeseries in data that has multiple rows with the same timestamp. |\n",
|
||||||
|
"| **track_child_runs** | Flag to disable tracking of child runs. Only best run is tracked if the flag is set to False (this includes the model and metrics of the run). |\n",
|
||||||
|
"| **pipeline_fetch_max_batch_size** | Determines how many pipelines (training algorithms) to fetch at a time for training, this helps reduce throttling when training at large scale. |\n",
|
||||||
|
"| **partition_column_names** | The names of columns used to group your models. For timeseries, the groups must not split up individual time-series. That is, each group must contain one or more whole time-series. |"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"gather": {
|
||||||
|
"logged": 1613007061544
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"from azureml.train.automl.runtime._many_models.many_models_parameters import (\n",
|
||||||
|
" ManyModelsTrainParameters,\n",
|
||||||
|
")\n",
|
||||||
|
"\n",
|
||||||
|
"partition_column_names = [\"Store\", \"Brand\"]\n",
|
||||||
|
"automl_settings = {\n",
|
||||||
|
" \"task\": \"forecasting\",\n",
|
||||||
|
" \"primary_metric\": \"normalized_root_mean_squared_error\",\n",
|
||||||
|
" \"iteration_timeout_minutes\": 10, # This needs to be changed based on the dataset. We ask customer to explore how long training is taking before settings this value\n",
|
||||||
|
" \"iterations\": 15,\n",
|
||||||
|
" \"experiment_timeout_hours\": 0.25,\n",
|
||||||
|
" \"label_column_name\": \"Quantity\",\n",
|
||||||
|
" \"n_cross_validations\": 3,\n",
|
||||||
|
" \"time_column_name\": \"WeekStarting\",\n",
|
||||||
|
" \"drop_column_names\": \"Revenue\",\n",
|
||||||
|
" \"max_horizon\": 6,\n",
|
||||||
|
" \"grain_column_names\": partition_column_names,\n",
|
||||||
|
" \"track_child_runs\": False,\n",
|
||||||
|
"}\n",
|
||||||
|
"\n",
|
||||||
|
"mm_paramters = ManyModelsTrainParameters(\n",
|
||||||
|
" automl_settings=automl_settings, partition_column_names=partition_column_names\n",
|
||||||
|
")"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"### Set up many models pipeline"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"Parallel run step is leveraged to train multiple models at once. To configure the ParallelRunConfig you will need to determine the appropriate number of workers and nodes for your use case. The process_count_per_node is based off the number of cores of the compute VM. The node_count will determine the number of master nodes to use, increasing the node count will speed up the training process.\n",
|
||||||
|
"\n",
|
||||||
|
"| Property | Description|\n",
|
||||||
|
"| :--------------- | :------------------- |\n",
|
||||||
|
"| **experiment** | The experiment used for training. |\n",
|
||||||
|
"| **train_data** | The file dataset to be used as input to the training run. |\n",
|
||||||
|
"| **node_count** | The number of compute nodes to be used for running the user script. We recommend to start with 3 and increase the node_count if the training time is taking too long. |\n",
|
||||||
|
"| **process_count_per_node** | Process count per node, we recommend 2:1 ratio for number of cores: number of processes per node. eg. If node has 16 cores then configure 8 or less process count per node or optimal performance. |\n",
|
||||||
|
"| **train_pipeline_parameters** | The set of configuration parameters defined in the previous section. |\n",
|
||||||
|
"\n",
|
||||||
|
"Calling this method will create a new aggregated dataset which is generated dynamically on pipeline execution."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"from azureml.contrib.automl.pipeline.steps import AutoMLPipelineBuilder\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"training_pipeline_steps = AutoMLPipelineBuilder.get_many_models_train_steps(\n",
|
||||||
|
" experiment=experiment,\n",
|
||||||
|
" train_data=input_ds_small,\n",
|
||||||
|
" compute_target=compute_target,\n",
|
||||||
|
" node_count=2,\n",
|
||||||
|
" process_count_per_node=8,\n",
|
||||||
|
" run_invocation_timeout=920,\n",
|
||||||
|
" train_pipeline_parameters=mm_paramters,\n",
|
||||||
|
")"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"from azureml.pipeline.core import Pipeline\n",
|
||||||
|
"\n",
|
||||||
|
"training_pipeline = Pipeline(ws, steps=training_pipeline_steps)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"### Submit the pipeline to run\n",
|
||||||
|
"Next we submit our pipeline to run. The whole training pipeline takes about 40m using a STANDARD_D16S_V3 VM with our current ParallelRunConfig setting."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"training_run = experiment.submit(training_pipeline)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"training_run.wait_for_completion(show_output=False)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"Check the run status, if training_run is in completed state, continue to forecasting. If training_run is in another state, check the portal for failures."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## 5.0 Publish and schedule the train pipeline (Optional)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"### 5.1 Publish the pipeline\n",
|
||||||
|
"\n",
|
||||||
|
"Once you have a pipeline you're happy with, you can publish a pipeline so you can call it programmatically later on. See this [tutorial](https://docs.microsoft.com/en-us/azure/machine-learning/how-to-create-your-first-pipeline#publish-a-pipeline) for additional information on publishing and calling pipelines."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"# published_pipeline = training_pipeline.publish(name = 'automl_train_many_models',\n",
|
||||||
|
"# description = 'train many models',\n",
|
||||||
|
"# version = '1',\n",
|
||||||
|
"# continue_on_step_failure = False)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"### 7.2 Schedule the pipeline\n",
|
||||||
|
"You can also [schedule the pipeline](https://docs.microsoft.com/en-us/azure/machine-learning/how-to-schedule-pipelines) to run on a time-based or change-based schedule. This could be used to automatically retrain models every month or based on another trigger such as data drift."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"# from azureml.pipeline.core import Schedule, ScheduleRecurrence\n",
|
||||||
|
"\n",
|
||||||
|
"# training_pipeline_id = published_pipeline.id\n",
|
||||||
|
"\n",
|
||||||
|
"# recurrence = ScheduleRecurrence(frequency=\"Month\", interval=1, start_time=\"2020-01-01T09:00:00\")\n",
|
||||||
|
"# recurring_schedule = Schedule.create(ws, name=\"automl_training_recurring_schedule\",\n",
|
||||||
|
"# description=\"Schedule Training Pipeline to run on the first day of every month\",\n",
|
||||||
|
"# pipeline_id=training_pipeline_id,\n",
|
||||||
|
"# experiment_name=experiment.name,\n",
|
||||||
|
"# recurrence=recurrence)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## 6.0 Forecasting"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"### Set up output dataset for inference data\n",
|
||||||
|
"Output of inference can be represented as [OutputFileDatasetConfig](https://docs.microsoft.com/en-us/python/api/azureml-core/azureml.data.output_dataset_config.outputdatasetconfig?view=azure-ml-py) object and OutputFileDatasetConfig can be registered as a dataset. "
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"from azureml.data import OutputFileDatasetConfig\n",
|
||||||
|
"\n",
|
||||||
|
"output_inference_data_ds = OutputFileDatasetConfig(\n",
|
||||||
|
" name=\"many_models_inference_output\", destination=(dstore, \"oj/inference_data/\")\n",
|
||||||
|
").register_on_complete(name=\"oj_inference_data_ds\")"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"For many models we need to provide the ManyModelsInferenceParameters object.\n",
|
||||||
|
"\n",
|
||||||
|
"#### ManyModelsInferenceParameters arguments\n",
|
||||||
|
"| Property | Description|\n",
|
||||||
|
"| :--------------- | :------------------- |\n",
|
||||||
|
"| **partition_column_names** | List of column names that identifies groups. |\n",
|
||||||
|
"| **target_column_name** | \\[Optional] Column name only if the inference dataset has the target. |\n",
|
||||||
|
"| **time_column_name** | \\[Optional] Column name only if it is timeseries. |\n",
|
||||||
|
"| **many_models_run_id** | \\[Optional] Many models run id where models were trained. |\n",
|
||||||
|
"\n",
|
||||||
|
"#### get_many_models_batch_inference_steps arguments\n",
|
||||||
|
"| Property | Description|\n",
|
||||||
|
"| :--------------- | :------------------- |\n",
|
||||||
|
"| **experiment** | The experiment used for inference run. |\n",
|
||||||
|
"| **inference_data** | The data to use for inferencing. It should be the same schema as used for training.\n",
|
||||||
|
"| **compute_target** The compute target that runs the inference pipeline.|\n",
|
||||||
|
"| **node_count** | The number of compute nodes to be used for running the user script. We recommend to start with the number of cores per node (varies by compute sku). |\n",
|
||||||
|
"| **process_count_per_node** The number of processes per node.\n",
|
||||||
|
"| **train_run_id** | \\[Optional] The run id of the hierarchy training, by default it is the latest successful training many model run in the experiment. |\n",
|
||||||
|
"| **train_experiment_name** | \\[Optional] The train experiment that contains the train pipeline. This one is only needed when the train pipeline is not in the same experiement as the inference pipeline. |\n",
|
||||||
|
"| **process_count_per_node** | \\[Optional] The number of processes per node, by default it's 4. |"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"from azureml.contrib.automl.pipeline.steps import AutoMLPipelineBuilder\n",
|
||||||
|
"from azureml.train.automl.runtime._many_models.many_models_parameters import (\n",
|
||||||
|
" ManyModelsInferenceParameters,\n",
|
||||||
|
")\n",
|
||||||
|
"\n",
|
||||||
|
"mm_parameters = ManyModelsInferenceParameters(\n",
|
||||||
|
" partition_column_names=[\"Store\", \"Brand\"],\n",
|
||||||
|
" time_column_name=\"WeekStarting\",\n",
|
||||||
|
" target_column_name=\"Quantity\",\n",
|
||||||
|
")\n",
|
||||||
|
"\n",
|
||||||
|
"inference_steps = AutoMLPipelineBuilder.get_many_models_batch_inference_steps(\n",
|
||||||
|
" experiment=experiment,\n",
|
||||||
|
" inference_data=inference_ds_small,\n",
|
||||||
|
" node_count=2,\n",
|
||||||
|
" process_count_per_node=8,\n",
|
||||||
|
" compute_target=compute_target,\n",
|
||||||
|
" run_invocation_timeout=300,\n",
|
||||||
|
" output_datastore=output_inference_data_ds,\n",
|
||||||
|
" train_run_id=training_run.id,\n",
|
||||||
|
" train_experiment_name=training_run.experiment.name,\n",
|
||||||
|
" inference_pipeline_parameters=mm_parameters,\n",
|
||||||
|
")"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"from azureml.pipeline.core import Pipeline\n",
|
||||||
|
"\n",
|
||||||
|
"inference_pipeline = Pipeline(ws, steps=inference_steps)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"inference_run = experiment.submit(inference_pipeline)\n",
|
||||||
|
"inference_run.wait_for_completion(show_output=False)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Retrieve results\n",
|
||||||
|
"\n",
|
||||||
|
"The forecasting pipeline forecasts the orange juice quantity for a Store by Brand. The pipeline returns one file with the predictions for each store and outputs the result to the forecasting_output Blob container. The details of the blob container is listed in 'forecasting_output.txt' under Outputs+logs. \n",
|
||||||
|
"\n",
|
||||||
|
"The following code snippet:\n",
|
||||||
|
"1. Downloads the contents of the output folder that is passed in the parallel run step \n",
|
||||||
|
"2. Reads the parallel_run_step.txt file that has the predictions as pandas dataframe and \n",
|
||||||
|
"3. Displays the top 10 rows of the predictions"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"from azureml.contrib.automl.pipeline.steps.utilities import get_output_from_mm_pipeline\n",
|
||||||
|
"\n",
|
||||||
|
"forecasting_results_name = \"forecasting_results\"\n",
|
||||||
|
"forecasting_output_name = \"many_models_inference_output\"\n",
|
||||||
|
"forecast_file = get_output_from_mm_pipeline(\n",
|
||||||
|
" inference_run, forecasting_results_name, forecasting_output_name\n",
|
||||||
|
")\n",
|
||||||
|
"df = pd.read_csv(forecast_file, delimiter=\" \", header=None)\n",
|
||||||
|
"df.columns = [\n",
|
||||||
|
" \"Week Starting\",\n",
|
||||||
|
" \"Store\",\n",
|
||||||
|
" \"Brand\",\n",
|
||||||
|
" \"Quantity\",\n",
|
||||||
|
" \"Advert\",\n",
|
||||||
|
" \"Price\",\n",
|
||||||
|
" \"Revenue\",\n",
|
||||||
|
" \"Predicted\",\n",
|
||||||
|
"]\n",
|
||||||
|
"print(\n",
|
||||||
|
" \"Prediction has \", df.shape[0], \" rows. Here the first 10 rows are being displayed.\"\n",
|
||||||
|
")\n",
|
||||||
|
"df.head(10)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## 7.0 Publish and schedule the inference pipeline (Optional)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"### 7.1 Publish the pipeline\n",
|
||||||
|
"\n",
|
||||||
|
"Once you have a pipeline you're happy with, you can publish a pipeline so you can call it programmatically later on. See this [tutorial](https://docs.microsoft.com/en-us/azure/machine-learning/how-to-create-your-first-pipeline#publish-a-pipeline) for additional information on publishing and calling pipelines."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"# published_pipeline_inf = inference_pipeline.publish(name = 'automl_forecast_many_models',\n",
|
||||||
|
"# description = 'forecast many models',\n",
|
||||||
|
"# version = '1',\n",
|
||||||
|
"# continue_on_step_failure = False)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"### 7.2 Schedule the pipeline\n",
|
||||||
|
"You can also [schedule the pipeline](https://docs.microsoft.com/en-us/azure/machine-learning/how-to-schedule-pipelines) to run on a time-based or change-based schedule. This could be used to automatically retrain or forecast models every month or based on another trigger such as data drift."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"# from azureml.pipeline.core import Schedule, ScheduleRecurrence\n",
|
||||||
|
"\n",
|
||||||
|
"# forecasting_pipeline_id = published_pipeline.id\n",
|
||||||
|
"\n",
|
||||||
|
"# recurrence = ScheduleRecurrence(frequency=\"Month\", interval=1, start_time=\"2020-01-01T09:00:00\")\n",
|
||||||
|
"# recurring_schedule = Schedule.create(ws, name=\"automl_forecasting_recurring_schedule\",\n",
|
||||||
|
"# description=\"Schedule Forecasting Pipeline to run on the first day of every week\",\n",
|
||||||
|
"# pipeline_id=forecasting_pipeline_id,\n",
|
||||||
|
"# experiment_name=experiment.name,\n",
|
||||||
|
"# recurrence=recurrence)"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"authors": [
|
||||||
|
{
|
||||||
|
"name": "jialiu"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"categories": [
|
||||||
|
"how-to-use-azureml",
|
||||||
|
"automated-machine-learning"
|
||||||
|
],
|
||||||
|
"kernelspec": {
|
||||||
|
"display_name": "Python 3.6",
|
||||||
|
"language": "python",
|
||||||
|
"name": "python36"
|
||||||
|
},
|
||||||
|
"language_info": {
|
||||||
|
"codemirror_mode": {
|
||||||
|
"name": "ipython",
|
||||||
|
"version": 3
|
||||||
|
},
|
||||||
|
"file_extension": ".py",
|
||||||
|
"mimetype": "text/x-python",
|
||||||
|
"name": "python",
|
||||||
|
"nbconvert_exporter": "python",
|
||||||
|
"pygments_lexer": "ipython3",
|
||||||
|
"version": "3.6.8"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 4
|
||||||
|
}
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
name: auto-ml-forecasting-many-models
|
||||||
|
dependencies:
|
||||||
|
- pip:
|
||||||
|
- azureml-sdk
|
||||||
Binary file not shown.
|
After Width: | Height: | Size: 176 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 165 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 162 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 166 KiB |
@@ -60,7 +60,6 @@
|
|||||||
"source": [
|
"source": [
|
||||||
"import azureml.core\n",
|
"import azureml.core\n",
|
||||||
"import pandas as pd\n",
|
"import pandas as pd\n",
|
||||||
"import numpy as np\n",
|
|
||||||
"import logging\n",
|
"import logging\n",
|
||||||
"\n",
|
"\n",
|
||||||
"from azureml.core.workspace import Workspace\n",
|
"from azureml.core.workspace import Workspace\n",
|
||||||
@@ -82,7 +81,7 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"print(\"This notebook was created using version 1.32.0 of the Azure ML SDK\")\n",
|
"print(\"This notebook was created using version 1.36.0 of the Azure ML SDK\")\n",
|
||||||
"print(\"You are currently using version\", azureml.core.VERSION, \"of the Azure ML SDK\")"
|
"print(\"You are currently using version\", azureml.core.VERSION, \"of the Azure ML SDK\")"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
@@ -102,19 +101,19 @@
|
|||||||
"ws = Workspace.from_config()\n",
|
"ws = Workspace.from_config()\n",
|
||||||
"\n",
|
"\n",
|
||||||
"# choose a name for the run history container in the workspace\n",
|
"# choose a name for the run history container in the workspace\n",
|
||||||
"experiment_name = 'automl-ojforecasting'\n",
|
"experiment_name = \"automl-ojforecasting\"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"experiment = Experiment(ws, experiment_name)\n",
|
"experiment = Experiment(ws, experiment_name)\n",
|
||||||
"\n",
|
"\n",
|
||||||
"output = {}\n",
|
"output = {}\n",
|
||||||
"output['Subscription ID'] = ws.subscription_id\n",
|
"output[\"Subscription ID\"] = ws.subscription_id\n",
|
||||||
"output['Workspace'] = ws.name\n",
|
"output[\"Workspace\"] = ws.name\n",
|
||||||
"output['SKU'] = ws.sku\n",
|
"output[\"SKU\"] = ws.sku\n",
|
||||||
"output['Resource Group'] = ws.resource_group\n",
|
"output[\"Resource Group\"] = ws.resource_group\n",
|
||||||
"output['Location'] = ws.location\n",
|
"output[\"Location\"] = ws.location\n",
|
||||||
"output['Run History Name'] = experiment_name\n",
|
"output[\"Run History Name\"] = experiment_name\n",
|
||||||
"pd.set_option('display.max_colwidth', -1)\n",
|
"pd.set_option(\"display.max_colwidth\", -1)\n",
|
||||||
"outputDf = pd.DataFrame(data = output, index = [''])\n",
|
"outputDf = pd.DataFrame(data=output, index=[\"\"])\n",
|
||||||
"outputDf.T"
|
"outputDf.T"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
@@ -147,10 +146,11 @@
|
|||||||
"# Verify that cluster does not exist already\n",
|
"# Verify that cluster does not exist already\n",
|
||||||
"try:\n",
|
"try:\n",
|
||||||
" compute_target = ComputeTarget(workspace=ws, name=amlcompute_cluster_name)\n",
|
" compute_target = ComputeTarget(workspace=ws, name=amlcompute_cluster_name)\n",
|
||||||
" print('Found existing cluster, use it.')\n",
|
" print(\"Found existing cluster, use it.\")\n",
|
||||||
"except ComputeTargetException:\n",
|
"except ComputeTargetException:\n",
|
||||||
" compute_config = AmlCompute.provisioning_configuration(vm_size='STANDARD_D12_V2',\n",
|
" compute_config = AmlCompute.provisioning_configuration(\n",
|
||||||
" max_nodes=6)\n",
|
" vm_size=\"STANDARD_D12_V2\", max_nodes=6\n",
|
||||||
|
" )\n",
|
||||||
" compute_target = ComputeTarget.create(ws, amlcompute_cluster_name, compute_config)\n",
|
" compute_target = ComputeTarget.create(ws, amlcompute_cluster_name, compute_config)\n",
|
||||||
"\n",
|
"\n",
|
||||||
"compute_target.wait_for_completion(show_output=True)"
|
"compute_target.wait_for_completion(show_output=True)"
|
||||||
@@ -170,11 +170,11 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"time_column_name = 'WeekStarting'\n",
|
"time_column_name = \"WeekStarting\"\n",
|
||||||
"data = pd.read_csv(\"dominicks_OJ.csv\", parse_dates=[time_column_name])\n",
|
"data = pd.read_csv(\"dominicks_OJ.csv\", parse_dates=[time_column_name])\n",
|
||||||
"\n",
|
"\n",
|
||||||
"# Drop the columns 'logQuantity' as it is a leaky feature.\n",
|
"# Drop the columns 'logQuantity' as it is a leaky feature.\n",
|
||||||
"data.drop('logQuantity', axis=1, inplace=True)\n",
|
"data.drop(\"logQuantity\", axis=1, inplace=True)\n",
|
||||||
"\n",
|
"\n",
|
||||||
"data.head()"
|
"data.head()"
|
||||||
]
|
]
|
||||||
@@ -194,9 +194,9 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"time_series_id_column_names = ['Store', 'Brand']\n",
|
"time_series_id_column_names = [\"Store\", \"Brand\"]\n",
|
||||||
"nseries = data.groupby(time_series_id_column_names).ngroups\n",
|
"nseries = data.groupby(time_series_id_column_names).ngroups\n",
|
||||||
"print('Data contains {0} individual time-series.'.format(nseries))"
|
"print(\"Data contains {0} individual time-series.\".format(nseries))"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -215,7 +215,7 @@
|
|||||||
"use_stores = [2, 5, 8]\n",
|
"use_stores = [2, 5, 8]\n",
|
||||||
"data_subset = data[data.Store.isin(use_stores)]\n",
|
"data_subset = data[data.Store.isin(use_stores)]\n",
|
||||||
"nseries = data_subset.groupby(time_series_id_column_names).ngroups\n",
|
"nseries = data_subset.groupby(time_series_id_column_names).ngroups\n",
|
||||||
"print('Data subset contains {0} individual time-series.'.format(nseries))"
|
"print(\"Data subset contains {0} individual time-series.\".format(nseries))"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -234,14 +234,17 @@
|
|||||||
"source": [
|
"source": [
|
||||||
"n_test_periods = 20\n",
|
"n_test_periods = 20\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
"\n",
|
||||||
"def split_last_n_by_series_id(df, n):\n",
|
"def split_last_n_by_series_id(df, n):\n",
|
||||||
" \"\"\"Group df by series identifiers and split on last n rows for each group.\"\"\"\n",
|
" \"\"\"Group df by series identifiers and split on last n rows for each group.\"\"\"\n",
|
||||||
" df_grouped = (df.sort_values(time_column_name) # Sort by ascending time\n",
|
" df_grouped = df.sort_values(time_column_name).groupby( # Sort by ascending time\n",
|
||||||
" .groupby(time_series_id_column_names, group_keys=False))\n",
|
" time_series_id_column_names, group_keys=False\n",
|
||||||
|
" )\n",
|
||||||
" df_head = df_grouped.apply(lambda dfg: dfg.iloc[:-n])\n",
|
" df_head = df_grouped.apply(lambda dfg: dfg.iloc[:-n])\n",
|
||||||
" df_tail = df_grouped.apply(lambda dfg: dfg.iloc[-n:])\n",
|
" df_tail = df_grouped.apply(lambda dfg: dfg.iloc[-n:])\n",
|
||||||
" return df_head, df_tail\n",
|
" return df_head, df_tail\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
"\n",
|
||||||
"train, test = split_last_n_by_series_id(data_subset, n_test_periods)"
|
"train, test = split_last_n_by_series_id(data_subset, n_test_periods)"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
@@ -259,8 +262,8 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"train.to_csv (r'./dominicks_OJ_train.csv', index = None, header=True)\n",
|
"train.to_csv(r\"./dominicks_OJ_train.csv\", index=None, header=True)\n",
|
||||||
"test.to_csv (r'./dominicks_OJ_test.csv', index = None, header=True)"
|
"test.to_csv(r\"./dominicks_OJ_test.csv\", index=None, header=True)"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -270,7 +273,12 @@
|
|||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"datastore = ws.get_default_datastore()\n",
|
"datastore = ws.get_default_datastore()\n",
|
||||||
"datastore.upload_files(files = ['./dominicks_OJ_train.csv', './dominicks_OJ_test.csv'], target_path = 'dataset/', overwrite = True,show_progress = True)"
|
"datastore.upload_files(\n",
|
||||||
|
" files=[\"./dominicks_OJ_train.csv\", \"./dominicks_OJ_test.csv\"],\n",
|
||||||
|
" target_path=\"dataset/\",\n",
|
||||||
|
" overwrite=True,\n",
|
||||||
|
" show_progress=True,\n",
|
||||||
|
")"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -287,8 +295,13 @@
|
|||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"from azureml.core.dataset import Dataset\n",
|
"from azureml.core.dataset import Dataset\n",
|
||||||
"train_dataset = Dataset.Tabular.from_delimited_files(path=datastore.path('dataset/dominicks_OJ_train.csv'))\n",
|
"\n",
|
||||||
"test_dataset = Dataset.Tabular.from_delimited_files(path=datastore.path('dataset/dominicks_OJ_test.csv'))"
|
"train_dataset = Dataset.Tabular.from_delimited_files(\n",
|
||||||
|
" path=datastore.path(\"dataset/dominicks_OJ_train.csv\")\n",
|
||||||
|
")\n",
|
||||||
|
"test_dataset = Dataset.Tabular.from_delimited_files(\n",
|
||||||
|
" path=datastore.path(\"dataset/dominicks_OJ_test.csv\")\n",
|
||||||
|
")"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -324,7 +337,7 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"target_column_name = 'Quantity'"
|
"target_column_name = \"Quantity\""
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -352,13 +365,17 @@
|
|||||||
"source": [
|
"source": [
|
||||||
"featurization_config = FeaturizationConfig()\n",
|
"featurization_config = FeaturizationConfig()\n",
|
||||||
"# Force the CPWVOL5 feature to be numeric type.\n",
|
"# Force the CPWVOL5 feature to be numeric type.\n",
|
||||||
"featurization_config.add_column_purpose('CPWVOL5', 'Numeric')\n",
|
"featurization_config.add_column_purpose(\"CPWVOL5\", \"Numeric\")\n",
|
||||||
"# Fill missing values in the target column, Quantity, with zeros.\n",
|
"# Fill missing values in the target column, Quantity, with zeros.\n",
|
||||||
"featurization_config.add_transformer_params('Imputer', ['Quantity'], {\"strategy\": \"constant\", \"fill_value\": 0})\n",
|
"featurization_config.add_transformer_params(\n",
|
||||||
|
" \"Imputer\", [\"Quantity\"], {\"strategy\": \"constant\", \"fill_value\": 0}\n",
|
||||||
|
")\n",
|
||||||
"# Fill missing values in the INCOME column with median value.\n",
|
"# Fill missing values in the INCOME column with median value.\n",
|
||||||
"featurization_config.add_transformer_params('Imputer', ['INCOME'], {\"strategy\": \"median\"})\n",
|
"featurization_config.add_transformer_params(\n",
|
||||||
|
" \"Imputer\", [\"INCOME\"], {\"strategy\": \"median\"}\n",
|
||||||
|
")\n",
|
||||||
"# Fill missing values in the Price column with forward fill (last value carried forward).\n",
|
"# Fill missing values in the Price column with forward fill (last value carried forward).\n",
|
||||||
"featurization_config.add_transformer_params('Imputer', ['Price'], {\"strategy\": \"ffill\"})"
|
"featurization_config.add_transformer_params(\"Imputer\", [\"Price\"], {\"strategy\": \"ffill\"})"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -424,16 +441,18 @@
|
|||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"from azureml.automl.core.forecasting_parameters import ForecastingParameters\n",
|
"from azureml.automl.core.forecasting_parameters import ForecastingParameters\n",
|
||||||
|
"\n",
|
||||||
"forecasting_parameters = ForecastingParameters(\n",
|
"forecasting_parameters = ForecastingParameters(\n",
|
||||||
" time_column_name=time_column_name,\n",
|
" time_column_name=time_column_name,\n",
|
||||||
" forecast_horizon=n_test_periods,\n",
|
" forecast_horizon=n_test_periods,\n",
|
||||||
" time_series_id_column_names=time_series_id_column_names,\n",
|
" time_series_id_column_names=time_series_id_column_names,\n",
|
||||||
" freq='W-THU' # Set the forecast frequency to be weekly (start on each Thursday)\n",
|
" freq=\"W-THU\", # Set the forecast frequency to be weekly (start on each Thursday)\n",
|
||||||
")\n",
|
")\n",
|
||||||
"\n",
|
"\n",
|
||||||
"automl_config = AutoMLConfig(task='forecasting',\n",
|
"automl_config = AutoMLConfig(\n",
|
||||||
" debug_log='automl_oj_sales_errors.log',\n",
|
" task=\"forecasting\",\n",
|
||||||
" primary_metric='normalized_mean_absolute_error',\n",
|
" debug_log=\"automl_oj_sales_errors.log\",\n",
|
||||||
|
" primary_metric=\"normalized_mean_absolute_error\",\n",
|
||||||
" experiment_timeout_hours=0.25,\n",
|
" experiment_timeout_hours=0.25,\n",
|
||||||
" training_data=train_dataset,\n",
|
" training_data=train_dataset,\n",
|
||||||
" label_column_name=target_column_name,\n",
|
" label_column_name=target_column_name,\n",
|
||||||
@@ -443,7 +462,8 @@
|
|||||||
" n_cross_validations=3,\n",
|
" n_cross_validations=3,\n",
|
||||||
" verbosity=logging.INFO,\n",
|
" verbosity=logging.INFO,\n",
|
||||||
" max_cores_per_iteration=-1,\n",
|
" max_cores_per_iteration=-1,\n",
|
||||||
" forecasting_parameters=forecasting_parameters)"
|
" forecasting_parameters=forecasting_parameters,\n",
|
||||||
|
")"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -488,7 +508,7 @@
|
|||||||
"source": [
|
"source": [
|
||||||
"best_run, fitted_model = remote_run.get_output()\n",
|
"best_run, fitted_model = remote_run.get_output()\n",
|
||||||
"print(fitted_model.steps)\n",
|
"print(fitted_model.steps)\n",
|
||||||
"model_name = best_run.properties['model_name']"
|
"model_name = best_run.properties[\"model_name\"]"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -506,7 +526,7 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"custom_featurizer = fitted_model.named_steps['timeseriestransformer']"
|
"custom_featurizer = fitted_model.named_steps[\"timeseriestransformer\"]"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -560,15 +580,18 @@
|
|||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"from run_forecast import run_remote_inference\n",
|
"from run_forecast import run_remote_inference\n",
|
||||||
"remote_run_infer = run_remote_inference(test_experiment=test_experiment, \n",
|
"\n",
|
||||||
|
"remote_run_infer = run_remote_inference(\n",
|
||||||
|
" test_experiment=test_experiment,\n",
|
||||||
" compute_target=compute_target,\n",
|
" compute_target=compute_target,\n",
|
||||||
" train_run=best_run,\n",
|
" train_run=best_run,\n",
|
||||||
" test_dataset=test_dataset,\n",
|
" test_dataset=test_dataset,\n",
|
||||||
" target_column_name=target_column_name)\n",
|
" target_column_name=target_column_name,\n",
|
||||||
|
")\n",
|
||||||
"remote_run_infer.wait_for_completion(show_output=False)\n",
|
"remote_run_infer.wait_for_completion(show_output=False)\n",
|
||||||
"\n",
|
"\n",
|
||||||
"# download the forecast file to the local machine\n",
|
"# download the forecast file to the local machine\n",
|
||||||
"remote_run_infer.download_file('outputs/predictions.csv', 'predictions.csv')"
|
"remote_run_infer.download_file(\"outputs/predictions.csv\", \"predictions.csv\")"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -589,7 +612,7 @@
|
|||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"# load forecast data frame\n",
|
"# load forecast data frame\n",
|
||||||
"fcst_df = pd.read_csv('predictions.csv', parse_dates=[time_column_name])\n",
|
"fcst_df = pd.read_csv(\"predictions.csv\", parse_dates=[time_column_name])\n",
|
||||||
"fcst_df.head()"
|
"fcst_df.head()"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
@@ -606,18 +629,23 @@
|
|||||||
"# use automl scoring module\n",
|
"# use automl scoring module\n",
|
||||||
"scores = scoring.score_regression(\n",
|
"scores = scoring.score_regression(\n",
|
||||||
" y_test=fcst_df[target_column_name],\n",
|
" y_test=fcst_df[target_column_name],\n",
|
||||||
" y_pred=fcst_df['predicted'],\n",
|
" y_pred=fcst_df[\"predicted\"],\n",
|
||||||
" metrics=list(constants.Metric.SCALAR_REGRESSION_SET))\n",
|
" metrics=list(constants.Metric.SCALAR_REGRESSION_SET),\n",
|
||||||
|
")\n",
|
||||||
"\n",
|
"\n",
|
||||||
"print(\"[Test data scores]\\n\")\n",
|
"print(\"[Test data scores]\\n\")\n",
|
||||||
"for key, value in scores.items(): \n",
|
"for key, value in scores.items():\n",
|
||||||
" print('{}: {:.3f}'.format(key, value))\n",
|
" print(\"{}: {:.3f}\".format(key, value))\n",
|
||||||
" \n",
|
"\n",
|
||||||
"# Plot outputs\n",
|
"# Plot outputs\n",
|
||||||
"%matplotlib inline\n",
|
"%matplotlib inline\n",
|
||||||
"test_pred = plt.scatter(fcst_df[target_column_name], fcst_df['predicted'], color='b')\n",
|
"test_pred = plt.scatter(fcst_df[target_column_name], fcst_df[\"predicted\"], color=\"b\")\n",
|
||||||
"test_test = plt.scatter(fcst_df[target_column_name], fcst_df[target_column_name], color='g')\n",
|
"test_test = plt.scatter(\n",
|
||||||
"plt.legend((test_pred, test_test), ('prediction', 'truth'), loc='upper left', fontsize=8)\n",
|
" fcst_df[target_column_name], fcst_df[target_column_name], color=\"g\"\n",
|
||||||
|
")\n",
|
||||||
|
"plt.legend(\n",
|
||||||
|
" (test_pred, test_test), (\"prediction\", \"truth\"), loc=\"upper left\", fontsize=8\n",
|
||||||
|
")\n",
|
||||||
"plt.show()"
|
"plt.show()"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
@@ -641,9 +669,11 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"description = 'AutoML OJ forecaster'\n",
|
"description = \"AutoML OJ forecaster\"\n",
|
||||||
"tags = None\n",
|
"tags = None\n",
|
||||||
"model = remote_run.register_model(model_name = model_name, description = description, tags = tags)\n",
|
"model = remote_run.register_model(\n",
|
||||||
|
" model_name=model_name, description=description, tags=tags\n",
|
||||||
|
")\n",
|
||||||
"\n",
|
"\n",
|
||||||
"print(remote_run.model_id)"
|
"print(remote_run.model_id)"
|
||||||
]
|
]
|
||||||
@@ -663,8 +693,8 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"script_file_name = 'score_fcast.py'\n",
|
"script_file_name = \"score_fcast.py\"\n",
|
||||||
"best_run.download_file('outputs/scoring_file_v_1_0_0.py', script_file_name)"
|
"best_run.download_file(\"outputs/scoring_file_v_1_0_0.py\", script_file_name)"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -685,15 +715,18 @@
|
|||||||
"from azureml.core.webservice import Webservice\n",
|
"from azureml.core.webservice import Webservice\n",
|
||||||
"from azureml.core.model import Model\n",
|
"from azureml.core.model import Model\n",
|
||||||
"\n",
|
"\n",
|
||||||
"inference_config = InferenceConfig(environment = best_run.get_environment(), \n",
|
"inference_config = InferenceConfig(\n",
|
||||||
" entry_script = script_file_name)\n",
|
" environment=best_run.get_environment(), entry_script=script_file_name\n",
|
||||||
|
")\n",
|
||||||
"\n",
|
"\n",
|
||||||
"aciconfig = AciWebservice.deploy_configuration(cpu_cores = 1, \n",
|
"aciconfig = AciWebservice.deploy_configuration(\n",
|
||||||
" memory_gb = 2, \n",
|
" cpu_cores=2,\n",
|
||||||
" tags = {'type': \"automl-forecasting\"},\n",
|
" memory_gb=4,\n",
|
||||||
" description = \"Automl forecasting sample service\")\n",
|
" tags={\"type\": \"automl-forecasting\"},\n",
|
||||||
|
" description=\"Automl forecasting sample service\",\n",
|
||||||
|
")\n",
|
||||||
"\n",
|
"\n",
|
||||||
"aci_service_name = 'automl-oj-forecast-01'\n",
|
"aci_service_name = \"automl-oj-forecast-01\"\n",
|
||||||
"print(aci_service_name)\n",
|
"print(aci_service_name)\n",
|
||||||
"aci_service = Model.deploy(ws, aci_service_name, [model], inference_config, aciconfig)\n",
|
"aci_service = Model.deploy(ws, aci_service_name, [model], inference_config, aciconfig)\n",
|
||||||
"aci_service.wait_for_deployment(True)\n",
|
"aci_service.wait_for_deployment(True)\n",
|
||||||
@@ -723,20 +756,27 @@
|
|||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"import json\n",
|
"import json\n",
|
||||||
|
"\n",
|
||||||
"X_query = test.copy()\n",
|
"X_query = test.copy()\n",
|
||||||
"X_query.pop(target_column_name)\n",
|
"X_query.pop(target_column_name)\n",
|
||||||
"# We have to convert datetime to string, because Timestamps cannot be serialized to JSON.\n",
|
"# We have to convert datetime to string, because Timestamps cannot be serialized to JSON.\n",
|
||||||
"X_query[time_column_name] = X_query[time_column_name].astype(str)\n",
|
"X_query[time_column_name] = X_query[time_column_name].astype(str)\n",
|
||||||
"# The Service object accept the complex dictionary, which is internally converted to JSON string.\n",
|
"# The Service object accept the complex dictionary, which is internally converted to JSON string.\n",
|
||||||
"# The section 'data' contains the data frame in the form of dictionary.\n",
|
"# The section 'data' contains the data frame in the form of dictionary.\n",
|
||||||
"test_sample = json.dumps({\"data\": json.loads(X_query.to_json(orient=\"records\"))})\n",
|
"sample_quantiles = [0.025, 0.975]\n",
|
||||||
"response = aci_service.run(input_data = test_sample)\n",
|
"test_sample = json.dumps(\n",
|
||||||
|
" {\"data\": X_query.to_dict(orient=\"records\"), \"quantiles\": sample_quantiles}\n",
|
||||||
|
")\n",
|
||||||
|
"response = aci_service.run(input_data=test_sample)\n",
|
||||||
"# translate from networkese to datascientese\n",
|
"# translate from networkese to datascientese\n",
|
||||||
"try: \n",
|
"try:\n",
|
||||||
" res_dict = json.loads(response)\n",
|
" res_dict = json.loads(response)\n",
|
||||||
" y_fcst_all = pd.DataFrame(res_dict['index'])\n",
|
" y_fcst_all = pd.DataFrame(res_dict[\"index\"])\n",
|
||||||
" y_fcst_all[time_column_name] = pd.to_datetime(y_fcst_all[time_column_name], unit = 'ms')\n",
|
" y_fcst_all[time_column_name] = pd.to_datetime(\n",
|
||||||
" y_fcst_all['forecast'] = res_dict['forecast'] \n",
|
" y_fcst_all[time_column_name], unit=\"ms\"\n",
|
||||||
|
" )\n",
|
||||||
|
" y_fcst_all[\"forecast\"] = res_dict[\"forecast\"]\n",
|
||||||
|
" y_fcst_all[\"prediction_interval\"] = res_dict[\"prediction_interval\"]\n",
|
||||||
"except:\n",
|
"except:\n",
|
||||||
" print(res_dict)"
|
" print(res_dict)"
|
||||||
]
|
]
|
||||||
@@ -763,7 +803,7 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"serv = Webservice(ws, 'automl-oj-forecast-01')\n",
|
"serv = Webservice(ws, \"automl-oj-forecast-01\")\n",
|
||||||
"serv.delete() # don't do it accidentally"
|
"serv.delete() # don't do it accidentally"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,62 +5,20 @@ compute instance.
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
import pandas as pd
|
|
||||||
import numpy as np
|
|
||||||
from azureml.core import Dataset, Run
|
from azureml.core import Dataset, Run
|
||||||
from azureml.automl.core.shared.constants import TimeSeriesInternal
|
|
||||||
from sklearn.externals import joblib
|
from sklearn.externals import joblib
|
||||||
from pandas.tseries.frequencies import to_offset
|
from pandas.tseries.frequencies import to_offset
|
||||||
|
|
||||||
|
|
||||||
def align_outputs(y_predicted, X_trans, X_test, y_test, target_column_name,
|
|
||||||
predicted_column_name='predicted',
|
|
||||||
horizon_colname='horizon_origin'):
|
|
||||||
"""
|
|
||||||
Demonstrates how to get the output aligned to the inputs
|
|
||||||
using pandas indexes. Helps understand what happened if
|
|
||||||
the output's shape differs from the input shape, or if
|
|
||||||
the data got re-sorted by time and grain during forecasting.
|
|
||||||
|
|
||||||
Typical causes of misalignment are:
|
|
||||||
* we predicted some periods that were missing in actuals -> drop from eval
|
|
||||||
* model was asked to predict past max_horizon -> increase max horizon
|
|
||||||
* data at start of X_test was needed for lags -> provide previous periods
|
|
||||||
"""
|
|
||||||
|
|
||||||
if (horizon_colname in X_trans):
|
|
||||||
df_fcst = pd.DataFrame({predicted_column_name: y_predicted,
|
|
||||||
horizon_colname: X_trans[horizon_colname]})
|
|
||||||
else:
|
|
||||||
df_fcst = pd.DataFrame({predicted_column_name: y_predicted})
|
|
||||||
|
|
||||||
# y and X outputs are aligned by forecast() function contract
|
|
||||||
df_fcst.index = X_trans.index
|
|
||||||
|
|
||||||
# align original X_test to y_test
|
|
||||||
X_test_full = X_test.copy()
|
|
||||||
X_test_full[target_column_name] = y_test
|
|
||||||
|
|
||||||
# X_test_full's index does not include origin, so reset for merge
|
|
||||||
df_fcst.reset_index(inplace=True)
|
|
||||||
X_test_full = X_test_full.reset_index().drop(columns='index')
|
|
||||||
together = df_fcst.merge(X_test_full, how='right')
|
|
||||||
|
|
||||||
# drop rows where prediction or actuals are nan
|
|
||||||
# happens because of missing actuals
|
|
||||||
# or at edges of time due to lags/rolling windows
|
|
||||||
clean = together[together[[target_column_name,
|
|
||||||
predicted_column_name]].notnull().all(axis=1)]
|
|
||||||
return(clean)
|
|
||||||
|
|
||||||
|
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
'--target_column_name', type=str, dest='target_column_name',
|
"--target_column_name",
|
||||||
help='Target Column Name')
|
type=str,
|
||||||
|
dest="target_column_name",
|
||||||
|
help="Target Column Name",
|
||||||
|
)
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
'--test_dataset', type=str, dest='test_dataset',
|
"--test_dataset", type=str, dest="test_dataset", help="Test Dataset"
|
||||||
help='Test Dataset')
|
)
|
||||||
|
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
target_column_name = args.target_column_name
|
target_column_name = args.target_column_name
|
||||||
@@ -76,14 +34,28 @@ X_test = test_dataset.to_pandas_dataframe().reset_index(drop=True)
|
|||||||
y_test = X_test.pop(target_column_name).values
|
y_test = X_test.pop(target_column_name).values
|
||||||
|
|
||||||
# generate forecast
|
# generate forecast
|
||||||
fitted_model = joblib.load('model.pkl')
|
fitted_model = joblib.load("model.pkl")
|
||||||
y_predictions, X_trans = fitted_model.forecast(X_test)
|
# We have default quantiles values set as below(95th percentile)
|
||||||
|
quantiles = [0.025, 0.5, 0.975]
|
||||||
|
predicted_column_name = "predicted"
|
||||||
|
PI = "prediction_interval"
|
||||||
|
fitted_model.quantiles = quantiles
|
||||||
|
pred_quantiles = fitted_model.forecast_quantiles(X_test)
|
||||||
|
pred_quantiles[PI] = pred_quantiles[[min(quantiles), max(quantiles)]].apply(
|
||||||
|
lambda x: "[{}, {}]".format(x[0], x[1]), axis=1
|
||||||
|
)
|
||||||
|
X_test[target_column_name] = y_test
|
||||||
|
X_test[PI] = pred_quantiles[PI]
|
||||||
|
X_test[predicted_column_name] = pred_quantiles[0.5]
|
||||||
|
# drop rows where prediction or actuals are nan
|
||||||
|
# happens because of missing actuals
|
||||||
|
# or at edges of time due to lags/rolling windows
|
||||||
|
clean = X_test[
|
||||||
|
X_test[[target_column_name, predicted_column_name]].notnull().all(axis=1)
|
||||||
|
]
|
||||||
|
|
||||||
# align output
|
file_name = "outputs/predictions.csv"
|
||||||
df_all = align_outputs(y_predictions, X_trans, X_test, y_test, target_column_name)
|
export_csv = clean.to_csv(file_name, header=True, index=False) # added Index
|
||||||
|
|
||||||
file_name = 'outputs/predictions.csv'
|
|
||||||
export_csv = df_all.to_csv(file_name, header=True, index=False) # added Index
|
|
||||||
|
|
||||||
# Upload the predictions into artifacts
|
# Upload the predictions into artifacts
|
||||||
run.upload_file(name=file_name, path_or_stream=file_name)
|
run.upload_file(name=file_name, path_or_stream=file_name)
|
||||||
|
|||||||
@@ -3,36 +3,47 @@ import shutil
|
|||||||
from azureml.core import ScriptRunConfig
|
from azureml.core import ScriptRunConfig
|
||||||
|
|
||||||
|
|
||||||
def run_remote_inference(test_experiment, compute_target, train_run,
|
def run_remote_inference(
|
||||||
test_dataset, target_column_name, inference_folder='./forecast'):
|
test_experiment,
|
||||||
|
compute_target,
|
||||||
|
train_run,
|
||||||
|
test_dataset,
|
||||||
|
target_column_name,
|
||||||
|
inference_folder="./forecast",
|
||||||
|
):
|
||||||
# Create local directory to copy the model.pkl and forecsting_script.py files into.
|
# Create local directory to copy the model.pkl and forecsting_script.py files into.
|
||||||
# These files will be uploaded to and executed on the compute instance.
|
# These files will be uploaded to and executed on the compute instance.
|
||||||
os.makedirs(inference_folder, exist_ok=True)
|
os.makedirs(inference_folder, exist_ok=True)
|
||||||
shutil.copy('forecasting_script.py', inference_folder)
|
shutil.copy("forecasting_script.py", inference_folder)
|
||||||
|
|
||||||
train_run.download_file('outputs/model.pkl',
|
train_run.download_file(
|
||||||
os.path.join(inference_folder, 'model.pkl'))
|
"outputs/model.pkl", os.path.join(inference_folder, "model.pkl")
|
||||||
|
)
|
||||||
|
|
||||||
inference_env = train_run.get_environment()
|
inference_env = train_run.get_environment()
|
||||||
|
|
||||||
config = ScriptRunConfig(source_directory=inference_folder,
|
config = ScriptRunConfig(
|
||||||
script='forecasting_script.py',
|
source_directory=inference_folder,
|
||||||
arguments=['--target_column_name',
|
script="forecasting_script.py",
|
||||||
|
arguments=[
|
||||||
|
"--target_column_name",
|
||||||
target_column_name,
|
target_column_name,
|
||||||
'--test_dataset',
|
"--test_dataset",
|
||||||
test_dataset.as_named_input(test_dataset.name)],
|
test_dataset.as_named_input(test_dataset.name),
|
||||||
|
],
|
||||||
compute_target=compute_target,
|
compute_target=compute_target,
|
||||||
environment=inference_env)
|
environment=inference_env,
|
||||||
|
)
|
||||||
|
|
||||||
run = test_experiment.submit(config,
|
run = test_experiment.submit(
|
||||||
tags={'training_run_id':
|
config,
|
||||||
train_run.id,
|
tags={
|
||||||
'run_algorithm':
|
"training_run_id": train_run.id,
|
||||||
train_run.properties['run_algorithm'],
|
"run_algorithm": train_run.properties["run_algorithm"],
|
||||||
'valid_score':
|
"valid_score": train_run.properties["score"],
|
||||||
train_run.properties['score'],
|
"primary_metric": train_run.properties["primary_metric"],
|
||||||
'primary_metric':
|
},
|
||||||
train_run.properties['primary_metric']})
|
)
|
||||||
|
|
||||||
run.log("run_algorithm", run.tags['run_algorithm'])
|
run.log("run_algorithm", run.tags["run_algorithm"])
|
||||||
return run
|
return run
|
||||||
|
|||||||
@@ -0,0 +1,494 @@
|
|||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"Copyright (c) Microsoft Corporation. All rights reserved.\n",
|
||||||
|
"\n",
|
||||||
|
"Licensed under the MIT License."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
""
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"In this notebook we will explore the univaraite time-series data to determine the settings for an automated ML experiment. We will follow the thought process depicted in the following diagram:<br/>\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"The objective is to answer the following questions:\n",
|
||||||
|
"\n",
|
||||||
|
"<ol>\n",
|
||||||
|
" <li>Is there a seasonal pattern in the data? </li>\n",
|
||||||
|
" <ul style=\"margin-top:-1px; list-style-type:none\"> \n",
|
||||||
|
" <li> Importance: If we are able to detect regular seasonal patterns, the forecast accuracy may be improved by extracting these patterns and including them as features into the model. </li>\n",
|
||||||
|
" </ul>\n",
|
||||||
|
" <li>Is the data stationary? </li>\n",
|
||||||
|
" <ul style=\"margin-top:-1px; list-style-type:none\"> \n",
|
||||||
|
" <li> Importance: In the absense of features that capture trend behavior, ML models (regression and tree based) are not well equiped to predict stochastic trends. Working with stationary data solves this problem. </li>\n",
|
||||||
|
" </ul>\n",
|
||||||
|
" <li>Is there a detectable auto-regressive pattern in the stationary data? </li>\n",
|
||||||
|
" <ul style=\"margin-top:-1px; list-style-type:none\"> \n",
|
||||||
|
" <li> Importance: The accuracy of ML models can be improved if serial correlation is modeled by including lags of the dependent/target varaible as features. Including target lags in every experiment by default will result in a regression in accuracy scores if such setting is not warranted. </li>\n",
|
||||||
|
" </ul>\n",
|
||||||
|
"</ol>\n",
|
||||||
|
"\n",
|
||||||
|
"The answers to these questions will help determine the appropriate settings for the automated ML experiment.\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"import os\n",
|
||||||
|
"import warnings\n",
|
||||||
|
"import pandas as pd\n",
|
||||||
|
"\n",
|
||||||
|
"from statsmodels.graphics.tsaplots import plot_acf, plot_pacf\n",
|
||||||
|
"import matplotlib.pyplot as plt\n",
|
||||||
|
"from pandas.plotting import register_matplotlib_converters\n",
|
||||||
|
"\n",
|
||||||
|
"register_matplotlib_converters() # fixes the future warning issue\n",
|
||||||
|
"\n",
|
||||||
|
"from helper_functions import unit_root_test_wrapper\n",
|
||||||
|
"from statsmodels.tools.sm_exceptions import InterpolationWarning\n",
|
||||||
|
"\n",
|
||||||
|
"warnings.simplefilter(\"ignore\", InterpolationWarning)\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"# set printing options\n",
|
||||||
|
"pd.set_option(\"display.max_columns\", 500)\n",
|
||||||
|
"pd.set_option(\"display.width\", 1000)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"# load data\n",
|
||||||
|
"main_data_loc = \"data\"\n",
|
||||||
|
"train_file_name = \"S4248SM144SCEN.csv\"\n",
|
||||||
|
"\n",
|
||||||
|
"TARGET_COLNAME = \"S4248SM144SCEN\"\n",
|
||||||
|
"TIME_COLNAME = \"observation_date\"\n",
|
||||||
|
"COVID_PERIOD_START = \"2020-03-01\"\n",
|
||||||
|
"\n",
|
||||||
|
"df = pd.read_csv(os.path.join(main_data_loc, train_file_name))\n",
|
||||||
|
"df[TIME_COLNAME] = pd.to_datetime(df[TIME_COLNAME], format=\"%Y-%m-%d\")\n",
|
||||||
|
"df.sort_values(by=TIME_COLNAME, inplace=True)\n",
|
||||||
|
"df.set_index(TIME_COLNAME, inplace=True)\n",
|
||||||
|
"df.head(2)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"# plot the entire dataset\n",
|
||||||
|
"fig, ax = plt.subplots(figsize=(6, 2), dpi=180)\n",
|
||||||
|
"ax.plot(df)\n",
|
||||||
|
"ax.title.set_text(\"Original Data Series\")\n",
|
||||||
|
"locs, labels = plt.xticks()\n",
|
||||||
|
"plt.xticks(rotation=45)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"The graph plots the alcohol sales in the United States. Because the data is trending, it can be difficult to see cycles, seasonality or other interestng behaviors due to the scaling issues. For example, if there is a seasonal pattern, which we will discuss later, we cannot see them on the trending data. In such case, it is worth plotting the same data in first differences."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"# plot the entire dataset in first differences\n",
|
||||||
|
"fig, ax = plt.subplots(figsize=(6, 2), dpi=180)\n",
|
||||||
|
"ax.plot(df.diff().dropna())\n",
|
||||||
|
"ax.title.set_text(\"Data in first differences\")\n",
|
||||||
|
"locs, labels = plt.xticks()\n",
|
||||||
|
"plt.xticks(rotation=45)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"In the previous plot we observe that the data is more volatile towards the end of the series. This period coincides with the Covid-19 period, so we will exclude it from our experiment. Since in this example there are no user-provided features it is hard to make an argument that a model trained on the less volatile pre-covid data will be able to accurately predict the covid period."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# 1. Seasonality\n",
|
||||||
|
"\n",
|
||||||
|
"#### Questions that need to be answered in this section:\n",
|
||||||
|
"1. Is there a seasonality?\n",
|
||||||
|
"2. If it's seasonal, does the data exhibit a trend (up or down)?\n",
|
||||||
|
"\n",
|
||||||
|
"It is hard to visually detect seasonality when the data is trending. The reason being is scale of seasonal fluctuations is dwarfed by the range of the trend in the data. One way to deal with this is to de-trend the data by taking the first differences. We will discuss this in more detail in the next section."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"# plot the entire dataset in first differences\n",
|
||||||
|
"fig, ax = plt.subplots(figsize=(6, 2), dpi=180)\n",
|
||||||
|
"ax.plot(df.diff().dropna())\n",
|
||||||
|
"ax.title.set_text(\"Data in first differences\")\n",
|
||||||
|
"locs, labels = plt.xticks()\n",
|
||||||
|
"plt.xticks(rotation=45)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"For the next plot, we will exclude the Covid period again. We will also shorten the length of data because plotting a very long time series may prevent us from seeing seasonal patterns, if there are any, because the plot may look like a random walk."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"# remove COVID period\n",
|
||||||
|
"df = df[:COVID_PERIOD_START]\n",
|
||||||
|
"\n",
|
||||||
|
"# plot the entire dataset in first differences\n",
|
||||||
|
"fig, ax = plt.subplots(figsize=(6, 2), dpi=180)\n",
|
||||||
|
"ax.plot(df[\"2015-01-01\":].diff().dropna())\n",
|
||||||
|
"ax.title.set_text(\"Data in first differences\")\n",
|
||||||
|
"locs, labels = plt.xticks()\n",
|
||||||
|
"plt.xticks(rotation=45)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"<p style=\"font-size:150%; color:blue\"> Conclusion </p>\n",
|
||||||
|
"\n",
|
||||||
|
"Visual examination does not suggest clear seasonal patterns. We will set the STL_TYPE = None, and we will move to the next section that examines stationarity. \n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"Say, we are working with a different data set that shows clear patterns of seasonality, we have several options for setting the settings:is hard to say which option will work best in your case, hence you will need to run both options to see which one results in more accurate forecasts. </li>\n",
|
||||||
|
"<ol>\n",
|
||||||
|
" <li> If the data does not appear to be trending, set DIFFERENCE_SERIES=False, TARGET_LAGS=None and STL_TYPE = \"season\" </li>\n",
|
||||||
|
" <li> If the data appears to be trending, consider one of the following two settings:\n",
|
||||||
|
" <ul>\n",
|
||||||
|
" <ol type=\"a\">\n",
|
||||||
|
" <li> DIFFERENCE_SERIES=True, TARGET_LAGS=None and STL_TYPE = \"season\", or </li>\n",
|
||||||
|
" <li> DIFFERENCE_SERIES=False, TARGET_LAGS=None and STL_TYPE = \"trend_season\" </li>\n",
|
||||||
|
" </ol>\n",
|
||||||
|
" <li> In the first case, by taking first differences we are removing stochastic trend, but we do not remove seasonal patterns. In the second case, we do not remove the stochastic trend and it can be captured by the trend component of the STL decomposition. It is hard to say which option will work best in your case, hence you will need to run both options to see which one results in more accurate forecasts. </li>\n",
|
||||||
|
" </ul>\n",
|
||||||
|
"</ol>"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# 2. Stationarity\n",
|
||||||
|
"If the data does not exhibit seasonal patterns, we would like to see if the data is non-stationary. Particularly, we want to see if there is a clear trending behavior. If such behavior is observed, we would like to first difference the data and examine the plot of an auto-correlation function (ACF) known as correlogram. If the data is seasonal, differencing it will not get rid off the seasonality and this will be shown on the correlogram as well.\n",
|
||||||
|
"\n",
|
||||||
|
"<ul>\n",
|
||||||
|
" <li> Question: What is stationarity and how to we detect it? </li>\n",
|
||||||
|
" <ul>\n",
|
||||||
|
" <li> This is a fairly complex topic. Please read the following <a href=\"https://otexts.com/fpp2/stationarity.html\"> link </a> for a high level discussion on this subject. </li>\n",
|
||||||
|
" <li> Simply put, we are looking for scenario when examining the time series plots the mean of the series is roughly the same, regardless which time interval you pick to compute it. Thus, trending and seasonal data are examples of non-stationary series. </li>\n",
|
||||||
|
" </ul>\n",
|
||||||
|
"</ul>\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"<ul>\n",
|
||||||
|
" <li> Question: Why do want to work with stationary data?</li>\n",
|
||||||
|
" <ul> \n",
|
||||||
|
" <li> In the absence of features that capture stochastic trends, the ML models that use (deterministic) time based features (hour of the day, day of the week, month of the year, etc) cannot capture such trends, and will over or under predict depending on the behavior of the time series. By working with stationary data, we eliminate the need to predict such trends, which improves the forecast accuracy. Classical time series models such as Arima and Exponential Smoothing handle non-stationary series by design and do not need such transformations. By differencing the data we are still able to run the same family of models. </li>\n",
|
||||||
|
" </ul>\n",
|
||||||
|
"</ul>\n",
|
||||||
|
"\n",
|
||||||
|
"#### Questions that need to be answered in this section:\n",
|
||||||
|
"<ol> \n",
|
||||||
|
" <li> Is the data stationary? </li>\n",
|
||||||
|
" <li> Does the stationarized data (either the original or the differenced series) exhibit a clear auto-regressive pattern?</li>\n",
|
||||||
|
"</ol>\n",
|
||||||
|
"\n",
|
||||||
|
"To answer the first question, we run a series of tests (we call them unit root tests)."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"# unit root tests\n",
|
||||||
|
"test = unit_root_test_wrapper(df[TARGET_COLNAME])\n",
|
||||||
|
"print(\"---------------\", \"\\n\")\n",
|
||||||
|
"print(\"Summary table\", \"\\n\", test[\"summary\"], \"\\n\")\n",
|
||||||
|
"print(\"Is the {} series stationary?: {}\".format(TARGET_COLNAME, test[\"stationary\"]))\n",
|
||||||
|
"print(\"---------------\", \"\\n\")"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"In the previous cell, we ran a series of unit root tests. The summary table contains the following columns:\n",
|
||||||
|
"<ul> \n",
|
||||||
|
" <li> test_name is the name of the test.\n",
|
||||||
|
" <ul> \n",
|
||||||
|
" <li> ADF: Augmented Dickey-Fuller test </li>\n",
|
||||||
|
" <li> KPSS: Kwiatkowski-Phillips\u00e2\u20ac\u201cSchmidt\u00e2\u20ac\u201cShin test </li>\n",
|
||||||
|
" <li> PP: Phillips-Perron test\n",
|
||||||
|
" <li> ADF GLS: Augmented Dickey-Fuller using generalized least squares method </li>\n",
|
||||||
|
" <li> AZ: Andrews-Zivot test </li>\n",
|
||||||
|
" </ul>\n",
|
||||||
|
" <li> statistic: test statistic </li>\n",
|
||||||
|
" <li> crit_val: critical value of the test statistic </li>\n",
|
||||||
|
" <li> p_val: p-value of the test statistic. If the p-val is less than 0.05, the null hypothesis is rejected. </li>\n",
|
||||||
|
" <li> stationary: is the series stationary based on the test result? </li>\n",
|
||||||
|
" <li> Null hypothesis: what is being tested. Notice, some test such as ADF and PP assume the process has a unit root and looks for evidence to reject this hypothesis. Other tests, ex.g: KPSS, assumes the process is stationary and looks for evidence to reject such claim.\n",
|
||||||
|
"</ul>\n",
|
||||||
|
"\n",
|
||||||
|
"Each of the tests shows that the original time series is non-stationary. The final decision is based on the majority rule. If, there is a split decision, the algorithm will claim it is stationary. We run a series of tests because each test by itself may not be accurate. In many cases when there are conflicting test results, the user needs to make determination if the series is stationary or not.\n",
|
||||||
|
"\n",
|
||||||
|
"Since we found the series to be non-stationary, we will difference it and then test if the differenced series is stationary."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"# unit root tests\n",
|
||||||
|
"test = unit_root_test_wrapper(df[TARGET_COLNAME].diff().dropna())\n",
|
||||||
|
"print(\"---------------\", \"\\n\")\n",
|
||||||
|
"print(\"Summary table\", \"\\n\", test[\"summary\"], \"\\n\")\n",
|
||||||
|
"print(\"Is the {} series stationary?: {}\".format(TARGET_COLNAME, test[\"stationary\"]))\n",
|
||||||
|
"print(\"---------------\", \"\\n\")"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"Four out of five tests show that the series in first differences is stationary. Notice that this decision is not unanimous. Next, let's plot the original series in first-differences to illustrate the difference between non-stationary (unit root) process vs the stationary one."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"# plot original and stationary data\n",
|
||||||
|
"fig = plt.figure(figsize=(10, 10))\n",
|
||||||
|
"ax1 = fig.add_subplot(211)\n",
|
||||||
|
"ax1.plot(df[TARGET_COLNAME], \"-b\")\n",
|
||||||
|
"ax2 = fig.add_subplot(212)\n",
|
||||||
|
"ax2.plot(df[TARGET_COLNAME].diff().dropna(), \"-b\")\n",
|
||||||
|
"ax1.title.set_text(\"Original data\")\n",
|
||||||
|
"ax2.title.set_text(\"Data in first differences\")"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"If you were asked a question \"What is the mean of the series before and after 2008?\", for the series titled \"Original data\" the mean values will be significantly different. This implies that the first moment of the series (in this case, it is the mean) is time dependent, i.e., mean changes depending on the interval one is looking at. Thus, the series is deemed to be non-stationary. On the other hand, for the series titled \"Data in first differences\" the means for both periods are roughly the same. Hence, the first moment is time invariant; meaning it does not depend on the interval of time one is looking at. In this example it is easy to visually distinguish between stationary and non-stationary data. Often this distinction is not easy to make, therefore we rely on the statistical tests described above to help us make an informed decision. "
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"<p style=\"font-size:150%; color:blue\"> Conclusion </p>\n",
|
||||||
|
"Since we found the original process to be non-stationary (contains unit root), we will have to model the data in first differences. As a result, we will set the DIFFERENCE_SERIES parameter to True."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# 3 Check if there is a clear autoregressive pattern\n",
|
||||||
|
"We need to determine if we should include lags of the target variable as features in order to improve forecast accuracy. To do this, we will examine the ACF and partial ACF (PACF) plots of the stationary series. In our case, it is a series in first diffrences.\n",
|
||||||
|
"\n",
|
||||||
|
"<ul>\n",
|
||||||
|
" <li> Question: What is an Auto-regressive pattern? What are we looking for? </li>\n",
|
||||||
|
" <ul style=\"list-style-type:none;\">\n",
|
||||||
|
" <li> We are looking for a classical profiles for an AR(p) process such as an exponential decay of an ACF and a the first $p$ significant lags of the PACF. For a more detailed explanation of ACF and PACF please refer to the appendix at the end of this notebook. For illustration purposes, let's examine the ACF/PACF profiles of the simulated data that follows a second order auto-regressive process, abbreviated as an AR(2). <li/>\n",
|
||||||
|
" <li><img src=\"figures/ACF_PACF_for_AR2.png\" class=\"img_class\">\n",
|
||||||
|
" <br/>\n",
|
||||||
|
" The lag order is on the x-axis while the auto- and partial-correlation coefficients are on the y-axis. Vertical lines that are outside the shaded area represent statistically significant lags. Notice, the ACF function decays to zero and the PACF shows 2 significant spikes (we ignore the first spike for lag 0 in both plots since the linear relationship of any series with itself is always 1). <li/>\n",
|
||||||
|
" </ul>\n",
|
||||||
|
"<ul/>"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"<ul>\n",
|
||||||
|
" <li> Question: What do I do if I observe an auto-regressive behavior? </li>\n",
|
||||||
|
" <ul style=\"list-style-type:none;\">\n",
|
||||||
|
" <li> If such behavior is observed, we might improve the forecast accuracy by enabling the target lags feature in AutoML. There are a few options of doing this </li>\n",
|
||||||
|
" <ol>\n",
|
||||||
|
" <li> Set the target lags parameter to 'auto', or </li>\n",
|
||||||
|
" <li> Specify the list of lags you want to include. Ex.g: target_lags = [1,2,5] </li>\n",
|
||||||
|
" </ol>\n",
|
||||||
|
" </ul>\n",
|
||||||
|
" <br/>\n",
|
||||||
|
" <li> Next, let's examine the ACF and PACF plots of the stationary target variable (depicted below). Here, we do not see a decay in the ACF, instead we see a decay in PACF. It is hard to make an argument the the target variable exhibits auto-regressive behavior. </li>\n",
|
||||||
|
" </ul>"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"# Plot the ACF/PACF for the series in differences\n",
|
||||||
|
"fig, ax = plt.subplots(1, 2, figsize=(10, 5))\n",
|
||||||
|
"plot_acf(df[TARGET_COLNAME].diff().dropna().values.squeeze(), ax=ax[0])\n",
|
||||||
|
"plot_pacf(df[TARGET_COLNAME].diff().dropna().values.squeeze(), ax=ax[1])\n",
|
||||||
|
"plt.show()"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"<p style=\"font-size:150%; color:blue\"> Conclusion </p>\n",
|
||||||
|
"Since we do not see a clear indication of an AR(p) process, we will not be using target lags and will set the TARGET_LAGS parameter to None."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"<p style=\"font-size:150%; color:blue; font-weight: bold\"> AutoML Experiment Settings </p>\n",
|
||||||
|
"Based on the analysis performed, we should try the following settings for the AutoML experiment and use them in the \"2_run_experiment\" notebook.\n",
|
||||||
|
"<ul>\n",
|
||||||
|
" <li> STL_TYPE=None </li>\n",
|
||||||
|
" <li> DIFFERENCE_SERIES=True </li>\n",
|
||||||
|
" <li> TARGET_LAGS=None </li>\n",
|
||||||
|
"</ul>"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Appendix: ACF, PACF and Lag Selection\n",
|
||||||
|
"To do this, we will examine the ACF and partial ACF (PACF) plots of the differenced series. \n",
|
||||||
|
"\n",
|
||||||
|
"<ul>\n",
|
||||||
|
" <li> Question: What is the ACF? </li>\n",
|
||||||
|
" <ul style=\"list-style-type:none;\">\n",
|
||||||
|
" <li> To understand the ACF, first let's look at the correlation coefficient $\\rho_{xz}$\n",
|
||||||
|
" \\begin{equation}\n",
|
||||||
|
" \\rho_{xz} = \\frac{\\sigma_{xz}}{\\sigma_{x} \\sigma_{zy}}\n",
|
||||||
|
" \\end{equation}\n",
|
||||||
|
" </li>\n",
|
||||||
|
" where $\\sigma_{xzy}$ is the covariance between two random variables $X$ and $Z$; $\\sigma_x$ and $\\sigma_z$ is the variance for $X$ and $Z$, respectively. The correlation coefficient measures the strength of linear relationship between two random variables. This metric can take any value from -1 to 1. <li/>\n",
|
||||||
|
" <br/>\n",
|
||||||
|
" <li> The auto-correlation coefficient $\\rho_{Y_{t} Y_{t-k}}$ is the time series equivalent of the correlation coefficient, except instead of measuring linear association between two random variables $X$ and $Z$, it measures the strength of a linear relationship between a random variable $Y_t$ and its lag $Y_{t-k}$ for any positive interger value of $k$. </li> \n",
|
||||||
|
" <br />\n",
|
||||||
|
" <li> To visualize the ACF for a particular lag, say lag 2, plot the second lag of a series $y_{t-2}$ on the x-axis, and plot the series itself $y_t$ on the y-axis. The autocorrelation coefficient is the slope of the best fitted regression line and can be interpreted as follows. A one unit increase in the lag of a variable one period ago leads to a $\\rho_{Y_{t} Y_{t-2}}$ units change in the variable in the current period. This interpreation can be applied to any lag. </li> \n",
|
||||||
|
" <br />\n",
|
||||||
|
" <li> In the interpretation posted above we need to be careful not to confuse the word \"leads\" with \"causes\" since these are not the same thing. We do not know the lagged value of the varaible causes it to change. Afterall, there are probably many other features that may explain the movement in $Y_t$. All we are trying to do in this section is to identify situations when the variable contains the strong auto-regressive components that needs to be included in the model to improve forecast accuracy. </li>\n",
|
||||||
|
" </ul>\n",
|
||||||
|
"</ul>"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"<ul>\n",
|
||||||
|
" <li> Question: What is the PACF? </li>\n",
|
||||||
|
" <ul style=\"list-style-type:none;\">\n",
|
||||||
|
" <li> When describing the ACF we essentially running a regression between a partigular lag of a series, say, lag 4, and the series itself. What this implies is the regression coefficient for lag 4 captures the impact of everything that happens in lags 1, 2 and 3. In other words, if lag 1 is the most important lag and we exclude it from the regression, naturally, the regression model will assign the importance of the 1st lag to the 4th one. Partial auto-correlation function fixes this problem since it measures the contribution of each lag accounting for the information added by the intermediary lags. If we were to illustrate ACF and PACF for the fourth lag using the regression analogy, the difference is a follows: \n",
|
||||||
|
" \\begin{align}\n",
|
||||||
|
" Y_{t} &= a_{0} + a_{4} Y_{t-4} + e_{t} \\\\\n",
|
||||||
|
" Y_{t} &= b_{0} + b_{1} Y_{t-1} + b_{2} Y_{t-2} + b_{3} Y_{t-3} + b_{4} Y_{t-4} + \\varepsilon_{t} \\\\\n",
|
||||||
|
" \\end{align}\n",
|
||||||
|
" </li>\n",
|
||||||
|
" <br/>\n",
|
||||||
|
" <li>\n",
|
||||||
|
" Here, you can think of $a_4$ and $b_{4}$ as the auto- and partial auto-correlation coefficients for lag 4. Notice, in the second equation we explicitely accounting for the intermediate lags by adding them as regrerssors.\n",
|
||||||
|
" </li>\n",
|
||||||
|
" </ul>\n",
|
||||||
|
"</ul>"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"<ul>\n",
|
||||||
|
" <li> Question: Auto-regressive pattern? What are we looking for? </li>\n",
|
||||||
|
" <ul style=\"list-style-type:none;\">\n",
|
||||||
|
" <li> We are looking for a classical profiles for an AR(p) process such as an exponential decay of an ACF and a the first $p$ significant lags of the PACF. Let's examine the ACF/PACF profiles of the same simulated AR(2) shown in Section 3, and check if the ACF/PACF explanation are refelcted in these plots. <li/>\n",
|
||||||
|
" <li><img src=\"figures/ACF_PACF_for_AR2.png\" class=\"img_class\">\n",
|
||||||
|
" <li> The autocorrelation coefficient for the 3rd lag is 0.6, which can be interpreted that a one unit increase in the value of the target varaible three periods ago leads to 0.6 units increase in the current period. However, the PACF plot shows that the partial autocorrealtion coefficient is zero (from a statistical point of view since it lies within the shaded region). This is happening because the 1st and 2nd lags are good predictors of the target variable. Ommiting these two lags from the regression results in the misleading conclusion that the third lag is a good prediciton. <li/>\n",
|
||||||
|
" <br/>\n",
|
||||||
|
" <li> This is why it is important to examine both the ACF and the PACF plots when tring to determine the auto regressive order for the variable in question. <li/>\n",
|
||||||
|
" </ul>\n",
|
||||||
|
"</ul> "
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"authors": [
|
||||||
|
{
|
||||||
|
"name": "vlbejan"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"kernelspec": {
|
||||||
|
"display_name": "Python 3.6",
|
||||||
|
"language": "python",
|
||||||
|
"name": "python36"
|
||||||
|
},
|
||||||
|
"language_info": {
|
||||||
|
"codemirror_mode": {
|
||||||
|
"name": "ipython",
|
||||||
|
"version": 3
|
||||||
|
},
|
||||||
|
"file_extension": ".py",
|
||||||
|
"mimetype": "text/x-python",
|
||||||
|
"name": "python",
|
||||||
|
"nbconvert_exporter": "python",
|
||||||
|
"pygments_lexer": "ipython3",
|
||||||
|
"version": "3.6.9"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 4
|
||||||
|
}
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
name: auto-ml-forecasting-univariate-recipe-experiment-settings
|
||||||
|
dependencies:
|
||||||
|
- pip:
|
||||||
|
- azureml-sdk
|
||||||
@@ -0,0 +1,593 @@
|
|||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"Copyright (c) Microsoft Corporation. All rights reserved.\n",
|
||||||
|
"\n",
|
||||||
|
"Licensed under the MIT License."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
""
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Running AutoML experiments\n",
|
||||||
|
"\n",
|
||||||
|
"See the `auto-ml-forecasting-univariate-recipe-experiment-settings` notebook on how to determine settings for seasonal features, target lags and whether the series needs to be differenced or not. To make experimentation user-friendly, the user has to specify several parameters: DIFFERENCE_SERIES, TARGET_LAGS and STL_TYPE. Once these parameters are set, the notebook will generate correct transformations and settings to run experiments, generate forecasts, compute inference set metrics and plot forecast vs actuals. It will also convert the forecast from first differences to levels (original units of measurement) if the DIFFERENCE_SERIES parameter is set to True before calculating inference set metrics.\n",
|
||||||
|
"\n",
|
||||||
|
"<br/>\n",
|
||||||
|
"\n",
|
||||||
|
"The output generated by this notebook is saved in the `experiment_output`folder."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"### Setup"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"import os\n",
|
||||||
|
"import logging\n",
|
||||||
|
"import pandas as pd\n",
|
||||||
|
"import numpy as np\n",
|
||||||
|
"\n",
|
||||||
|
"import azureml.automl.runtime\n",
|
||||||
|
"from azureml.core.compute import AmlCompute\n",
|
||||||
|
"from azureml.core.compute import ComputeTarget\n",
|
||||||
|
"import matplotlib.pyplot as plt\n",
|
||||||
|
"from helper_functions import ts_train_test_split, compute_metrics\n",
|
||||||
|
"\n",
|
||||||
|
"import azureml.core\n",
|
||||||
|
"from azureml.core.workspace import Workspace\n",
|
||||||
|
"from azureml.core.experiment import Experiment\n",
|
||||||
|
"from azureml.train.automl import AutoMLConfig\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"# set printing options\n",
|
||||||
|
"np.set_printoptions(precision=4, suppress=True, linewidth=100)\n",
|
||||||
|
"pd.set_option(\"display.max_columns\", 500)\n",
|
||||||
|
"pd.set_option(\"display.width\", 1000)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"As part of the setup you have already created a **Workspace**. You will also need to create a [compute target](https://docs.microsoft.com/en-us/azure/machine-learning/service/how-to-set-up-training-targets#amlcompute) for your AutoML run. In this tutorial, you create AmlCompute as your training compute resource.\n",
|
||||||
|
"> Note that if you have an AzureML Data Scientist role, you will not have permission to create compute resources. Talk to your workspace or IT admin to create the compute targets described in this section, if they do not already exist."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"ws = Workspace.from_config()\n",
|
||||||
|
"amlcompute_cluster_name = \"recipe-cluster\"\n",
|
||||||
|
"\n",
|
||||||
|
"found = False\n",
|
||||||
|
"# Check if this compute target already exists in the workspace.\n",
|
||||||
|
"cts = ws.compute_targets\n",
|
||||||
|
"if amlcompute_cluster_name in cts and cts[amlcompute_cluster_name].type == \"AmlCompute\":\n",
|
||||||
|
" found = True\n",
|
||||||
|
" print(\"Found existing compute target.\")\n",
|
||||||
|
" compute_target = cts[amlcompute_cluster_name]\n",
|
||||||
|
"\n",
|
||||||
|
"if not found:\n",
|
||||||
|
" print(\"Creating a new compute target...\")\n",
|
||||||
|
" provisioning_config = AmlCompute.provisioning_configuration(\n",
|
||||||
|
" vm_size=\"STANDARD_D2_V2\", max_nodes=6\n",
|
||||||
|
" )\n",
|
||||||
|
"\n",
|
||||||
|
" # Create the cluster.\\n\",\n",
|
||||||
|
" compute_target = ComputeTarget.create(\n",
|
||||||
|
" ws, amlcompute_cluster_name, provisioning_config\n",
|
||||||
|
" )\n",
|
||||||
|
"\n",
|
||||||
|
"print(\"Checking cluster status...\")\n",
|
||||||
|
"# Can poll for a minimum number of nodes and for a specific timeout.\n",
|
||||||
|
"# If no min_node_count is provided, it will use the scale settings for the cluster.\n",
|
||||||
|
"compute_target.wait_for_completion(\n",
|
||||||
|
" show_output=True, min_node_count=None, timeout_in_minutes=20\n",
|
||||||
|
")"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"### Data\n",
|
||||||
|
"\n",
|
||||||
|
"Here, we will load the data from the csv file and drop the Covid period."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"main_data_loc = \"data\"\n",
|
||||||
|
"train_file_name = \"S4248SM144SCEN.csv\"\n",
|
||||||
|
"\n",
|
||||||
|
"TARGET_COLNAME = \"S4248SM144SCEN\"\n",
|
||||||
|
"TIME_COLNAME = \"observation_date\"\n",
|
||||||
|
"COVID_PERIOD_START = (\n",
|
||||||
|
" \"2020-03-01\" # start of the covid period. To be excluded from evaluation.\n",
|
||||||
|
")\n",
|
||||||
|
"\n",
|
||||||
|
"# load data\n",
|
||||||
|
"df = pd.read_csv(os.path.join(main_data_loc, train_file_name))\n",
|
||||||
|
"df[TIME_COLNAME] = pd.to_datetime(df[TIME_COLNAME], format=\"%Y-%m-%d\")\n",
|
||||||
|
"df.sort_values(by=TIME_COLNAME, inplace=True)\n",
|
||||||
|
"\n",
|
||||||
|
"# remove the Covid period\n",
|
||||||
|
"df = df.query('{} <= \"{}\"'.format(TIME_COLNAME, COVID_PERIOD_START))"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"### Set parameters\n",
|
||||||
|
"\n",
|
||||||
|
"The first set of parameters is based on the analysis performed in the `auto-ml-forecasting-univariate-recipe-experiment-settings` notebook. "
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"# set parameters based on the settings notebook analysis\n",
|
||||||
|
"DIFFERENCE_SERIES = True\n",
|
||||||
|
"TARGET_LAGS = None\n",
|
||||||
|
"STL_TYPE = None"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"Next, define additional parameters to be used in the <a href=\"https://docs.microsoft.com/en-us/python/api/azureml-train-automl-client/azureml.train.automl.automlconfig?view=azure-ml-py\"> AutoML config </a> class.\n",
|
||||||
|
"\n",
|
||||||
|
"<ul> \n",
|
||||||
|
" <li> FORECAST_HORIZON: The forecast horizon is the number of periods into the future that the model should predict. Here, we set the horizon to 12 periods (i.e. 12 quarters). For more discussion of forecast horizons and guiding principles for setting them, please see the <a href=\"https://github.com/Azure/MachineLearningNotebooks/tree/master/how-to-use-azureml/automated-machine-learning/forecasting-energy-demand\"> energy demand notebook </a>. \n",
|
||||||
|
" </li>\n",
|
||||||
|
" <li> TIME_SERIES_ID_COLNAMES: The names of columns used to group a timeseries. It can be used to create multiple series. If time series identifier is not defined, the data set is assumed to be one time-series. This parameter is used with task type forecasting. Since we are working with a single series, this list is empty.\n",
|
||||||
|
" </li>\n",
|
||||||
|
" <li> BLOCKED_MODELS: Optional list of models to be blocked from consideration during model selection stage. At this point we want to consider all ML and Time Series models.\n",
|
||||||
|
" <ul>\n",
|
||||||
|
" <li> See the following <a href=\"https://docs.microsoft.com/en-us/python/api/azureml-train-automl-client/azureml.train.automl.constants.supportedmodels.forecasting?view=azure-ml-py\"> link </a> for a list of supported Forecasting models</li>\n",
|
||||||
|
" </ul>\n",
|
||||||
|
" </li>\n",
|
||||||
|
"</ul>\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"# set other parameters\n",
|
||||||
|
"FORECAST_HORIZON = 12\n",
|
||||||
|
"TIME_SERIES_ID_COLNAMES = []\n",
|
||||||
|
"BLOCKED_MODELS = []"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"To run AutoML, you also need to create an **Experiment**. An Experiment corresponds to a prediction problem you are trying to solve, while a Run corresponds to a specific approach to the problem."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"# choose a name for the run history container in the workspace\n",
|
||||||
|
"if isinstance(TARGET_LAGS, list):\n",
|
||||||
|
" TARGET_LAGS_STR = (\n",
|
||||||
|
" \"-\".join(map(str, TARGET_LAGS)) if (len(TARGET_LAGS) > 0) else None\n",
|
||||||
|
" )\n",
|
||||||
|
"else:\n",
|
||||||
|
" TARGET_LAGS_STR = TARGET_LAGS\n",
|
||||||
|
"\n",
|
||||||
|
"experiment_desc = \"diff-{}_lags-{}_STL-{}\".format(\n",
|
||||||
|
" DIFFERENCE_SERIES, TARGET_LAGS_STR, STL_TYPE\n",
|
||||||
|
")\n",
|
||||||
|
"experiment_name = \"alcohol_{}\".format(experiment_desc)\n",
|
||||||
|
"experiment = Experiment(ws, experiment_name)\n",
|
||||||
|
"\n",
|
||||||
|
"output = {}\n",
|
||||||
|
"output[\"SDK version\"] = azureml.core.VERSION\n",
|
||||||
|
"output[\"Subscription ID\"] = ws.subscription_id\n",
|
||||||
|
"output[\"Workspace\"] = ws.name\n",
|
||||||
|
"output[\"SKU\"] = ws.sku\n",
|
||||||
|
"output[\"Resource Group\"] = ws.resource_group\n",
|
||||||
|
"output[\"Location\"] = ws.location\n",
|
||||||
|
"output[\"Run History Name\"] = experiment_name\n",
|
||||||
|
"pd.set_option(\"display.max_colwidth\", -1)\n",
|
||||||
|
"outputDf = pd.DataFrame(data=output, index=[\"\"])\n",
|
||||||
|
"print(outputDf.T)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"# create output directory\n",
|
||||||
|
"output_dir = \"experiment_output/{}\".format(experiment_desc)\n",
|
||||||
|
"if not os.path.exists(output_dir):\n",
|
||||||
|
" os.makedirs(output_dir)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"# difference data and test for unit root\n",
|
||||||
|
"if DIFFERENCE_SERIES:\n",
|
||||||
|
" df_delta = df.copy()\n",
|
||||||
|
" df_delta[TARGET_COLNAME] = df[TARGET_COLNAME].diff()\n",
|
||||||
|
" df_delta.dropna(axis=0, inplace=True)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"# split the data into train and test set\n",
|
||||||
|
"if DIFFERENCE_SERIES:\n",
|
||||||
|
" # generate train/inference sets using data in first differences\n",
|
||||||
|
" df_train, df_test = ts_train_test_split(\n",
|
||||||
|
" df_input=df_delta,\n",
|
||||||
|
" n=FORECAST_HORIZON,\n",
|
||||||
|
" time_colname=TIME_COLNAME,\n",
|
||||||
|
" ts_id_colnames=TIME_SERIES_ID_COLNAMES,\n",
|
||||||
|
" )\n",
|
||||||
|
"else:\n",
|
||||||
|
" df_train, df_test = ts_train_test_split(\n",
|
||||||
|
" df_input=df,\n",
|
||||||
|
" n=FORECAST_HORIZON,\n",
|
||||||
|
" time_colname=TIME_COLNAME,\n",
|
||||||
|
" ts_id_colnames=TIME_SERIES_ID_COLNAMES,\n",
|
||||||
|
" )"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"### Upload files to the Datastore\n",
|
||||||
|
"The [Machine Learning service workspace](https://docs.microsoft.com/en-us/azure/machine-learning/service/concept-workspace) is paired with the storage account, which contains the default data store. We will use it to upload the bike share data and create [tabular dataset](https://docs.microsoft.com/en-us/python/api/azureml-core/azureml.data.tabulardataset?view=azure-ml-py) for training. A tabular dataset defines a series of lazily-evaluated, immutable operations to load data from the data source into tabular representation."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"df_train.to_csv(\"train.csv\", index=False)\n",
|
||||||
|
"df_test.to_csv(\"test.csv\", index=False)\n",
|
||||||
|
"\n",
|
||||||
|
"datastore = ws.get_default_datastore()\n",
|
||||||
|
"datastore.upload_files(\n",
|
||||||
|
" files=[\"./train.csv\"],\n",
|
||||||
|
" target_path=\"uni-recipe-dataset/tabular/\",\n",
|
||||||
|
" overwrite=True,\n",
|
||||||
|
" show_progress=True,\n",
|
||||||
|
")\n",
|
||||||
|
"datastore.upload_files(\n",
|
||||||
|
" files=[\"./test.csv\"],\n",
|
||||||
|
" target_path=\"uni-recipe-dataset/tabular/\",\n",
|
||||||
|
" overwrite=True,\n",
|
||||||
|
" show_progress=True,\n",
|
||||||
|
")\n",
|
||||||
|
"\n",
|
||||||
|
"from azureml.core import Dataset\n",
|
||||||
|
"\n",
|
||||||
|
"train_dataset = Dataset.Tabular.from_delimited_files(\n",
|
||||||
|
" path=[(datastore, \"uni-recipe-dataset/tabular/train.csv\")]\n",
|
||||||
|
")\n",
|
||||||
|
"test_dataset = Dataset.Tabular.from_delimited_files(\n",
|
||||||
|
" path=[(datastore, \"uni-recipe-dataset/tabular/test.csv\")]\n",
|
||||||
|
")\n",
|
||||||
|
"\n",
|
||||||
|
"# print the first 5 rows of the Dataset\n",
|
||||||
|
"train_dataset.to_pandas_dataframe().reset_index(drop=True).head(5)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"### Config AutoML"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"time_series_settings = {\n",
|
||||||
|
" \"time_column_name\": TIME_COLNAME,\n",
|
||||||
|
" \"forecast_horizon\": FORECAST_HORIZON,\n",
|
||||||
|
" \"target_lags\": TARGET_LAGS,\n",
|
||||||
|
" \"use_stl\": STL_TYPE,\n",
|
||||||
|
" \"blocked_models\": BLOCKED_MODELS,\n",
|
||||||
|
" \"time_series_id_column_names\": TIME_SERIES_ID_COLNAMES,\n",
|
||||||
|
"}\n",
|
||||||
|
"\n",
|
||||||
|
"automl_config = AutoMLConfig(\n",
|
||||||
|
" task=\"forecasting\",\n",
|
||||||
|
" debug_log=\"sample_experiment.log\",\n",
|
||||||
|
" primary_metric=\"normalized_root_mean_squared_error\",\n",
|
||||||
|
" experiment_timeout_minutes=20,\n",
|
||||||
|
" iteration_timeout_minutes=5,\n",
|
||||||
|
" enable_early_stopping=True,\n",
|
||||||
|
" training_data=train_dataset,\n",
|
||||||
|
" label_column_name=TARGET_COLNAME,\n",
|
||||||
|
" n_cross_validations=5,\n",
|
||||||
|
" verbosity=logging.INFO,\n",
|
||||||
|
" max_cores_per_iteration=-1,\n",
|
||||||
|
" compute_target=compute_target,\n",
|
||||||
|
" **time_series_settings,\n",
|
||||||
|
")"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"We will now run the experiment, you can go to Azure ML portal to view the run details."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"remote_run = experiment.submit(automl_config, show_output=False)\n",
|
||||||
|
"remote_run.wait_for_completion()"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"### Retrieve the best model\n",
|
||||||
|
"Below we select the best model from all the training iterations using get_output method."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"best_run, fitted_model = remote_run.get_output()\n",
|
||||||
|
"fitted_model.steps"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"### Inference\n",
|
||||||
|
"\n",
|
||||||
|
"We now use the best fitted model from the AutoML Run to make forecasts for the test set. We will do batch scoring on the test dataset which should have the same schema as training dataset.\n",
|
||||||
|
"\n",
|
||||||
|
"The inference will run on a remote compute. In this example, it will re-use the training compute."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"test_experiment = Experiment(ws, experiment_name + \"_inference\")"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Retreiving forecasts from the model\n",
|
||||||
|
"We have created a function called `run_forecast` that submits the test data to the best model determined during the training run and retrieves forecasts. This function uses a helper script `forecasting_script` which is uploaded and expecuted on the remote compute."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"from run_forecast import run_remote_inference\n",
|
||||||
|
"\n",
|
||||||
|
"remote_run = run_remote_inference(\n",
|
||||||
|
" test_experiment=test_experiment,\n",
|
||||||
|
" compute_target=compute_target,\n",
|
||||||
|
" train_run=best_run,\n",
|
||||||
|
" test_dataset=test_dataset,\n",
|
||||||
|
" target_column_name=TARGET_COLNAME,\n",
|
||||||
|
")\n",
|
||||||
|
"remote_run.wait_for_completion(show_output=False)\n",
|
||||||
|
"\n",
|
||||||
|
"remote_run.download_file(\"outputs/predictions.csv\", f\"{output_dir}/predictions.csv\")"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"### Download the prediction result for metrics calcuation\n",
|
||||||
|
"The test data with predictions are saved in artifact `outputs/predictions.csv`. We will use it to calculate accuracy metrics and vizualize predictions versus actuals."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"X_trans = pd.read_csv(f\"{output_dir}/predictions.csv\", parse_dates=[TIME_COLNAME])\n",
|
||||||
|
"X_trans.head()"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"# convert forecast in differences to levels\n",
|
||||||
|
"def convert_fcst_diff_to_levels(fcst, yt, df_orig):\n",
|
||||||
|
" \"\"\"Convert forecast from first differences to levels.\"\"\"\n",
|
||||||
|
" fcst = fcst.reset_index(drop=False, inplace=False)\n",
|
||||||
|
" fcst[\"predicted_level\"] = fcst[\"predicted\"].cumsum()\n",
|
||||||
|
" fcst[\"predicted_level\"] = fcst[\"predicted_level\"].astype(float) + float(yt)\n",
|
||||||
|
" # merge actuals\n",
|
||||||
|
" out = pd.merge(\n",
|
||||||
|
" fcst, df_orig[[TIME_COLNAME, TARGET_COLNAME]], on=[TIME_COLNAME], how=\"inner\"\n",
|
||||||
|
" )\n",
|
||||||
|
" out.rename(columns={TARGET_COLNAME: \"actual_level\"}, inplace=True)\n",
|
||||||
|
" return out"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"if DIFFERENCE_SERIES:\n",
|
||||||
|
" # convert forecast in differences to the levels\n",
|
||||||
|
" INFORMATION_SET_DATE = max(df_train[TIME_COLNAME])\n",
|
||||||
|
" YT = df.query(\"{} == @INFORMATION_SET_DATE\".format(TIME_COLNAME))[TARGET_COLNAME]\n",
|
||||||
|
"\n",
|
||||||
|
" fcst_df = convert_fcst_diff_to_levels(fcst=X_trans, yt=YT, df_orig=df)\n",
|
||||||
|
"else:\n",
|
||||||
|
" fcst_df = X_trans.copy()\n",
|
||||||
|
" fcst_df[\"actual_level\"] = y_test\n",
|
||||||
|
" fcst_df[\"predicted_level\"] = y_predictions\n",
|
||||||
|
"\n",
|
||||||
|
"del X_trans"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"### Calculate metrics and save output"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"# compute metrics\n",
|
||||||
|
"metrics_df = compute_metrics(fcst_df=fcst_df, metric_name=None, ts_id_colnames=None)\n",
|
||||||
|
"# save output\n",
|
||||||
|
"metrics_file_name = \"{}_metrics.csv\".format(experiment_name)\n",
|
||||||
|
"fcst_file_name = \"{}_forecst.csv\".format(experiment_name)\n",
|
||||||
|
"plot_file_name = \"{}_plot.pdf\".format(experiment_name)\n",
|
||||||
|
"\n",
|
||||||
|
"metrics_df.to_csv(os.path.join(output_dir, metrics_file_name), index=True)\n",
|
||||||
|
"fcst_df.to_csv(os.path.join(output_dir, fcst_file_name), index=True)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"### Generate and save visuals"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"plot_df = df.query('{} > \"2010-01-01\"'.format(TIME_COLNAME))\n",
|
||||||
|
"plot_df.set_index(TIME_COLNAME, inplace=True)\n",
|
||||||
|
"fcst_df.set_index(TIME_COLNAME, inplace=True)\n",
|
||||||
|
"\n",
|
||||||
|
"# generate and save plots\n",
|
||||||
|
"fig, ax = plt.subplots(dpi=180)\n",
|
||||||
|
"ax.plot(plot_df[TARGET_COLNAME], \"-g\", label=\"Historical\")\n",
|
||||||
|
"ax.plot(fcst_df[\"actual_level\"], \"-b\", label=\"Actual\")\n",
|
||||||
|
"ax.plot(fcst_df[\"predicted_level\"], \"-r\", label=\"Forecast\")\n",
|
||||||
|
"ax.legend()\n",
|
||||||
|
"ax.set_title(\"Forecast vs Actuals\")\n",
|
||||||
|
"ax.set_xlabel(TIME_COLNAME)\n",
|
||||||
|
"ax.set_ylabel(TARGET_COLNAME)\n",
|
||||||
|
"locs, labels = plt.xticks()\n",
|
||||||
|
"\n",
|
||||||
|
"plt.setp(labels, rotation=45)\n",
|
||||||
|
"plt.savefig(os.path.join(output_dir, plot_file_name))"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"authors": [
|
||||||
|
{
|
||||||
|
"name": "vlbejan"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"kernelspec": {
|
||||||
|
"display_name": "Python 3.6",
|
||||||
|
"language": "python",
|
||||||
|
"name": "python36"
|
||||||
|
},
|
||||||
|
"language_info": {
|
||||||
|
"codemirror_mode": {
|
||||||
|
"name": "ipython",
|
||||||
|
"version": 3
|
||||||
|
},
|
||||||
|
"file_extension": ".py",
|
||||||
|
"mimetype": "text/x-python",
|
||||||
|
"name": "python",
|
||||||
|
"nbconvert_exporter": "python",
|
||||||
|
"pygments_lexer": "ipython3",
|
||||||
|
"version": "3.6.9"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 4
|
||||||
|
}
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
name: auto-ml-forecasting-univariate-recipe-run-experiment
|
||||||
|
dependencies:
|
||||||
|
- pip:
|
||||||
|
- azureml-sdk
|
||||||
@@ -0,0 +1,350 @@
|
|||||||
|
observation_date,S4248SM144SCEN
|
||||||
|
1992-01-01,4302
|
||||||
|
1992-02-01,4323
|
||||||
|
1992-03-01,4199
|
||||||
|
1992-04-01,4397
|
||||||
|
1992-05-01,4159
|
||||||
|
1992-06-01,4091
|
||||||
|
1992-07-01,4109
|
||||||
|
1992-08-01,4116
|
||||||
|
1992-09-01,4093
|
||||||
|
1992-10-01,4095
|
||||||
|
1992-11-01,4169
|
||||||
|
1992-12-01,4169
|
||||||
|
1993-01-01,4124
|
||||||
|
1993-02-01,4107
|
||||||
|
1993-03-01,4168
|
||||||
|
1993-04-01,4254
|
||||||
|
1993-05-01,4290
|
||||||
|
1993-06-01,4163
|
||||||
|
1993-07-01,4274
|
||||||
|
1993-08-01,4253
|
||||||
|
1993-09-01,4312
|
||||||
|
1993-10-01,4296
|
||||||
|
1993-11-01,4221
|
||||||
|
1993-12-01,4233
|
||||||
|
1994-01-01,4218
|
||||||
|
1994-02-01,4237
|
||||||
|
1994-03-01,4343
|
||||||
|
1994-04-01,4357
|
||||||
|
1994-05-01,4264
|
||||||
|
1994-06-01,4392
|
||||||
|
1994-07-01,4381
|
||||||
|
1994-08-01,4290
|
||||||
|
1994-09-01,4348
|
||||||
|
1994-10-01,4357
|
||||||
|
1994-11-01,4417
|
||||||
|
1994-12-01,4411
|
||||||
|
1995-01-01,4417
|
||||||
|
1995-02-01,4339
|
||||||
|
1995-03-01,4256
|
||||||
|
1995-04-01,4276
|
||||||
|
1995-05-01,4290
|
||||||
|
1995-06-01,4413
|
||||||
|
1995-07-01,4305
|
||||||
|
1995-08-01,4476
|
||||||
|
1995-09-01,4393
|
||||||
|
1995-10-01,4447
|
||||||
|
1995-11-01,4492
|
||||||
|
1995-12-01,4489
|
||||||
|
1996-01-01,4635
|
||||||
|
1996-02-01,4697
|
||||||
|
1996-03-01,4588
|
||||||
|
1996-04-01,4633
|
||||||
|
1996-05-01,4685
|
||||||
|
1996-06-01,4672
|
||||||
|
1996-07-01,4666
|
||||||
|
1996-08-01,4726
|
||||||
|
1996-09-01,4571
|
||||||
|
1996-10-01,4624
|
||||||
|
1996-11-01,4691
|
||||||
|
1996-12-01,4604
|
||||||
|
1997-01-01,4657
|
||||||
|
1997-02-01,4711
|
||||||
|
1997-03-01,4810
|
||||||
|
1997-04-01,4626
|
||||||
|
1997-05-01,4860
|
||||||
|
1997-06-01,4757
|
||||||
|
1997-07-01,4916
|
||||||
|
1997-08-01,4921
|
||||||
|
1997-09-01,4985
|
||||||
|
1997-10-01,4905
|
||||||
|
1997-11-01,4880
|
||||||
|
1997-12-01,5165
|
||||||
|
1998-01-01,4885
|
||||||
|
1998-02-01,4925
|
||||||
|
1998-03-01,5049
|
||||||
|
1998-04-01,5090
|
||||||
|
1998-05-01,5094
|
||||||
|
1998-06-01,4929
|
||||||
|
1998-07-01,5132
|
||||||
|
1998-08-01,5061
|
||||||
|
1998-09-01,5471
|
||||||
|
1998-10-01,5327
|
||||||
|
1998-11-01,5257
|
||||||
|
1998-12-01,5354
|
||||||
|
1999-01-01,5427
|
||||||
|
1999-02-01,5415
|
||||||
|
1999-03-01,5387
|
||||||
|
1999-04-01,5483
|
||||||
|
1999-05-01,5510
|
||||||
|
1999-06-01,5539
|
||||||
|
1999-07-01,5532
|
||||||
|
1999-08-01,5625
|
||||||
|
1999-09-01,5799
|
||||||
|
1999-10-01,5843
|
||||||
|
1999-11-01,5836
|
||||||
|
1999-12-01,5724
|
||||||
|
2000-01-01,5757
|
||||||
|
2000-02-01,5731
|
||||||
|
2000-03-01,5839
|
||||||
|
2000-04-01,5825
|
||||||
|
2000-05-01,5877
|
||||||
|
2000-06-01,5979
|
||||||
|
2000-07-01,5828
|
||||||
|
2000-08-01,6016
|
||||||
|
2000-09-01,6113
|
||||||
|
2000-10-01,6150
|
||||||
|
2000-11-01,6111
|
||||||
|
2000-12-01,6088
|
||||||
|
2001-01-01,6360
|
||||||
|
2001-02-01,6300
|
||||||
|
2001-03-01,5935
|
||||||
|
2001-04-01,6204
|
||||||
|
2001-05-01,6164
|
||||||
|
2001-06-01,6231
|
||||||
|
2001-07-01,6336
|
||||||
|
2001-08-01,6179
|
||||||
|
2001-09-01,6120
|
||||||
|
2001-10-01,6134
|
||||||
|
2001-11-01,6381
|
||||||
|
2001-12-01,6521
|
||||||
|
2002-01-01,6333
|
||||||
|
2002-02-01,6541
|
||||||
|
2002-03-01,6692
|
||||||
|
2002-04-01,6591
|
||||||
|
2002-05-01,6554
|
||||||
|
2002-06-01,6596
|
||||||
|
2002-07-01,6620
|
||||||
|
2002-08-01,6577
|
||||||
|
2002-09-01,6625
|
||||||
|
2002-10-01,6441
|
||||||
|
2002-11-01,6584
|
||||||
|
2002-12-01,6923
|
||||||
|
2003-01-01,6600
|
||||||
|
2003-02-01,6742
|
||||||
|
2003-03-01,6831
|
||||||
|
2003-04-01,6782
|
||||||
|
2003-05-01,6714
|
||||||
|
2003-06-01,6736
|
||||||
|
2003-07-01,7146
|
||||||
|
2003-08-01,7027
|
||||||
|
2003-09-01,6896
|
||||||
|
2003-10-01,7107
|
||||||
|
2003-11-01,6997
|
||||||
|
2003-12-01,7075
|
||||||
|
2004-01-01,7235
|
||||||
|
2004-02-01,7072
|
||||||
|
2004-03-01,6968
|
||||||
|
2004-04-01,7144
|
||||||
|
2004-05-01,7232
|
||||||
|
2004-06-01,7095
|
||||||
|
2004-07-01,7181
|
||||||
|
2004-08-01,7146
|
||||||
|
2004-09-01,7230
|
||||||
|
2004-10-01,7327
|
||||||
|
2004-11-01,7328
|
||||||
|
2004-12-01,7425
|
||||||
|
2005-01-01,7520
|
||||||
|
2005-02-01,7551
|
||||||
|
2005-03-01,7572
|
||||||
|
2005-04-01,7701
|
||||||
|
2005-05-01,7819
|
||||||
|
2005-06-01,7770
|
||||||
|
2005-07-01,7627
|
||||||
|
2005-08-01,7816
|
||||||
|
2005-09-01,7718
|
||||||
|
2005-10-01,7772
|
||||||
|
2005-11-01,7788
|
||||||
|
2005-12-01,7576
|
||||||
|
2006-01-01,7940
|
||||||
|
2006-02-01,8027
|
||||||
|
2006-03-01,7884
|
||||||
|
2006-04-01,8043
|
||||||
|
2006-05-01,7995
|
||||||
|
2006-06-01,8218
|
||||||
|
2006-07-01,8159
|
||||||
|
2006-08-01,8331
|
||||||
|
2006-09-01,8320
|
||||||
|
2006-10-01,8397
|
||||||
|
2006-11-01,8603
|
||||||
|
2006-12-01,8515
|
||||||
|
2007-01-01,8336
|
||||||
|
2007-02-01,8233
|
||||||
|
2007-03-01,8475
|
||||||
|
2007-04-01,8310
|
||||||
|
2007-05-01,8583
|
||||||
|
2007-06-01,8645
|
||||||
|
2007-07-01,8713
|
||||||
|
2007-08-01,8636
|
||||||
|
2007-09-01,8791
|
||||||
|
2007-10-01,8984
|
||||||
|
2007-11-01,8867
|
||||||
|
2007-12-01,9059
|
||||||
|
2008-01-01,8911
|
||||||
|
2008-02-01,8701
|
||||||
|
2008-03-01,8956
|
||||||
|
2008-04-01,9095
|
||||||
|
2008-05-01,9102
|
||||||
|
2008-06-01,9170
|
||||||
|
2008-07-01,9194
|
||||||
|
2008-08-01,9164
|
||||||
|
2008-09-01,9337
|
||||||
|
2008-10-01,9186
|
||||||
|
2008-11-01,9029
|
||||||
|
2008-12-01,9025
|
||||||
|
2009-01-01,9486
|
||||||
|
2009-02-01,9219
|
||||||
|
2009-03-01,9059
|
||||||
|
2009-04-01,9171
|
||||||
|
2009-05-01,9114
|
||||||
|
2009-06-01,8926
|
||||||
|
2009-07-01,9150
|
||||||
|
2009-08-01,9105
|
||||||
|
2009-09-01,9011
|
||||||
|
2009-10-01,8743
|
||||||
|
2009-11-01,8958
|
||||||
|
2009-12-01,8969
|
||||||
|
2010-01-01,8984
|
||||||
|
2010-02-01,9068
|
||||||
|
2010-03-01,9335
|
||||||
|
2010-04-01,9481
|
||||||
|
2010-05-01,9132
|
||||||
|
2010-06-01,9192
|
||||||
|
2010-07-01,9123
|
||||||
|
2010-08-01,9091
|
||||||
|
2010-09-01,9155
|
||||||
|
2010-10-01,9556
|
||||||
|
2010-11-01,9477
|
||||||
|
2010-12-01,9436
|
||||||
|
2011-01-01,9519
|
||||||
|
2011-02-01,9667
|
||||||
|
2011-03-01,9668
|
||||||
|
2011-04-01,9628
|
||||||
|
2011-05-01,9376
|
||||||
|
2011-06-01,9830
|
||||||
|
2011-07-01,9626
|
||||||
|
2011-08-01,9802
|
||||||
|
2011-09-01,9858
|
||||||
|
2011-10-01,9838
|
||||||
|
2011-11-01,9846
|
||||||
|
2011-12-01,9789
|
||||||
|
2012-01-01,9955
|
||||||
|
2012-02-01,9909
|
||||||
|
2012-03-01,9897
|
||||||
|
2012-04-01,9909
|
||||||
|
2012-05-01,10127
|
||||||
|
2012-06-01,10175
|
||||||
|
2012-07-01,10129
|
||||||
|
2012-08-01,10251
|
||||||
|
2012-09-01,10227
|
||||||
|
2012-10-01,10174
|
||||||
|
2012-11-01,10402
|
||||||
|
2012-12-01,10664
|
||||||
|
2013-01-01,10585
|
||||||
|
2013-02-01,10661
|
||||||
|
2013-03-01,10649
|
||||||
|
2013-04-01,10676
|
||||||
|
2013-05-01,10863
|
||||||
|
2013-06-01,10690
|
||||||
|
2013-07-01,11007
|
||||||
|
2013-08-01,10835
|
||||||
|
2013-09-01,10900
|
||||||
|
2013-10-01,10749
|
||||||
|
2013-11-01,10946
|
||||||
|
2013-12-01,10864
|
||||||
|
2014-01-01,10726
|
||||||
|
2014-02-01,10821
|
||||||
|
2014-03-01,10789
|
||||||
|
2014-04-01,10892
|
||||||
|
2014-05-01,10892
|
||||||
|
2014-06-01,10789
|
||||||
|
2014-07-01,10662
|
||||||
|
2014-08-01,10767
|
||||||
|
2014-09-01,10779
|
||||||
|
2014-10-01,10922
|
||||||
|
2014-11-01,10662
|
||||||
|
2014-12-01,10808
|
||||||
|
2015-01-01,10865
|
||||||
|
2015-02-01,10740
|
||||||
|
2015-03-01,10917
|
||||||
|
2015-04-01,10933
|
||||||
|
2015-05-01,11074
|
||||||
|
2015-06-01,11108
|
||||||
|
2015-07-01,11493
|
||||||
|
2015-08-01,11386
|
||||||
|
2015-09-01,11502
|
||||||
|
2015-10-01,11487
|
||||||
|
2015-11-01,11375
|
||||||
|
2015-12-01,11445
|
||||||
|
2016-01-01,11787
|
||||||
|
2016-02-01,11792
|
||||||
|
2016-03-01,11649
|
||||||
|
2016-04-01,11810
|
||||||
|
2016-05-01,11496
|
||||||
|
2016-06-01,11600
|
||||||
|
2016-07-01,11503
|
||||||
|
2016-08-01,11715
|
||||||
|
2016-09-01,11732
|
||||||
|
2016-10-01,11885
|
||||||
|
2016-11-01,12092
|
||||||
|
2016-12-01,11857
|
||||||
|
2017-01-01,11881
|
||||||
|
2017-02-01,12355
|
||||||
|
2017-03-01,12027
|
||||||
|
2017-04-01,12183
|
||||||
|
2017-05-01,12170
|
||||||
|
2017-06-01,12387
|
||||||
|
2017-07-01,12041
|
||||||
|
2017-08-01,12139
|
||||||
|
2017-09-01,11861
|
||||||
|
2017-10-01,12202
|
||||||
|
2017-11-01,12178
|
||||||
|
2017-12-01,12126
|
||||||
|
2018-01-01,11942
|
||||||
|
2018-02-01,12206
|
||||||
|
2018-03-01,12362
|
||||||
|
2018-04-01,12287
|
||||||
|
2018-05-01,12497
|
||||||
|
2018-06-01,12621
|
||||||
|
2018-07-01,12729
|
||||||
|
2018-08-01,12689
|
||||||
|
2018-09-01,12874
|
||||||
|
2018-10-01,12776
|
||||||
|
2018-11-01,12995
|
||||||
|
2018-12-01,13291
|
||||||
|
2019-01-01,13364
|
||||||
|
2019-02-01,13135
|
||||||
|
2019-03-01,13123
|
||||||
|
2019-04-01,13110
|
||||||
|
2019-05-01,13152
|
||||||
|
2019-06-01,13201
|
||||||
|
2019-07-01,13354
|
||||||
|
2019-08-01,13427
|
||||||
|
2019-09-01,13472
|
||||||
|
2019-10-01,13436
|
||||||
|
2019-11-01,13430
|
||||||
|
2019-12-01,13588
|
||||||
|
2020-01-01,13533
|
||||||
|
2020-02-01,13575
|
||||||
|
2020-03-01,13867
|
||||||
|
2020-04-01,12319
|
||||||
|
2020-05-01,13909
|
||||||
|
2020-06-01,13982
|
||||||
|
2020-07-01,15384
|
||||||
|
2020-08-01,15701
|
||||||
|
2020-09-01,15006
|
||||||
|
2020-10-01,15741
|
||||||
|
2020-11-01,14934
|
||||||
|
2020-12-01,13061
|
||||||
|
2021-01-01,15743
|
||||||
|
Binary file not shown.
|
After Width: | Height: | Size: 18 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 212 KiB |
@@ -0,0 +1,70 @@
|
|||||||
|
"""
|
||||||
|
This is the script that is executed on the compute instance. It relies
|
||||||
|
on the model.pkl file which is uploaded along with this script to the
|
||||||
|
compute instance.
|
||||||
|
"""
|
||||||
|
|
||||||
|
import argparse
|
||||||
|
from azureml.core import Dataset, Run
|
||||||
|
from azureml.automl.core.shared.constants import TimeSeriesInternal
|
||||||
|
from sklearn.externals import joblib
|
||||||
|
|
||||||
|
parser = argparse.ArgumentParser()
|
||||||
|
parser.add_argument(
|
||||||
|
"--target_column_name",
|
||||||
|
type=str,
|
||||||
|
dest="target_column_name",
|
||||||
|
help="Target Column Name",
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"--test_dataset", type=str, dest="test_dataset", help="Test Dataset"
|
||||||
|
)
|
||||||
|
|
||||||
|
args = parser.parse_args()
|
||||||
|
target_column_name = args.target_column_name
|
||||||
|
test_dataset_id = args.test_dataset
|
||||||
|
|
||||||
|
run = Run.get_context()
|
||||||
|
ws = run.experiment.workspace
|
||||||
|
|
||||||
|
# get the input dataset by id
|
||||||
|
test_dataset = Dataset.get_by_id(ws, id=test_dataset_id)
|
||||||
|
|
||||||
|
X_test = (
|
||||||
|
test_dataset.drop_columns(columns=[target_column_name])
|
||||||
|
.to_pandas_dataframe()
|
||||||
|
.reset_index(drop=True)
|
||||||
|
)
|
||||||
|
y_test_df = (
|
||||||
|
test_dataset.with_timestamp_columns(None)
|
||||||
|
.keep_columns(columns=[target_column_name])
|
||||||
|
.to_pandas_dataframe()
|
||||||
|
)
|
||||||
|
|
||||||
|
# generate forecast
|
||||||
|
fitted_model = joblib.load("model.pkl")
|
||||||
|
# We have default quantiles values set as below(95th percentile)
|
||||||
|
quantiles = [0.025, 0.5, 0.975]
|
||||||
|
predicted_column_name = "predicted"
|
||||||
|
PI = "prediction_interval"
|
||||||
|
fitted_model.quantiles = quantiles
|
||||||
|
pred_quantiles = fitted_model.forecast_quantiles(X_test)
|
||||||
|
pred_quantiles[PI] = pred_quantiles[[min(quantiles), max(quantiles)]].apply(
|
||||||
|
lambda x: "[{}, {}]".format(x[0], x[1]), axis=1
|
||||||
|
)
|
||||||
|
X_test[target_column_name] = y_test_df[target_column_name]
|
||||||
|
X_test[PI] = pred_quantiles[PI]
|
||||||
|
X_test[predicted_column_name] = pred_quantiles[0.5]
|
||||||
|
# drop rows where prediction or actuals are nan
|
||||||
|
# happens because of missing actuals
|
||||||
|
# or at edges of time due to lags/rolling windows
|
||||||
|
clean = X_test[
|
||||||
|
X_test[[target_column_name, predicted_column_name]].notnull().all(axis=1)
|
||||||
|
]
|
||||||
|
clean.rename(columns={target_column_name: "actual"}, inplace=True)
|
||||||
|
|
||||||
|
file_name = "outputs/predictions.csv"
|
||||||
|
export_csv = clean.to_csv(file_name, header=True, index=False) # added Index
|
||||||
|
|
||||||
|
# Upload the predictions into artifacts
|
||||||
|
run.upload_file(name=file_name, path_or_stream=file_name)
|
||||||
@@ -0,0 +1,263 @@
|
|||||||
|
"""
|
||||||
|
Helper functions to determine AutoML experiment settings for forecasting.
|
||||||
|
"""
|
||||||
|
import pandas as pd
|
||||||
|
import statsmodels.tsa.stattools as stattools
|
||||||
|
from arch import unitroot
|
||||||
|
from azureml.automl.core.shared import constants
|
||||||
|
from azureml.automl.runtime.shared.score import scoring
|
||||||
|
|
||||||
|
|
||||||
|
def adf_test(series, **kw):
|
||||||
|
"""
|
||||||
|
Wrapper for the augmented Dickey-Fuller test. Allows users to set the lag order.
|
||||||
|
|
||||||
|
:param series: series to test
|
||||||
|
:return: dictionary of results
|
||||||
|
"""
|
||||||
|
if "lags" in kw.keys():
|
||||||
|
msg = "Lag order of {} detected. Running the ADF test...".format(
|
||||||
|
str(kw["lags"])
|
||||||
|
)
|
||||||
|
print(msg)
|
||||||
|
statistic, pval, critval, resstore = stattools.adfuller(
|
||||||
|
series, maxlag=kw["lags"], autolag=kw["autolag"], store=kw["store"]
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
statistic, pval, critval, resstore = stattools.adfuller(
|
||||||
|
series, autolag=kw["IC"], store=kw["store"]
|
||||||
|
)
|
||||||
|
|
||||||
|
output = {
|
||||||
|
"statistic": statistic,
|
||||||
|
"pval": pval,
|
||||||
|
"critical": critval,
|
||||||
|
"resstore": resstore,
|
||||||
|
}
|
||||||
|
return output
|
||||||
|
|
||||||
|
|
||||||
|
def kpss_test(series, **kw):
|
||||||
|
"""
|
||||||
|
Wrapper for the KPSS test. Allows users to set the lag order.
|
||||||
|
|
||||||
|
:param series: series to test
|
||||||
|
:return: dictionary of results
|
||||||
|
"""
|
||||||
|
if kw["store"]:
|
||||||
|
statistic, p_value, critical_values, rstore = stattools.kpss(
|
||||||
|
series, regression=kw["reg_type"], lags=kw["lags"], store=kw["store"]
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
statistic, p_value, lags, critical_values = stattools.kpss(
|
||||||
|
series, regression=kw["reg_type"], lags=kw["lags"]
|
||||||
|
)
|
||||||
|
output = {
|
||||||
|
"statistic": statistic,
|
||||||
|
"pval": p_value,
|
||||||
|
"critical": critical_values,
|
||||||
|
"lags": rstore.lags if kw["store"] else lags,
|
||||||
|
}
|
||||||
|
|
||||||
|
if kw["store"]:
|
||||||
|
output.update({"resstore": rstore})
|
||||||
|
return output
|
||||||
|
|
||||||
|
|
||||||
|
def format_test_output(test_name, test_res, H0_unit_root=True):
|
||||||
|
"""
|
||||||
|
Helper function to format output. Return a dictionary with specific keys. Will be used to
|
||||||
|
construct the summary data frame for all unit root tests.
|
||||||
|
|
||||||
|
TODO: Add functionality of choosing based on the max lag order specified by user.
|
||||||
|
|
||||||
|
:param test_name: name of the test
|
||||||
|
:param test_res: object that contains corresponding test information. Can be None if test failed.
|
||||||
|
:param H0_unit_root: does the null hypothesis of the test assume a unit root process? Some tests do (ADF),
|
||||||
|
some don't (KPSS).
|
||||||
|
:return: dictionary of summary table for all tests and final decision on stationary vs non-stationary.
|
||||||
|
If test failed (test_res is None), return empty dictionary.
|
||||||
|
"""
|
||||||
|
# Check if the test failed by trying to extract the test statistic
|
||||||
|
if test_name in ("ADF", "KPSS"):
|
||||||
|
try:
|
||||||
|
test_res["statistic"]
|
||||||
|
except BaseException:
|
||||||
|
test_res = None
|
||||||
|
else:
|
||||||
|
try:
|
||||||
|
test_res.stat
|
||||||
|
except BaseException:
|
||||||
|
test_res = None
|
||||||
|
|
||||||
|
if test_res is None:
|
||||||
|
return {}
|
||||||
|
|
||||||
|
# extract necessary information
|
||||||
|
if test_name in ("ADF", "KPSS"):
|
||||||
|
statistic = test_res["statistic"]
|
||||||
|
crit_val = test_res["critical"]["5%"]
|
||||||
|
p_val = test_res["pval"]
|
||||||
|
lags = test_res["resstore"].usedlag if test_name == "ADF" else test_res["lags"]
|
||||||
|
else:
|
||||||
|
statistic = test_res.stat
|
||||||
|
crit_val = test_res.critical_values["5%"]
|
||||||
|
p_val = test_res.pvalue
|
||||||
|
lags = test_res.lags
|
||||||
|
|
||||||
|
if H0_unit_root:
|
||||||
|
H0 = "The process is non-stationary"
|
||||||
|
stationary = "yes" if p_val < 0.05 else "not"
|
||||||
|
else:
|
||||||
|
H0 = "The process is stationary"
|
||||||
|
stationary = "yes" if p_val > 0.05 else "not"
|
||||||
|
|
||||||
|
out = {
|
||||||
|
"test_name": test_name,
|
||||||
|
"statistic": statistic,
|
||||||
|
"crit_val": crit_val,
|
||||||
|
"p_val": p_val,
|
||||||
|
"lags": int(lags),
|
||||||
|
"stationary": stationary,
|
||||||
|
"Null Hypothesis": H0,
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
|
||||||
|
|
||||||
|
def unit_root_test_wrapper(series, lags=None):
|
||||||
|
"""
|
||||||
|
Main function to run multiple stationarity tests. Runs five tests and returns a summary table + decision
|
||||||
|
based on the majority rule. If the number of tests that determine a series is stationary equals to the
|
||||||
|
number of tests that deem it non-stationary, we assume the series is non-stationary.
|
||||||
|
* Augmented Dickey-Fuller (ADF),
|
||||||
|
* KPSS,
|
||||||
|
* ADF using GLS,
|
||||||
|
* Phillips-Perron (PP),
|
||||||
|
* Zivot-Andrews (ZA)
|
||||||
|
|
||||||
|
:param lags: (optional) parameter that allows user to run a series of tests for a specific lag value.
|
||||||
|
:param series: series to test
|
||||||
|
:return: dictionary of summary table for all tests and final decision on stationary vs nonstaionary
|
||||||
|
"""
|
||||||
|
# setting for ADF and KPSS tests
|
||||||
|
adf_settings = {"IC": "AIC", "store": True}
|
||||||
|
|
||||||
|
kpss_settings = {"reg_type": "c", "lags": "auto", "store": True}
|
||||||
|
|
||||||
|
arch_test_settings = {} # settings for PP, ADF GLS and ZA tests
|
||||||
|
if lags is not None:
|
||||||
|
adf_settings.update({"lags": lags, "autolag": None})
|
||||||
|
kpss_settings.update({"lags:": lags})
|
||||||
|
arch_test_settings = {"lags": lags}
|
||||||
|
# Run individual tests
|
||||||
|
adf = adf_test(series, **adf_settings) # ADF test
|
||||||
|
kpss = kpss_test(series, **kpss_settings) # KPSS test
|
||||||
|
pp = unitroot.PhillipsPerron(series, **arch_test_settings) # Phillips-Perron test
|
||||||
|
adfgls = unitroot.DFGLS(series, **arch_test_settings) # ADF using GLS test
|
||||||
|
za = unitroot.ZivotAndrews(series, **arch_test_settings) # Zivot-Andrews test
|
||||||
|
|
||||||
|
# generate output table
|
||||||
|
adf_dict = format_test_output(test_name="ADF", test_res=adf, H0_unit_root=True)
|
||||||
|
kpss_dict = format_test_output(test_name="KPSS", test_res=kpss, H0_unit_root=False)
|
||||||
|
pp_dict = format_test_output(
|
||||||
|
test_name="Philips Perron", test_res=pp, H0_unit_root=True
|
||||||
|
)
|
||||||
|
adfgls_dict = format_test_output(
|
||||||
|
test_name="ADF GLS", test_res=adfgls, H0_unit_root=True
|
||||||
|
)
|
||||||
|
za_dict = format_test_output(
|
||||||
|
test_name="Zivot-Andrews", test_res=za, H0_unit_root=True
|
||||||
|
)
|
||||||
|
|
||||||
|
test_dict = {
|
||||||
|
"ADF": adf_dict,
|
||||||
|
"KPSS": kpss_dict,
|
||||||
|
"PP": pp_dict,
|
||||||
|
"ADF GLS": adfgls_dict,
|
||||||
|
"ZA": za_dict,
|
||||||
|
}
|
||||||
|
test_sum = pd.DataFrame.from_dict(test_dict, orient="index").reset_index(drop=True)
|
||||||
|
|
||||||
|
# decision based on the majority rule
|
||||||
|
if test_sum.shape[0] > 0:
|
||||||
|
ratio = test_sum[test_sum["stationary"] == "yes"].shape[0] / test_sum.shape[0]
|
||||||
|
else:
|
||||||
|
ratio = 1 # all tests fail, assume the series is stationary
|
||||||
|
|
||||||
|
# Majority rule. If the ratio is exactly 0.5, assume the series in non-stationary.
|
||||||
|
stationary = "YES" if (ratio > 0.5) else "NO"
|
||||||
|
|
||||||
|
out = {"summary": test_sum, "stationary": stationary}
|
||||||
|
return out
|
||||||
|
|
||||||
|
|
||||||
|
def ts_train_test_split(df_input, n, time_colname, ts_id_colnames=None):
|
||||||
|
"""
|
||||||
|
Group data frame by time series ID and split on last n rows for each group.
|
||||||
|
|
||||||
|
:param df_input: input data frame
|
||||||
|
:param n: number of observations in the test set
|
||||||
|
:param time_colname: time column
|
||||||
|
:param ts_id_colnames: (optional) list of grain column names
|
||||||
|
:return train and test data frames
|
||||||
|
"""
|
||||||
|
if ts_id_colnames is None:
|
||||||
|
ts_id_colnames = []
|
||||||
|
ts_id_colnames_original = ts_id_colnames.copy()
|
||||||
|
if len(ts_id_colnames) == 0:
|
||||||
|
ts_id_colnames = ["Grain"]
|
||||||
|
df_input[ts_id_colnames[0]] = "dummy"
|
||||||
|
# Sort by ascending time
|
||||||
|
df_grouped = df_input.sort_values(time_colname).groupby(
|
||||||
|
ts_id_colnames, group_keys=False
|
||||||
|
)
|
||||||
|
df_head = df_grouped.apply(lambda dfg: dfg.iloc[:-n])
|
||||||
|
df_tail = df_grouped.apply(lambda dfg: dfg.iloc[-n:])
|
||||||
|
# drop group column name if it was not originally provided
|
||||||
|
if len(ts_id_colnames_original) == 0:
|
||||||
|
df_head.drop(ts_id_colnames, axis=1, inplace=True)
|
||||||
|
df_tail.drop(ts_id_colnames, axis=1, inplace=True)
|
||||||
|
return df_head, df_tail
|
||||||
|
|
||||||
|
|
||||||
|
def compute_metrics(fcst_df, metric_name=None, ts_id_colnames=None):
|
||||||
|
"""
|
||||||
|
Calculate metrics per grain.
|
||||||
|
|
||||||
|
:param fcst_df: forecast data frame. Must contain 2 columns: 'actual_level' and 'predicted_level'
|
||||||
|
:param metric_name: (optional) name of the metric to return
|
||||||
|
:param ts_id_colnames: (optional) list of grain column names
|
||||||
|
:return: dictionary of summary table for all tests and final decision on stationary vs nonstaionary
|
||||||
|
"""
|
||||||
|
if ts_id_colnames is None:
|
||||||
|
ts_id_colnames = []
|
||||||
|
if len(ts_id_colnames) == 0:
|
||||||
|
ts_id_colnames = ["TS_ID"]
|
||||||
|
fcst_df[ts_id_colnames[0]] = "dummy"
|
||||||
|
metrics_list = []
|
||||||
|
for grain, df in fcst_df.groupby(ts_id_colnames):
|
||||||
|
try:
|
||||||
|
scores = scoring.score_regression(
|
||||||
|
y_test=df["actual_level"],
|
||||||
|
y_pred=df["predicted_level"],
|
||||||
|
metrics=list(constants.Metric.SCALAR_REGRESSION_SET),
|
||||||
|
)
|
||||||
|
except BaseException:
|
||||||
|
msg = "{}: metrics calculation failed.".format(grain)
|
||||||
|
print(msg)
|
||||||
|
scores = {}
|
||||||
|
one_grain_metrics_df = pd.DataFrame(
|
||||||
|
list(scores.items()), columns=["metric_name", "metric"]
|
||||||
|
).sort_values(["metric_name"])
|
||||||
|
one_grain_metrics_df.reset_index(inplace=True, drop=True)
|
||||||
|
if len(ts_id_colnames) < 2:
|
||||||
|
one_grain_metrics_df["grain"] = ts_id_colnames[0]
|
||||||
|
else:
|
||||||
|
one_grain_metrics_df["grain"] = "|".join(list(grain))
|
||||||
|
|
||||||
|
metrics_list.append(one_grain_metrics_df)
|
||||||
|
# collect into a data frame
|
||||||
|
grain_metrics = pd.concat(metrics_list)
|
||||||
|
if metric_name is not None:
|
||||||
|
grain_metrics = grain_metrics.query("metric_name == @metric_name")
|
||||||
|
return grain_metrics
|
||||||
@@ -0,0 +1,49 @@
|
|||||||
|
import os
|
||||||
|
import shutil
|
||||||
|
from azureml.core import ScriptRunConfig
|
||||||
|
|
||||||
|
|
||||||
|
def run_remote_inference(
|
||||||
|
test_experiment,
|
||||||
|
compute_target,
|
||||||
|
train_run,
|
||||||
|
test_dataset,
|
||||||
|
target_column_name,
|
||||||
|
inference_folder="./forecast",
|
||||||
|
):
|
||||||
|
# Create local directory to copy the model.pkl and forecsting_script.py files into.
|
||||||
|
# These files will be uploaded to and executed on the compute instance.
|
||||||
|
os.makedirs(inference_folder, exist_ok=True)
|
||||||
|
shutil.copy("forecasting_script.py", inference_folder)
|
||||||
|
|
||||||
|
train_run.download_file(
|
||||||
|
"outputs/model.pkl", os.path.join(inference_folder, "model.pkl")
|
||||||
|
)
|
||||||
|
|
||||||
|
inference_env = train_run.get_environment()
|
||||||
|
|
||||||
|
config = ScriptRunConfig(
|
||||||
|
source_directory=inference_folder,
|
||||||
|
script="forecasting_script.py",
|
||||||
|
arguments=[
|
||||||
|
"--target_column_name",
|
||||||
|
target_column_name,
|
||||||
|
"--test_dataset",
|
||||||
|
test_dataset.as_named_input(test_dataset.name),
|
||||||
|
],
|
||||||
|
compute_target=compute_target,
|
||||||
|
environment=inference_env,
|
||||||
|
)
|
||||||
|
|
||||||
|
run = test_experiment.submit(
|
||||||
|
config,
|
||||||
|
tags={
|
||||||
|
"training_run_id": train_run.id,
|
||||||
|
"run_algorithm": train_run.properties["run_algorithm"],
|
||||||
|
"valid_score": train_run.properties["score"],
|
||||||
|
"primary_metric": train_run.properties["primary_metric"],
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
run.log("run_algorithm", run.tags["run_algorithm"])
|
||||||
|
return run
|
||||||
@@ -96,7 +96,7 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"print(\"This notebook was created using version 1.32.0 of the Azure ML SDK\")\n",
|
"print(\"This notebook was created using version 1.36.0 of the Azure ML SDK\")\n",
|
||||||
"print(\"You are currently using version\", azureml.core.VERSION, \"of the Azure ML SDK\")"
|
"print(\"You are currently using version\", azureml.core.VERSION, \"of the Azure ML SDK\")"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
@@ -173,7 +173,7 @@
|
|||||||
"source": [
|
"source": [
|
||||||
"automl_settings = {\n",
|
"automl_settings = {\n",
|
||||||
" \"n_cross_validations\": 3,\n",
|
" \"n_cross_validations\": 3,\n",
|
||||||
" \"primary_metric\": 'average_precision_score_weighted',\n",
|
" \"primary_metric\": 'AUC_weighted',\n",
|
||||||
" \"experiment_timeout_hours\": 0.25, # This is a time limit for testing purposes, remove it for real use cases, this will drastically limit ability to find the best model possible\n",
|
" \"experiment_timeout_hours\": 0.25, # This is a time limit for testing purposes, remove it for real use cases, this will drastically limit ability to find the best model possible\n",
|
||||||
" \"verbosity\": logging.INFO,\n",
|
" \"verbosity\": logging.INFO,\n",
|
||||||
" \"enable_stack_ensemble\": False\n",
|
" \"enable_stack_ensemble\": False\n",
|
||||||
|
|||||||
@@ -77,7 +77,6 @@
|
|||||||
"import azureml.core\n",
|
"import azureml.core\n",
|
||||||
"from azureml.core.experiment import Experiment\n",
|
"from azureml.core.experiment import Experiment\n",
|
||||||
"from azureml.core.workspace import Workspace\n",
|
"from azureml.core.workspace import Workspace\n",
|
||||||
"import azureml.dataprep as dprep\n",
|
|
||||||
"from azureml.automl.core.featurization import FeaturizationConfig\n",
|
"from azureml.automl.core.featurization import FeaturizationConfig\n",
|
||||||
"from azureml.train.automl import AutoMLConfig\n",
|
"from azureml.train.automl import AutoMLConfig\n",
|
||||||
"from azureml.core.dataset import Dataset"
|
"from azureml.core.dataset import Dataset"
|
||||||
@@ -96,7 +95,7 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"print(\"This notebook was created using version 1.32.0 of the Azure ML SDK\")\n",
|
"print(\"This notebook was created using version 1.36.0 of the Azure ML SDK\")\n",
|
||||||
"print(\"You are currently using version\", azureml.core.VERSION, \"of the Azure ML SDK\")"
|
"print(\"You are currently using version\", azureml.core.VERSION, \"of the Azure ML SDK\")"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
@@ -541,8 +540,6 @@
|
|||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"from azureml.core.runconfig import RunConfiguration\n",
|
"from azureml.core.runconfig import RunConfiguration\n",
|
||||||
"from azureml.core.conda_dependencies import CondaDependencies\n",
|
|
||||||
"import pkg_resources\n",
|
|
||||||
"\n",
|
"\n",
|
||||||
"# create a new RunConfig object\n",
|
"# create a new RunConfig object\n",
|
||||||
"conda_run_config = RunConfiguration(framework=\"python\")\n",
|
"conda_run_config = RunConfiguration(framework=\"python\")\n",
|
||||||
@@ -720,14 +717,13 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"from azureml.core.webservice import Webservice\n",
|
|
||||||
"from azureml.core.model import InferenceConfig\n",
|
"from azureml.core.model import InferenceConfig\n",
|
||||||
"from azureml.core.webservice import AciWebservice\n",
|
"from azureml.core.webservice import AciWebservice\n",
|
||||||
"from azureml.core.model import Model\n",
|
"from azureml.core.model import Model\n",
|
||||||
"from azureml.core.environment import Environment\n",
|
"from azureml.core.environment import Environment\n",
|
||||||
"\n",
|
"\n",
|
||||||
"aciconfig = AciWebservice.deploy_configuration(cpu_cores=1, \n",
|
"aciconfig = AciWebservice.deploy_configuration(cpu_cores=2, \n",
|
||||||
" memory_gb=1, \n",
|
" memory_gb=2, \n",
|
||||||
" tags={\"data\": \"Machine Data\", \n",
|
" tags={\"data\": \"Machine Data\", \n",
|
||||||
" \"method\" : \"local_explanation\"}, \n",
|
" \"method\" : \"local_explanation\"}, \n",
|
||||||
" description='Get local explanations for Machine test data')\n",
|
" description='Get local explanations for Machine test data')\n",
|
||||||
|
|||||||
@@ -92,7 +92,7 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"print(\"This notebook was created using version 1.32.0 of the Azure ML SDK\")\n",
|
"print(\"This notebook was created using version 1.36.0 of the Azure ML SDK\")\n",
|
||||||
"print(\"You are currently using version\", azureml.core.VERSION, \"of the Azure ML SDK\")"
|
"print(\"You are currently using version\", azureml.core.VERSION, \"of the Azure ML SDK\")"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
@@ -213,7 +213,7 @@
|
|||||||
"source": [
|
"source": [
|
||||||
"automl_settings = {\n",
|
"automl_settings = {\n",
|
||||||
" \"n_cross_validations\": 3,\n",
|
" \"n_cross_validations\": 3,\n",
|
||||||
" \"primary_metric\": 'r2_score',\n",
|
" \"primary_metric\": 'normalized_root_mean_squared_error',\n",
|
||||||
" \"enable_early_stopping\": True, \n",
|
" \"enable_early_stopping\": True, \n",
|
||||||
" \"experiment_timeout_hours\": 0.3, #for real scenarios we reccommend a timeout of at least one hour \n",
|
" \"experiment_timeout_hours\": 0.3, #for real scenarios we reccommend a timeout of at least one hour \n",
|
||||||
" \"max_concurrent_iterations\": 4,\n",
|
" \"max_concurrent_iterations\": 4,\n",
|
||||||
|
|||||||
@@ -2,22 +2,23 @@
|
|||||||
"cells": [
|
"cells": [
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"Copyright (c) Microsoft Corporation. All rights reserved.\n",
|
"Copyright (c) Microsoft Corporation. All rights reserved.\n",
|
||||||
"\n",
|
"\n",
|
||||||
"Licensed under the MIT License."
|
"Licensed under the MIT License."
|
||||||
],
|
]
|
||||||
"metadata": {}
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
""
|
""
|
||||||
],
|
]
|
||||||
"metadata": {}
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"# Register Spark Model and deploy as Webservice\n",
|
"# Register Spark Model and deploy as Webservice\n",
|
||||||
"\n",
|
"\n",
|
||||||
@@ -25,110 +26,109 @@
|
|||||||
"\n",
|
"\n",
|
||||||
" 1. Register Spark Model\n",
|
" 1. Register Spark Model\n",
|
||||||
" 2. Deploy Spark Model as Webservice"
|
" 2. Deploy Spark Model as Webservice"
|
||||||
],
|
]
|
||||||
"metadata": {}
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"## Prerequisites\n",
|
"## Prerequisites\n",
|
||||||
"If you are using an Azure Machine Learning Notebook VM, you are all set. Otherwise, make sure you go through the [configuration](../../../configuration.ipynb) Notebook first if you haven't."
|
"If you are using an Azure Machine Learning Notebook VM, you are all set. Otherwise, make sure you go through the [configuration](../../../configuration.ipynb) Notebook first if you haven't."
|
||||||
],
|
]
|
||||||
"metadata": {}
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": null,
|
||||||
"source": [
|
"metadata": {},
|
||||||
"# Check core SDK version number\r\n",
|
|
||||||
"import azureml.core\r\n",
|
|
||||||
"\r\n",
|
|
||||||
"print(\"SDK version:\", azureml.core.VERSION)"
|
|
||||||
],
|
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"metadata": {}
|
"source": [
|
||||||
|
"# Check core SDK version number\n",
|
||||||
|
"import azureml.core\n",
|
||||||
|
"\n",
|
||||||
|
"print(\"SDK version:\", azureml.core.VERSION)"
|
||||||
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"## Initialize Workspace\n",
|
"## Initialize Workspace\n",
|
||||||
"\n",
|
"\n",
|
||||||
"Initialize a workspace object from persisted configuration."
|
"Initialize a workspace object from persisted configuration."
|
||||||
],
|
]
|
||||||
"metadata": {}
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": null,
|
||||||
"source": [
|
|
||||||
"from azureml.core import Workspace\r\n",
|
|
||||||
"\r\n",
|
|
||||||
"ws = Workspace.from_config()\r\n",
|
|
||||||
"print(ws.name, ws.resource_group, ws.location, ws.subscription_id, sep='\\n')"
|
|
||||||
],
|
|
||||||
"outputs": [],
|
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"tags": [
|
"tags": [
|
||||||
"create workspace"
|
"create workspace"
|
||||||
]
|
]
|
||||||
}
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"from azureml.core import Workspace\n",
|
||||||
|
"\n",
|
||||||
|
"ws = Workspace.from_config()\n",
|
||||||
|
"print(ws.name, ws.resource_group, ws.location, ws.subscription_id, sep='\\n')"
|
||||||
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"### Register Model"
|
"### Register Model"
|
||||||
],
|
]
|
||||||
"metadata": {}
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"You can add tags and descriptions to your Models. Note you need to have a `iris.model` file in the current directory. This model file is generated using [train in spark](../training/train-in-spark/train-in-spark.ipynb) notebook. The below call registers that file as a Model with the same name `iris.model` in the workspace.\n",
|
"You can add tags and descriptions to your Models. Note you need to have a `iris.model` file in the current directory. This model file is generated using [train in spark](../training/train-in-spark/train-in-spark.ipynb) notebook. The below call registers that file as a Model with the same name `iris.model` in the workspace.\n",
|
||||||
"\n",
|
"\n",
|
||||||
"Using tags, you can track useful information such as the name and version of the machine learning library used to train the model. Note that tags must be alphanumeric."
|
"Using tags, you can track useful information such as the name and version of the machine learning library used to train the model. Note that tags must be alphanumeric."
|
||||||
],
|
]
|
||||||
"metadata": {}
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": null,
|
||||||
"source": [
|
|
||||||
"from azureml.core.model import Model\r\n",
|
|
||||||
"\r\n",
|
|
||||||
"model = Model.register(model_path=\"iris.model\",\r\n",
|
|
||||||
" model_name=\"iris.model\",\r\n",
|
|
||||||
" tags={'type': \"regression\"},\r\n",
|
|
||||||
" description=\"Logistic regression model to predict iris species\",\r\n",
|
|
||||||
" workspace=ws)"
|
|
||||||
],
|
|
||||||
"outputs": [],
|
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"tags": [
|
"tags": [
|
||||||
"register model from file"
|
"register model from file"
|
||||||
]
|
]
|
||||||
}
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"from azureml.core.model import Model\n",
|
||||||
|
"\n",
|
||||||
|
"model = Model.register(model_path=\"iris.model\",\n",
|
||||||
|
" model_name=\"iris.model\",\n",
|
||||||
|
" tags={'type': \"regression\"},\n",
|
||||||
|
" description=\"Logistic regression model to predict iris species\",\n",
|
||||||
|
" workspace=ws)"
|
||||||
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"### Fetch Environment"
|
"### Fetch Environment"
|
||||||
],
|
]
|
||||||
"metadata": {}
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"You can now create and/or use an Environment object when deploying a Webservice. The Environment can have been previously registered with your Workspace, or it will be registered with it as a part of the Webservice deployment.\n",
|
"You can now create and/or use an Environment object when deploying a Webservice. The Environment can have been previously registered with your Workspace, or it will be registered with it as a part of the Webservice deployment.\n",
|
||||||
"\n",
|
"\n",
|
||||||
"In this notebook, we will be using 'AzureML-PySpark-MmlSpark-0.15', a curated environment.\n",
|
|
||||||
"\n",
|
|
||||||
"More information can be found in our [using environments notebook](../training/using-environments/using-environments.ipynb)."
|
"More information can be found in our [using environments notebook](../training/using-environments/using-environments.ipynb)."
|
||||||
],
|
]
|
||||||
"metadata": {}
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"from azureml.core import Environment\r\n",
|
"from azureml.core import Environment\r\n",
|
||||||
"from azureml.core.environment import SparkPackage\r\n",
|
"from azureml.core.environment import SparkPackage\r\n",
|
||||||
@@ -141,12 +141,11 @@
|
|||||||
"myenv.python.conda_dependencies.add_channel(\"conda-forge\")\r\n",
|
"myenv.python.conda_dependencies.add_channel(\"conda-forge\")\r\n",
|
||||||
"myenv.spark.packages = [SparkPackage(\"com.microsoft.ml.spark\", \"mmlspark_2.11\", \"0.15\"), SparkPackage(\"com.microsoft.azure\", \"azure-storage\", \"2.0.0\"), SparkPackage(\"org.apache.hadoop\", \"hadoop-azure\", \"2.7.0\")]\r\n",
|
"myenv.spark.packages = [SparkPackage(\"com.microsoft.ml.spark\", \"mmlspark_2.11\", \"0.15\"), SparkPackage(\"com.microsoft.azure\", \"azure-storage\", \"2.0.0\"), SparkPackage(\"org.apache.hadoop\", \"hadoop-azure\", \"2.7.0\")]\r\n",
|
||||||
"myenv.spark.repositories = [\"https://mmlspark.azureedge.net/maven\"]\r\n"
|
"myenv.spark.repositories = [\"https://mmlspark.azureedge.net/maven\"]\r\n"
|
||||||
],
|
]
|
||||||
"outputs": [],
|
|
||||||
"metadata": {}
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"## Create Inference Configuration\n",
|
"## Create Inference Configuration\n",
|
||||||
"\n",
|
"\n",
|
||||||
@@ -164,109 +163,109 @@
|
|||||||
" - source_directory = holds source path as string, this entire folder gets added in image so its really easy to access any files within this folder or subfolder\n",
|
" - source_directory = holds source path as string, this entire folder gets added in image so its really easy to access any files within this folder or subfolder\n",
|
||||||
" - entry_script = contains logic specific to initializing your model and running predictions\n",
|
" - entry_script = contains logic specific to initializing your model and running predictions\n",
|
||||||
" - environment = An environment object to use for the deployment. Doesn't have to be registered"
|
" - environment = An environment object to use for the deployment. Doesn't have to be registered"
|
||||||
],
|
]
|
||||||
"metadata": {}
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": null,
|
||||||
"source": [
|
|
||||||
"from azureml.core.model import InferenceConfig\r\n",
|
|
||||||
"\r\n",
|
|
||||||
"inference_config = InferenceConfig(entry_script=\"score.py\", environment=myenv)"
|
|
||||||
],
|
|
||||||
"outputs": [],
|
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"tags": [
|
"tags": [
|
||||||
"create image"
|
"create image"
|
||||||
]
|
]
|
||||||
}
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"from azureml.core.model import InferenceConfig\n",
|
||||||
|
"\n",
|
||||||
|
"inference_config = InferenceConfig(entry_script=\"score.py\", environment=myenv)"
|
||||||
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"### Deploy Model as Webservice on Azure Container Instance\n",
|
"### Deploy Model as Webservice on Azure Container Instance\n",
|
||||||
"\n",
|
"\n",
|
||||||
"Note that the service creation can take few minutes."
|
"Note that the service creation can take few minutes."
|
||||||
],
|
]
|
||||||
"metadata": {}
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": null,
|
||||||
"source": [
|
|
||||||
"from azureml.core.webservice import AciWebservice, Webservice\r\n",
|
|
||||||
"from azureml.exceptions import WebserviceException\r\n",
|
|
||||||
"\r\n",
|
|
||||||
"deployment_config = AciWebservice.deploy_configuration(cpu_cores=1, memory_gb=1)\r\n",
|
|
||||||
"aci_service_name = 'aciservice1'\r\n",
|
|
||||||
"\r\n",
|
|
||||||
"try:\r\n",
|
|
||||||
" # if you want to get existing service below is the command\r\n",
|
|
||||||
" # since aci name needs to be unique in subscription deleting existing aci if any\r\n",
|
|
||||||
" # we use aci_service_name to create azure aci\r\n",
|
|
||||||
" service = Webservice(ws, name=aci_service_name)\r\n",
|
|
||||||
" if service:\r\n",
|
|
||||||
" service.delete()\r\n",
|
|
||||||
"except WebserviceException as e:\r\n",
|
|
||||||
" print()\r\n",
|
|
||||||
"\r\n",
|
|
||||||
"service = Model.deploy(ws, aci_service_name, [model], inference_config, deployment_config)\r\n",
|
|
||||||
"\r\n",
|
|
||||||
"service.wait_for_deployment(True)\r\n",
|
|
||||||
"print(service.state)"
|
|
||||||
],
|
|
||||||
"outputs": [],
|
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"tags": [
|
"tags": [
|
||||||
"azuremlexception-remarks-sample"
|
"azuremlexception-remarks-sample"
|
||||||
]
|
]
|
||||||
}
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"from azureml.core.webservice import AciWebservice, Webservice\n",
|
||||||
|
"from azureml.exceptions import WebserviceException\n",
|
||||||
|
"\n",
|
||||||
|
"deployment_config = AciWebservice.deploy_configuration(cpu_cores=1, memory_gb=1)\n",
|
||||||
|
"aci_service_name = 'aciservice1'\n",
|
||||||
|
"\n",
|
||||||
|
"try:\n",
|
||||||
|
" # if you want to get existing service below is the command\n",
|
||||||
|
" # since aci name needs to be unique in subscription deleting existing aci if any\n",
|
||||||
|
" # we use aci_service_name to create azure aci\n",
|
||||||
|
" service = Webservice(ws, name=aci_service_name)\n",
|
||||||
|
" if service:\n",
|
||||||
|
" service.delete()\n",
|
||||||
|
"except WebserviceException as e:\n",
|
||||||
|
" print()\n",
|
||||||
|
"\n",
|
||||||
|
"service = Model.deploy(ws, aci_service_name, [model], inference_config, deployment_config)\n",
|
||||||
|
"\n",
|
||||||
|
"service.wait_for_deployment(True)\n",
|
||||||
|
"print(service.state)"
|
||||||
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"#### Test web service"
|
"#### Test web service"
|
||||||
],
|
]
|
||||||
"metadata": {}
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": null,
|
||||||
"source": [
|
"metadata": {},
|
||||||
"import json\r\n",
|
|
||||||
"test_sample = json.dumps({'features':{'type':1,'values':[4.3,3.0,1.1,0.1]},'label':2.0})\r\n",
|
|
||||||
"\r\n",
|
|
||||||
"test_sample_encoded = bytes(test_sample, encoding='utf8')\r\n",
|
|
||||||
"prediction = service.run(input_data=test_sample_encoded)\r\n",
|
|
||||||
"print(prediction)"
|
|
||||||
],
|
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"metadata": {}
|
"source": [
|
||||||
|
"import json\n",
|
||||||
|
"test_sample = json.dumps({'features':{'type':1,'values':[4.3,3.0,1.1,0.1]},'label':2.0})\n",
|
||||||
|
"\n",
|
||||||
|
"test_sample_encoded = bytes(test_sample, encoding='utf8')\n",
|
||||||
|
"prediction = service.run(input_data=test_sample_encoded)\n",
|
||||||
|
"print(prediction)"
|
||||||
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"#### Delete ACI to clean up"
|
"#### Delete ACI to clean up"
|
||||||
],
|
]
|
||||||
"metadata": {}
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": null,
|
||||||
"source": [
|
|
||||||
"service.delete()"
|
|
||||||
],
|
|
||||||
"outputs": [],
|
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"tags": [
|
"tags": [
|
||||||
"deploy service",
|
"deploy service",
|
||||||
"aci"
|
"aci"
|
||||||
]
|
]
|
||||||
}
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"service.delete()"
|
||||||
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"### Model Profiling\n",
|
"### Model Profiling\n",
|
||||||
"\n",
|
"\n",
|
||||||
@@ -278,11 +277,11 @@
|
|||||||
"profiling_results = profile.get_results()\n",
|
"profiling_results = profile.get_results()\n",
|
||||||
"print(profiling_results)\n",
|
"print(profiling_results)\n",
|
||||||
"```"
|
"```"
|
||||||
],
|
]
|
||||||
"metadata": {}
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"### Model Packaging\n",
|
"### Model Packaging\n",
|
||||||
"\n",
|
"\n",
|
||||||
@@ -303,8 +302,7 @@
|
|||||||
"package.wait_for_creation(show_output=True)\n",
|
"package.wait_for_creation(show_output=True)\n",
|
||||||
"package.save(\"./local_context_dir\")\n",
|
"package.save(\"./local_context_dir\")\n",
|
||||||
"```"
|
"```"
|
||||||
],
|
]
|
||||||
"metadata": {}
|
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"metadata": {
|
"metadata": {
|
||||||
|
|||||||
@@ -0,0 +1,44 @@
|
|||||||
|
# Copyright (c) Microsoft. All rights reserved.
|
||||||
|
# Licensed under the MIT license.
|
||||||
|
|
||||||
|
from azureml.core.run import Run
|
||||||
|
import joblib
|
||||||
|
import os
|
||||||
|
import shap
|
||||||
|
import xgboost
|
||||||
|
|
||||||
|
OUTPUT_DIR = './outputs/'
|
||||||
|
os.makedirs(OUTPUT_DIR, exist_ok=True)
|
||||||
|
|
||||||
|
run = Run.get_context()
|
||||||
|
|
||||||
|
# get a dataset on income prediction
|
||||||
|
X, y = shap.datasets.adult()
|
||||||
|
|
||||||
|
# train an XGBoost model (but any other tree model type should work)
|
||||||
|
model = xgboost.XGBClassifier()
|
||||||
|
model.fit(X, y)
|
||||||
|
|
||||||
|
explainer = shap.explainers.GPUTree(model, X)
|
||||||
|
X_shap = X[:100]
|
||||||
|
shap_values = explainer(X_shap)
|
||||||
|
|
||||||
|
print("computed shap values:")
|
||||||
|
print(shap_values)
|
||||||
|
|
||||||
|
# write X_shap out as a pickle file for later visualization
|
||||||
|
x_shap_pkl = 'x_shap.pkl'
|
||||||
|
with open(x_shap_pkl, 'wb') as file:
|
||||||
|
joblib.dump(value=X_shap, filename=os.path.join(OUTPUT_DIR, x_shap_pkl))
|
||||||
|
run.upload_file('x_shap_adult_census.pkl', os.path.join(OUTPUT_DIR, x_shap_pkl))
|
||||||
|
|
||||||
|
model_file_name = 'xgboost_.pkl'
|
||||||
|
# save model in the outputs folder so it automatically gets uploaded
|
||||||
|
with open(model_file_name, 'wb') as file:
|
||||||
|
joblib.dump(value=model, filename=os.path.join(OUTPUT_DIR,
|
||||||
|
model_file_name))
|
||||||
|
|
||||||
|
# register the model
|
||||||
|
run.upload_file('xgboost_model.pkl', os.path.join('./outputs/', model_file_name))
|
||||||
|
original_model = run.register_model(model_name='xgboost_with_gpu_tree_explainer',
|
||||||
|
model_path='xgboost_model.pkl')
|
||||||
@@ -0,0 +1,297 @@
|
|||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"Copyright (c) Microsoft Corporation. All rights reserved.\n",
|
||||||
|
"\n",
|
||||||
|
"Licensed under the MIT License."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
""
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Explain tree-based models on GPU using GPUTreeExplainer\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"_**This notebook illustrates how to use shap's GPUTreeExplainer on an Azure GPU machine.**_\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"Problem: Train a tree-based model and explain the model on an Azure GPU machine using the GPUTreeExplainer.\n",
|
||||||
|
"\n",
|
||||||
|
"---\n",
|
||||||
|
"\n",
|
||||||
|
"## Table of Contents\n",
|
||||||
|
"\n",
|
||||||
|
"1. [Introduction](#Introduction)\n",
|
||||||
|
"1. [Setup](#Setup)\n",
|
||||||
|
"1. [Run model explainer locally at training time](#Explain)\n",
|
||||||
|
" 1. Apply feature transformations\n",
|
||||||
|
" 1. Train a binary classification model\n",
|
||||||
|
" 1. Explain the model on raw features\n",
|
||||||
|
" 1. Generate global explanations\n",
|
||||||
|
" 1. Generate local explanations\n",
|
||||||
|
"1. [Visualize explanations](#Visualize)\n",
|
||||||
|
"1. [Deploy model and scoring explainer](#Deploy)\n",
|
||||||
|
"1. [Next steps](#Next)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Introduction\n",
|
||||||
|
"This notebook demonstrates how to use the GPUTreeExplainer on some simple datasets. Like the TreeExplainer, the GPUTreeExplainer is specifically designed for tree-based machine learning models, but it is designed to accelerate the computations using NVIDIA GPUs.\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"Make sure you have executed the [configuration](../../../configuration.ipynb) before running this notebook.\n",
|
||||||
|
"\n",
|
||||||
|
"Notebook synopsis:\n",
|
||||||
|
"\n",
|
||||||
|
"1. Creating an Experiment in an existing Workspace\n",
|
||||||
|
"2. Configuration and remote run with a GPU machine"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Setup"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"import logging\n",
|
||||||
|
"import os\n",
|
||||||
|
"import shutil\n",
|
||||||
|
"\n",
|
||||||
|
"import pandas as pd\n",
|
||||||
|
"\n",
|
||||||
|
"import azureml.core\n",
|
||||||
|
"from azureml.core.experiment import Experiment\n",
|
||||||
|
"from azureml.core.workspace import Workspace\n",
|
||||||
|
"from azureml.core.dataset import Dataset\n",
|
||||||
|
"from azureml.core.compute import AmlCompute\n",
|
||||||
|
"from azureml.core.compute import ComputeTarget\n",
|
||||||
|
"from azureml.core.run import Run\n",
|
||||||
|
"from azureml.core.model import Model"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"This sample notebook may use features that are not available in previous versions of the Azure ML SDK."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"print(\"This notebook was created using version 1.36.0 of the Azure ML SDK\")\n",
|
||||||
|
"print(\"You are currently using version\", azureml.core.VERSION, \"of the Azure ML SDK\")"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"As part of the setup you have already created a <b>Workspace</b>. To run the script, you also need to create an <b>Experiment</b>. An Experiment corresponds to a prediction problem you are trying to solve, while a Run corresponds to a specific approach to the problem."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"ws = Workspace.from_config()\n",
|
||||||
|
"\n",
|
||||||
|
"# Choose an experiment name.\n",
|
||||||
|
"experiment_name = 'gpu-tree-explainer'\n",
|
||||||
|
"\n",
|
||||||
|
"experiment = Experiment(ws, experiment_name)\n",
|
||||||
|
"\n",
|
||||||
|
"output = {}\n",
|
||||||
|
"output['Subscription ID'] = ws.subscription_id\n",
|
||||||
|
"output['Workspace Name'] = ws.name\n",
|
||||||
|
"output['Resource Group'] = ws.resource_group\n",
|
||||||
|
"output['Location'] = ws.location\n",
|
||||||
|
"output['Experiment Name'] = experiment.name\n",
|
||||||
|
"pd.set_option('display.max_colwidth', -1)\n",
|
||||||
|
"outputDf = pd.DataFrame(data = output, index = [''])\n",
|
||||||
|
"outputDf.T"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"### Create project directory\n",
|
||||||
|
"\n",
|
||||||
|
"Create a directory that will contain all the necessary code from your local machine that you will need access to on the remote resource. This includes the training script, and any additional files your training script depends on"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"import os\n",
|
||||||
|
"import shutil\n",
|
||||||
|
"\n",
|
||||||
|
"project_folder = './azureml-shap-gpu-tree-explainer'\n",
|
||||||
|
"os.makedirs(project_folder, exist_ok=True)\n",
|
||||||
|
"shutil.copy('gpu_tree_explainer.py', project_folder)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Set up a compute cluster\n",
|
||||||
|
"This section uses a user-provided compute cluster (named \"gpu-shap-cluster\" in this example). If a cluster with this name does not exist in the user's workspace, the below code will create a new cluster. You can choose the parameters of the cluster as mentioned in the comments."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"from azureml.core.compute import ComputeTarget, AmlCompute\n",
|
||||||
|
"from azureml.core.compute_target import ComputeTargetException\n",
|
||||||
|
"\n",
|
||||||
|
"num_nodes = 1\n",
|
||||||
|
"\n",
|
||||||
|
"# Choose a name for your cluster.\n",
|
||||||
|
"amlcompute_cluster_name = \"gpu-shap-cluster\"\n",
|
||||||
|
"\n",
|
||||||
|
"# Verify that cluster does not exist already\n",
|
||||||
|
"try:\n",
|
||||||
|
" compute_target = ComputeTarget(workspace=ws, name=amlcompute_cluster_name)\n",
|
||||||
|
" print('Found existing cluster, use it.')\n",
|
||||||
|
"except ComputeTargetException:\n",
|
||||||
|
" compute_config = AmlCompute.provisioning_configuration(vm_size = \"STANDARD_NC6\",\n",
|
||||||
|
" # To use GPUTreeExplainer, select a GPU such as \"STANDARD_NC6\" \n",
|
||||||
|
" # or similar GPU option\n",
|
||||||
|
" # available in your workspace\n",
|
||||||
|
" max_nodes = num_nodes)\n",
|
||||||
|
" compute_target = ComputeTarget.create(ws, amlcompute_cluster_name, compute_config)\n",
|
||||||
|
"\n",
|
||||||
|
"compute_target.wait_for_completion(show_output=True)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"### Configure & Run"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"from azureml.core.runconfig import RunConfiguration\n",
|
||||||
|
"from azureml.core.conda_dependencies import CondaDependencies\n",
|
||||||
|
"\n",
|
||||||
|
"# Create a new RunConfig object\n",
|
||||||
|
"run_config = RunConfiguration(framework=\"python\")\n",
|
||||||
|
"\n",
|
||||||
|
"# Set compute target to AmlCompute target created in previous step\n",
|
||||||
|
"run_config.target = amlcompute_cluster_name\n",
|
||||||
|
"\n",
|
||||||
|
"from azureml.core import Environment\n",
|
||||||
|
"\n",
|
||||||
|
"environment_name = \"shap-gpu-tree\"\n",
|
||||||
|
"\n",
|
||||||
|
"env = Environment(environment_name)\n",
|
||||||
|
"\n",
|
||||||
|
"env.docker.enabled = True\n",
|
||||||
|
"env.docker.base_image = None\n",
|
||||||
|
"env.docker.base_dockerfile = \"\"\"\n",
|
||||||
|
"FROM rapidsai/rapidsai:cuda10.0-devel-ubuntu18.04\n",
|
||||||
|
"RUN apt-get update && \\\n",
|
||||||
|
"apt-get install -y fuse && \\\n",
|
||||||
|
"apt-get install -y build-essential && \\\n",
|
||||||
|
"apt-get install -y python3-dev && \\\n",
|
||||||
|
"source activate rapids && \\\n",
|
||||||
|
"apt-get install -y g++ && \\\n",
|
||||||
|
"printenv && \\\n",
|
||||||
|
"echo \"which nvcc: \" && \\\n",
|
||||||
|
"which nvcc && \\\n",
|
||||||
|
"pip install azureml-defaults && \\\n",
|
||||||
|
"pip install azureml-telemetry && \\\n",
|
||||||
|
"cd /usr/local/src && \\\n",
|
||||||
|
"git clone https://github.com/slundberg/shap && \\\n",
|
||||||
|
"cd shap && \\\n",
|
||||||
|
"mkdir build && \\\n",
|
||||||
|
"python setup.py install --user && \\\n",
|
||||||
|
"pip uninstall -y xgboost && \\\n",
|
||||||
|
"rm /conda/envs/rapids/lib/libxgboost.so && \\\n",
|
||||||
|
"pip install xgboost==1.4.2\n",
|
||||||
|
"\"\"\"\n",
|
||||||
|
"\n",
|
||||||
|
"env.python.user_managed_dependencies = True\n",
|
||||||
|
"\n",
|
||||||
|
"from azureml.core import Run\n",
|
||||||
|
"from azureml.core import ScriptRunConfig\n",
|
||||||
|
"\n",
|
||||||
|
"src = ScriptRunConfig(source_directory=project_folder, \n",
|
||||||
|
" script='gpu_tree_explainer.py', \n",
|
||||||
|
" compute_target=amlcompute_cluster_name,\n",
|
||||||
|
" environment=env) \n",
|
||||||
|
"run = experiment.submit(config=src)\n",
|
||||||
|
"run"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"authors": [
|
||||||
|
{
|
||||||
|
"name": "ilmat"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"kernelspec": {
|
||||||
|
"display_name": "Python 3.6",
|
||||||
|
"language": "python",
|
||||||
|
"name": "python36"
|
||||||
|
},
|
||||||
|
"language_info": {
|
||||||
|
"codemirror_mode": {
|
||||||
|
"name": "ipython",
|
||||||
|
"version": 3
|
||||||
|
},
|
||||||
|
"file_extension": ".py",
|
||||||
|
"mimetype": "text/x-python",
|
||||||
|
"name": "python",
|
||||||
|
"nbconvert_exporter": "python",
|
||||||
|
"pygments_lexer": "ipython3",
|
||||||
|
"version": "3.6.8"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 2
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
name: train-explain-model-gpu-tree-explainer
|
||||||
|
dependencies:
|
||||||
|
- pip:
|
||||||
|
- azureml-sdk
|
||||||
|
- azureml-interpret
|
||||||
@@ -11,4 +11,4 @@ dependencies:
|
|||||||
- matplotlib
|
- matplotlib
|
||||||
- azureml-dataset-runtime
|
- azureml-dataset-runtime
|
||||||
- ipywidgets
|
- ipywidgets
|
||||||
- raiwidgets~=0.7.0
|
- raiwidgets~=0.10.0
|
||||||
|
|||||||
@@ -10,4 +10,4 @@ dependencies:
|
|||||||
- ipython
|
- ipython
|
||||||
- matplotlib
|
- matplotlib
|
||||||
- ipywidgets
|
- ipywidgets
|
||||||
- raiwidgets~=0.7.0
|
- raiwidgets~=0.10.0
|
||||||
|
|||||||
@@ -10,4 +10,4 @@ dependencies:
|
|||||||
- ipython
|
- ipython
|
||||||
- matplotlib
|
- matplotlib
|
||||||
- ipywidgets
|
- ipywidgets
|
||||||
- raiwidgets~=0.7.0
|
- raiwidgets~=0.10.0
|
||||||
|
|||||||
@@ -12,4 +12,4 @@ dependencies:
|
|||||||
- azureml-dataset-runtime
|
- azureml-dataset-runtime
|
||||||
- azureml-core
|
- azureml-core
|
||||||
- ipywidgets
|
- ipywidgets
|
||||||
- raiwidgets~=0.7.0
|
- raiwidgets~=0.10.0
|
||||||
|
|||||||
@@ -126,7 +126,7 @@
|
|||||||
},
|
},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"from msrest.exceptions import HttpOperationError\n",
|
"from azureml.exceptions import UserErrorException\n",
|
||||||
"\n",
|
"\n",
|
||||||
"blob_datastore_name='MyBlobDatastore'\n",
|
"blob_datastore_name='MyBlobDatastore'\n",
|
||||||
"account_name=os.getenv(\"BLOB_ACCOUNTNAME_62\", \"<my-account-name>\") # Storage account name\n",
|
"account_name=os.getenv(\"BLOB_ACCOUNTNAME_62\", \"<my-account-name>\") # Storage account name\n",
|
||||||
@@ -136,7 +136,7 @@
|
|||||||
"try:\n",
|
"try:\n",
|
||||||
" blob_datastore = Datastore.get(ws, blob_datastore_name)\n",
|
" blob_datastore = Datastore.get(ws, blob_datastore_name)\n",
|
||||||
" print(\"Found Blob Datastore with name: %s\" % blob_datastore_name)\n",
|
" print(\"Found Blob Datastore with name: %s\" % blob_datastore_name)\n",
|
||||||
"except HttpOperationError:\n",
|
"except UserErrorException:\n",
|
||||||
" blob_datastore = Datastore.register_azure_blob_container(\n",
|
" blob_datastore = Datastore.register_azure_blob_container(\n",
|
||||||
" workspace=ws,\n",
|
" workspace=ws,\n",
|
||||||
" datastore_name=blob_datastore_name,\n",
|
" datastore_name=blob_datastore_name,\n",
|
||||||
@@ -180,7 +180,7 @@
|
|||||||
"try:\n",
|
"try:\n",
|
||||||
" adls_datastore = Datastore.get(ws, datastore_name)\n",
|
" adls_datastore = Datastore.get(ws, datastore_name)\n",
|
||||||
" print(\"Found datastore with name: %s\" % datastore_name)\n",
|
" print(\"Found datastore with name: %s\" % datastore_name)\n",
|
||||||
"except HttpOperationError:\n",
|
"except UserErrorException:\n",
|
||||||
" adls_datastore = Datastore.register_azure_data_lake(\n",
|
" adls_datastore = Datastore.register_azure_data_lake(\n",
|
||||||
" workspace=ws,\n",
|
" workspace=ws,\n",
|
||||||
" datastore_name=datastore_name,\n",
|
" datastore_name=datastore_name,\n",
|
||||||
@@ -270,7 +270,7 @@
|
|||||||
"try:\n",
|
"try:\n",
|
||||||
" sql_datastore = Datastore.get(ws, sql_datastore_name)\n",
|
" sql_datastore = Datastore.get(ws, sql_datastore_name)\n",
|
||||||
" print(\"Found sql database datastore with name: %s\" % sql_datastore_name)\n",
|
" print(\"Found sql database datastore with name: %s\" % sql_datastore_name)\n",
|
||||||
"except HttpOperationError:\n",
|
"except UserErrorException:\n",
|
||||||
" sql_datastore = Datastore.register_azure_sql_database(\n",
|
" sql_datastore = Datastore.register_azure_sql_database(\n",
|
||||||
" workspace=ws,\n",
|
" workspace=ws,\n",
|
||||||
" datastore_name=sql_datastore_name,\n",
|
" datastore_name=sql_datastore_name,\n",
|
||||||
@@ -312,7 +312,7 @@
|
|||||||
"try:\n",
|
"try:\n",
|
||||||
" psql_datastore = Datastore.get(ws, psql_datastore_name)\n",
|
" psql_datastore = Datastore.get(ws, psql_datastore_name)\n",
|
||||||
" print(\"Found PostgreSQL database datastore with name: %s\" % psql_datastore_name)\n",
|
" print(\"Found PostgreSQL database datastore with name: %s\" % psql_datastore_name)\n",
|
||||||
"except HttpOperationError:\n",
|
"except UserErrorException:\n",
|
||||||
" psql_datastore = Datastore.register_azure_postgre_sql(\n",
|
" psql_datastore = Datastore.register_azure_postgre_sql(\n",
|
||||||
" workspace=ws,\n",
|
" workspace=ws,\n",
|
||||||
" datastore_name=psql_datastore_name,\n",
|
" datastore_name=psql_datastore_name,\n",
|
||||||
@@ -353,7 +353,7 @@
|
|||||||
"try:\n",
|
"try:\n",
|
||||||
" mysql_datastore = Datastore.get(ws, mysql_datastore_name)\n",
|
" mysql_datastore = Datastore.get(ws, mysql_datastore_name)\n",
|
||||||
" print(\"Found MySQL database datastore with name: %s\" % mysql_datastore_name)\n",
|
" print(\"Found MySQL database datastore with name: %s\" % mysql_datastore_name)\n",
|
||||||
"except HttpOperationError:\n",
|
"except UserErrorException:\n",
|
||||||
" mysql_datastore = Datastore.register_azure_my_sql(\n",
|
" mysql_datastore = Datastore.register_azure_my_sql(\n",
|
||||||
" workspace=ws,\n",
|
" workspace=ws,\n",
|
||||||
" datastore_name=mysql_datastore_name,\n",
|
" datastore_name=mysql_datastore_name,\n",
|
||||||
|
|||||||
@@ -47,8 +47,9 @@
|
|||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"import azureml.core\n",
|
"import azureml.core\n",
|
||||||
"from azureml.core import Workspace, Experiment, Dataset\n",
|
"from azureml.core import Workspace, Experiment, Dataset, RunConfiguration\n",
|
||||||
"from azureml.core.compute import ComputeTarget, AmlCompute\n",
|
"from azureml.core.compute import ComputeTarget, AmlCompute\n",
|
||||||
|
"from azureml.core.environment import CondaDependencies\n",
|
||||||
"from azureml.data.dataset_consumption_config import DatasetConsumptionConfig\n",
|
"from azureml.data.dataset_consumption_config import DatasetConsumptionConfig\n",
|
||||||
"from azureml.widgets import RunDetails\n",
|
"from azureml.widgets import RunDetails\n",
|
||||||
"\n",
|
"\n",
|
||||||
@@ -223,6 +224,18 @@
|
|||||||
"Note that the ```file_ds_consumption``` and ```tabular_ds_consumption``` are specified as both arguments and inputs to create a step."
|
"Note that the ```file_ds_consumption``` and ```tabular_ds_consumption``` are specified as both arguments and inputs to create a step."
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"conda_dep = CondaDependencies()\n",
|
||||||
|
"conda_dep.add_pip_package(\"pandas\")\n",
|
||||||
|
"\n",
|
||||||
|
"run_config = RunConfiguration(conda_dependencies=conda_dep)"
|
||||||
|
]
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": null,
|
||||||
@@ -235,7 +248,8 @@
|
|||||||
" arguments=[\"--param1\", file_ds_consumption, \"--param2\", tabular_ds_consumption],\n",
|
" arguments=[\"--param1\", file_ds_consumption, \"--param2\", tabular_ds_consumption],\n",
|
||||||
" inputs=[file_ds_consumption, tabular_ds_consumption],\n",
|
" inputs=[file_ds_consumption, tabular_ds_consumption],\n",
|
||||||
" compute_target=compute_target,\n",
|
" compute_target=compute_target,\n",
|
||||||
" source_directory=source_directory)\n",
|
" source_directory=source_directory,\n",
|
||||||
|
" runconfig=run_config)\n",
|
||||||
"\n",
|
"\n",
|
||||||
"print(\"train_step created\")\n",
|
"print(\"train_step created\")\n",
|
||||||
"\n",
|
"\n",
|
||||||
@@ -498,7 +512,7 @@
|
|||||||
"pygments_lexer": "ipython3",
|
"pygments_lexer": "ipython3",
|
||||||
"version": "3.6.7"
|
"version": "3.6.7"
|
||||||
},
|
},
|
||||||
"order_index": 13,
|
"order_index": 13.0,
|
||||||
"star_tag": [
|
"star_tag": [
|
||||||
"featured"
|
"featured"
|
||||||
],
|
],
|
||||||
|
|||||||
@@ -27,6 +27,7 @@
|
|||||||
"2. Running an arbitrary Python script that the customer has in DBFS\n",
|
"2. Running an arbitrary Python script that the customer has in DBFS\n",
|
||||||
"3. Running an arbitrary Python script that is available on local computer (will upload to DBFS, and then run in Databricks) \n",
|
"3. Running an arbitrary Python script that is available on local computer (will upload to DBFS, and then run in Databricks) \n",
|
||||||
"4. Running a JAR job that the customer has in DBFS.\n",
|
"4. Running a JAR job that the customer has in DBFS.\n",
|
||||||
|
"5. How to get run context in a Databricks interactive cluster\n",
|
||||||
"\n",
|
"\n",
|
||||||
"## Before you begin:\n",
|
"## Before you begin:\n",
|
||||||
"\n",
|
"\n",
|
||||||
@@ -699,14 +700,14 @@
|
|||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"### 5. Running demo notebook already added to the Databricks workspace using existing cluster\n",
|
"### 5. Running demo notebook already added to the Databricks workspace using existing cluster\n",
|
||||||
"First you need register DBFS datastore and make sure path_on_datastore does exist in databricks file system, you can browser the files by refering [this](https://docs.azuredatabricks.net/user-guide/dbfs-databricks-file-system.html).\n",
|
"First you need register DBFS datastore and make sure path_on_datastore does exist in databricks file system, you can browser the files by refering [this](https://docs.azuredatabricks.net/user-guide/dbfs-databricks-file-system.html).\n",
|
||||||
"\n",
|
"\n",
|
||||||
"Find existing_cluster_id by opeing Azure Databricks UI with Clusters page and in url you will find a string connected with '-' right after \"clusters/\"."
|
"Find existing_cluster_id by opeing Azure Databricks UI with Clusters page and in url you will find a string connected with '-' right after \"clusters/\"."
|
||||||
],
|
]
|
||||||
"cell_type": "markdown",
|
|
||||||
"metadata": {}
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
@@ -745,11 +746,11 @@
|
|||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"#### Build and submit the Experiment"
|
"#### Build and submit the Experiment"
|
||||||
],
|
]
|
||||||
"cell_type": "markdown",
|
|
||||||
"metadata": {}
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
@@ -764,11 +765,11 @@
|
|||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"#### View Run Details"
|
"#### View Run Details"
|
||||||
],
|
]
|
||||||
"cell_type": "markdown",
|
|
||||||
"metadata": {}
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
@@ -781,14 +782,14 @@
|
|||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"### 6. Running a Python script in Databricks that currenlty is in local computer with existing cluster\n",
|
"### 6. Running a Python script in Databricks that is currently in local computer with existing cluster\n",
|
||||||
"When you access azure blob or data lake storage from an existing (interactive) cluster, you need to ensure the Spark configuration is set up correctly to access this storage and this set up may require the cluster to be restarted.\n",
|
"When you access azure blob or data lake storage from an existing (interactive) cluster, you need to ensure the Spark configuration is set up correctly to access this storage and this set up may require the cluster to be restarted.\n",
|
||||||
"\n",
|
"\n",
|
||||||
"If you set permit_cluster_restart to True, AML will check if the spark configuration needs to be updated and restart the cluster for you if required. This will ensure that the storage can be correctly accessed from the Databricks cluster."
|
"If you set permit_cluster_restart to True, AML will check if the spark configuration needs to be updated and restart the cluster for you if required. This will ensure that the storage can be correctly accessed from the Databricks cluster."
|
||||||
],
|
]
|
||||||
"cell_type": "markdown",
|
|
||||||
"metadata": {}
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
@@ -813,11 +814,11 @@
|
|||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"#### Build and submit the Experiment"
|
"#### Build and submit the Experiment"
|
||||||
],
|
]
|
||||||
"cell_type": "markdown",
|
|
||||||
"metadata": {}
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
@@ -832,11 +833,11 @@
|
|||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"#### View Run Details"
|
"#### View Run Details"
|
||||||
],
|
]
|
||||||
"cell_type": "markdown",
|
|
||||||
"metadata": {}
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
@@ -849,18 +850,71 @@
|
|||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"### How to get run context in a Databricks interactive cluster\n",
|
||||||
|
"\n",
|
||||||
|
"Users are used to being able to use Run.get_context() to retrieve the parent_run_id for a given run_id. In DatabricksStep, however, a little more work is required to achieve this.\n",
|
||||||
|
"\n",
|
||||||
|
"The solution is to parse the script arguments and set corresponding environment variables to access the run context from within Databricks.\n",
|
||||||
|
"Note that this workaround is not required for job clusters. \n",
|
||||||
|
"\n",
|
||||||
|
"Here is a code sample:"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"```python\n",
|
||||||
|
"from azureml.core import Run\n",
|
||||||
|
"import argparse\n",
|
||||||
|
"import os\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"def populate_environ():\n",
|
||||||
|
" parser = argparse.ArgumentParser(description='Process arguments passed to script')\n",
|
||||||
|
" parser.add_argument('--AZUREML_SCRIPT_DIRECTORY_NAME')\n",
|
||||||
|
" parser.add_argument('--AZUREML_RUN_TOKEN')\n",
|
||||||
|
" parser.add_argument('--AZUREML_RUN_TOKEN_EXPIRY')\n",
|
||||||
|
" parser.add_argument('--AZUREML_RUN_ID')\n",
|
||||||
|
" parser.add_argument('--AZUREML_ARM_SUBSCRIPTION')\n",
|
||||||
|
" parser.add_argument('--AZUREML_ARM_RESOURCEGROUP')\n",
|
||||||
|
" parser.add_argument('--AZUREML_ARM_WORKSPACE_NAME')\n",
|
||||||
|
" parser.add_argument('--AZUREML_ARM_PROJECT_NAME')\n",
|
||||||
|
" parser.add_argument('--AZUREML_SERVICE_ENDPOINT')\n",
|
||||||
|
"\n",
|
||||||
|
" args = parser.parse_args()\n",
|
||||||
|
" os.environ['AZUREML_SCRIPT_DIRECTORY_NAME'] = args.AZUREML_SCRIPT_DIRECTORY_NAME\n",
|
||||||
|
" os.environ['AZUREML_RUN_TOKEN'] = args.AZUREML_RUN_TOKEN\n",
|
||||||
|
" os.environ['AZUREML_RUN_TOKEN_EXPIRY'] = args.AZUREML_RUN_TOKEN_EXPIRY\n",
|
||||||
|
" os.environ['AZUREML_RUN_ID'] = args.AZUREML_RUN_ID\n",
|
||||||
|
" os.environ['AZUREML_ARM_SUBSCRIPTION'] = args.AZUREML_ARM_SUBSCRIPTION\n",
|
||||||
|
" os.environ['AZUREML_ARM_RESOURCEGROUP'] = args.AZUREML_ARM_RESOURCEGROUP\n",
|
||||||
|
" os.environ['AZUREML_ARM_WORKSPACE_NAME'] = args.AZUREML_ARM_WORKSPACE_NAME\n",
|
||||||
|
" os.environ['AZUREML_ARM_PROJECT_NAME'] = args.AZUREML_ARM_PROJECT_NAME\n",
|
||||||
|
" os.environ['AZUREML_SERVICE_ENDPOINT'] = args.AZUREML_SERVICE_ENDPOINT\n",
|
||||||
|
"\n",
|
||||||
|
"populate_environ()\n",
|
||||||
|
"run = Run.get_context(allow_offline=False)\n",
|
||||||
|
"print(run._run_dto[\"parent_run_id\"])\n",
|
||||||
|
"```"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"# Next: ADLA as a Compute Target\n",
|
"# Next: ADLA as a Compute Target\n",
|
||||||
"To use ADLA as a compute target from Azure Machine Learning Pipeline, a AdlaStep is used. This [notebook](https://aka.ms/pl-adla) demonstrates the use of AdlaStep in Azure Machine Learning Pipeline."
|
"To use ADLA as a compute target from Azure Machine Learning Pipeline, a AdlaStep is used. This [notebook](https://aka.ms/pl-adla) demonstrates the use of AdlaStep in Azure Machine Learning Pipeline."
|
||||||
],
|
]
|
||||||
"cell_type": "markdown",
|
|
||||||
"metadata": {}
|
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"authors": [
|
"authors": [
|
||||||
{
|
{
|
||||||
"name": "sanpil"
|
"name": "shbijlan"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"category": "tutorial",
|
"category": "tutorial",
|
||||||
|
|||||||
@@ -40,7 +40,7 @@ def get_num(arg_num, file_num):
|
|||||||
|
|
||||||
|
|
||||||
def write_num_to_file(num, file_path):
|
def write_num_to_file(num, file_path):
|
||||||
if file_path is not None and file_path is not '':
|
if file_path is not None and file_path != '':
|
||||||
output_dir = file_path
|
output_dir = file_path
|
||||||
else:
|
else:
|
||||||
output_dir = '.'
|
output_dir = '.'
|
||||||
|
|||||||
@@ -28,13 +28,21 @@ replaced_distance_vals_df = (replaced_stfor_vals_df.replace({"distance": ".00"},
|
|||||||
|
|
||||||
normalized_df = replaced_distance_vals_df.astype({"distance": 'float64'})
|
normalized_df = replaced_distance_vals_df.astype({"distance": 'float64'})
|
||||||
|
|
||||||
|
|
||||||
|
def time_to_us(time_str):
|
||||||
|
hh, mm , ss = map(int, time_str.split(':'))
|
||||||
|
return (ss + 60 * (mm + 60 * hh)) * (10**6)
|
||||||
|
|
||||||
|
|
||||||
temp = pd.DatetimeIndex(normalized_df["pickup_datetime"])
|
temp = pd.DatetimeIndex(normalized_df["pickup_datetime"])
|
||||||
normalized_df["pickup_date"] = temp.date
|
normalized_df["pickup_date"] = pd.to_datetime(temp.date)
|
||||||
normalized_df["pickup_time"] = temp.time
|
normalized_df["pickup_time"] = temp.time
|
||||||
|
normalized_df["pickup_time"] = normalized_df["pickup_time"].apply(lambda x: time_to_us(str(x)))
|
||||||
|
|
||||||
temp = pd.DatetimeIndex(normalized_df["dropoff_datetime"])
|
temp = pd.DatetimeIndex(normalized_df["dropoff_datetime"])
|
||||||
normalized_df["dropoff_date"] = temp.date
|
normalized_df["dropoff_date"] = pd.to_datetime(temp.date)
|
||||||
normalized_df["dropoff_time"] = temp.time
|
normalized_df["dropoff_time"] = temp.time
|
||||||
|
normalized_df["dropoff_time"] = normalized_df["dropoff_time"].apply(lambda x: time_to_us(str(x)))
|
||||||
|
|
||||||
del normalized_df["pickup_datetime"]
|
del normalized_df["pickup_datetime"]
|
||||||
del normalized_df["dropoff_datetime"]
|
del normalized_df["dropoff_datetime"]
|
||||||
|
|||||||
@@ -0,0 +1,49 @@
|
|||||||
|
# Copyright (c) Microsoft. All rights reserved.
|
||||||
|
# Licensed under the MIT license.
|
||||||
|
|
||||||
|
import os
|
||||||
|
|
||||||
|
|
||||||
|
def init():
|
||||||
|
print("Init")
|
||||||
|
|
||||||
|
|
||||||
|
# For partition per folder/column jobs, ParallelRunStep pass an optional positional parameter `mini_batch_context`
|
||||||
|
# to the `run` function in user's entry script, which contains information of the mini_batch.
|
||||||
|
def run(mini_batch, mini_batch_context):
|
||||||
|
print(f"run method start: {__file__}, run({mini_batch}, {mini_batch_context})")
|
||||||
|
# `partition_key_value` is a dict that corresponds to the mini_batch, the keys of the dict are those specified
|
||||||
|
# in `partition_keys` in ParallelRunConfig.
|
||||||
|
print(f"partition_key_value = {mini_batch_context.partition_key_value}")
|
||||||
|
# `dataset` is the dataset object that corresponds to the mini_batch, which is a subset of the input dataset
|
||||||
|
# filtered by condition specified in `partition_key_value`.
|
||||||
|
print(f"dataset = {mini_batch_context.dataset}")
|
||||||
|
|
||||||
|
print(f"file_count_of_mini_batch = {len(mini_batch)}")
|
||||||
|
file_name_list = []
|
||||||
|
file_size_list = []
|
||||||
|
total_file_size_of_mini_batch = 0
|
||||||
|
for file_path in mini_batch:
|
||||||
|
file_name_list.append(os.path.basename(file_path))
|
||||||
|
file_size = os.path.getsize(file_path)
|
||||||
|
file_size_list.append(file_size)
|
||||||
|
total_file_size_of_mini_batch += file_size
|
||||||
|
print(f"total_file_size_of_mini_batch = {total_file_size_of_mini_batch}")
|
||||||
|
file_size_ratio_list = [file_size * 1.0 / total_file_size_of_mini_batch for file_size in file_size_list]
|
||||||
|
|
||||||
|
# If `output_action` is set to `append_row` in ParallelRunConfig for FileDataset input(as is in this sample
|
||||||
|
# notebook), the return value of `run` method is expected to be a list/tuple of same length with the
|
||||||
|
# input parameter `mini_batch`, and each element in the list/tuple would form a row in the result file by
|
||||||
|
# calling the Python builtin `str` function.
|
||||||
|
# If you want to specify the output format, please format and return str value as in this example.
|
||||||
|
return [
|
||||||
|
",".join([str(x) for x in fields])
|
||||||
|
for fields in zip(
|
||||||
|
file_name_list,
|
||||||
|
file_size_list,
|
||||||
|
file_size_ratio_list,
|
||||||
|
[mini_batch_context.partition_key_value["user"]] * len(mini_batch),
|
||||||
|
[mini_batch_context.partition_key_value["genres"]] * len(mini_batch),
|
||||||
|
[total_file_size_of_mini_batch] * len(mini_batch),
|
||||||
|
)
|
||||||
|
]
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
# Copyright (c) Microsoft. All rights reserved.
|
||||||
|
# Licensed under the MIT license.
|
||||||
|
|
||||||
|
import os
|
||||||
|
|
||||||
|
|
||||||
|
def init():
|
||||||
|
print("Init")
|
||||||
|
|
||||||
|
|
||||||
|
def run(mini_batch):
|
||||||
|
print(f'run method start: {__file__}, run({mini_batch})')
|
||||||
|
total_income = mini_batch["INCOME"].sum()
|
||||||
|
print(f'total_income = {total_income}')
|
||||||
|
mini_batch["total_income"] = total_income
|
||||||
|
|
||||||
|
return mini_batch
|
||||||
@@ -32,6 +32,7 @@ To run a Batch Inference job, you will need to gather some configuration data.
|
|||||||
- **node_count**: number of compute nodes to use.
|
- **node_count**: number of compute nodes to use.
|
||||||
- **process_count_per_node**: number of processes per node (optional, default value is 1).
|
- **process_count_per_node**: number of processes per node (optional, default value is 1).
|
||||||
- **mini_batch_size**: the approximate amount of input data passed to each run() invocation. For FileDataset input, this is number of files user script can process in one run() call. For TabularDataset input it is approximate size of data user script can process in one run() call. E.g. 1024, 1024KB, 10MB, 1GB (optional, default value 10 files for FileDataset and 1MB for TabularDataset.)
|
- **mini_batch_size**: the approximate amount of input data passed to each run() invocation. For FileDataset input, this is number of files user script can process in one run() call. For TabularDataset input it is approximate size of data user script can process in one run() call. E.g. 1024, 1024KB, 10MB, 1GB (optional, default value 10 files for FileDataset and 1MB for TabularDataset.)
|
||||||
|
- **partition_keys**: the keys used to partition the input data into mini-batches passed to each run() invocation. This parameter is mutually exclusive with `mini_batch_size`, and it requires the input datasets to have `partition_keys` attribute, the value of which is a superset of the value of this parameter. Each run() call would process a part of data that has identical value on the `partition_keys` specified. You can follow the examples in [file-dataset-partition-per-folder.ipynb](./file-dataset-partition-per-folder.ipynb) and [tabular-dataset-partition-per-column.ipynb](./tabular-dataset-partition-per-column.ipynb) to see how to create such datasets.
|
||||||
- **logging_level**: log verbosity. Values in increasing verbosity are: 'WARNING', 'INFO', 'DEBUG' (optional, default value is 'INFO').
|
- **logging_level**: log verbosity. Values in increasing verbosity are: 'WARNING', 'INFO', 'DEBUG' (optional, default value is 'INFO').
|
||||||
- **run_invocation_timeout**: run method invocation timeout period in seconds (optional, default value is 60).
|
- **run_invocation_timeout**: run method invocation timeout period in seconds (optional, default value is 60).
|
||||||
- **environment**: The environment definition. This field configures the Python environment. It can be configured to use an existing Python environment or to set up a temp environment for the experiment. The definition is also responsible for setting the required application dependencies.
|
- **environment**: The environment definition. This field configures the Python environment. It can be configured to use an existing Python environment or to set up a temp environment for the experiment. The definition is also responsible for setting the required application dependencies.
|
||||||
@@ -121,6 +122,8 @@ pipeline_run.wait_for_completion(show_output=True)
|
|||||||
- [file-dataset-image-inference-mnist.ipynb](./file-dataset-image-inference-mnist.ipynb) demonstrates how to run batch inference on an MNIST dataset using FileDataset.
|
- [file-dataset-image-inference-mnist.ipynb](./file-dataset-image-inference-mnist.ipynb) demonstrates how to run batch inference on an MNIST dataset using FileDataset.
|
||||||
- [tabular-dataset-inference-iris.ipynb](./tabular-dataset-inference-iris.ipynb) demonstrates how to run batch inference on an IRIS dataset using TabularDataset.
|
- [tabular-dataset-inference-iris.ipynb](./tabular-dataset-inference-iris.ipynb) demonstrates how to run batch inference on an IRIS dataset using TabularDataset.
|
||||||
- [pipeline-style-transfer.ipynb](../pipeline-style-transfer/pipeline-style-transfer-parallel-run.ipynb) demonstrates using ParallelRunStep in multi-step pipeline and using output from one step as input to ParallelRunStep.
|
- [pipeline-style-transfer.ipynb](../pipeline-style-transfer/pipeline-style-transfer-parallel-run.ipynb) demonstrates using ParallelRunStep in multi-step pipeline and using output from one step as input to ParallelRunStep.
|
||||||
|
- [file-dataset-partition-per-folder.ipynb](./file-dataset-partition-per-folder.ipynb) demonstrates how to run batch inference on file data by treating files inside each leaf folder as a mini-batch.
|
||||||
|
- [tabular-dataset-partition-per-column.ipynb](./tabular-dataset-partition-per-column.ipynb) demonstrates how to run batch inference on tabular data by treating rows with identical value on specified columns as a mini-batch.
|
||||||
|
|
||||||
# Troubleshooting guide
|
# Troubleshooting guide
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,404 @@
|
|||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"Copyright (c) Microsoft Corporation. All rights reserved. \n",
|
||||||
|
"Licensed under the MIT License."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
""
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Using Azure Machine Learning Pipelines for Batch Inference for files input partitioned by folder structure\n",
|
||||||
|
"\n",
|
||||||
|
"In this notebook, we will demonstrate how to make predictions on large quantities of data asynchronously using the ML pipelines with Azure Machine Learning. Batch inference (or batch scoring) provides cost-effective inference, with unparalleled throughput for asynchronous applications. Batch prediction pipelines can scale to perform inference on terabytes of production data. Batch prediction is optimized for high throughput, fire-and-forget predictions for a large collection of data.\n",
|
||||||
|
"\n",
|
||||||
|
"> **Tip**\n",
|
||||||
|
"If your system requires low-latency processing (to process a single document or small set of documents quickly), use [real-time scoring](https://docs.microsoft.com/en-us/azure/machine-learning/service/how-to-consume-web-service) instead of batch prediction.\n",
|
||||||
|
"\n",
|
||||||
|
"This example will create a sample dataset with nested folder structure, where the folder name corresponds to the attribute of the files inside it. The Batch Inference job would split the files inside the dataset according to their attributes, so that all files with identical value on the specified attribute will form up a single mini-batch to be processed.\n",
|
||||||
|
"\n",
|
||||||
|
"The outline of this notebook is as follows:\n",
|
||||||
|
"\n",
|
||||||
|
"- Create a dataset with nested folder structure and `partition_format` to interpret the folder structure into the attributes of files inside.\n",
|
||||||
|
"- Do batch inference on each mini-batch defined by the folder structure.\n",
|
||||||
|
"\n",
|
||||||
|
"## Prerequisites\n",
|
||||||
|
"If you are using an Azure Machine Learning Notebook VM, you are all set. Otherwise, make sure you go through the configuration Notebook located at https://github.com/Azure/MachineLearningNotebooks first. This sets you up with a working config file that has information on your workspace, subscription id, etc. "
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"### Connect to workspace"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"from azureml.core.workspace import Workspace\n",
|
||||||
|
"ws = Workspace.from_config()\n",
|
||||||
|
"print('Workspace name: ' + ws.name, \n",
|
||||||
|
" 'Azure region: ' + ws.location, \n",
|
||||||
|
" 'Subscription id: ' + ws.subscription_id, \n",
|
||||||
|
" 'Resource group: ' + ws.resource_group, sep = '\\n')\n",
|
||||||
|
"\n",
|
||||||
|
"datastore = ws.get_default_datastore()"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"import azureml.core\n",
|
||||||
|
"print(azureml.core.VERSION)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"### Upload local test data to datastore\n",
|
||||||
|
"The destination folder in the datastore is structured so that the name of each folder layer corresponds to a property of all the files inside the foler."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"from azureml.core import Dataset\n",
|
||||||
|
"\n",
|
||||||
|
"datastore.upload('test_files/disco', 'dataset_partition_test/user1/winter', overwrite=True, show_progress=False)\n",
|
||||||
|
"datastore.upload('test_files/orchestra', 'dataset_partition_test/user1/fall', overwrite=True, show_progress=False)\n",
|
||||||
|
"datastore.upload('test_files/piano', 'dataset_partition_test/user2/summer', overwrite=True, show_progress=False)\n",
|
||||||
|
"datastore.upload('test_files/spirituality', 'dataset_partition_test/user3/fall', overwrite=True, show_progress=False)\n",
|
||||||
|
"datastore.upload('test_files/piano', 'dataset_partition_test/user4/spring', overwrite=True, show_progress=False)\n",
|
||||||
|
"datastore.upload('test_files/piano', 'dataset_partition_test/user4/fall', overwrite=True, show_progress=False)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"### Create partitioned file dataset\n",
|
||||||
|
"Create a file dataset partitioned by 'user', 'season', and 'genres', each corresponds to a folder layer specified in `partition_format`. You can get a partition of data by specifying the value of one or more partition keys. E.g., by specifying `user=user1 and genres=piano`, you can get all the file that matches `dataset_partition_test/user1/*/piano.wav`."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"partitioned_file_dataset = Dataset.File.from_files(path=(datastore, 'dataset_partition_test/*/*/*.wav'),\n",
|
||||||
|
" partition_format=\"dataset_partition_test/{user}/{season}/{genres}.wav\",\n",
|
||||||
|
" validate=False)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"partitioned_file_dataset.partition_keys"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"### Create or Attach existing compute resource"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"import os\n",
|
||||||
|
"from azureml.core.compute import AmlCompute, ComputeTarget\n",
|
||||||
|
"\n",
|
||||||
|
"# choose a name for your cluster\n",
|
||||||
|
"compute_name = os.environ.get(\"AML_COMPUTE_CLUSTER_NAME\", \"cpu-cluster\")\n",
|
||||||
|
"compute_min_nodes = os.environ.get(\"AML_COMPUTE_CLUSTER_MIN_NODES\", 0)\n",
|
||||||
|
"compute_max_nodes = os.environ.get(\"AML_COMPUTE_CLUSTER_MAX_NODES\", 2)\n",
|
||||||
|
"\n",
|
||||||
|
"# This example uses CPU VM. For using GPU VM, set SKU to STANDARD_NC6\n",
|
||||||
|
"vm_size = os.environ.get(\"AML_COMPUTE_CLUSTER_SKU\", \"STANDARD_D2_V2\")\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"if compute_name in ws.compute_targets:\n",
|
||||||
|
" compute_target = ws.compute_targets[compute_name]\n",
|
||||||
|
" if compute_target and type(compute_target) is AmlCompute:\n",
|
||||||
|
" print('found compute target. just use it. ' + compute_name)\n",
|
||||||
|
"else:\n",
|
||||||
|
" print('creating a new compute target...')\n",
|
||||||
|
" provisioning_config = AmlCompute.provisioning_configuration(vm_size = vm_size,\n",
|
||||||
|
" min_nodes = compute_min_nodes, \n",
|
||||||
|
" max_nodes = compute_max_nodes)\n",
|
||||||
|
"\n",
|
||||||
|
" # create the cluster\n",
|
||||||
|
" compute_target = ComputeTarget.create(ws, compute_name, provisioning_config)\n",
|
||||||
|
" \n",
|
||||||
|
" # can poll for a minimum number of nodes and for a specific timeout. \n",
|
||||||
|
" # if no min node count is provided it will use the scale settings for the cluster\n",
|
||||||
|
" compute_target.wait_for_completion(show_output=True, min_node_count=None, timeout_in_minutes=20)\n",
|
||||||
|
" \n",
|
||||||
|
" # For a more detailed view of current AmlCompute status, use get_status()\n",
|
||||||
|
" print(compute_target.get_status().serialize())"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"### Intermediate/Output Data"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"from azureml.pipeline.core import Pipeline, PipelineData\n",
|
||||||
|
"\n",
|
||||||
|
"output_dir = PipelineData(name=\"file_dataset_inferences\", datastore=datastore)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"### Calculate total file size of each mini-batch partitioned by dataset partition key(s)\n",
|
||||||
|
"The script is to sum up the total size of files in each mini-batch."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"scripts_folder = \"Code\"\n",
|
||||||
|
"script_file = \"total_file_size.py\"\n",
|
||||||
|
"\n",
|
||||||
|
"# peek at contents\n",
|
||||||
|
"with open(os.path.join(scripts_folder, script_file)) as inference_file:\n",
|
||||||
|
" print(inference_file.read())"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Build and run the batch inference pipeline\n",
|
||||||
|
"### Specify the environment to run the script\n",
|
||||||
|
"You would need to specify the required private azureml packages in dependencies. "
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"from azureml.core import Environment\n",
|
||||||
|
"from azureml.core.runconfig import CondaDependencies, DEFAULT_CPU_IMAGE\n",
|
||||||
|
"\n",
|
||||||
|
"batch_conda_deps = CondaDependencies.create(pip_packages=[\"azureml-core\", \"azureml-dataset-runtime[fuse]\"])\n",
|
||||||
|
"batch_env = Environment(name=\"batch_environment\")\n",
|
||||||
|
"batch_env.python.conda_dependencies = batch_conda_deps\n",
|
||||||
|
"batch_env.docker.base_image = DEFAULT_CPU_IMAGE"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"### Create the configuration to wrap the inference script\n",
|
||||||
|
"The parameter `partition_keys` is a list containing a subset of the dataset partition keys, specifying how is the input dataset partitioned. Each and every possible combination of values of partition_keys will form up a mini-batch. E.g., by specifying `partition_keys=['user', 'genres']` will result in 5 mini-batches, i.e. `user=halit && genres=disco`, `user=halit && genres=orchestra`, `user=chunyu && genres=piano`, `user=kin && genres=spirituality` and `user=ramandeep && genres=piano`"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"from azureml.pipeline.steps import ParallelRunStep, ParallelRunConfig\n",
|
||||||
|
"\n",
|
||||||
|
"# In a real-world scenario, you'll want to shape your process per node and nodes to fit your problem domain.\n",
|
||||||
|
"parallel_run_config = ParallelRunConfig(\n",
|
||||||
|
" source_directory=scripts_folder,\n",
|
||||||
|
" entry_script=script_file, # the user script to run against each input\n",
|
||||||
|
" partition_keys=['user', 'genres'],\n",
|
||||||
|
" error_threshold=5,\n",
|
||||||
|
" output_action='append_row',\n",
|
||||||
|
" append_row_file_name=\"file_size_outputs.txt\",\n",
|
||||||
|
" environment=batch_env,\n",
|
||||||
|
" compute_target=compute_target, \n",
|
||||||
|
" node_count=2,\n",
|
||||||
|
" run_invocation_timeout=600\n",
|
||||||
|
")"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"### Create the pipeline step"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"parallel_run_step = ParallelRunStep(\n",
|
||||||
|
" name='summarize-file-size',\n",
|
||||||
|
" inputs=[partitioned_file_dataset.as_named_input(\"partitioned_file_input\")],\n",
|
||||||
|
" output=output_dir,\n",
|
||||||
|
" parallel_run_config=parallel_run_config,\n",
|
||||||
|
" allow_reuse=False\n",
|
||||||
|
")"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"### Run the pipeline"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"from azureml.core import Experiment\n",
|
||||||
|
"from azureml.pipeline.core import Pipeline\n",
|
||||||
|
"\n",
|
||||||
|
"pipeline = Pipeline(workspace=ws, steps=[parallel_run_step])\n",
|
||||||
|
"\n",
|
||||||
|
"pipeline_run = Experiment(ws, 'file-dataset-partition').submit(pipeline)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"pipeline_run.wait_for_completion(show_output=True)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## View the prediction results\n",
|
||||||
|
"In the total_file_size.py file above you can see that the ResultList with the filename and the prediction result gets returned. These are written to the DataStore specified in the PipelineData object as the output data, which in this case is called inferences. This containers the outputs from all of the worker nodes used in the compute cluster. You can download this data to view the results ... below just filters to the first 10 rows"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"import pandas as pd\n",
|
||||||
|
"import tempfile\n",
|
||||||
|
"\n",
|
||||||
|
"batch_run = pipeline_run.find_step_run(parallel_run_step.name)[0]\n",
|
||||||
|
"batch_output = batch_run.get_output_data(output_dir.name)\n",
|
||||||
|
"\n",
|
||||||
|
"target_dir = tempfile.mkdtemp()\n",
|
||||||
|
"batch_output.download(local_path=target_dir)\n",
|
||||||
|
"result_file = os.path.join(target_dir, batch_output.path_on_datastore, parallel_run_config.append_row_file_name)\n",
|
||||||
|
"\n",
|
||||||
|
"df = pd.read_csv(result_file, delimiter=\",\", header=None)\n",
|
||||||
|
"df.columns = [\"File Name\", \"File Size\", \"Ratio of Size in Partition\", \"user\", \"genres\", \"Total File Size of Partition\"]\n",
|
||||||
|
"print(\"Prediction has\", df.shape[0], \"rows\")\n",
|
||||||
|
"df.head(10)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": []
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"authors": [
|
||||||
|
{
|
||||||
|
"name": "pansav"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "tracych"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "migu"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"category": "Other notebooks",
|
||||||
|
"compute": [
|
||||||
|
"AML Compute"
|
||||||
|
],
|
||||||
|
"datasets": [
|
||||||
|
"None"
|
||||||
|
],
|
||||||
|
"deployment": [
|
||||||
|
"None"
|
||||||
|
],
|
||||||
|
"exclude_from_index": false,
|
||||||
|
"framework": [
|
||||||
|
"None"
|
||||||
|
],
|
||||||
|
"friendly_name": "Batch inferencing file data partitioned by folder using ParallelRunStep",
|
||||||
|
"index_order": 1,
|
||||||
|
"kernelspec": {
|
||||||
|
"display_name": "Python 3.6",
|
||||||
|
"language": "python",
|
||||||
|
"name": "python36"
|
||||||
|
},
|
||||||
|
"language_info": {
|
||||||
|
"codemirror_mode": {
|
||||||
|
"name": "ipython",
|
||||||
|
"version": 3
|
||||||
|
},
|
||||||
|
"file_extension": ".py",
|
||||||
|
"mimetype": "text/x-python",
|
||||||
|
"name": "python",
|
||||||
|
"nbconvert_exporter": "python",
|
||||||
|
"pygments_lexer": "ipython3",
|
||||||
|
"version": "3.6.9"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 4
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
name: file-dataset-partition-per-folder
|
||||||
|
dependencies:
|
||||||
|
- pip:
|
||||||
|
- azureml-sdk
|
||||||
|
- azureml-pipeline-steps
|
||||||
|
- azureml-widgets
|
||||||
|
- pandas
|
||||||
@@ -0,0 +1,427 @@
|
|||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"Copyright (c) Microsoft Corporation. All rights reserved. \n",
|
||||||
|
"Licensed under the MIT License."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
""
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Using Azure Machine Learning Pipelines for Batch Inference for tabular input partitioned by column value\n",
|
||||||
|
"\n",
|
||||||
|
"In this notebook, we will demonstrate how to make predictions on large quantities of data asynchronously using the ML pipelines with Azure Machine Learning. Batch inference (or batch scoring) provides cost-effective inference, with unparalleled throughput for asynchronous applications. Batch prediction pipelines can scale to perform inference on terabytes of production data. Batch prediction is optimized for high throughput, fire-and-forget predictions for a large collection of data.\n",
|
||||||
|
"\n",
|
||||||
|
"> **Tip**\n",
|
||||||
|
"If your system requires low-latency processing (to process a single document or small set of documents quickly), use [real-time scoring](https://docs.microsoft.com/en-us/azure/machine-learning/service/how-to-consume-web-service) instead of batch prediction.\n",
|
||||||
|
"\n",
|
||||||
|
"This example will create a partitioned tabular dataset by splitting the rows in a large csv file by its value on specified column. Each partition will form up a mini-batch in the parallel processing procedure.\n",
|
||||||
|
"\n",
|
||||||
|
"The outline of this notebook is as follows:\n",
|
||||||
|
"\n",
|
||||||
|
"- Create a tabular dataset partitioned by value on specified column.\n",
|
||||||
|
"- Do batch inference on the dataset with each mini-batch corresponds to one partition.\n",
|
||||||
|
"\n",
|
||||||
|
"## Prerequisites\n",
|
||||||
|
"If you are using an Azure Machine Learning Notebook VM, you are all set. Otherwise, make sure you go through the configuration Notebook located at https://github.com/Azure/MachineLearningNotebooks first. This sets you up with a working config file that has information on your workspace, subscription id, etc. "
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"### Connect to workspace"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"from azureml.core.workspace import Workspace\n",
|
||||||
|
"ws = Workspace.from_config()\n",
|
||||||
|
"print('Workspace name: ' + ws.name, \n",
|
||||||
|
" 'Azure region: ' + ws.location, \n",
|
||||||
|
" 'Subscription id: ' + ws.subscription_id, \n",
|
||||||
|
" 'Resource group: ' + ws.resource_group, sep = '\\n')\n",
|
||||||
|
"\n",
|
||||||
|
"datastore = ws.get_default_datastore()"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"import azureml.core\n",
|
||||||
|
"print(azureml.core.VERSION)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"### Download OJ sales data from opendataset url"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"import requests\n",
|
||||||
|
"\n",
|
||||||
|
"oj_sales_path = \"./oj.csv\"\n",
|
||||||
|
"r = requests.get(\"http://www.cs.unitn.it/~taufer/Data/oj.csv\")\n",
|
||||||
|
"open(oj_sales_path, \"wb\").write(r.content)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"### Upload OJ sales data to datastore"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"datastore.upload_files([oj_sales_path], \".\", \"oj_sales_data\", overwrite=True, show_progress=False)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"### Create tabular dataset\n",
|
||||||
|
"Create normal tabular dataset"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"from azureml.core import Dataset\n",
|
||||||
|
"\n",
|
||||||
|
"dataset = Dataset.Tabular.from_delimited_files(path=(datastore, 'oj_sales_data/*.csv'))\n",
|
||||||
|
"print(dataset.to_pandas_dataframe())"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"### Partition the tabular dataset\n",
|
||||||
|
"Partition the dataset by column 'store' and 'brand'. You can get a partition of data by specifying the value of one or more partition keys. E.g., by specifying `store=1000 and brand='tropicana'`, you can get all the rows that matches this condition in the dataset."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"partitioned_dataset = dataset.partition_by(partition_keys=['store', 'brand'], target=(datastore, \"partition_by_key_res\"), name=\"partitioned_oj_data\")\n",
|
||||||
|
"partitioned_dataset.partition_keys"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"### Create or Attach existing compute resource"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"import os\n",
|
||||||
|
"from azureml.core.compute import AmlCompute, ComputeTarget\n",
|
||||||
|
"\n",
|
||||||
|
"# choose a name for your cluster\n",
|
||||||
|
"compute_name = os.environ.get(\"AML_COMPUTE_CLUSTER_NAME\", \"cpu-cluster\")\n",
|
||||||
|
"compute_min_nodes = os.environ.get(\"AML_COMPUTE_CLUSTER_MIN_NODES\", 0)\n",
|
||||||
|
"compute_max_nodes = os.environ.get(\"AML_COMPUTE_CLUSTER_MAX_NODES\", 2)\n",
|
||||||
|
"\n",
|
||||||
|
"# This example uses CPU VM. For using GPU VM, set SKU to STANDARD_NC6\n",
|
||||||
|
"vm_size = os.environ.get(\"AML_COMPUTE_CLUSTER_SKU\", \"STANDARD_D2_V2\")\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"if compute_name in ws.compute_targets:\n",
|
||||||
|
" compute_target = ws.compute_targets[compute_name]\n",
|
||||||
|
" if compute_target and type(compute_target) is AmlCompute:\n",
|
||||||
|
" print('found compute target. just use it. ' + compute_name)\n",
|
||||||
|
"else:\n",
|
||||||
|
" print('creating a new compute target...')\n",
|
||||||
|
" provisioning_config = AmlCompute.provisioning_configuration(vm_size = vm_size,\n",
|
||||||
|
" min_nodes = compute_min_nodes, \n",
|
||||||
|
" max_nodes = compute_max_nodes)\n",
|
||||||
|
"\n",
|
||||||
|
" # create the cluster\n",
|
||||||
|
" compute_target = ComputeTarget.create(ws, compute_name, provisioning_config)\n",
|
||||||
|
" \n",
|
||||||
|
" # can poll for a minimum number of nodes and for a specific timeout. \n",
|
||||||
|
" # if no min node count is provided it will use the scale settings for the cluster\n",
|
||||||
|
" compute_target.wait_for_completion(show_output=True, min_node_count=None, timeout_in_minutes=20)\n",
|
||||||
|
" \n",
|
||||||
|
" # For a more detailed view of current AmlCompute status, use get_status()\n",
|
||||||
|
" print(compute_target.get_status().serialize())"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"### Intermediate/Output Data"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"from azureml.pipeline.core import Pipeline, PipelineData\n",
|
||||||
|
"\n",
|
||||||
|
"output_dir = PipelineData(name=\"inferences\", datastore=datastore)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"### Calculate total revenue of each mini-batch partitioned by dataset partition key(s)\n",
|
||||||
|
"The script sum up the total revenue of a mini-batch."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"scripts_folder = \"Code\"\n",
|
||||||
|
"script_file = \"total_income.py\"\n",
|
||||||
|
"\n",
|
||||||
|
"# peek at contents\n",
|
||||||
|
"with open(os.path.join(scripts_folder, script_file)) as inference_file:\n",
|
||||||
|
" print(inference_file.read())"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Build and run the batch inference pipeline\n",
|
||||||
|
"### Specify the environment to run the script\n",
|
||||||
|
"You would need to specify the required private azureml packages in dependencies. "
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"from azureml.core import Environment\n",
|
||||||
|
"from azureml.core.runconfig import CondaDependencies, DEFAULT_CPU_IMAGE\n",
|
||||||
|
"\n",
|
||||||
|
"batch_conda_deps = CondaDependencies.create(pip_packages=[\"azureml-core\", \"azureml-dataset-runtime[fuse,pandas]\"])\n",
|
||||||
|
"batch_env = Environment(name=\"batch_environment\")\n",
|
||||||
|
"batch_env.python.conda_dependencies = batch_conda_deps\n",
|
||||||
|
"batch_env.docker.base_image = DEFAULT_CPU_IMAGE"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"### Create the configuration to wrap the inference script\n",
|
||||||
|
"The parameter `partition_keys` is a list containing a subset of the dataset partition keys, specifying how is the input dataset partitioned. Each and every possible combination of values of partition_keys will form up a mini-batch. E.g., by specifying `partition_keys=['store', 'brand']` will result in mini-batches like `store=1000 && brand=tropicana`, `store=1000 && brand=dominicks`, `store=1001 && brand=dominicks`, ..."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"from azureml.pipeline.steps import ParallelRunStep, ParallelRunConfig\n",
|
||||||
|
"\n",
|
||||||
|
"# In a real-world scenario, you'll want to shape your process per node and nodes to fit your problem domain.\n",
|
||||||
|
"parallel_run_config = ParallelRunConfig(\n",
|
||||||
|
" source_directory=scripts_folder,\n",
|
||||||
|
" entry_script=script_file, # the user script to run against each input\n",
|
||||||
|
" partition_keys=['store', 'brand'],\n",
|
||||||
|
" error_threshold=5,\n",
|
||||||
|
" output_action='append_row',\n",
|
||||||
|
" append_row_file_name=\"revenue_outputs.txt\",\n",
|
||||||
|
" environment=batch_env,\n",
|
||||||
|
" compute_target=compute_target, \n",
|
||||||
|
" node_count=2,\n",
|
||||||
|
" run_invocation_timeout=600\n",
|
||||||
|
")"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"### Create the pipeline step"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"parallel_run_step = ParallelRunStep(\n",
|
||||||
|
" name='summarize-revenue',\n",
|
||||||
|
" inputs=[partitioned_dataset.as_named_input(\"partitioned_tabular_input\")],\n",
|
||||||
|
" output=output_dir,\n",
|
||||||
|
" parallel_run_config=parallel_run_config,\n",
|
||||||
|
" allow_reuse=False\n",
|
||||||
|
")"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"### Run the pipeline"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"from azureml.core import Experiment\n",
|
||||||
|
"from azureml.pipeline.core import Pipeline\n",
|
||||||
|
"\n",
|
||||||
|
"pipeline = Pipeline(workspace=ws, steps=[parallel_run_step])\n",
|
||||||
|
"\n",
|
||||||
|
"pipeline_run = Experiment(ws, 'tabular-dataset-partition').submit(pipeline)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"pipeline_run.wait_for_completion(show_output=True)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## View the prediction results\n",
|
||||||
|
"In the total_income.py file above you can see that the ResultList with the filename and the prediction result gets returned. These are written to the DataStore specified in the PipelineData object as the output data, which in this case is called inferences. This containers the outputs from all of the worker nodes used in the compute cluster. You can download this data to view the results ... below just filters to the first 10 rows"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"import pandas as pd\n",
|
||||||
|
"import tempfile\n",
|
||||||
|
"\n",
|
||||||
|
"batch_run = pipeline_run.find_step_run(parallel_run_step.name)[0]\n",
|
||||||
|
"batch_output = batch_run.get_output_data(output_dir.name)\n",
|
||||||
|
"\n",
|
||||||
|
"target_dir = tempfile.mkdtemp()\n",
|
||||||
|
"batch_output.download(local_path=target_dir)\n",
|
||||||
|
"result_file = os.path.join(target_dir, batch_output.path_on_datastore, parallel_run_config.append_row_file_name)\n",
|
||||||
|
"\n",
|
||||||
|
"df = pd.read_csv(result_file, delimiter=\" \", header=None)\n",
|
||||||
|
"\n",
|
||||||
|
"df.columns = [\"week\", \"logmove\", \"feat\", \"price\", \"AGE60\", \"EDUC\", \"ETHNIC\", \"INCOME\", \"HHLARGE\", \"WORKWOM\", \"HVAL150\", \"SSTRDIST\", \"SSTRVOL\", \"CPDIST5\", \"CPWVOL5\", \"store\", \"brand\", \"total_income\"]\n",
|
||||||
|
"print(\"Prediction has \", df.shape[0], \" rows\")\n",
|
||||||
|
"df.head(10)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": []
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"authors": [
|
||||||
|
{
|
||||||
|
"name": "pansav"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "tracych"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "migu"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"category": "Other notebooks",
|
||||||
|
"compute": [
|
||||||
|
"AML Compute"
|
||||||
|
],
|
||||||
|
"datasets": [
|
||||||
|
"OJ Sales Data"
|
||||||
|
],
|
||||||
|
"deployment": [
|
||||||
|
"None"
|
||||||
|
],
|
||||||
|
"exclude_from_index": false,
|
||||||
|
"framework": [
|
||||||
|
"None"
|
||||||
|
],
|
||||||
|
"friendly_name": "Batch inferencing OJ Sales Data partitioned by column using ParallelRunStep",
|
||||||
|
"index_order": 1,
|
||||||
|
"kernelspec": {
|
||||||
|
"display_name": "Python 3.6",
|
||||||
|
"language": "python",
|
||||||
|
"name": "python36"
|
||||||
|
},
|
||||||
|
"language_info": {
|
||||||
|
"codemirror_mode": {
|
||||||
|
"name": "ipython",
|
||||||
|
"version": 3
|
||||||
|
},
|
||||||
|
"file_extension": ".py",
|
||||||
|
"mimetype": "text/x-python",
|
||||||
|
"name": "python",
|
||||||
|
"nbconvert_exporter": "python",
|
||||||
|
"pygments_lexer": "ipython3",
|
||||||
|
"version": "3.6.9"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 4
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
name: tabular-dataset-partition-per-column
|
||||||
|
dependencies:
|
||||||
|
- pip:
|
||||||
|
- azureml-sdk
|
||||||
|
- azureml-pipeline-steps
|
||||||
|
- azureml-widgets
|
||||||
|
- pandas
|
||||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -258,7 +258,7 @@
|
|||||||
" - azureml-defaults\n",
|
" - azureml-defaults\n",
|
||||||
" - azureml-opendatasets\n",
|
" - azureml-opendatasets\n",
|
||||||
" - chainer==5.1.0\n",
|
" - chainer==5.1.0\n",
|
||||||
" - cupy-cuda90==5.1.0\n",
|
" - cupy-cuda100==5.1.0\n",
|
||||||
" - mpi4py==3.0.0\n",
|
" - mpi4py==3.0.0\n",
|
||||||
" - pytest"
|
" - pytest"
|
||||||
]
|
]
|
||||||
@@ -275,7 +275,7 @@
|
|||||||
"chainer_env = Environment.from_conda_specification(name = 'chainer-5.1.0-gpu', file_path = './conda_dependencies.yml')\n",
|
"chainer_env = Environment.from_conda_specification(name = 'chainer-5.1.0-gpu', file_path = './conda_dependencies.yml')\n",
|
||||||
"\n",
|
"\n",
|
||||||
"# Specify a GPU base image\n",
|
"# Specify a GPU base image\n",
|
||||||
"chainer_env.docker.base_image = 'mcr.microsoft.com/azureml/intelmpi2018.3-cuda9.0-cudnn7-ubuntu16.04'\n",
|
"chainer_env.docker.base_image = 'mcr.microsoft.com/azureml/openmpi3.1.2-cuda10.0-cudnn7-ubuntu18.04'\n",
|
||||||
"\n",
|
"\n",
|
||||||
"docker_config = DockerConfiguration(use_docker=True)"
|
"docker_config = DockerConfiguration(use_docker=True)"
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -183,7 +183,7 @@
|
|||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"# make sure utils.py is in the same directory as this code\n",
|
"# make sure utils.py is in the same directory as this code\n",
|
||||||
"from utils import load_data, one_hot_encode\n",
|
"from utils import load_data\n",
|
||||||
"\n",
|
"\n",
|
||||||
"# note we also shrink the intensity values (X) from 0-255 to 0-1. This helps the model converge faster.\n",
|
"# note we also shrink the intensity values (X) from 0-255 to 0-1. This helps the model converge faster.\n",
|
||||||
"X_train = load_data(os.path.join(data_folder, 'train-images-idx3-ubyte.gz'), False) / 255.0\n",
|
"X_train = load_data(os.path.join(data_folder, 'train-images-idx3-ubyte.gz'), False) / 255.0\n",
|
||||||
@@ -253,11 +253,12 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
|
"from azureml.exceptions import UserErrorException\n",
|
||||||
"dataset_registered = False\n",
|
"dataset_registered = False\n",
|
||||||
"try:\n",
|
"try:\n",
|
||||||
" temp = Dataset.get_by_name(workspace = ws, name = 'mnist-dataset')\n",
|
" temp = Dataset.get_by_name(workspace = ws, name = 'mnist-dataset')\n",
|
||||||
" dataset_registered = True\n",
|
" dataset_registered = True\n",
|
||||||
"except:\n",
|
"except UserErrorException:\n",
|
||||||
" print(\"The dataset mnist-dataset is not registered in workspace yet.\")\n",
|
" print(\"The dataset mnist-dataset is not registered in workspace yet.\")\n",
|
||||||
"\n",
|
"\n",
|
||||||
"if not dataset_registered:\n",
|
"if not dataset_registered:\n",
|
||||||
@@ -297,7 +298,7 @@
|
|||||||
"from azureml.core.compute_target import ComputeTargetException\n",
|
"from azureml.core.compute_target import ComputeTargetException\n",
|
||||||
"\n",
|
"\n",
|
||||||
"# choose a name for your cluster\n",
|
"# choose a name for your cluster\n",
|
||||||
"cluster_name = \"gpu-cluster\"\n",
|
"cluster_name = \"hd-cluster\"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"try:\n",
|
"try:\n",
|
||||||
" compute_target = ComputeTarget(workspace=ws, name=cluster_name)\n",
|
" compute_target = ComputeTarget(workspace=ws, name=cluster_name)\n",
|
||||||
@@ -1009,15 +1010,14 @@
|
|||||||
"from azureml.core.webservice import AciWebservice\n",
|
"from azureml.core.webservice import AciWebservice\n",
|
||||||
"from azureml.core.model import InferenceConfig\n",
|
"from azureml.core.model import InferenceConfig\n",
|
||||||
"from azureml.core.model import Model\n",
|
"from azureml.core.model import Model\n",
|
||||||
"from azureml.core.environment import Environment\n",
|
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"myenv = Environment.from_conda_specification(name=\"myenv\", file_path=\"myenv.yml\")\n",
|
"myenv = Environment.from_conda_specification(name=\"myenv\", file_path=\"myenv.yml\")\n",
|
||||||
"inference_config = InferenceConfig(entry_script=\"score.py\", environment=myenv)\n",
|
"inference_config = InferenceConfig(entry_script=\"score.py\", environment=myenv)\n",
|
||||||
"\n",
|
"\n",
|
||||||
"aciconfig = AciWebservice.deploy_configuration(cpu_cores=1,\n",
|
"aciconfig = AciWebservice.deploy_configuration(cpu_cores=2,\n",
|
||||||
" auth_enabled=True, # this flag generates API keys to secure access\n",
|
" auth_enabled=True, # this flag generates API keys to secure access\n",
|
||||||
" memory_gb=1,\n",
|
" memory_gb=2,\n",
|
||||||
" tags={'name': 'mnist', 'framework': 'Keras'},\n",
|
" tags={'name': 'mnist', 'framework': 'Keras'},\n",
|
||||||
" description='Keras MLP on MNIST')\n",
|
" description='Keras MLP on MNIST')\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
|||||||
@@ -117,7 +117,7 @@
|
|||||||
"from azureml.core.compute_target import ComputeTargetException\n",
|
"from azureml.core.compute_target import ComputeTargetException\n",
|
||||||
"\n",
|
"\n",
|
||||||
"# choose a name for your cluster\n",
|
"# choose a name for your cluster\n",
|
||||||
"cluster_name = \"gpu-cluster\"\n",
|
"cluster_name = \"hd-cluster\"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"try:\n",
|
"try:\n",
|
||||||
" compute_target = ComputeTarget(workspace=ws, name=cluster_name)\n",
|
" compute_target = ComputeTarget(workspace=ws, name=cluster_name)\n",
|
||||||
@@ -579,13 +579,12 @@
|
|||||||
"source": [
|
"source": [
|
||||||
"from azureml.core.webservice import AciWebservice\n",
|
"from azureml.core.webservice import AciWebservice\n",
|
||||||
"from azureml.core.model import InferenceConfig\n",
|
"from azureml.core.model import InferenceConfig\n",
|
||||||
"from azureml.core.webservice import Webservice\n",
|
|
||||||
"from azureml.core.model import Model\n",
|
"from azureml.core.model import Model\n",
|
||||||
"\n",
|
"\n",
|
||||||
"inference_config = InferenceConfig(entry_script=\"pytorch_score.py\", environment=pytorch_env)\n",
|
"inference_config = InferenceConfig(entry_script=\"pytorch_score.py\", environment=pytorch_env)\n",
|
||||||
"\n",
|
"\n",
|
||||||
"aciconfig = AciWebservice.deploy_configuration(cpu_cores=1, \n",
|
"aciconfig = AciWebservice.deploy_configuration(cpu_cores=2, \n",
|
||||||
" memory_gb=1, \n",
|
" memory_gb=2, \n",
|
||||||
" tags={'data': 'birds', 'method':'transfer learning', 'framework':'pytorch'},\n",
|
" tags={'data': 'birds', 'method':'transfer learning', 'framework':'pytorch'},\n",
|
||||||
" description='Classify turkey/chickens using transfer learning with PyTorch')\n",
|
" description='Classify turkey/chickens using transfer learning with PyTorch')\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
|||||||
@@ -142,7 +142,7 @@
|
|||||||
"from azureml.core.compute_target import ComputeTargetException\n",
|
"from azureml.core.compute_target import ComputeTargetException\n",
|
||||||
"\n",
|
"\n",
|
||||||
"# choose a name for your cluster\n",
|
"# choose a name for your cluster\n",
|
||||||
"cluster_name = \"cpu-cluster\"\n",
|
"cluster_name = \"hd-cluster\"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"try:\n",
|
"try:\n",
|
||||||
" compute_target = ComputeTarget(workspace=ws, name=cluster_name)\n",
|
" compute_target = ComputeTarget(workspace=ws, name=cluster_name)\n",
|
||||||
|
|||||||
@@ -295,7 +295,7 @@
|
|||||||
"from azureml.core.compute_target import ComputeTargetException\n",
|
"from azureml.core.compute_target import ComputeTargetException\n",
|
||||||
"\n",
|
"\n",
|
||||||
"# choose a name for your cluster\n",
|
"# choose a name for your cluster\n",
|
||||||
"cluster_name = \"gpu-cluster\"\n",
|
"cluster_name = \"hd-cluster\"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"try:\n",
|
"try:\n",
|
||||||
" compute_target = ComputeTarget(workspace=ws, name=cluster_name)\n",
|
" compute_target = ComputeTarget(workspace=ws, name=cluster_name)\n",
|
||||||
|
|||||||
@@ -265,11 +265,12 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
|
"from azureml.exceptions import UserErrorException\n",
|
||||||
"dataset_registered = False\n",
|
"dataset_registered = False\n",
|
||||||
"try:\n",
|
"try:\n",
|
||||||
" temp = Dataset.get_by_name(workspace = ws, name = 'mnist-dataset')\n",
|
" temp = Dataset.get_by_name(workspace = ws, name = 'mnist-dataset')\n",
|
||||||
" dataset_registered = True\n",
|
" dataset_registered = True\n",
|
||||||
"except:\n",
|
"except UserErrorException:\n",
|
||||||
" print(\"The dataset mnist-dataset is not registered in workspace yet.\")\n",
|
" print(\"The dataset mnist-dataset is not registered in workspace yet.\")\n",
|
||||||
"\n",
|
"\n",
|
||||||
"if not dataset_registered:\n",
|
"if not dataset_registered:\n",
|
||||||
@@ -311,7 +312,7 @@
|
|||||||
"from azureml.core.compute_target import ComputeTargetException\n",
|
"from azureml.core.compute_target import ComputeTargetException\n",
|
||||||
"\n",
|
"\n",
|
||||||
"# choose a name for your cluster\n",
|
"# choose a name for your cluster\n",
|
||||||
"cluster_name = \"gpu-cluster\"\n",
|
"cluster_name = \"hd-cluster\"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"try:\n",
|
"try:\n",
|
||||||
" compute_target = ComputeTarget(workspace=ws, name=cluster_name)\n",
|
" compute_target = ComputeTarget(workspace=ws, name=cluster_name)\n",
|
||||||
@@ -964,14 +965,13 @@
|
|||||||
"from azureml.core.webservice import AciWebservice\n",
|
"from azureml.core.webservice import AciWebservice\n",
|
||||||
"from azureml.core.model import InferenceConfig\n",
|
"from azureml.core.model import InferenceConfig\n",
|
||||||
"from azureml.core.model import Model\n",
|
"from azureml.core.model import Model\n",
|
||||||
"from azureml.core.environment import Environment\n",
|
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"myenv = Environment.from_conda_specification(name=\"myenv\", file_path=\"myenv.yml\")\n",
|
"myenv = Environment.from_conda_specification(name=\"myenv\", file_path=\"myenv.yml\")\n",
|
||||||
"inference_config = InferenceConfig(entry_script=\"score.py\", environment=myenv)\n",
|
"inference_config = InferenceConfig(entry_script=\"score.py\", environment=myenv)\n",
|
||||||
"\n",
|
"\n",
|
||||||
"aciconfig = AciWebservice.deploy_configuration(cpu_cores=1, \n",
|
"aciconfig = AciWebservice.deploy_configuration(cpu_cores=2, \n",
|
||||||
" memory_gb=1, \n",
|
" memory_gb=2, \n",
|
||||||
" tags={'name':'mnist', 'framework': 'TensorFlow DNN'},\n",
|
" tags={'name':'mnist', 'framework': 'TensorFlow DNN'},\n",
|
||||||
" description='Tensorflow DNN on MNIST')\n",
|
" description='Tensorflow DNN on MNIST')\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
|||||||
@@ -6,15 +6,15 @@ import mlflow.keras
|
|||||||
import numpy as np
|
import numpy as np
|
||||||
import warnings
|
import warnings
|
||||||
|
|
||||||
import keras
|
from tensorflow import keras
|
||||||
from keras.models import Sequential
|
from tensorflow.keras.models import Sequential
|
||||||
from keras.layers import Dense
|
from tensorflow.keras.layers import Dense
|
||||||
from keras.optimizers import RMSprop
|
from tensorflow.keras.optimizers import RMSprop
|
||||||
|
|
||||||
print("Keras version:", keras.__version__)
|
print("Keras version:", keras.__version__)
|
||||||
|
|
||||||
# Enable auto-logging to MLflow to capture Keras metrics.
|
# Enable auto-logging to MLflow to capture Keras metrics.
|
||||||
mlflow.keras.autolog()
|
mlflow.autolog()
|
||||||
|
|
||||||
# Model / data parameters
|
# Model / data parameters
|
||||||
n_inputs = 28 * 28
|
n_inputs = 28 * 28
|
||||||
|
|||||||
@@ -450,6 +450,10 @@
|
|||||||
" # GPU\n",
|
" # GPU\n",
|
||||||
" use_gpu=False, \n",
|
" use_gpu=False, \n",
|
||||||
" \n",
|
" \n",
|
||||||
|
" # Shared memory size\n",
|
||||||
|
" # Uncomment line below to set shm_size for workers (requires Azure Machine Learning SDK 1.33 or greater)\n",
|
||||||
|
" # shm_size=1024*1024*1024, \n",
|
||||||
|
" \n",
|
||||||
" # PIP packages to use\n",
|
" # PIP packages to use\n",
|
||||||
")"
|
")"
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -256,7 +256,6 @@
|
|||||||
" dockerfile=f.read()\n",
|
" dockerfile=f.read()\n",
|
||||||
"\n",
|
"\n",
|
||||||
" xvfb_env = Environment(name='xvfb-vdisplay')\n",
|
" xvfb_env = Environment(name='xvfb-vdisplay')\n",
|
||||||
" xvfb_env.docker.enabled = True\n",
|
|
||||||
" xvfb_env.docker.base_image = None\n",
|
" xvfb_env.docker.base_image = None\n",
|
||||||
" xvfb_env.docker.base_dockerfile = dockerfile\n",
|
" xvfb_env.docker.base_dockerfile = dockerfile\n",
|
||||||
" \n",
|
" \n",
|
||||||
@@ -713,7 +712,6 @@
|
|||||||
" dockerfile=f.read()\n",
|
" dockerfile=f.read()\n",
|
||||||
"\n",
|
"\n",
|
||||||
"xvfb_env = Environment(name='xvfb-vdisplay')\n",
|
"xvfb_env = Environment(name='xvfb-vdisplay')\n",
|
||||||
"xvfb_env.docker.enabled = True\n",
|
|
||||||
"xvfb_env.docker.base_image = None\n",
|
"xvfb_env.docker.base_image = None\n",
|
||||||
"xvfb_env.docker.base_dockerfile = dockerfile\n",
|
"xvfb_env.docker.base_dockerfile = dockerfile\n",
|
||||||
" \n",
|
" \n",
|
||||||
|
|||||||
@@ -14,6 +14,8 @@ RUN conda install -y conda=4.7.12 python=3.7 && conda clean -ay && \
|
|||||||
azureml-dataset-runtime[fuse,pandas] \
|
azureml-dataset-runtime[fuse,pandas] \
|
||||||
azureml-contrib-reinforcementlearning \
|
azureml-contrib-reinforcementlearning \
|
||||||
gputil \
|
gputil \
|
||||||
|
scipy \
|
||||||
|
pyglet \
|
||||||
cloudpickle==1.3.0 \
|
cloudpickle==1.3.0 \
|
||||||
tensorboardX \
|
tensorboardX \
|
||||||
tensorflow==1.14.0 \
|
tensorflow==1.14.0 \
|
||||||
|
|||||||
@@ -0,0 +1,703 @@
|
|||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"Copyright (c) Microsoft Corporation. All rights reserved.\n",
|
||||||
|
"\n",
|
||||||
|
"Licensed under the MIT License."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
""
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Automated Machine Learning\n",
|
||||||
|
"_**Regression with Aml Compute**_\n",
|
||||||
|
"\n",
|
||||||
|
"## Contents\n",
|
||||||
|
"1. [Introduction](#Introduction)\n",
|
||||||
|
"1. [Setup](#Setup)\n",
|
||||||
|
"1. [Data](#Data)\n",
|
||||||
|
"1. [Train](#Train)\n",
|
||||||
|
"1. [Results](#Results)\n",
|
||||||
|
"1. [Test](#Test)\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Introduction\n",
|
||||||
|
"In this example we use the Hardware Performance Dataset to showcase how you can use AutoML for a simple regression problem. The regression goal is to predict the performance of certain combinations of hardware parts.\n",
|
||||||
|
"After training AutoML models for this regression data set, we show how you can compute model explanations on your remote compute using a sample explainer script.\n",
|
||||||
|
"\n",
|
||||||
|
"If you are using an Azure Machine Learning Compute Instance, you are all set. Otherwise, go through the [configuration](../../../configuration.ipynb) notebook first if you haven't already to establish your connection to the AzureML Workspace. \n",
|
||||||
|
"\n",
|
||||||
|
"In this notebook you will learn how to:\n",
|
||||||
|
"1. Create an `Experiment` in an existing `Workspace`.\n",
|
||||||
|
"2. Instantiate AutoMLConfig with FeaturizationConfig for customization.\n",
|
||||||
|
"3. Train the model using remote compute.\n",
|
||||||
|
"4. Explore the results and featurization transparency options.\n",
|
||||||
|
"5. Setup remote compute for computing the model explanations for a given AutoML model.\n",
|
||||||
|
"6. Start an AzureML experiment on your remote compute.\n",
|
||||||
|
"7. Submit model analysis, explain runs and counterfactual runs for a specific AutoML model.\n",
|
||||||
|
"8. Download the feature importance for raw features and visualize the explanations for raw features on azure portal. \n",
|
||||||
|
"10. Download counterfactual examples and view them in the notebook.\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Setup\n",
|
||||||
|
"\n",
|
||||||
|
"As part of the setup you have already created an Azure ML `Workspace` object. For Automated ML you will need to create an `Experiment` object, which is a named object in a `Workspace` used to run experiments."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"import logging\n",
|
||||||
|
"\n",
|
||||||
|
"import pandas as pd\n",
|
||||||
|
"\n",
|
||||||
|
"import azureml.core\n",
|
||||||
|
"from azureml.core.experiment import Experiment\n",
|
||||||
|
"from azureml.core.workspace import Workspace\n",
|
||||||
|
"import azureml.dataprep as dprep\n",
|
||||||
|
"from azureml.automl.core.featurization import FeaturizationConfig\n",
|
||||||
|
"from azureml.train.automl import AutoMLConfig\n",
|
||||||
|
"from azureml.core.dataset import Dataset"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"This sample notebook may use features that are not available in previous versions of the Azure ML SDK."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"print(\"This notebook was created using version 1.36.0 of the Azure ML SDK\")\n",
|
||||||
|
"print(\"You are currently using version\", azureml.core.VERSION, \"of the Azure ML SDK\")"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"ws = Workspace.from_config()\n",
|
||||||
|
"\n",
|
||||||
|
"# Choose a name for the experiment.\n",
|
||||||
|
"experiment_name = 'automl-regression-rai'\n",
|
||||||
|
"experiment = Experiment(ws, experiment_name)\n",
|
||||||
|
"\n",
|
||||||
|
"output = {}\n",
|
||||||
|
"output['Subscription ID'] = ws.subscription_id\n",
|
||||||
|
"output['Workspace Name'] = ws.name\n",
|
||||||
|
"output['Resource Group'] = ws.resource_group\n",
|
||||||
|
"output['Location'] = ws.location\n",
|
||||||
|
"output['Experiment Name'] = experiment.name\n",
|
||||||
|
"pd.set_option('display.max_colwidth', -1)\n",
|
||||||
|
"outputDf = pd.DataFrame(data = output, index = [''])\n",
|
||||||
|
"outputDf.T"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"### Create or Attach existing AmlCompute\n",
|
||||||
|
"You will need to create a [compute target](https://docs.microsoft.com/azure/machine-learning/service/concept-azure-machine-learning-architecture#compute-target) for your AutoML run. In this tutorial, you create `AmlCompute` as your training compute resource.\n",
|
||||||
|
"\n",
|
||||||
|
"> Note that if you have an AzureML Data Scientist role, you will not have permission to create compute resources. Talk to your workspace or IT admin to create the compute targets described in this section, if they do not already exist.\n",
|
||||||
|
"\n",
|
||||||
|
"**Creation of AmlCompute takes approximately 5 minutes.** If the AmlCompute with that name is already in your workspace this code will skip the creation process.\n",
|
||||||
|
"\n",
|
||||||
|
"As with other Azure services, there are limits on certain resources (e.g. AmlCompute) associated with the Azure Machine Learning service. Please read [this article](https://docs.microsoft.com/en-us/azure/machine-learning/service/how-to-manage-quotas) on the default limits and how to request more quota."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"from azureml.core.compute import ComputeTarget, AmlCompute\n",
|
||||||
|
"from azureml.core.compute_target import ComputeTargetException\n",
|
||||||
|
"\n",
|
||||||
|
"# Choose a name for your cluster.\n",
|
||||||
|
"amlcompute_cluster_name = \"hardware-rai\"\n",
|
||||||
|
"\n",
|
||||||
|
"# Verify that cluster does not exist already\n",
|
||||||
|
"try:\n",
|
||||||
|
" compute_target = ComputeTarget(workspace=ws, name=amlcompute_cluster_name)\n",
|
||||||
|
" print('Found existing cluster, use it.')\n",
|
||||||
|
"except ComputeTargetException:\n",
|
||||||
|
" compute_config = AmlCompute.provisioning_configuration(vm_size='STANDARD_D2_V2',\n",
|
||||||
|
" max_nodes=4)\n",
|
||||||
|
" compute_target = ComputeTarget.create(ws, amlcompute_cluster_name, compute_config)\n",
|
||||||
|
"\n",
|
||||||
|
"compute_target.wait_for_completion(show_output=True)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"### Setup Training and Test Data for AutoML experiment\n",
|
||||||
|
"\n",
|
||||||
|
"Load the hardware dataset from a csv file containing both training features and labels. The features are inputs to the model, while the training labels represent the expected output of the model. Next, we'll split the data using random_split and extract the training data for the model. We also register the datasets in your workspace using a name so that these datasets may be accessed from the remote compute."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"data = 'https://automlsamplenotebookdata.blob.core.windows.net/automl-sample-notebook-data/machineData.csv'\n",
|
||||||
|
"\n",
|
||||||
|
"dataset = Dataset.Tabular.from_delimited_files(data)\n",
|
||||||
|
"\n",
|
||||||
|
"# Split the dataset into train and test datasets\n",
|
||||||
|
"train_data, test_data = dataset.random_split(percentage=0.8, seed=223)\n",
|
||||||
|
"\n",
|
||||||
|
"# Drop ModelName\n",
|
||||||
|
"train_data = train_data.drop_columns(['ModelName'])\n",
|
||||||
|
"test_data = test_data.drop_columns(['ModelName'])\n",
|
||||||
|
"\n",
|
||||||
|
"# Register the train dataset with your workspace\n",
|
||||||
|
"train_data.register(workspace = ws, name = 'rai_machine_train_dataset',\n",
|
||||||
|
" description = 'hardware performance training data',\n",
|
||||||
|
" create_new_version=True)\n",
|
||||||
|
"\n",
|
||||||
|
"# Register the test dataset with your workspace\n",
|
||||||
|
"test_data.register(workspace = ws, name = 'rai_machine_test_dataset', description = 'hardware performance test data', create_new_version=True)\n",
|
||||||
|
"\n",
|
||||||
|
"label =\"ERP\"\n",
|
||||||
|
"\n",
|
||||||
|
"train_data.to_pandas_dataframe().head()"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Train\n",
|
||||||
|
"\n",
|
||||||
|
"Instantiate an `AutoMLConfig` object to specify the settings and data used to run the experiment.\n",
|
||||||
|
"\n",
|
||||||
|
"|Property|Description|\n",
|
||||||
|
"|-|-|\n",
|
||||||
|
"|**task**|classification, regression or forecasting|\n",
|
||||||
|
"|**primary_metric**|This is the metric that you want to optimize. Regression supports the following primary metrics: <br><i>spearman_correlation</i><br><i>normalized_root_mean_squared_error</i><br><i>r2_score</i><br><i>normalized_mean_absolute_error</i>|\n",
|
||||||
|
"|**experiment_timeout_hours**| Maximum amount of time in hours that all iterations combined can take before the experiment terminates.|\n",
|
||||||
|
"|**enable_early_stopping**| Flag to enble early termination if the score is not improving in the short term.|\n",
|
||||||
|
"|**featurization**| 'auto' / 'off' / FeaturizationConfig Indicator for whether featurization step should be done automatically or not, or whether customized featurization should be used. Setting this enables AutoML to perform featurization on the input to handle *missing data*, and to perform some common *feature extraction*. Note: If the input data is sparse, featurization cannot be turned on.|\n",
|
||||||
|
"|**n_cross_validations**|Number of cross validation splits.|\n",
|
||||||
|
"|**training_data**|(sparse) array-like, shape = [n_samples, n_features]|\n",
|
||||||
|
"|**label_column_name**|(sparse) array-like, shape = [n_samples, ], targets values.|"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Customization\n",
|
||||||
|
"\n",
|
||||||
|
"Supported customization includes:\n",
|
||||||
|
"\n",
|
||||||
|
"1. Column purpose update: Override feature type for the specified column.\n",
|
||||||
|
"2. Transformer parameter update: Update parameters for the specified transformer. Currently supports Imputer and HashOneHotEncoder.\n",
|
||||||
|
"3. Drop columns: Columns to drop from being featurized.\n",
|
||||||
|
"4. Block transformers: Allow/Block transformers to be used on featurization process."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"Create FeaturizationConfig object using API calls"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"tags": [
|
||||||
|
"sample-featurizationconfig-remarks2"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"featurization_config = FeaturizationConfig()\n",
|
||||||
|
"featurization_config.blocked_transformers = ['LabelEncoder']\n",
|
||||||
|
"#featurization_config.drop_columns = ['MMIN']\n",
|
||||||
|
"featurization_config.add_column_purpose('MYCT', 'Numeric')\n",
|
||||||
|
"featurization_config.add_column_purpose('VendorName', 'CategoricalHash')\n",
|
||||||
|
"#default strategy mean, add transformer param for for 3 columns\n",
|
||||||
|
"featurization_config.add_transformer_params('Imputer', ['CACH'], {\"strategy\": \"median\"})\n",
|
||||||
|
"featurization_config.add_transformer_params('Imputer', ['CHMIN'], {\"strategy\": \"median\"})\n",
|
||||||
|
"featurization_config.add_transformer_params('Imputer', ['PRP'], {\"strategy\": \"most_frequent\"})\n",
|
||||||
|
"#featurization_config.add_transformer_params('HashOneHotEncoder', [], {\"number_of_bits\": 3})"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"tags": [
|
||||||
|
"sample-featurizationconfig-remarks3"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"automl_settings = {\n",
|
||||||
|
" \"enable_early_stopping\": True, \n",
|
||||||
|
" \"experiment_timeout_hours\" : 0.25,\n",
|
||||||
|
" \"max_concurrent_iterations\": 4,\n",
|
||||||
|
" \"max_cores_per_iteration\": -1,\n",
|
||||||
|
" \"n_cross_validations\": 5,\n",
|
||||||
|
" \"primary_metric\": 'normalized_root_mean_squared_error',\n",
|
||||||
|
" \"verbosity\": logging.INFO\n",
|
||||||
|
"}\n",
|
||||||
|
"\n",
|
||||||
|
"automl_config = AutoMLConfig(task = 'regression',\n",
|
||||||
|
" debug_log = 'automl_errors.log',\n",
|
||||||
|
" compute_target=compute_target,\n",
|
||||||
|
" featurization=featurization_config,\n",
|
||||||
|
" training_data = train_data,\n",
|
||||||
|
" label_column_name = label,\n",
|
||||||
|
" **automl_settings\n",
|
||||||
|
" )"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"Call the `submit` method on the experiment object and pass the `AutoMLConfig`. Execution of local runs is synchronous. Depending on the data and the number of iterations this can run for a while.\n",
|
||||||
|
"In this example, we specify `show_output=False` to suppress output for each iteration. You can monitor the run by clicking on the link in the output."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"remote_run = experiment.submit(automl_config, show_output=False)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"Run the following cell to access previous runs. Uncomment the cell below and update the run_id."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"#from azureml.train.automl.run import AutoMLRun\n",
|
||||||
|
"#remote_run = AutoMLRun(experiment=experiment, run_id='AutoML_1723d4fe-c33d-41f7-83ad-c010215583b0')\n",
|
||||||
|
"#remote_run"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"remote_run.wait_for_completion(wait_post_processing=True)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Generating Responsible AI insights for AutoML model\n",
|
||||||
|
"This section will walk you through the workflow to compute Responsible AI insights like model explanations and counterfactual examples using model analysis workflow for an AutoML model on your remote compute.\n",
|
||||||
|
"\n",
|
||||||
|
"### Retrieve any AutoML Model for explanations\n",
|
||||||
|
"\n",
|
||||||
|
"Below we select an AutoML pipeline from our iterations. The `get_best_child` method returns the a AutoML run with the best score for the specified metric"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"automl_run = remote_run.get_best_child(metric='mean_absolute_error')"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"### Setup model analysis on the remote compute\n",
|
||||||
|
"The following section provides details on how to setup an AzureML experiment to run model analysis for an AutoML model on your remote compute."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"#### Create conda configuration for model analysis and explanations runs from automl_run object."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"from azureml.core.runconfig import RunConfiguration\n",
|
||||||
|
"from azureml.core.conda_dependencies import CondaDependencies\n",
|
||||||
|
"\n",
|
||||||
|
"# create a new RunConfiguration object\n",
|
||||||
|
"conda_run_config = RunConfiguration(framework=\"python\")\n",
|
||||||
|
"\n",
|
||||||
|
"# Set compute target to AmlCompute\n",
|
||||||
|
"conda_run_config.target = compute_target\n",
|
||||||
|
"\n",
|
||||||
|
"# specify CondaDependencies obj\n",
|
||||||
|
"conda_run_config.environment = automl_run.get_environment()"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"Register the AutoML model and create a `PickleModelLoader` for the model analysis so that the model analysis can instantiate the model downloaded from AzureML."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"from azureml.core import Model\n",
|
||||||
|
"from azureml.responsibleai.common.pickle_model_loader import PickleModelLoader\n",
|
||||||
|
"from azureml.responsibleai.tools.model_analysis.model_analysis_config import ModelAnalysisConfig\n",
|
||||||
|
"from azureml.responsibleai.tools.model_analysis.explain_config import ExplainConfig\n",
|
||||||
|
"from azureml.automl.core.shared.constants import MODEL_PATH\n",
|
||||||
|
"\n",
|
||||||
|
"automl_run.download_file(name=MODEL_PATH, output_file_path='model.pkl')\n",
|
||||||
|
"\n",
|
||||||
|
"model = automl_run.register_model(model_name='automl_rai', \n",
|
||||||
|
" model_path='outputs/model.pkl')\n",
|
||||||
|
"\n",
|
||||||
|
"model_loader = PickleModelLoader('model.pkl')"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"Construct the list of the feature column names by dropping the name of the label column from the list of all column names."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"X_column_names = train_data.to_pandas_dataframe().columns.values\n",
|
||||||
|
"X_column_names = X_column_names[X_column_names!=label]\n",
|
||||||
|
"X_column_names"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"Get the train and test dataset for the model analysis."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"train_dataset = Dataset.get_by_name(workspace=ws, name='rai_machine_train_dataset')\n",
|
||||||
|
"test_dataset = Dataset.get_by_name(workspace=ws, name='rai_machine_test_dataset')"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"In the `ModelAnalysisConfig` below, `confidential_datastore_name` is the name of the datastore where the analyses will be uploaded. This example uses the default data store because the dataset is also in the default datastore. If you have confidential data in the dataset, you should specify a different data store as the `confidential_datastore_name` because analysis makes a copy of the data in this data store."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"categorical_features = ['VendorName']\n",
|
||||||
|
"\n",
|
||||||
|
"model_analysis_config = ModelAnalysisConfig(\n",
|
||||||
|
" title=\"Model analysis\",\n",
|
||||||
|
" model=model,\n",
|
||||||
|
" model_type='regression',\n",
|
||||||
|
" model_loader=model_loader,\n",
|
||||||
|
" train_dataset=train_dataset,\n",
|
||||||
|
" test_dataset=test_dataset,\n",
|
||||||
|
" X_column_names=X_column_names,\n",
|
||||||
|
" target_column_name=label,\n",
|
||||||
|
" confidential_datastore_name=ws.get_default_datastore().name,\n",
|
||||||
|
" run_configuration=conda_run_config,\n",
|
||||||
|
" categorical_column_names=categorical_features\n",
|
||||||
|
")"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"### Run model analysis\n",
|
||||||
|
"\n",
|
||||||
|
"The model analysis run takes a snapshot of the data in preparation for model explanation, error analysis, causal and counterfactual.\n",
|
||||||
|
"The model analysis run is the parent run for the model explanation, error analysis, causal and counterfactual runs.\n",
|
||||||
|
"In this example we will just generate an explanation and counterfactuals, but causal and error analyses may be performed as well."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"model_analysis_run = experiment.submit(model_analysis_config)\n",
|
||||||
|
"model_analysis_run.wait_for_completion(raise_on_error=True, wait_post_processing=True)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Compute explanations"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"Run model explanation based on the model analysis.\n",
|
||||||
|
"The explanation run is a child run of the model analysis run.\n",
|
||||||
|
"In the future, the `add_request` method will allow extra parameters to configure the explanation generated."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"explain_config = ExplainConfig(model_analysis_run, conda_run_config)\n",
|
||||||
|
"explain_config.add_request()\n",
|
||||||
|
"explain_run = model_analysis_run.submit_child(explain_config)\n",
|
||||||
|
"explain_run.wait_for_completion(raise_on_error=True, wait_post_processing=True)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"The `explanation_manager.list` method below returns a list of metadata dictionaries for each explain run. In this case, there is a single explain run. So, the list contains a single dictionary."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"explanations = model_analysis_run.explanation_manager.list()\n",
|
||||||
|
"explanation = explanations[0]"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"### Feature importance and visualizing explanation dashboard\n",
|
||||||
|
"In this section we describe how you can download the explanation results from the explanations experiment and visualize the feature importance for your AutoML model on the azure portal."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"feature_explanations = model_analysis_run.explanation_manager.download_by_id(explanation['id'])\n",
|
||||||
|
"print(feature_explanations.get_feature_importance_dict())\n",
|
||||||
|
"print(\"You can visualize the explanations for your features under the 'Explanations (preview)' tab in the explain run at:-\\n\" + explain_run.get_portal_url())"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Generate counterfactual examples\n",
|
||||||
|
"\n",
|
||||||
|
"Generate counterfactuals for all the samples in the `test_dataset` based on the model analysis.\n",
|
||||||
|
"The counterfactual run is a child run of the model analysis run.\n",
|
||||||
|
"In the future, the `add_request` method will allow extra parameters to configure the counterfactuals generated."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"from azureml.responsibleai.tools.model_analysis.counterfactual_config import CounterfactualConfig\n",
|
||||||
|
"\n",
|
||||||
|
"cf_config = CounterfactualConfig(model_analysis_run, conda_run_config)\n",
|
||||||
|
"cf_config.add_request(total_CFs=10, desired_range=[10, 300])\n",
|
||||||
|
"cf_run = model_analysis_run.submit_child(cf_config)\n",
|
||||||
|
"cf_run.wait_for_completion(raise_on_error=True, wait_post_processing=True)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"### Downloading counterfactual examples\n",
|
||||||
|
"The `counterfactual_manager.list` method below returns a list of metadata dictionaries for each counterfactual run. In this case, there is a single counterfactual run. So, the list contains a single dictionary.\n",
|
||||||
|
"\n",
|
||||||
|
"The `download_by_id()` method available in the `counterfactual_manager` can be used to download the counterfactual examples."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"cf_meta = model_analysis_run.counterfactual_manager.list()\n",
|
||||||
|
"counterfactual_object = model_analysis_run.counterfactual_manager.download_by_id(cf_meta[0]['id'])"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"### Visualizing the generated counterfactuals\n",
|
||||||
|
"You can use `visualize_as_dataframe()` method to view the generated counterfactual examples for the samples in `test_dataset`."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"counterfactual_object.visualize_as_dataframe(show_only_changes=True)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"### Visualize counterfactual feature importance"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"counterfactual_object.summary_importance"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"authors": [
|
||||||
|
{
|
||||||
|
"name": "jeffshep"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"categories": [
|
||||||
|
"how-to-use-azureml",
|
||||||
|
"automated-machine-learning"
|
||||||
|
],
|
||||||
|
"category": "tutorial",
|
||||||
|
"compute": [
|
||||||
|
"AML"
|
||||||
|
],
|
||||||
|
"datasets": [
|
||||||
|
"MachineData"
|
||||||
|
],
|
||||||
|
"deployment": [
|
||||||
|
"ACI"
|
||||||
|
],
|
||||||
|
"exclude_from_index": false,
|
||||||
|
"framework": [
|
||||||
|
"None"
|
||||||
|
],
|
||||||
|
"friendly_name": "Automated ML run with featurization and model explainability.",
|
||||||
|
"index_order": 5,
|
||||||
|
"kernelspec": {
|
||||||
|
"display_name": "Python 3.6",
|
||||||
|
"language": "python",
|
||||||
|
"name": "python36"
|
||||||
|
},
|
||||||
|
"language_info": {
|
||||||
|
"codemirror_mode": {
|
||||||
|
"name": "ipython",
|
||||||
|
"version": 3
|
||||||
|
},
|
||||||
|
"file_extension": ".py",
|
||||||
|
"mimetype": "text/x-python",
|
||||||
|
"name": "python",
|
||||||
|
"nbconvert_exporter": "python",
|
||||||
|
"pygments_lexer": "ipython3",
|
||||||
|
"version": "3.6.12"
|
||||||
|
},
|
||||||
|
"tags": [
|
||||||
|
"featurization",
|
||||||
|
"explainability",
|
||||||
|
"remote_run",
|
||||||
|
"AutomatedML"
|
||||||
|
],
|
||||||
|
"task": "Regression"
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 2
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
name: auto-ml-regression-responsibleai
|
||||||
|
dependencies:
|
||||||
|
- pip:
|
||||||
|
- azureml-sdk
|
||||||
|
- azureml-responsibleai
|
||||||
@@ -8,5 +8,5 @@ dependencies:
|
|||||||
- matplotlib
|
- matplotlib
|
||||||
- azureml-dataset-runtime
|
- azureml-dataset-runtime
|
||||||
- ipywidgets
|
- ipywidgets
|
||||||
- raiwidgets~=0.7.0
|
- raiwidgets~=0.13.0
|
||||||
- liac-arff
|
- liac-arff
|
||||||
|
|||||||
@@ -100,7 +100,7 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"# Check core SDK version number\n",
|
"# Check core SDK version number\n",
|
||||||
"\n",
|
"\n",
|
||||||
"print(\"This notebook was created using SDK version 1.32.0, you are currently running version\", azureml.core.VERSION)"
|
"print(\"This notebook was created using SDK version 1.36.0, you are currently running version\", azureml.core.VERSION)"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -225,9 +225,8 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"Try out these notebooks to learn more about MLflow-Azure Machine Learning integration:\n",
|
"Try out these notebooks to learn more about MLflow-Azure Machine Learning integration:\n",
|
||||||
"\n",
|
"\n",
|
||||||
" * [Train a model using remote compute on Azure Cloud](../train-on-remote/train-on-remote.ipynb)\n",
|
" * [Train a model using remote compute on Azure Cloud](../train-remote/train-remote.ipynb)\n",
|
||||||
" * [Deploy the model as a web service](../deploy-model/deploy-model.ipynb)\n",
|
" * [Train a model using Pytorch and MLflow](../../../ml-frameworks/using-mlflow/train-and-deploy-pytorch)\n",
|
||||||
" * [Train a model using Pytorch and MLflow](../../ml-frameworks/using-mlflow/train-and-deploy-pytorch)\n",
|
|
||||||
"\n"
|
"\n"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
# Copyright (c) Microsoft. All rights reserved.
|
# Copyright (c) Microsoft. All rights reserved.
|
||||||
# Licensed under the MIT license.
|
# Licensed under the MIT license.
|
||||||
|
|
||||||
|
import matplotlib.pyplot as plt
|
||||||
import numpy as np
|
import numpy as np
|
||||||
from sklearn.datasets import load_diabetes
|
from sklearn.datasets import load_diabetes
|
||||||
from sklearn.linear_model import Ridge
|
from sklearn.linear_model import Ridge
|
||||||
@@ -11,7 +12,6 @@ import mlflow.sklearn
|
|||||||
|
|
||||||
import matplotlib
|
import matplotlib
|
||||||
matplotlib.use('Agg')
|
matplotlib.use('Agg')
|
||||||
import matplotlib.pyplot as plt
|
|
||||||
|
|
||||||
with mlflow.start_run():
|
with mlflow.start_run():
|
||||||
X, y = load_diabetes(return_X_y=True)
|
X, y = load_diabetes(return_X_y=True)
|
||||||
|
|||||||
@@ -129,6 +129,7 @@
|
|||||||
"for env in envs:\n",
|
"for env in envs:\n",
|
||||||
" if env.startswith(\"AzureML\"):\n",
|
" if env.startswith(\"AzureML\"):\n",
|
||||||
" print(\"Name\",env)\n",
|
" print(\"Name\",env)\n",
|
||||||
|
" if envs[env].python.conda_dependencies is not None:\n",
|
||||||
" print(\"packages\", envs[env].python.conda_dependencies.serialize_to_string())"
|
" print(\"packages\", envs[env].python.conda_dependencies.serialize_to_string())"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -4,15 +4,15 @@ import sys
|
|||||||
|
|
||||||
def convert(imgf, labelf, outf, n):
|
def convert(imgf, labelf, outf, n):
|
||||||
f = open(imgf, "rb")
|
f = open(imgf, "rb")
|
||||||
l = open(labelf, "rb")
|
temp = open(labelf, "rb")
|
||||||
o = open(outf, "w")
|
o = open(outf, "w")
|
||||||
|
|
||||||
f.read(16)
|
f.read(16)
|
||||||
l.read(8)
|
temp.read(8)
|
||||||
images = []
|
images = []
|
||||||
|
|
||||||
for i in range(n):
|
for i in range(n):
|
||||||
image = [ord(l.read(1))]
|
image = [ord(temp.read(1))]
|
||||||
for j in range(28 * 28):
|
for j in range(28 * 28):
|
||||||
image.append(ord(f.read(1)))
|
image.append(ord(f.read(1)))
|
||||||
images.append(image)
|
images.append(image)
|
||||||
@@ -21,7 +21,7 @@ def convert(imgf, labelf, outf, n):
|
|||||||
o.write(",".join(str(pix) for pix in image) + "\n")
|
o.write(",".join(str(pix) for pix in image) + "\n")
|
||||||
f.close()
|
f.close()
|
||||||
o.close()
|
o.close()
|
||||||
l.close()
|
temp.close()
|
||||||
|
|
||||||
|
|
||||||
mounted_input_path = sys.argv[1]
|
mounted_input_path = sys.argv[1]
|
||||||
|
|||||||
@@ -272,9 +272,10 @@
|
|||||||
"dependencies:\n",
|
"dependencies:\n",
|
||||||
"- python=3.6.2\n",
|
"- python=3.6.2\n",
|
||||||
"- pip:\n",
|
"- pip:\n",
|
||||||
" - azureml-defaults\n",
|
" - azureml-core\n",
|
||||||
" - keras\n",
|
" - azureml-dataset-runtime\n",
|
||||||
" - tensorflow<=2.4.*\n",
|
" - keras==2.4.3\n",
|
||||||
|
" - tensorflow==2.4.3\n",
|
||||||
" - numpy\n",
|
" - numpy\n",
|
||||||
" - scikit-learn\n",
|
" - scikit-learn\n",
|
||||||
" - pandas\n",
|
" - pandas\n",
|
||||||
|
|||||||
@@ -0,0 +1,151 @@
|
|||||||
|
sepal_length,sepal_width,petal_length,petal_width,species
|
||||||
|
5.1,3.5,1.4,0.2,Iris-setosa
|
||||||
|
4.9,3,1.4,0.2,Iris-setosa
|
||||||
|
4.7,3.2,1.3,0.2,Iris-setosa
|
||||||
|
4.6,3.1,1.5,0.2,Iris-setosa
|
||||||
|
5,3.6,1.4,0.2,Iris-setosa
|
||||||
|
5.4,3.9,1.7,0.4,Iris-setosa
|
||||||
|
4.6,3.4,1.4,0.3,Iris-setosa
|
||||||
|
5,3.4,1.5,0.2,Iris-setosa
|
||||||
|
4.4,2.9,1.4,0.2,Iris-setosa
|
||||||
|
4.9,3.1,1.5,0.1,Iris-setosa
|
||||||
|
5.4,3.7,1.5,0.2,Iris-setosa
|
||||||
|
4.8,3.4,1.6,0.2,Iris-setosa
|
||||||
|
4.8,3,1.4,0.1,Iris-setosa
|
||||||
|
4.3,3,1.1,0.1,Iris-setosa
|
||||||
|
5.8,4,1.2,0.2,Iris-setosa
|
||||||
|
5.7,4.4,1.5,0.4,Iris-setosa
|
||||||
|
5.4,3.9,1.3,0.4,Iris-setosa
|
||||||
|
5.1,3.5,1.4,0.3,Iris-setosa
|
||||||
|
5.7,3.8,1.7,0.3,Iris-setosa
|
||||||
|
5.1,3.8,1.5,0.3,Iris-setosa
|
||||||
|
5.4,3.4,1.7,0.2,Iris-setosa
|
||||||
|
5.1,3.7,1.5,0.4,Iris-setosa
|
||||||
|
4.6,3.6,1,0.2,Iris-setosa
|
||||||
|
5.1,3.3,1.7,0.5,Iris-setosa
|
||||||
|
4.8,3.4,1.9,0.2,Iris-setosa
|
||||||
|
5,3,1.6,0.2,Iris-setosa
|
||||||
|
5,3.4,1.6,0.4,Iris-setosa
|
||||||
|
5.2,3.5,1.5,0.2,Iris-setosa
|
||||||
|
5.2,3.4,1.4,0.2,Iris-setosa
|
||||||
|
4.7,3.2,1.6,0.2,Iris-setosa
|
||||||
|
4.8,3.1,1.6,0.2,Iris-setosa
|
||||||
|
5.4,3.4,1.5,0.4,Iris-setosa
|
||||||
|
5.2,4.1,1.5,0.1,Iris-setosa
|
||||||
|
5.5,4.2,1.4,0.2,Iris-setosa
|
||||||
|
4.9,3.1,1.5,0.1,Iris-setosa
|
||||||
|
5,3.2,1.2,0.2,Iris-setosa
|
||||||
|
5.5,3.5,1.3,0.2,Iris-setosa
|
||||||
|
4.9,3.1,1.5,0.1,Iris-setosa
|
||||||
|
4.4,3,1.3,0.2,Iris-setosa
|
||||||
|
5.1,3.4,1.5,0.2,Iris-setosa
|
||||||
|
5,3.5,1.3,0.3,Iris-setosa
|
||||||
|
4.5,2.3,1.3,0.3,Iris-setosa
|
||||||
|
4.4,3.2,1.3,0.2,Iris-setosa
|
||||||
|
5,3.5,1.6,0.6,Iris-setosa
|
||||||
|
5.1,3.8,1.9,0.4,Iris-setosa
|
||||||
|
4.8,3,1.4,0.3,Iris-setosa
|
||||||
|
5.1,3.8,1.6,0.2,Iris-setosa
|
||||||
|
4.6,3.2,1.4,0.2,Iris-setosa
|
||||||
|
5.3,3.7,1.5,0.2,Iris-setosa
|
||||||
|
5,3.3,1.4,0.2,Iris-setosa
|
||||||
|
7,3.2,4.7,1.4,Iris-versicolor
|
||||||
|
6.4,3.2,4.5,1.5,Iris-versicolor
|
||||||
|
6.9,3.1,4.9,1.5,Iris-versicolor
|
||||||
|
5.5,2.3,4,1.3,Iris-versicolor
|
||||||
|
6.5,2.8,4.6,1.5,Iris-versicolor
|
||||||
|
5.7,2.8,4.5,1.3,Iris-versicolor
|
||||||
|
6.3,3.3,4.7,1.6,Iris-versicolor
|
||||||
|
4.9,2.4,3.3,1,Iris-versicolor
|
||||||
|
6.6,2.9,4.6,1.3,Iris-versicolor
|
||||||
|
5.2,2.7,3.9,1.4,Iris-versicolor
|
||||||
|
5,2,3.5,1,Iris-versicolor
|
||||||
|
5.9,3,4.2,1.5,Iris-versicolor
|
||||||
|
6,2.2,4,1,Iris-versicolor
|
||||||
|
6.1,2.9,4.7,1.4,Iris-versicolor
|
||||||
|
5.6,2.9,3.6,1.3,Iris-versicolor
|
||||||
|
6.7,3.1,4.4,1.4,Iris-versicolor
|
||||||
|
5.6,3,4.5,1.5,Iris-versicolor
|
||||||
|
5.8,2.7,4.1,1,Iris-versicolor
|
||||||
|
6.2,2.2,4.5,1.5,Iris-versicolor
|
||||||
|
5.6,2.5,3.9,1.1,Iris-versicolor
|
||||||
|
5.9,3.2,4.8,1.8,Iris-versicolor
|
||||||
|
6.1,2.8,4,1.3,Iris-versicolor
|
||||||
|
6.3,2.5,4.9,1.5,Iris-versicolor
|
||||||
|
6.1,2.8,4.7,1.2,Iris-versicolor
|
||||||
|
6.4,2.9,4.3,1.3,Iris-versicolor
|
||||||
|
6.6,3,4.4,1.4,Iris-versicolor
|
||||||
|
6.8,2.8,4.8,1.4,Iris-versicolor
|
||||||
|
6.7,3,5,1.7,Iris-versicolor
|
||||||
|
6,2.9,4.5,1.5,Iris-versicolor
|
||||||
|
5.7,2.6,3.5,1,Iris-versicolor
|
||||||
|
5.5,2.4,3.8,1.1,Iris-versicolor
|
||||||
|
5.5,2.4,3.7,1,Iris-versicolor
|
||||||
|
5.8,2.7,3.9,1.2,Iris-versicolor
|
||||||
|
6,2.7,5.1,1.6,Iris-versicolor
|
||||||
|
5.4,3,4.5,1.5,Iris-versicolor
|
||||||
|
6,3.4,4.5,1.6,Iris-versicolor
|
||||||
|
6.7,3.1,4.7,1.5,Iris-versicolor
|
||||||
|
6.3,2.3,4.4,1.3,Iris-versicolor
|
||||||
|
5.6,3,4.1,1.3,Iris-versicolor
|
||||||
|
5.5,2.5,4,1.3,Iris-versicolor
|
||||||
|
5.5,2.6,4.4,1.2,Iris-versicolor
|
||||||
|
6.1,3,4.6,1.4,Iris-versicolor
|
||||||
|
5.8,2.6,4,1.2,Iris-versicolor
|
||||||
|
5,2.3,3.3,1,Iris-versicolor
|
||||||
|
5.6,2.7,4.2,1.3,Iris-versicolor
|
||||||
|
5.7,3,4.2,1.2,Iris-versicolor
|
||||||
|
5.7,2.9,4.2,1.3,Iris-versicolor
|
||||||
|
6.2,2.9,4.3,1.3,Iris-versicolor
|
||||||
|
5.1,2.5,3,1.1,Iris-versicolor
|
||||||
|
5.7,2.8,4.1,1.3,Iris-versicolor
|
||||||
|
6.3,3.3,6,2.5,Iris-virginica
|
||||||
|
5.8,2.7,5.1,1.9,Iris-virginica
|
||||||
|
7.1,3,5.9,2.1,Iris-virginica
|
||||||
|
6.3,2.9,5.6,1.8,Iris-virginica
|
||||||
|
6.5,3,5.8,2.2,Iris-virginica
|
||||||
|
7.6,3,6.6,2.1,Iris-virginica
|
||||||
|
4.9,2.5,4.5,1.7,Iris-virginica
|
||||||
|
7.3,2.9,6.3,1.8,Iris-virginica
|
||||||
|
6.7,2.5,5.8,1.8,Iris-virginica
|
||||||
|
7.2,3.6,6.1,2.5,Iris-virginica
|
||||||
|
6.5,3.2,5.1,2,Iris-virginica
|
||||||
|
6.4,2.7,5.3,1.9,Iris-virginica
|
||||||
|
6.8,3,5.5,2.1,Iris-virginica
|
||||||
|
5.7,2.5,5,2,Iris-virginica
|
||||||
|
5.8,2.8,5.1,2.4,Iris-virginica
|
||||||
|
6.4,3.2,5.3,2.3,Iris-virginica
|
||||||
|
6.5,3,5.5,1.8,Iris-virginica
|
||||||
|
7.7,3.8,6.7,2.2,Iris-virginica
|
||||||
|
7.7,2.6,6.9,2.3,Iris-virginica
|
||||||
|
6,2.2,5,1.5,Iris-virginica
|
||||||
|
6.9,3.2,5.7,2.3,Iris-virginica
|
||||||
|
5.6,2.8,4.9,2,Iris-virginica
|
||||||
|
7.7,2.8,6.7,2,Iris-virginica
|
||||||
|
6.3,2.7,4.9,1.8,Iris-virginica
|
||||||
|
6.7,3.3,5.7,2.1,Iris-virginica
|
||||||
|
7.2,3.2,6,1.8,Iris-virginica
|
||||||
|
6.2,2.8,4.8,1.8,Iris-virginica
|
||||||
|
6.1,3,4.9,1.8,Iris-virginica
|
||||||
|
6.4,2.8,5.6,2.1,Iris-virginica
|
||||||
|
7.2,3,5.8,1.6,Iris-virginica
|
||||||
|
7.4,2.8,6.1,1.9,Iris-virginica
|
||||||
|
7.9,3.8,6.4,2,Iris-virginica
|
||||||
|
6.4,2.8,5.6,2.2,Iris-virginica
|
||||||
|
6.3,2.8,5.1,1.5,Iris-virginica
|
||||||
|
6.1,2.6,5.6,1.4,Iris-virginica
|
||||||
|
7.7,3,6.1,2.3,Iris-virginica
|
||||||
|
6.3,3.4,5.6,2.4,Iris-virginica
|
||||||
|
6.4,3.1,5.5,1.8,Iris-virginica
|
||||||
|
6,3,4.8,1.8,Iris-virginica
|
||||||
|
6.9,3.1,5.4,2.1,Iris-virginica
|
||||||
|
6.7,3.1,5.6,2.4,Iris-virginica
|
||||||
|
6.9,3.1,5.1,2.3,Iris-virginica
|
||||||
|
5.8,2.7,5.1,1.9,Iris-virginica
|
||||||
|
6.8,3.2,5.9,2.3,Iris-virginica
|
||||||
|
6.7,3.3,5.7,2.5,Iris-virginica
|
||||||
|
6.7,3,5.2,2.3,Iris-virginica
|
||||||
|
6.3,2.5,5,1.9,Iris-virginica
|
||||||
|
6.5,3,5.2,2,Iris-virginica
|
||||||
|
6.2,3.4,5.4,2.3,Iris-virginica
|
||||||
|
5.9,3,5.1,1.8,Iris-virginica
|
||||||
|
6
index.md
6
index.md
@@ -27,6 +27,7 @@ Machine Learning notebook samples and encourage efficient retrieval of topics an
|
|||||||
| [Classification of credit card fraudulent transactions using Automated ML](https://github.com/Azure/MachineLearningNotebooks/blob/master//how-to-use-azureml/automated-machine-learning/classification-credit-card-fraud/auto-ml-classification-credit-card-fraud.ipynb) | Classification | Creditcard | AML Compute | None | None | remote_run, AutomatedML |
|
| [Classification of credit card fraudulent transactions using Automated ML](https://github.com/Azure/MachineLearningNotebooks/blob/master//how-to-use-azureml/automated-machine-learning/classification-credit-card-fraud/auto-ml-classification-credit-card-fraud.ipynb) | Classification | Creditcard | AML Compute | None | None | remote_run, AutomatedML |
|
||||||
| [Classification of credit card fraudulent transactions using Automated ML](https://github.com/Azure/MachineLearningNotebooks/blob/master//how-to-use-azureml/automated-machine-learning/experimental/classification-credit-card-fraud-local-managed/auto-ml-classification-credit-card-fraud-local-managed.ipynb) | Classification | Creditcard | AML Compute | None | None | AutomatedML |
|
| [Classification of credit card fraudulent transactions using Automated ML](https://github.com/Azure/MachineLearningNotebooks/blob/master//how-to-use-azureml/automated-machine-learning/experimental/classification-credit-card-fraud-local-managed/auto-ml-classification-credit-card-fraud-local-managed.ipynb) | Classification | Creditcard | AML Compute | None | None | AutomatedML |
|
||||||
| [Automated ML run with featurization and model explainability.](https://github.com/Azure/MachineLearningNotebooks/blob/master//how-to-use-azureml/automated-machine-learning/regression-explanation-featurization/auto-ml-regression-explanation-featurization.ipynb) | Regression | MachineData | AML | ACI | None | featurization, explainability, remote_run, AutomatedML |
|
| [Automated ML run with featurization and model explainability.](https://github.com/Azure/MachineLearningNotebooks/blob/master//how-to-use-azureml/automated-machine-learning/regression-explanation-featurization/auto-ml-regression-explanation-featurization.ipynb) | Regression | MachineData | AML | ACI | None | featurization, explainability, remote_run, AutomatedML |
|
||||||
|
| [Automated ML run with featurization and model explainability.](https://github.com/Azure/MachineLearningNotebooks/blob/master//how-to-use-azureml/responsible-ai/auto-ml-regression-responsibleai/auto-ml-regression-responsibleai.ipynb) | Regression | MachineData | AML | ACI | None | featurization, explainability, remote_run, AutomatedML |
|
||||||
| :star:[Azure Machine Learning Pipeline with DataTranferStep](https://github.com/Azure/MachineLearningNotebooks/blob/master//how-to-use-azureml/machine-learning-pipelines/intro-to-pipelines/aml-pipelines-data-transfer.ipynb) | Demonstrates the use of DataTranferStep | Custom | ADF | None | Azure ML | None |
|
| :star:[Azure Machine Learning Pipeline with DataTranferStep](https://github.com/Azure/MachineLearningNotebooks/blob/master//how-to-use-azureml/machine-learning-pipelines/intro-to-pipelines/aml-pipelines-data-transfer.ipynb) | Demonstrates the use of DataTranferStep | Custom | ADF | None | Azure ML | None |
|
||||||
| [Getting Started with Azure Machine Learning Pipelines](https://github.com/Azure/MachineLearningNotebooks/blob/master//how-to-use-azureml/machine-learning-pipelines/intro-to-pipelines/aml-pipelines-getting-started.ipynb) | Getting Started notebook for ANML Pipelines | Custom | AML Compute | None | Azure ML | None |
|
| [Getting Started with Azure Machine Learning Pipelines](https://github.com/Azure/MachineLearningNotebooks/blob/master//how-to-use-azureml/machine-learning-pipelines/intro-to-pipelines/aml-pipelines-getting-started.ipynb) | Getting Started notebook for ANML Pipelines | Custom | AML Compute | None | Azure ML | None |
|
||||||
| [Azure Machine Learning Pipeline with AzureBatchStep](https://github.com/Azure/MachineLearningNotebooks/blob/master//how-to-use-azureml/machine-learning-pipelines/intro-to-pipelines/aml-pipelines-how-to-use-azurebatch-to-run-a-windows-executable.ipynb) | Demonstrates the use of AzureBatchStep | Custom | Azure Batch | None | Azure ML | None |
|
| [Azure Machine Learning Pipeline with AzureBatchStep](https://github.com/Azure/MachineLearningNotebooks/blob/master//how-to-use-azureml/machine-learning-pipelines/intro-to-pipelines/aml-pipelines-how-to-use-azurebatch-to-run-a-windows-executable.ipynb) | Demonstrates the use of AzureBatchStep | Custom | Azure Batch | None | Azure ML | None |
|
||||||
@@ -107,6 +108,10 @@ Machine Learning notebook samples and encourage efficient retrieval of topics an
|
|||||||
| [auto-ml-regression-model-proxy](https://github.com/Azure/MachineLearningNotebooks/blob/master//how-to-use-azureml/automated-machine-learning/experimental/regression-model-proxy/auto-ml-regression-model-proxy.ipynb) | | | | | | |
|
| [auto-ml-regression-model-proxy](https://github.com/Azure/MachineLearningNotebooks/blob/master//how-to-use-azureml/automated-machine-learning/experimental/regression-model-proxy/auto-ml-regression-model-proxy.ipynb) | | | | | | |
|
||||||
| [auto-ml-forecasting-beer-remote](https://github.com/Azure/MachineLearningNotebooks/blob/master//how-to-use-azureml/automated-machine-learning/forecasting-beer-remote/auto-ml-forecasting-beer-remote.ipynb) | | | | | | |
|
| [auto-ml-forecasting-beer-remote](https://github.com/Azure/MachineLearningNotebooks/blob/master//how-to-use-azureml/automated-machine-learning/forecasting-beer-remote/auto-ml-forecasting-beer-remote.ipynb) | | | | | | |
|
||||||
| [auto-ml-forecasting-energy-demand](https://github.com/Azure/MachineLearningNotebooks/blob/master//how-to-use-azureml/automated-machine-learning/forecasting-energy-demand/auto-ml-forecasting-energy-demand.ipynb) | | | | | | |
|
| [auto-ml-forecasting-energy-demand](https://github.com/Azure/MachineLearningNotebooks/blob/master//how-to-use-azureml/automated-machine-learning/forecasting-energy-demand/auto-ml-forecasting-energy-demand.ipynb) | | | | | | |
|
||||||
|
| [auto-ml-forecasting-hierarchical-timeseries](https://github.com/Azure/MachineLearningNotebooks/blob/master//how-to-use-azureml/automated-machine-learning/forecasting-hierarchical-timeseries/auto-ml-forecasting-hierarchical-timeseries.ipynb) | | | | | | |
|
||||||
|
| [auto-ml-forecasting-many-models](https://github.com/Azure/MachineLearningNotebooks/blob/master//how-to-use-azureml/automated-machine-learning/forecasting-many-models/auto-ml-forecasting-many-models.ipynb) | | | | | | |
|
||||||
|
| [auto-ml-forecasting-univariate-recipe-experiment-settings](https://github.com/Azure/MachineLearningNotebooks/blob/master//how-to-use-azureml/automated-machine-learning/forecasting-recipes-univariate/auto-ml-forecasting-univariate-recipe-experiment-settings.ipynb) | | | | | | |
|
||||||
|
| [auto-ml-forecasting-univariate-recipe-run-experiment](https://github.com/Azure/MachineLearningNotebooks/blob/master//how-to-use-azureml/automated-machine-learning/forecasting-recipes-univariate/auto-ml-forecasting-univariate-recipe-run-experiment.ipynb) | | | | | | |
|
||||||
| [auto-ml-regression](https://github.com/Azure/MachineLearningNotebooks/blob/master//how-to-use-azureml/automated-machine-learning/regression/auto-ml-regression.ipynb) | | | | | | |
|
| [auto-ml-regression](https://github.com/Azure/MachineLearningNotebooks/blob/master//how-to-use-azureml/automated-machine-learning/regression/auto-ml-regression.ipynb) | | | | | | |
|
||||||
| [automl-databricks-local-01](https://github.com/Azure/MachineLearningNotebooks/blob/master//how-to-use-azureml/azure-databricks/automl/automl-databricks-local-01.ipynb) | | | | | | |
|
| [automl-databricks-local-01](https://github.com/Azure/MachineLearningNotebooks/blob/master//how-to-use-azureml/azure-databricks/automl/automl-databricks-local-01.ipynb) | | | | | | |
|
||||||
| [automl-databricks-local-with-deployment](https://github.com/Azure/MachineLearningNotebooks/blob/master//how-to-use-azureml/azure-databricks/automl/automl-databricks-local-with-deployment.ipynb) | | | | | | |
|
| [automl-databricks-local-with-deployment](https://github.com/Azure/MachineLearningNotebooks/blob/master//how-to-use-azureml/azure-databricks/automl/automl-databricks-local-with-deployment.ipynb) | | | | | | |
|
||||||
@@ -121,6 +126,7 @@ Machine Learning notebook samples and encourage efficient retrieval of topics an
|
|||||||
| [production-deploy-to-aks-ssl](https://github.com/Azure/MachineLearningNotebooks/blob/master//how-to-use-azureml/deployment/production-deploy-to-aks/production-deploy-to-aks-ssl.ipynb) | | | | | | |
|
| [production-deploy-to-aks-ssl](https://github.com/Azure/MachineLearningNotebooks/blob/master//how-to-use-azureml/deployment/production-deploy-to-aks/production-deploy-to-aks-ssl.ipynb) | | | | | | |
|
||||||
| [production-deploy-to-aks](https://github.com/Azure/MachineLearningNotebooks/blob/master//how-to-use-azureml/deployment/production-deploy-to-aks/production-deploy-to-aks.ipynb) | | | | | | |
|
| [production-deploy-to-aks](https://github.com/Azure/MachineLearningNotebooks/blob/master//how-to-use-azureml/deployment/production-deploy-to-aks/production-deploy-to-aks.ipynb) | | | | | | |
|
||||||
| [production-deploy-to-aks-gpu](https://github.com/Azure/MachineLearningNotebooks/blob/master//how-to-use-azureml/deployment/production-deploy-to-aks-gpu/production-deploy-to-aks-gpu.ipynb) | | | | | | |
|
| [production-deploy-to-aks-gpu](https://github.com/Azure/MachineLearningNotebooks/blob/master//how-to-use-azureml/deployment/production-deploy-to-aks-gpu/production-deploy-to-aks-gpu.ipynb) | | | | | | |
|
||||||
|
| [train-explain-model-gpu-tree-explainer](https://github.com/Azure/MachineLearningNotebooks/blob/master//how-to-use-azureml/explain-model/azure-integration/gpu-explanation/train-explain-model-gpu-tree-explainer.ipynb) | | | | | | |
|
||||||
| [explain-model-on-amlcompute](https://github.com/Azure/MachineLearningNotebooks/blob/master//how-to-use-azureml/explain-model/azure-integration/remote-explanation/explain-model-on-amlcompute.ipynb) | | | | | | |
|
| [explain-model-on-amlcompute](https://github.com/Azure/MachineLearningNotebooks/blob/master//how-to-use-azureml/explain-model/azure-integration/remote-explanation/explain-model-on-amlcompute.ipynb) | | | | | | |
|
||||||
| [save-retrieve-explanations-run-history](https://github.com/Azure/MachineLearningNotebooks/blob/master//how-to-use-azureml/explain-model/azure-integration/run-history/save-retrieve-explanations-run-history.ipynb) | | | | | | |
|
| [save-retrieve-explanations-run-history](https://github.com/Azure/MachineLearningNotebooks/blob/master//how-to-use-azureml/explain-model/azure-integration/run-history/save-retrieve-explanations-run-history.ipynb) | | | | | | |
|
||||||
| [train-explain-model-locally-and-deploy](https://github.com/Azure/MachineLearningNotebooks/blob/master//how-to-use-azureml/explain-model/azure-integration/scoring-time/train-explain-model-locally-and-deploy.ipynb) | | | | | | |
|
| [train-explain-model-locally-and-deploy](https://github.com/Azure/MachineLearningNotebooks/blob/master//how-to-use-azureml/explain-model/azure-integration/scoring-time/train-explain-model-locally-and-deploy.ipynb) | | | | | | |
|
||||||
|
|||||||
@@ -102,7 +102,7 @@
|
|||||||
"source": [
|
"source": [
|
||||||
"import azureml.core\n",
|
"import azureml.core\n",
|
||||||
"\n",
|
"\n",
|
||||||
"print(\"This notebook was created using version 1.32.0 of the Azure ML SDK\")\n",
|
"print(\"This notebook was created using version 1.36.0 of the Azure ML SDK\")\n",
|
||||||
"print(\"You are currently using version\", azureml.core.VERSION, \"of the Azure ML SDK\")"
|
"print(\"You are currently using version\", azureml.core.VERSION, \"of the Azure ML SDK\")"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
@@ -213,10 +213,7 @@
|
|||||||
"* You do not have permission to create a resource group if it's non-existing.\n",
|
"* You do not have permission to create a resource group if it's non-existing.\n",
|
||||||
"* You are not a subscription owner or contributor and no Azure ML workspaces have ever been created in this subscription\n",
|
"* You are not a subscription owner or contributor and no Azure ML workspaces have ever been created in this subscription\n",
|
||||||
"\n",
|
"\n",
|
||||||
"If workspace creation fails, please work with your IT admin to provide you with the appropriate permissions or to provision the required resources.\n",
|
"If workspace creation fails, please work with your IT admin to provide you with the appropriate permissions or to provision the required resources.\n"
|
||||||
"\n",
|
|
||||||
"**Note**: A Basic workspace is created by default. If you would like to create an Enterprise workspace, please specify sku = 'enterprise'.\n",
|
|
||||||
"Please visit our [pricing page](https://azure.microsoft.com/en-us/pricing/details/machine-learning/) for more details on our Enterprise edition.\n"
|
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -237,7 +234,6 @@
|
|||||||
" resource_group = resource_group, \n",
|
" resource_group = resource_group, \n",
|
||||||
" location = workspace_region,\n",
|
" location = workspace_region,\n",
|
||||||
" create_resource_group = True,\n",
|
" create_resource_group = True,\n",
|
||||||
" sku = 'basic',\n",
|
|
||||||
" exist_ok = True)\n",
|
" exist_ok = True)\n",
|
||||||
"ws.get_details()\n",
|
"ws.get_details()\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
|||||||
@@ -25,8 +25,8 @@ def get_class_label_dict(labels_dir):
|
|||||||
label = []
|
label = []
|
||||||
labels_path = os.path.join(labels_dir, 'labels.txt')
|
labels_path = os.path.join(labels_dir, 'labels.txt')
|
||||||
proto_as_ascii_lines = tf.gfile.GFile(labels_path).readlines()
|
proto_as_ascii_lines = tf.gfile.GFile(labels_path).readlines()
|
||||||
for l in proto_as_ascii_lines:
|
for temp in proto_as_ascii_lines:
|
||||||
label.append(l.rstrip())
|
label.append(temp.rstrip())
|
||||||
return label
|
return label
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -60,13 +60,6 @@
|
|||||||
"## Download and prepare data"
|
"## Download and prepare data"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
|
||||||
"cell_type": "markdown",
|
|
||||||
"metadata": {},
|
|
||||||
"source": [
|
|
||||||
"Import the necessary packages. The Open Datasets package contains a class representing each data source (`NycTlcGreen` for example) to easily filter date parameters before downloading."
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": null,
|
||||||
@@ -101,9 +94,7 @@
|
|||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"Now that the initial data is loaded, define a function to create various time-based features from the pickup datetime field. This will create new fields for the month number, day of month, day of week, and hour of day, and will allow the model to factor in time-based seasonality. \n",
|
"Remove some of the columns that you won't need for training or additional feature building. Automate machine learning will automatically handle time-based features such as lpepPickupDatetime."
|
||||||
"\n",
|
|
||||||
"Use the `apply()` function on the dataframe to iteratively apply the `build_time_features()` function to each row in the taxi data."
|
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -112,33 +103,7 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"def build_time_features(vector):\n",
|
"columns_to_remove = [\"lpepDropoffDatetime\", \"puLocationId\", \"doLocationId\", \"extra\", \"mtaTax\",\n",
|
||||||
" pickup_datetime = vector[0]\n",
|
|
||||||
" month_num = pickup_datetime.month\n",
|
|
||||||
" day_of_month = pickup_datetime.day\n",
|
|
||||||
" day_of_week = pickup_datetime.weekday()\n",
|
|
||||||
" hour_of_day = pickup_datetime.hour\n",
|
|
||||||
" \n",
|
|
||||||
" return pd.Series((month_num, day_of_month, day_of_week, hour_of_day))\n",
|
|
||||||
"\n",
|
|
||||||
"green_taxi_df[[\"month_num\", \"day_of_month\",\"day_of_week\", \"hour_of_day\"]] = green_taxi_df[[\"lpepPickupDatetime\"]].apply(build_time_features, axis=1)\n",
|
|
||||||
"green_taxi_df.head(10)"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "markdown",
|
|
||||||
"metadata": {},
|
|
||||||
"source": [
|
|
||||||
"Remove some of the columns that you won't need for training or additional feature building."
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "code",
|
|
||||||
"execution_count": null,
|
|
||||||
"metadata": {},
|
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
|
||||||
"columns_to_remove = [\"lpepPickupDatetime\", \"lpepDropoffDatetime\", \"puLocationId\", \"doLocationId\", \"extra\", \"mtaTax\",\n",
|
|
||||||
" \"improvementSurcharge\", \"tollsAmount\", \"ehailFee\", \"tripType\", \"rateCodeID\", \n",
|
" \"improvementSurcharge\", \"tollsAmount\", \"ehailFee\", \"tripType\", \"rateCodeID\", \n",
|
||||||
" \"storeAndFwdFlag\", \"paymentType\", \"fareAmount\", \"tipAmount\"\n",
|
" \"storeAndFwdFlag\", \"paymentType\", \"fareAmount\", \"tipAmount\"\n",
|
||||||
" ]\n",
|
" ]\n",
|
||||||
|
|||||||
@@ -2,4 +2,3 @@ name: regression-automated-ml
|
|||||||
dependencies:
|
dependencies:
|
||||||
- pip:
|
- pip:
|
||||||
- azureml-sdk
|
- azureml-sdk
|
||||||
- azureml-opendatasets
|
|
||||||
|
|||||||
Reference in New Issue
Block a user