Materiały kursów
Materiały / Azure ML (English)
Instrukcja do tego ćwiczeniaPobierz notatnik (.ipynb)

Introduction to the Azure ML SDK

Azure Machine Learning (Azure ML) is a cloud-based service for building, training, deploying, and managing machine learning solutions. It helps data scientists use familiar Python tools and machine learning frameworks while scaling workloads to Azure compute.

The Azure Machine Learning SDK v2 for Python provides classes for working with Azure ML resources in your Azure subscription, including workspaces, compute resources, datastores, data assets, jobs, models, environments, and endpoints.

Check the Azure ML SDK Version

Start by importing the Azure Machine Learning SDK v2 package and checking the installed version.

Note: The Azure Machine Learning SDK for Python is provided by the azure-ai-ml package.

In [ ]:
import sys
from importlib.metadata import version

print("Python executable:", sys.executable)
print("Ready to use Azure ML SDK v2")
print("azure-ai-ml version:", version("azure-ai-ml"))

Connect to Your Workspace

Azure Machine Learning resources are managed in an Azure Machine Learning workspace. In SDK v2, you use an MLClient object to connect to the workspace and manage its resources.

This notebook uses a config.json file to identify the workspace. In the Azure portal, open the Azure Machine Learning workspace, select Overview, and then select Download config.json. Save the file in the same folder as this notebook, or in one of its parent folders.

Note: Do not commit config.json to a public repository. Add it to .gitignore if necessary.

The code below uses DefaultAzureCredential for authentication. On an Azure Machine Learning compute instance, it can use the compute instance managed identity. If you run the notebook locally or in another development environment and authentication fails, sign in by running az login in a terminal.

In [ ]:
from azure.ai.ml import MLClient
from azure.identity import DefaultAzureCredential

credential = DefaultAzureCredential()
ml_client = MLClient.from_config(credential=credential)

print(f"{ml_client.workspace_name} loaded")

View Azure ML Resources

Now that you have connected to your workspace, you can use MLClient to view the resources it contains.

Note: Data in Azure ML is referenced through data assets. They are versioned, and can have types such as uri_file, uri_folder, or mltable.

In [ ]:
print("Compute resources:")
for compute in ml_client.compute.list():
    print(f"\t{compute.name}: {compute.type}")

print("\nDatastores:")
for datastore in ml_client.datastores.list():
    print(f"\t{datastore.name}: {datastore.type}")

print("\nData assets:")
for data_asset in ml_client.data.list():
    print(f"\t{data_asset.name}")

ml_client.data.list() with no argument lists only the names of data assets. The version and type don't belong to the name but to a specific version of the asset - which is why printing them in the loop above would give None.

To see the versions, pass the asset name:

In [ ]:
name = "diabetes_dataset"

print(f"Versions of {name}:")
for version in ml_client.data.list(name=name):
    print(f"\t version {version.version}, type {version.type}")

You have used the Azure Machine Learning SDK v2 to check the installed SDK version, connect to an Azure Machine Learning workspace, and list its compute resources, datastores, and data assets.

The Azure ML SDK v2 enables you to manage Azure Machine Learning resources through Python code. In later labs, you will use it to create and manage data assets, environments, training jobs, models, and endpoints.

For more information, see the Azure Machine Learning SDK v2 for Python documentation.

On the File menu, select Close and Halt to close this notebook. Then return to the lab instructions.