<jupyter_start><jupyter_text>Create Azure Machine Learning Custom Environment**Requirements** - In order to benefit from this tutorial, you will need:- A basic understanding of Machine Learning- An Azure account with an active subscription. [Create an account for free](https://azure.microsoft.com/free/?WT.mc_id=A261C142F)- An Azure ML workspace - [Configure workspace](../../jobs/configuration.ipynb) - A python environment- Installed Azure Machine Learning Python SDK v2 - [install instructions](../../README.md) - check the getting started section**Learning Objectives** - By the end of this tutorial, you should be able to:- Create a custom environment from python SDK using  - A docker image  - A docker context  - A docker image with a conda YAML**Motivations** - Azure Machine Learning environments are an encapsulation of the environment where your machine learning training happens. By default your workspace has several curated environments already available. This notebook explains how to create a custom environment to run your specific task if you need to.  1. Connect to Azure Machine Learning WorkspaceThe [workspace](https://docs.microsoft.com/en-us/azure/machine-learning/concept-workspace) is the top-level resource for Azure Machine Learning, providing a centralized place to work with all the artifacts you create when you use Azure Machine Learning. In this section we will connect to the workspace in which the job will be run. 1.1. Import the required libraries<jupyter_code># import required libraries
from azure.ai.ml import MLClient
from azure.ai.ml.entities import Environment, BuildContext
from azure.identity import DefaultAzureCredential<jupyter_output><empty_output><jupyter_text>1.2. Configure workspace details and get a handle to the workspaceTo connect to a workspace, we need identifier parameters - a subscription, resource group and workspace name. We will use these details in the `MLClient` from `azure.ai.ml` to get a handle to the required Azure Machine Learning workspace. We use the default [default azure authentication](https://docs.microsoft.com/en-us/python/api/azure-identity/azure.identity.defaultazurecredential?view=azure-python) for this tutorial. Check the [configuration notebook](../../jobs/configuration.ipynb) for more details on how to configure credentials and connect to a workspace.<jupyter_code># Enter details of your AML workspace
subscription_id = "<SUBSCRIPTION_ID>"
resource_group = "<RESOURCE_GROUP>"
workspace = "<AML_WORKSPACE_NAME>"
# get a handle to the workspace
ml_client = MLClient(
    DefaultAzureCredential(), subscription_id, resource_group, workspace
)<jupyter_output><empty_output><jupyter_text>2. Create EnvironmentAzure Machine Learning [environments](https://docs.microsoft.com/en-us/azure/machine-learning/concept-environments) are an encapsulation of the environment where your machine learning training happens. They specify the Python packages, environment variables, and software settings around your training and scoring scripts. They also specify run times (Python, Spark, or Docker). The environments are managed and versioned entities within your Machine Learning workspace that enable reproducible, auditable, and portable machine learning workflows across a variety of computes.The workspace contains several curated environments by default to use as-is. However, you can create your own custom environment to meet your specific needs.The `Environment` class will be used to create a custom environment. It accepts the following key parameters:- `name` - Name of the environment.		- `version`	- Version of the environment. If omitted, Azure ML will autogenerate a version.		- `image` - The Docker image to use for the environment. Either `image` or `build` is required to create environment.- `conda_file` - The standard conda YAML [configuration file](https://conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.htmlcreating-an-environment-file-manually) of the dependencies for a conda environment. It can be used with a `image`. If specified, Azure ML will build the conda environment on top of the Docker image provided.- `BuildContext`- The Docker build context configuration to use for the environment. Either `image` or `build` is required to create environment.  - `path`- Local path to the directory to use as the build context.		  - `dockerfile_path` - Relative path to the Dockerfile within the build context.- `description`	- Description of the environment.		 2.1 Create a custom environment from a docker imageIn this sample we will create an environment in Azure Machine Learning workspace using a docker image. The `Environment` is used to initialize the details. The `MLClient` is used to create the environment in the workspace.<jupyter_code>env_docker_image = Environment(
    image="pytorch/pytorch:latest",
    name="docker-image-example",
    description="Environment created from a Docker image.",
)
ml_client.environments.create_or_update(env_docker_image)<jupyter_output><empty_output><jupyter_text>2.2 Create a custom environment from Docker build context configurationIn this sample we will use a local docker build configuration to create an environment<jupyter_code>env_docker_context = Environment(
    build=BuildContext(path="docker-contexts/python-and-pip"),
    name="docker-context-example",
    description="Environment created from a Docker context.",
)
ml_client.environments.create_or_update(env_docker_context)<jupyter_output><empty_output><jupyter_text>2.3 Create environment from docker image with a conda YAMLIn this sample we will use a docker image, but customize the docker image with a local conda file.<jupyter_code>env_docker_conda = Environment(
    image="mcr.microsoft.com/azureml/openmpi3.1.2-ubuntu18.04",
    conda_file="conda-yamls/pydata.yml",
    name="docker-image-plus-conda-example",
    description="Environment created from a Docker image plus Conda environment.",
)
ml_client.environments.create_or_update(env_docker_conda)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.07261273209549071, 'fraction_numerical': 0.004310344827586207, 'mean_word_length': 5.370644139387539, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 24, 'https://': 5, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '245966', 'license': 'MIT', 'n_tokens_mistral': 1558, 'n_tokens_neox': 1478, 'n_words': 658, 'path': 'sdk/assets/environment/environment.ipynb', 'repo_name': 'jplummer01/azureml-examples', 'size': '6032'}