mirror of
https://github.com/Azure/MachineLearningNotebooks.git
synced 2025-12-20 09:37:04 -05:00
Compare commits
4 Commits
update-spa
...
release_up
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7d6a0a2051 | ||
|
|
6cc8af80a2 | ||
|
|
f61898f718 | ||
|
|
5cb465171e |
@@ -103,7 +103,7 @@
|
||||
"source": [
|
||||
"import azureml.core\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.33.0 of the Azure ML SDK\")\n",
|
||||
"print(\"You are currently using version\", azureml.core.VERSION, \"of the Azure ML SDK\")"
|
||||
]
|
||||
},
|
||||
|
||||
@@ -21,8 +21,9 @@ dependencies:
|
||||
|
||||
- pip:
|
||||
# Required packages for AzureML execution, history, and data preparation.
|
||||
- azureml-widgets~=1.32.0
|
||||
- azureml-widgets~=1.33.0
|
||||
- pytorch-transformers==1.0.0
|
||||
- spacy==2.1.8
|
||||
- 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://automlresources-prod.azureedge.net/validated-requirements/1.33.0/validated_win32_requirements.txt [--no-deps]
|
||||
- arch==4.14
|
||||
|
||||
@@ -21,8 +21,9 @@ dependencies:
|
||||
|
||||
- pip:
|
||||
# Required packages for AzureML execution, history, and data preparation.
|
||||
- azureml-widgets~=1.32.0
|
||||
- azureml-widgets~=1.33.0
|
||||
- pytorch-transformers==1.0.0
|
||||
- spacy==2.1.8
|
||||
- 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://automlresources-prod.azureedge.net/validated-requirements/1.33.0/validated_linux_requirements.txt [--no-deps]
|
||||
- arch==4.14
|
||||
|
||||
@@ -22,8 +22,9 @@ dependencies:
|
||||
|
||||
- pip:
|
||||
# Required packages for AzureML execution, history, and data preparation.
|
||||
- azureml-widgets~=1.32.0
|
||||
- azureml-widgets~=1.33.0
|
||||
- pytorch-transformers==1.0.0
|
||||
- spacy==2.1.8
|
||||
- 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://automlresources-prod.azureedge.net/validated-requirements/1.33.0/validated_darwin_requirements.txt [--no-deps]
|
||||
- arch==4.14
|
||||
|
||||
@@ -86,7 +86,6 @@
|
||||
"import azureml.core\n",
|
||||
"from azureml.core.experiment import Experiment\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.train.automl import AutoMLConfig\n",
|
||||
"from azureml.interpret import ExplanationClient"
|
||||
@@ -105,7 +104,7 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"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.33.0 of the Azure ML SDK\")\n",
|
||||
"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.train.automl import constants\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",
|
||||
"\n",
|
||||
"def get_onnx_res(run):\n",
|
||||
" res_path = 'onnx_resource.json'\n",
|
||||
" run.download_file(name=constants.MODEL_RESOURCE_PATH_ONNX, output_file_path=res_path)\n",
|
||||
" with open(res_path) as f:\n",
|
||||
" onnx_res = json.load(f)\n",
|
||||
" return onnx_res\n",
|
||||
" result = json.load(f)\n",
|
||||
" return result\n",
|
||||
"\n",
|
||||
"if python_version_compatible:\n",
|
||||
"if sys.version_info < OnnxConvertConstants.OnnxIncompatiblePythonVersion:\n",
|
||||
" test_df = test_dataset.to_pandas_dataframe()\n",
|
||||
" mdl_bytes = onnx_mdl.SerializeToString()\n",
|
||||
" onnx_res = get_onnx_res(best_run)\n",
|
||||
" onnx_result = get_onnx_res(best_run)\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",
|
||||
"\n",
|
||||
" print(pred_onnx)\n",
|
||||
@@ -708,14 +701,12 @@
|
||||
"source": [
|
||||
"from azureml.core.model import InferenceConfig\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.environment import Environment\n",
|
||||
"\n",
|
||||
"inference_config = InferenceConfig(entry_script=script_file_name)\n",
|
||||
"\n",
|
||||
"aciconfig = AciWebservice.deploy_configuration(cpu_cores = 1, \n",
|
||||
" memory_gb = 1, \n",
|
||||
"aciconfig = AciWebservice.deploy_configuration(cpu_cores = 2, \n",
|
||||
" memory_gb = 2, \n",
|
||||
" tags = {'area': \"bmData\", 'type': \"automl_classification\"}, \n",
|
||||
" description = 'sample service for Automl Classification')\n",
|
||||
"\n",
|
||||
@@ -792,7 +783,6 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import json\n",
|
||||
"import requests\n",
|
||||
"\n",
|
||||
"X_test_json = X_test.to_json(orient='records')\n",
|
||||
@@ -832,7 +822,6 @@
|
||||
"source": [
|
||||
"%matplotlib notebook\n",
|
||||
"from sklearn.metrics import confusion_matrix\n",
|
||||
"import numpy as np\n",
|
||||
"import itertools\n",
|
||||
"\n",
|
||||
"cf =confusion_matrix(actual,y_pred)\n",
|
||||
|
||||
@@ -93,7 +93,7 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"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.33.0 of the Azure ML SDK\")\n",
|
||||
"print(\"You are currently using version\", azureml.core.VERSION, \"of the Azure ML SDK\")"
|
||||
]
|
||||
},
|
||||
|
||||
@@ -96,7 +96,7 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"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.33.0 of the Azure ML SDK\")\n",
|
||||
"print(\"You are currently using version\", azureml.core.VERSION, \"of the Azure ML SDK\")"
|
||||
]
|
||||
},
|
||||
|
||||
@@ -81,7 +81,7 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"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.33.0 of the Azure ML SDK\")\n",
|
||||
"print(\"You are currently using version\", azureml.core.VERSION, \"of the Azure ML SDK\")"
|
||||
]
|
||||
},
|
||||
|
||||
@@ -92,7 +92,7 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"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.33.0 of the Azure ML SDK\")\n",
|
||||
"print(\"You are currently using version\", azureml.core.VERSION, \"of the Azure ML SDK\")"
|
||||
]
|
||||
},
|
||||
|
||||
@@ -91,7 +91,7 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"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.33.0 of the Azure ML SDK\")\n",
|
||||
"print(\"You are currently using version\", azureml.core.VERSION, \"of the Azure ML SDK\")"
|
||||
]
|
||||
},
|
||||
|
||||
@@ -113,7 +113,7 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"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.33.0 of the Azure ML SDK\")\n",
|
||||
"print(\"You are currently using version\", azureml.core.VERSION, \"of the Azure ML SDK\")"
|
||||
]
|
||||
},
|
||||
@@ -372,7 +372,8 @@
|
||||
" freq='MS' # Set the forecast frequency to be monthly (start of the month)\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",
|
||||
"automl_config = AutoMLConfig(task='forecasting',\n",
|
||||
" primary_metric='normalized_root_mean_squared_error',\n",
|
||||
" experiment_timeout_hours = 1,\n",
|
||||
" training_data=train_dataset,\n",
|
||||
@@ -383,6 +384,7 @@
|
||||
" max_concurrent_iterations=4,\n",
|
||||
" max_cores_per_iteration=-1,\n",
|
||||
" enable_dnn=True,\n",
|
||||
" enable_early_stopping=False,\n",
|
||||
" forecasting_parameters=forecasting_parameters)"
|
||||
]
|
||||
},
|
||||
|
||||
@@ -87,7 +87,7 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"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.33.0 of the Azure ML SDK\")\n",
|
||||
"print(\"You are currently using version\", azureml.core.VERSION, \"of the Azure ML SDK\")"
|
||||
]
|
||||
},
|
||||
|
||||
@@ -99,7 +99,7 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"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.33.0 of the Azure ML SDK\")\n",
|
||||
"print(\"You are currently using version\", azureml.core.VERSION, \"of the Azure ML SDK\")"
|
||||
]
|
||||
},
|
||||
|
||||
@@ -94,7 +94,7 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"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.33.0 of the Azure ML SDK\")\n",
|
||||
"print(\"You are currently using version\", azureml.core.VERSION, \"of the Azure ML SDK\")"
|
||||
]
|
||||
},
|
||||
|
||||
@@ -60,7 +60,6 @@
|
||||
"source": [
|
||||
"import azureml.core\n",
|
||||
"import pandas as pd\n",
|
||||
"import numpy as np\n",
|
||||
"import logging\n",
|
||||
"\n",
|
||||
"from azureml.core.workspace import Workspace\n",
|
||||
@@ -82,7 +81,7 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"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.33.0 of the Azure ML SDK\")\n",
|
||||
"print(\"You are currently using version\", azureml.core.VERSION, \"of the Azure ML SDK\")"
|
||||
]
|
||||
},
|
||||
@@ -688,8 +687,8 @@
|
||||
"inference_config = InferenceConfig(environment = best_run.get_environment(), \n",
|
||||
" entry_script = script_file_name)\n",
|
||||
"\n",
|
||||
"aciconfig = AciWebservice.deploy_configuration(cpu_cores = 1, \n",
|
||||
" memory_gb = 2, \n",
|
||||
"aciconfig = AciWebservice.deploy_configuration(cpu_cores = 2, \n",
|
||||
" memory_gb = 4, \n",
|
||||
" tags = {'type': \"automl-forecasting\"},\n",
|
||||
" description = \"Automl forecasting sample service\")\n",
|
||||
"\n",
|
||||
|
||||
@@ -0,0 +1,492 @@
|
||||
{
|
||||
"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",
|
||||
"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",
|
||||
"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,560 @@
|
||||
{
|
||||
"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(vm_size = \"STANDARD_D2_V2\",\n",
|
||||
" max_nodes = 6)\n",
|
||||
"\n",
|
||||
" # Create the cluster.\\n\",\n",
|
||||
" compute_target = ComputeTarget.create(ws, amlcompute_cluster_name, provisioning_config)\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(show_output = True, min_node_count = None, timeout_in_minutes = 20)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"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 = '2020-03-01' # start of the covid period. To be excluded from evaluation.\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 = '-'.join(map(str, TARGET_LAGS)) if (len(TARGET_LAGS) > 0) else None\n",
|
||||
"else:\n",
|
||||
" TARGET_LAGS_STR = TARGET_LAGS\n",
|
||||
"\n",
|
||||
"experiment_desc = 'diff-{}_lags-{}_STL-{}'.format(DIFFERENCE_SERIES, TARGET_LAGS_STR, STL_TYPE)\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(df_input=df_delta,\n",
|
||||
" n=FORECAST_HORIZON,\n",
|
||||
" time_colname=TIME_COLNAME,\n",
|
||||
" ts_id_colnames=TIME_SERIES_ID_COLNAMES)\n",
|
||||
"else:\n",
|
||||
" df_train, df_test = ts_train_test_split(df_input=df,\n",
|
||||
" n=FORECAST_HORIZON,\n",
|
||||
" time_colname=TIME_COLNAME,\n",
|
||||
" ts_id_colnames=TIME_SERIES_ID_COLNAMES)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"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(files = ['./train.csv'], target_path = 'uni-recipe-dataset/tabular/', overwrite = True,show_progress = True)\n",
|
||||
"datastore.upload_files(files = ['./test.csv'], target_path = 'uni-recipe-dataset/tabular/', overwrite = True,show_progress = True)\n",
|
||||
"\n",
|
||||
"from azureml.core import Dataset\n",
|
||||
"train_dataset = Dataset.Tabular.from_delimited_files(path = [(datastore, 'uni-recipe-dataset/tabular/train.csv')])\n",
|
||||
"test_dataset = Dataset.Tabular.from_delimited_files(path = [(datastore, 'uni-recipe-dataset/tabular/test.csv')])\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(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)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"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",
|
||||
"remote_run = run_remote_inference(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",
|
||||
"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(fcst,\n",
|
||||
" df_orig[[TIME_COLNAME, TARGET_COLNAME]], \n",
|
||||
" on=[TIME_COLNAME], how='inner')\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,\n",
|
||||
" metric_name=None,\n",
|
||||
" 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,46 @@
|
||||
"""
|
||||
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_df = 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')
|
||||
y_pred, X_trans = fitted_model.forecast(X_test_df)
|
||||
|
||||
# rename target column
|
||||
X_trans.reset_index(drop=False, inplace=True)
|
||||
X_trans.rename(columns={TimeSeriesInternal.DUMMY_TARGET_COLUMN: 'predicted'}, inplace=True)
|
||||
X_trans['actual'] = y_test_df[target_column_name].values
|
||||
|
||||
file_name = 'outputs/predictions.csv'
|
||||
export_csv = X_trans.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,250 @@
|
||||
"""
|
||||
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,38 @@
|
||||
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": {},
|
||||
"outputs": [],
|
||||
"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.33.0 of the Azure ML SDK\")\n",
|
||||
"print(\"You are currently using version\", azureml.core.VERSION, \"of the Azure ML SDK\")"
|
||||
]
|
||||
},
|
||||
|
||||
@@ -77,7 +77,6 @@
|
||||
"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"
|
||||
@@ -96,7 +95,7 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"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.33.0 of the Azure ML SDK\")\n",
|
||||
"print(\"You are currently using version\", azureml.core.VERSION, \"of the Azure ML SDK\")"
|
||||
]
|
||||
},
|
||||
@@ -541,8 +540,6 @@
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from azureml.core.runconfig import RunConfiguration\n",
|
||||
"from azureml.core.conda_dependencies import CondaDependencies\n",
|
||||
"import pkg_resources\n",
|
||||
"\n",
|
||||
"# create a new RunConfig object\n",
|
||||
"conda_run_config = RunConfiguration(framework=\"python\")\n",
|
||||
@@ -720,14 +717,13 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from azureml.core.webservice import Webservice\n",
|
||||
"from azureml.core.model import InferenceConfig\n",
|
||||
"from azureml.core.webservice import AciWebservice\n",
|
||||
"from azureml.core.model import Model\n",
|
||||
"from azureml.core.environment import Environment\n",
|
||||
"\n",
|
||||
"aciconfig = AciWebservice.deploy_configuration(cpu_cores=1, \n",
|
||||
" memory_gb=1, \n",
|
||||
"aciconfig = AciWebservice.deploy_configuration(cpu_cores=2, \n",
|
||||
" memory_gb=2, \n",
|
||||
" tags={\"data\": \"Machine Data\", \n",
|
||||
" \"method\" : \"local_explanation\"}, \n",
|
||||
" description='Get local explanations for Machine test data')\n",
|
||||
|
||||
@@ -92,7 +92,7 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"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.33.0 of the Azure ML SDK\")\n",
|
||||
"print(\"You are currently using version\", azureml.core.VERSION, \"of the Azure ML SDK\")"
|
||||
]
|
||||
},
|
||||
|
||||
@@ -2,22 +2,23 @@
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Copyright (c) Microsoft Corporation. All rights reserved.\n",
|
||||
"\n",
|
||||
"Licensed under the MIT License."
|
||||
],
|
||||
"metadata": {}
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
""
|
||||
],
|
||||
"metadata": {}
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Register Spark Model and deploy as Webservice\n",
|
||||
"\n",
|
||||
@@ -25,110 +26,109 @@
|
||||
"\n",
|
||||
" 1. Register Spark Model\n",
|
||||
" 2. Deploy Spark Model as Webservice"
|
||||
],
|
||||
"metadata": {}
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## 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."
|
||||
],
|
||||
"metadata": {}
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"source": [
|
||||
"# Check core SDK version number\r\n",
|
||||
"import azureml.core\r\n",
|
||||
"\r\n",
|
||||
"print(\"SDK version:\", azureml.core.VERSION)"
|
||||
],
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"metadata": {}
|
||||
"source": [
|
||||
"# Check core SDK version number\n",
|
||||
"import azureml.core\n",
|
||||
"\n",
|
||||
"print(\"SDK version:\", azureml.core.VERSION)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Initialize Workspace\n",
|
||||
"\n",
|
||||
"Initialize a workspace object from persisted configuration."
|
||||
],
|
||||
"metadata": {}
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"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": {
|
||||
"tags": [
|
||||
"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",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Register Model"
|
||||
],
|
||||
"metadata": {}
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"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",
|
||||
"\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."
|
||||
],
|
||||
"metadata": {}
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"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": {
|
||||
"tags": [
|
||||
"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",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Fetch Environment"
|
||||
],
|
||||
"metadata": {}
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"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",
|
||||
"\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)."
|
||||
],
|
||||
"metadata": {}
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from azureml.core import Environment\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.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"
|
||||
],
|
||||
"outputs": [],
|
||||
"metadata": {}
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Create Inference Configuration\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",
|
||||
" - 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"
|
||||
],
|
||||
"metadata": {}
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"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": {
|
||||
"tags": [
|
||||
"create image"
|
||||
]
|
||||
}
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from azureml.core.model import InferenceConfig\n",
|
||||
"\n",
|
||||
"inference_config = InferenceConfig(entry_script=\"score.py\", environment=myenv)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Deploy Model as Webservice on Azure Container Instance\n",
|
||||
"\n",
|
||||
"Note that the service creation can take few minutes."
|
||||
],
|
||||
"metadata": {}
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"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": {
|
||||
"tags": [
|
||||
"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",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"#### Test web service"
|
||||
],
|
||||
"metadata": {}
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"source": [
|
||||
"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)"
|
||||
],
|
||||
"metadata": {},
|
||||
"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",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"#### Delete ACI to clean up"
|
||||
],
|
||||
"metadata": {}
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"source": [
|
||||
"service.delete()"
|
||||
],
|
||||
"outputs": [],
|
||||
"metadata": {
|
||||
"tags": [
|
||||
"deploy service",
|
||||
"aci"
|
||||
]
|
||||
}
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"service.delete()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Model Profiling\n",
|
||||
"\n",
|
||||
@@ -278,11 +277,11 @@
|
||||
"profiling_results = profile.get_results()\n",
|
||||
"print(profiling_results)\n",
|
||||
"```"
|
||||
],
|
||||
"metadata": {}
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Model Packaging\n",
|
||||
"\n",
|
||||
@@ -303,8 +302,7 @@
|
||||
"package.wait_for_creation(show_output=True)\n",
|
||||
"package.save(\"./local_context_dir\")\n",
|
||||
"```"
|
||||
],
|
||||
"metadata": {}
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
|
||||
@@ -126,7 +126,7 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from msrest.exceptions import HttpOperationError\n",
|
||||
"from azureml.exceptions import UserErrorException\n",
|
||||
"\n",
|
||||
"blob_datastore_name='MyBlobDatastore'\n",
|
||||
"account_name=os.getenv(\"BLOB_ACCOUNTNAME_62\", \"<my-account-name>\") # Storage account name\n",
|
||||
@@ -136,7 +136,7 @@
|
||||
"try:\n",
|
||||
" blob_datastore = Datastore.get(ws, 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",
|
||||
" workspace=ws,\n",
|
||||
" datastore_name=blob_datastore_name,\n",
|
||||
@@ -180,7 +180,7 @@
|
||||
"try:\n",
|
||||
" adls_datastore = Datastore.get(ws, 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",
|
||||
" workspace=ws,\n",
|
||||
" datastore_name=datastore_name,\n",
|
||||
@@ -270,7 +270,7 @@
|
||||
"try:\n",
|
||||
" sql_datastore = Datastore.get(ws, 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",
|
||||
" workspace=ws,\n",
|
||||
" datastore_name=sql_datastore_name,\n",
|
||||
@@ -312,7 +312,7 @@
|
||||
"try:\n",
|
||||
" psql_datastore = Datastore.get(ws, 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",
|
||||
" workspace=ws,\n",
|
||||
" datastore_name=psql_datastore_name,\n",
|
||||
@@ -353,7 +353,7 @@
|
||||
"try:\n",
|
||||
" mysql_datastore = Datastore.get(ws, 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",
|
||||
" workspace=ws,\n",
|
||||
" datastore_name=mysql_datastore_name,\n",
|
||||
|
||||
@@ -47,8 +47,9 @@
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"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.environment import CondaDependencies\n",
|
||||
"from azureml.data.dataset_consumption_config import DatasetConsumptionConfig\n",
|
||||
"from azureml.widgets import RunDetails\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."
|
||||
]
|
||||
},
|
||||
{
|
||||
"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",
|
||||
"execution_count": null,
|
||||
@@ -235,7 +248,8 @@
|
||||
" arguments=[\"--param1\", file_ds_consumption, \"--param2\", tabular_ds_consumption],\n",
|
||||
" inputs=[file_ds_consumption, tabular_ds_consumption],\n",
|
||||
" compute_target=compute_target,\n",
|
||||
" source_directory=source_directory)\n",
|
||||
" source_directory=source_directory,\n",
|
||||
" runconfig=run_config)\n",
|
||||
"\n",
|
||||
"print(\"train_step created\")\n",
|
||||
"\n",
|
||||
@@ -498,7 +512,7 @@
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.6.7"
|
||||
},
|
||||
"order_index": 13,
|
||||
"order_index": 13.0,
|
||||
"star_tag": [
|
||||
"featured"
|
||||
],
|
||||
|
||||
@@ -183,7 +183,7 @@
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# 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",
|
||||
"# 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",
|
||||
@@ -253,11 +253,12 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from azureml.exceptions import UserErrorException\n",
|
||||
"dataset_registered = False\n",
|
||||
"try:\n",
|
||||
" temp = Dataset.get_by_name(workspace = ws, name = 'mnist-dataset')\n",
|
||||
" dataset_registered = True\n",
|
||||
"except:\n",
|
||||
"except UserErrorException:\n",
|
||||
" print(\"The dataset mnist-dataset is not registered in workspace yet.\")\n",
|
||||
"\n",
|
||||
"if not dataset_registered:\n",
|
||||
@@ -1009,15 +1010,14 @@
|
||||
"from azureml.core.webservice import AciWebservice\n",
|
||||
"from azureml.core.model import InferenceConfig\n",
|
||||
"from azureml.core.model import Model\n",
|
||||
"from azureml.core.environment import Environment\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"myenv = Environment.from_conda_specification(name=\"myenv\", file_path=\"myenv.yml\")\n",
|
||||
"inference_config = InferenceConfig(entry_script=\"score.py\", environment=myenv)\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",
|
||||
" memory_gb=1,\n",
|
||||
" memory_gb=2,\n",
|
||||
" tags={'name': 'mnist', 'framework': 'Keras'},\n",
|
||||
" description='Keras MLP on MNIST')\n",
|
||||
"\n",
|
||||
|
||||
@@ -579,13 +579,12 @@
|
||||
"source": [
|
||||
"from azureml.core.webservice import AciWebservice\n",
|
||||
"from azureml.core.model import InferenceConfig\n",
|
||||
"from azureml.core.webservice import Webservice\n",
|
||||
"from azureml.core.model import Model\n",
|
||||
"\n",
|
||||
"inference_config = InferenceConfig(entry_script=\"pytorch_score.py\", environment=pytorch_env)\n",
|
||||
"\n",
|
||||
"aciconfig = AciWebservice.deploy_configuration(cpu_cores=1, \n",
|
||||
" memory_gb=1, \n",
|
||||
"aciconfig = AciWebservice.deploy_configuration(cpu_cores=2, \n",
|
||||
" memory_gb=2, \n",
|
||||
" tags={'data': 'birds', 'method':'transfer learning', 'framework':'pytorch'},\n",
|
||||
" description='Classify turkey/chickens using transfer learning with PyTorch')\n",
|
||||
"\n",
|
||||
|
||||
@@ -265,11 +265,12 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from azureml.exceptions import UserErrorException\n",
|
||||
"dataset_registered = False\n",
|
||||
"try:\n",
|
||||
" temp = Dataset.get_by_name(workspace = ws, name = 'mnist-dataset')\n",
|
||||
" dataset_registered = True\n",
|
||||
"except:\n",
|
||||
"except UserErrorException:\n",
|
||||
" print(\"The dataset mnist-dataset is not registered in workspace yet.\")\n",
|
||||
"\n",
|
||||
"if not dataset_registered:\n",
|
||||
@@ -964,14 +965,13 @@
|
||||
"from azureml.core.webservice import AciWebservice\n",
|
||||
"from azureml.core.model import InferenceConfig\n",
|
||||
"from azureml.core.model import Model\n",
|
||||
"from azureml.core.environment import Environment\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"myenv = Environment.from_conda_specification(name=\"myenv\", file_path=\"myenv.yml\")\n",
|
||||
"inference_config = InferenceConfig(entry_script=\"score.py\", environment=myenv)\n",
|
||||
"\n",
|
||||
"aciconfig = AciWebservice.deploy_configuration(cpu_cores=1, \n",
|
||||
" memory_gb=1, \n",
|
||||
"aciconfig = AciWebservice.deploy_configuration(cpu_cores=2, \n",
|
||||
" memory_gb=2, \n",
|
||||
" tags={'name':'mnist', 'framework': 'TensorFlow DNN'},\n",
|
||||
" description='Tensorflow DNN on MNIST')\n",
|
||||
"\n",
|
||||
|
||||
@@ -100,7 +100,7 @@
|
||||
"\n",
|
||||
"# Check core SDK version number\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.33.0, you are currently running version\", azureml.core.VERSION)"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
||||
@@ -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
|
||||
|
2
index.md
2
index.md
@@ -107,6 +107,8 @@ 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-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-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) | | | | | | |
|
||||
| [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) | | | | | | |
|
||||
|
||||
@@ -102,7 +102,7 @@
|
||||
"source": [
|
||||
"import azureml.core\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.33.0 of the Azure ML SDK\")\n",
|
||||
"print(\"You are currently using version\", azureml.core.VERSION, \"of the Azure ML SDK\")"
|
||||
]
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user