mirror of
https://github.com/Azure/MachineLearningNotebooks.git
synced 2025-12-23 11:02:39 -05:00
tutorial update
This commit is contained in:
@@ -11,20 +11,19 @@
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Tutorial (part 2): Use automated machine learning to build your regression model \n",
|
||||
"# Tutorial: Use automated machine learning to build your regression model\n",
|
||||
"\n",
|
||||
"This tutorial is **part two of a two-part tutorial series**. In the previous tutorial, you [prepared the NYC taxi data for regression modeling](regression-part1-data-prep.ipynb).\n",
|
||||
"\n",
|
||||
"Now, you're ready to start building your model with Azure Machine Learning service. In this part of the tutorial, you will use the prepared data and automatically generate a regression model to predict taxi fare prices. Using the automated ML capabilities of the service, you define your machine learning goals and constraints, launch the automated machine learning process and then allow the algorithm selection and hyperparameter-tuning to happen for you. The automated ML technique iterates over many combinations of algorithms and hyperparameters until it finds the best model based on your criterion.\n",
|
||||
"Now you're ready to start building your model with Azure Machine Learning service. In this part of the tutorial, you use the prepared data and automatically generate a regression model to predict taxi fare prices. By using the automated machine learning capabilities of the service, you define your machine learning goals and constraints. You launch the automated machine learning process. Then allow the algorithm selection and hyperparameter tuning to happen for you. The automated machine learning technique iterates over many combinations of algorithms and hyperparameters until it finds the best model based on your criterion.\n",
|
||||
"\n",
|
||||
"In this tutorial, you learn how to:\n",
|
||||
"In this tutorial, you learn the following tasks:\n",
|
||||
"\n",
|
||||
"> * Setup a Python environment and import the SDK packages\n",
|
||||
"> * Set up a Python environment and import the SDK packages\n",
|
||||
"> * Configure an Azure Machine Learning service workspace\n",
|
||||
"> * Auto-train a regression model \n",
|
||||
"> * Run the model locally with custom parameters\n",
|
||||
"> * Explore the results\n",
|
||||
"> * Register the best model\n",
|
||||
"\n",
|
||||
"If you don\u00e2\u20ac\u2122t have an Azure subscription, create a [free account](https://aka.ms/AMLfree) before you begin. \n",
|
||||
"\n",
|
||||
@@ -33,17 +32,40 @@
|
||||
"\n",
|
||||
"## Prerequisites\n",
|
||||
"\n",
|
||||
"> * [Run the data preparation tutorial](regression-part1-data-prep.ipynb)\n",
|
||||
"To run the notebook you will need:\n",
|
||||
"\n",
|
||||
"> * Automated machine learning configured environment e.g. Azure notebooks, Local Python environment or Data Science Virtual Machine. [Setup](https://docs.microsoft.com/azure/machine-learning/service/samples-notebooks) automated machine learning."
|
||||
"* [Run the data preparation tutorial](regression-part1-data-prep.ipynb).\n",
|
||||
"* A Python 3.6 notebook server with the following installed:\n",
|
||||
" * The Azure Machine Learning SDK for Python with `automl` and `notebooks` extras\n",
|
||||
" * `matplotlib`\n",
|
||||
"* The tutorial notebook\n",
|
||||
"* A machine learning workspace\n",
|
||||
"* The configuration file for the workspace in the same directory as the notebook\n",
|
||||
"\n",
|
||||
"Navigate back to the [tutorial page](https://docs.microsoft.com/azure/machine-learning/service/tutorial-auto-train-models) for specific environment setup instructions.\n",
|
||||
"\n",
|
||||
"## <a name=\"start\"></a>Set up your development environment\n",
|
||||
"\n",
|
||||
"All the setup for your development work can be accomplished in a Python notebook. Setup includes the following actions:\n",
|
||||
"\n",
|
||||
"* Install the SDK\n",
|
||||
"* Import Python packages\n",
|
||||
"* Configure your workspace"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Import packages\n",
|
||||
"Import Python packages you need in this tutorial."
|
||||
"### Install and import packages\n",
|
||||
"\n",
|
||||
"If you are following the tutorial in your own Python environment, use the following to install necessary packages.\n",
|
||||
"\n",
|
||||
"```shell\n",
|
||||
"pip install azureml-sdk[automl,notebooks] matplotlib\n",
|
||||
"```\n",
|
||||
"\n",
|
||||
"Import the Python packages you need in this tutorial:"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -55,7 +77,8 @@
|
||||
"import azureml.core\n",
|
||||
"import pandas as pd\n",
|
||||
"from azureml.core.workspace import Workspace\n",
|
||||
"import logging"
|
||||
"import logging\n",
|
||||
"import os"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -64,9 +87,11 @@
|
||||
"source": [
|
||||
"### Configure workspace\n",
|
||||
"\n",
|
||||
"Create a workspace object from the existing workspace. A `Workspace` is a class that accepts your Azure subscription and resource information, and creates a cloud resource to monitor and track your model runs. `Workspace.from_config()` reads the file **aml_config/config.json** and loads the details into an object named `ws`. `ws` is used throughout the rest of the code in this tutorial.\n",
|
||||
"Create a workspace object from the existing workspace. A `Workspace` is a class that accepts your Azure subscription and resource information. It also creates a cloud resource to monitor and track your model runs.\n",
|
||||
"\n",
|
||||
"Once you have a workspace object, specify a name for the experiment and create and register a local directory with the workspace. The history of all runs is recorded under the specified experiment."
|
||||
"`Workspace.from_config()` reads the file **aml_config/config.json** and loads the details into an object named `ws`. `ws` is used throughout the rest of the code in this tutorial.\n",
|
||||
"\n",
|
||||
"After you have a workspace object, specify a name for the experiment. Create and register a local directory with the workspace. The history of all runs is recorded under the specified experiment and in the [Azure portal](https://portal.azure.com)."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -81,8 +106,6 @@
|
||||
"# project folder\n",
|
||||
"project_folder = './automated-ml-regression'\n",
|
||||
"\n",
|
||||
"import os\n",
|
||||
"\n",
|
||||
"output = {}\n",
|
||||
"output['SDK version'] = azureml.core.VERSION\n",
|
||||
"output['Subscription ID'] = ws.subscription_id\n",
|
||||
@@ -101,7 +124,7 @@
|
||||
"source": [
|
||||
"## Explore data\n",
|
||||
"\n",
|
||||
"Utilize the data flow object created in the previous tutorial. Open and execute the data flow and review the results."
|
||||
"Use the data flow object created in the previous tutorial. To summarize, part 1 of this tutorial cleaned the NYC Taxi data so it could be used in a machine learning model. Now, you use various features from the data set and allow an automated model to build relationships between the features and the price of a taxi trip. Open and run the data flow and review the results:"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -123,7 +146,7 @@
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"You prepare the data for the experiment by adding columns to `dflow_X` to be features for our model creation. You define `dflow_y` to be our prediction value; cost.\n"
|
||||
"You prepare the data for the experiment by adding columns to `dflow_x` to be features for our model creation. You define `dflow_y` to be our prediction value, **cost**:\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -142,7 +165,7 @@
|
||||
"source": [
|
||||
"### Split data into train and test sets\n",
|
||||
"\n",
|
||||
"Now you split the data into training and test sets using the `train_test_split` function in the `sklearn` library. This function segregates the data into the x (features) data set for model training and the y (values to predict) data set for testing. The `test_size` parameter determines the percentage of data to allocate to testing. The `random_state` parameter sets a seed to the random generator, so that your train-test splits are always deterministic."
|
||||
"Now you split the data into training and test sets by using the `train_test_split` function in the `sklearn` library. This function segregates the data into the x, **features**, dataset for model training and the y, **values to predict**, dataset for testing. The `test_size` parameter determines the percentage of data to allocate to testing. The `random_state` parameter sets a seed to the random generator, so that your train-test splits are always deterministic:"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -166,28 +189,28 @@
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"You now have the necessary packages and data ready for auto training for your model. \n",
|
||||
"The purpose of this step is to have data points to test the finished model that haven't been used to train the model, in order to measure true accuracy. In other words, a well-trained model should be able to accurately make predictions from data it hasn't already seen. You now have the necessary packages and data ready for autotraining your model.\n",
|
||||
"\n",
|
||||
"## Automatically train a model\n",
|
||||
"\n",
|
||||
"To automatically train a model:\n",
|
||||
"1. Define settings for the experiment run\n",
|
||||
"1. Submit the experiment for model tuning\n",
|
||||
"To automatically train a model, take the following steps:\n",
|
||||
"1. Define settings for the experiment run. Attach your training data to the configuration, and modify settings that control the training process.\n",
|
||||
"1. Submit the experiment for model tuning. After submitting the experiment, the process iterates through different machine learning algorithms and hyperparameter settings, adhering to your defined constraints. It chooses the best-fit model by optimizing an accuracy metric.\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"### Define settings for autogeneration and tuning\n",
|
||||
"\n",
|
||||
"Define the experiment parameters and models settings for autogeneration and tuning. View the full list of [settings](https://docs.microsoft.com/en-us/azure/machine-learning/service/how-to-configure-auto-train).\n",
|
||||
"Define the experiment parameters and models settings for autogeneration and tuning. View the full list of [settings](https://docs.microsoft.com/en-us/azure/machine-learning/service/how-to-configure-auto-train). Submitting the experiment with these default settings will take approximately 10-15 min, but if you want a shorter run time, reduce either `iterations` or `iteration_timeout_minutes`.\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"|Property| Value in this tutorial |Description|\n",
|
||||
"|----|----|---|\n",
|
||||
"|**iteration_timeout_minutes**|10|Time limit in minutes for each iteration|\n",
|
||||
"|**iterations**|30|Number of iterations. In each iteration, the model trains with the data with a specific pipeline|\n",
|
||||
"|**primary_metric**|spearman_correlation | Metric that you want to optimize.|\n",
|
||||
"|**preprocess**| True | True enables experiment to perform preprocessing on the input.|\n",
|
||||
"|**iteration_timeout_minutes**|10|Time limit in minutes for each iteration. Reduce this value to decrease total runtime.|\n",
|
||||
"|**iterations**|30|Number of iterations. In each iteration, a new machine learning model is trained with your data. This is the primary value that affects total run time.|\n",
|
||||
"|**primary_metric**|spearman_correlation | Metric that you want to optimize. The best-fit model will be chosen based on this metric.|\n",
|
||||
"|**preprocess**| True | By using **True**, the experiment can preprocess the input data (handling missing data, converting text to numeric, etc.)|\n",
|
||||
"|**verbosity**| logging.INFO | Controls the level of logging.|\n",
|
||||
"|**n_cross_validationss**|5|Number of cross validation splits\n"
|
||||
"|**n_cross_validationss**|5| Number of cross-validation splits to perform when validation data is not specified.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -206,6 +229,13 @@
|
||||
"}"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Use your defined training settings as a parameter to an `AutoMLConfig` object. Additionally, specify your training data and the type of model, which is `regression` in this case."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
@@ -233,7 +263,7 @@
|
||||
"source": [
|
||||
"### Train the automatic regression model\n",
|
||||
"\n",
|
||||
"Start the experiment to run locally. Pass the defined `automated_ml_config` object to the experiment, and set the output to `true` to view progress during the experiment."
|
||||
"Start the experiment to run locally. Pass the defined `automated_ml_config` object to the experiment. Set the output to `True` to view progress during the experiment:"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -252,6 +282,13 @@
|
||||
"local_run = experiment.submit(automated_ml_config, show_output=True)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"The output shown updates live as the experiment runs. For each iteration, you see the model type, the run duration, and the training accuracy. The field `BEST` tracks the best running training score based on your metric type."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
@@ -262,7 +299,7 @@
|
||||
"\n",
|
||||
"### Option 1: Add a Jupyter widget to see results\n",
|
||||
"\n",
|
||||
"Use the Jupyter notebook widget to see a graph and a table of all results."
|
||||
"If you use a Jupyter notebook, use this Jupyter notebook widget to see a graph and a table of all results:"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -285,7 +322,7 @@
|
||||
"source": [
|
||||
"### Option 2: Get and examine all run iterations in Python\n",
|
||||
"\n",
|
||||
"Alternatively, you can retrieve the history of each experiment and explore the individual metrics for each iteration run."
|
||||
"You can also retrieve the history of each experiment and explore the individual metrics for each iteration run. By examining RMSE (root_mean_squared_error) for each individual model run, you see that most iterations are predicting the taxi fair cost within a reasonable margin ($3-4).\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -316,7 +353,7 @@
|
||||
"source": [
|
||||
"## Retrieve the best model\n",
|
||||
"\n",
|
||||
"Select the best pipeline from our iterations. The `get_output` method on `automl_classifier` returns the best run and the fitted model for the last fit invocation. There are overloads on `get_output` that allow you to retrieve the best run and fitted model for any logged metric or a particular iteration."
|
||||
"Select the best pipeline from our iterations. The `get_output` method on `automl_classifier` returns the best run and the fitted model for the last fit invocation. By using the overloads on `get_output`, you can retrieve the best run and fitted model for any logged metric or a particular iteration:"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -330,34 +367,13 @@
|
||||
"print(fitted_model)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Register the model\n",
|
||||
"\n",
|
||||
"Register the model in your Azure Machine Learning Workspace."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"description = 'Automated Machine Learning Model'\n",
|
||||
"tags = None\n",
|
||||
"local_run.register_model(description=description, tags=tags)\n",
|
||||
"print(local_run.model_id) # Use this id to deploy the model as a web service in Azure"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Test the best model accuracy\n",
|
||||
"\n",
|
||||
"Use the best model to run predictions on the test data set. The function `predict` uses the best model, and predicts the values of y (trip cost) from the `x_test` data set. Print the first 10 predicted cost values from `y_predict`."
|
||||
"Use the best model to run predictions on the test dataset to predict taxi fares. The function `predict` uses the best model and predicts the values of y, **trip cost**, from the `x_test` dataset. Print the first 10 predicted cost values from `y_predict`:"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -374,7 +390,7 @@
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Create a scatter plot to visualize the predicted cost values compared to the actual cost values. The following code uses the `distance` feature as the x-axis, and trip `cost` as the y-axis. The first 100 predicted and actual cost values are created as separate series, in order to compare the variance of predicted cost at each trip distance value. Examining the plot shows that the distance/cost relationship is nearly linear, and the predicted cost values are in most cases very close to the actual cost values for the same trip distance."
|
||||
"Create a scatter plot to visualize the predicted cost values compared to the actual cost values. The following code uses the `distance` feature as the x-axis and trip `cost` as the y-axis. To compare the variance of predicted cost at each trip distance value, the first 100 predicted and actual cost values are created as separate series. Examining the plot shows that the distance/cost relationship is nearly linear, and the predicted cost values are in most cases very close to the actual cost values for the same trip distance."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -407,7 +423,7 @@
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Calculate the `root mean squared error` of the results. Use the `y_test` dataframe, and convert it to a list to compare to the predicted values. The function `mean_squared_error` takes two arrays of values, and calculates the average squared error between them. Taking the square root of the result gives an error in the same units as the y variable (cost), and indicates roughly how far your predictions are from the actual value. "
|
||||
" Calculate the `root mean squared error` of the results. Use the `y_test` dataframe. Convert it to a list to compare to the predicted values. The function `mean_squared_error` takes two arrays of values and calculates the average squared error between them. Taking the square root of the result gives an error in the same units as the y variable, **cost**. It indicates roughly how far the taxi fare predictions are from the actual fares:"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -427,7 +443,7 @@
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Run the following code to calculate MAPE (mean absolute percent error) using the full `y_actual` and `y_predict` data sets. This metric calculates an absolute difference between each predicted and actual value, sums all the differences, and then expresses that sum as a percent of the total of the actual values."
|
||||
"Run the following code to calculate mean absolute percent error (MAPE) by using the full `y_actual` and `y_predict` datasets. This metric calculates an absolute difference between each predicted and actual value and sums all the differences. Then it expresses that sum as a percent of the total of the actual values:"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -454,21 +470,46 @@
|
||||
"print(1 - mean_abs_percent_error)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"From the final prediction accuracy metrics, you see that the model is fairly good at predicting taxi fares from the data set's features, typically within +- $3.00. The traditional machine learning model development process is highly resource-intensive, and requires significant domain knowledge and time investment to run and compare the results of dozens of models. Using automated machine learning is a great way to rapidly test many different models for your scenario."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Clean up resources\n",
|
||||
"\n",
|
||||
">The resources you created can be used as prerequisites to other Azure Machine Learning service tutorials and how-to articles. \n",
|
||||
"\n",
|
||||
"\n",
|
||||
"If you don't plan to use the resources you created, delete them, so you don't incur any charges:\n",
|
||||
"\n",
|
||||
"1. In the Azure portal, select **Resource groups** on the far left.\n",
|
||||
"\n",
|
||||
"1. From the list, select the resource group you created.\n",
|
||||
"\n",
|
||||
"1. Select **Delete resource group**.\n",
|
||||
"\n",
|
||||
"1. Enter the resource group name. Then select **Delete**."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Next steps\n",
|
||||
"\n",
|
||||
"In this automated machine learning tutorial, you:\n",
|
||||
"In this automated machine learning tutorial, you did the following tasks:\n",
|
||||
"\n",
|
||||
"* Configured a workspace and prepared data for an experiment.\n",
|
||||
"* Trained by using an automated regression model locally with custom parameters.\n",
|
||||
"* Explored and reviewed training results.\n",
|
||||
"\n",
|
||||
"> * Configured a workspace and prepared data for an experiment\n",
|
||||
"> * Trained using an automated regression model locally with custom parameters\n",
|
||||
"> * Explored and reviewed training results\n",
|
||||
"> * Registered the best model\n",
|
||||
"\n",
|
||||
"You can also try out the [image classification tutorial](img-classification-part1-training.ipynb)."
|
||||
"[Deploy your model](https://docs.microsoft.com/azure/machine-learning/service/tutorial-deploy-models-with-aml) with Azure Machine Learning."
|
||||
]
|
||||
}
|
||||
],
|
||||
|
||||
Reference in New Issue
Block a user