Azure Machine Learning: Create Compute and modify a Sample DataSet

We are going to provide an introduction to Azure Machine Learning. This time, we will do some basic environment setup and use Sample data to make some changes to a Data Asset using Pandas in Python. What is Machine Learning? Within Data Sci

AI-assisted translation, approved by Mateo.

We are going to provide an introduction to Azure Machine Learning. This time, we will do some basic environment setup and use Sample data to make some changes to a Data Asset using Pandas in Python.

What is Machine Learning?

Within Data Science, ML is the discipline responsible for training and validating predictive models. We have data, we train the model, and then we perform inference.

It is a type of Artificial Intelligence that enables computers to learn from data, identify patterns, make decisions, and generate predictions autonomously. This learning is achieved through different approaches and techniques such as supervised, unsupervised, or reinforced learning (reinforce learning).

It is behind Chatbots, predictive text, language translation applications, suggestions from your favorite streaming service, or how your Instragram/Facebook/Twitter feed is presented. It is also part of autonomous vehicle software and diagnostic tools in medical industries.

What is Azure Machine Learning?

Azure ML is a cloud service that accelerates and manages the lifecycle of your Machine Learning projects. Its purpose is to be used by AI engineers, ML Pros, and Data Scientists in their day-to-day activities to train and manage models. ML operations are called MLOps.

It is integrated and can use Open Source models such as PyTorch, TensorFlow, or Llama. Azure ML tools allow models to be monitored, retrained, and redeployed.

It has different cross-compatible tools that allow the use of interfaces such as:

  • AzureML Stuido.
  • Python SDK.
  • Azure CLI.
  • ARM Restful APIs.

What are Workspaces?

Workspaces allow MLOps Projects to be organized in Azure ML Studio. They make it possible to define an administrative boundary for work and collaboration.

They allow related work such as experiments, jobs, datasets, models, components, or inference endpoints to be grouped.

Compute Instance in the Azure ML context

It is a cloud Workstation for managing MLOps. The goal is for it to be used as the Development environment with all prerequisites configured automatically, and it is optimized for ML. It will allow us to use Notebooks integrated with Jupyter, and it is natively integrated with the Service.

It will also allow us to reduce security risks with integrated policies. We can configure Schedules to start the compute only when we need it.

Azure Machine Learning Studio

It is the portal in our AzureML environment that will allow us to manage all our MLOps, from Compute to Pipelines and Data.

Some parts of Azure ML Studio:

  • The section  Authoring contains:
    • Notebooks where we can create Jupyter Notebooks to run scripts.
    • Automated ML to create models without writing code.
    • Designer where we will find drag-and-drop functionality to build our models.
  • The section Assets allows us to manage the assets we have created.
  • In Manage we can create Compute and External Services. Here we can also manage Data labeling.
Screenshot of Azure Machine Learning studio.

Now we are going to get started with creating and accessing Azure ML.

Create an Azure ML Resource

First, we are going to create the Azure ML Resource. In this case, we create a Resource Group called AI102-Labs, open it, and create a new resource.

Search for Azure Machine Learning. Click Create.

We are going to assign a name to the resource and leave the automatic creation of the remaining resources. We will create a new ACR by clicking Create New.

Assign a name to the ACR and select the premium SKU. Click Create.

Under Networking, we will select Public for the convenience of the demo.

Under identity, we do not select anything.

And we make sure to have credential-based access.

We leave the rest of the options at their default values. Click Create.

We verify that the deployment was completed correctly.

Once it has been created, we open it and click Launch Studio.

When we access it, we will be able to see Azure ML Studio.

We verify that we are in the Workspace.

Click Notebooks.

Click Create Compute (this will only be available if we do not have any Compute instances created).

Select the lowest SKU. Click Review + Create.

Under Schedule, make sure it has idle Shutdown to save costs.

Assign Managed Identity.

Review + Create.

Now we wait for the creation to be completed.

We verify that the creation has been completed.

We open the Terminal.

This is how the terminal should look:

Run a Sample

Now we will run one of the Samples. Go to the "Samples" tab, click SDK v2, get-started-notebooks, and click the "explore-data.ipynb" file.

A new terminal tab will open. Click "Clone..."

Clone

We will see that the folder was cloned correctly.

Now we will see the files in the root of our Compute Instance.

We reopen the terminal by clicking the indicated icon:

And enter the following code.

Bash
ls
cd get-started-notebooks
mkdir data
cd data
wget https://azuremlexamples.blob.core.windows.net/datasets/credit_card/default_of_credit_card_clients.csv

This will download the Data files.

Connect to Visual Studio Code (desktop)

Now we will open local VSCode. Under Editors, look for VSCode (Desktop).

Click Yes and Continue.

Install the extension.

Sign in to VSCode.

Now we should have VSCode connected.

IMPORTANT! Before running the Python scripts, we must make sure that our Notebook is authenticated with the SDK. We will do this in AML Studio.

Now we must create a script with the following code:

Python
from azure.ai.ml.entities import Data
from azure.ai.ml.constants import AssetTypes
from azure.ai.ml import MLClient
from azure.identity import DefaultAzureCredential
# authenticate
credential = DefaultAzureCredential()
# Get a handle to the workspace
ml_client = MLClient(
credential=credential,
subscription_id="<SUBSCRIPTION_ID>",
resource_group_name="<RESOURCE_GROUP>",
workspace_name="<AML_WORKSPACE_NAME>",
)
my_path = "./data/default_of_credit_card_clients.csv" ## Verify Path!
# set the version number of the data asset
v1 = "initial"
my_data = Data(
name="credit-card",
version=v1,
description="Credit card data",
path=my_path,
type=AssetTypes.URI_FILE,
)
## create data asset if it doesn't already exist:
try:
data_asset = ml_client.data.get(name="credit-card", version=v1)
print(
f"Data asset already exists. Name: {my_data.name}, version: {my_data.version}"
)
except:
ml_client.data.create_or_update(my_data)
print(f"Data asset created. Name: {my_data.name}, version: {my_data.version}")

In my case, when I tried to run it from the terminal, I had to install the azure.ai.ml module with the following command:

Bash
pip install azure.ai.ml

Now we run the created file:

We verify that we have the Dataset created in the Data section of our AzureML Studio:

In the Explore section, we can see a Preview of the data content.

In the Consume section, we will be able to see example code for consuming this Dataset using Pandas:

Now we are going to create a new file in our notebook called "tutorial-notebook.ipynb" to run the rest of our commands:

We are going to run the following code:

Python
import pandas as pd
from azure.ai.ml import MLClient
from azure.identity import DefaultAzureCredential
ml_client = MLClient.from_config(credential=DefaultAzureCredential())
data_asset = ml_client.data.get("credit-card", version="initial")
df = pd.read_csv(data_asset.path)
df
df.head()

Create a new version of the dataset

We run the following script:

Plain text
# read in data again, this time using the 2nd row as the header
df = pd.read_csv(data_asset.path, header=1)
# rename column
df.rename(columns={"default payment next month": "default"}, inplace=True)
# remove ID column
df.drop("ID", axis=1, inplace=True)
# write file to filesystem
df.to_parquet("./get-started-notebooks/data/cleaned-credit-card.parquet")

And verify that the .parquet file was created.

Now we create a new version of the data based on the parquet we created, using the following code:

Python
from azure.ai.ml.entities import Data
from azure.ai.ml.constants import AssetTypes
import time
# Next, create a new *version* of the data asset (the data is automatically uploaded to cloud storage):
v2 = "cleaned" + time.strftime("%Y.%m.%d.%H%M%S", time.gmtime())
my_path = "./get-started-notebooks/data/cleaned-credit-card.parquet"
# Define the data asset, and use tags to make it clear the asset can be used in training
my_data = Data(
name="credit-card",
version=v2,
description="Default of credit card clients data.",
tags={"training_data": "true", "format": "parquet"},
path=my_path,
type=AssetTypes.URI_FILE,
)
## create the data asset
my_data = ml_client.data.create_or_update(my_data)
print(f"Data asset created. Name: {my_data.name}, version: {my_data.version}")

We verify that we have a new version:

We verify the comparison between the datasets by running the following code in our Notebook:

Python
import pandas as pd
# get a handle of the data asset and print the URI
data_asset_v1 = ml_client.data.get(name="credit-card", version="initial")
data_asset_v2 = ml_client.data.get(name="credit-card", version=v2)
# print the v1 data
print(f"V1 Data asset URI: {data_asset_v1.path}")
v1df = pd.read_csv(data_asset_v1.path)
print(v1df.head(5))
# print the v2 data
print(
"_____________________________________________________________________________________________________________\n"
)
print(f"V2 Data asset URI: {data_asset_v2.path}")
v2df = pd.read_parquet(data_asset_v2.path)
print(v2df.head(5))

Thanks for reading. The learning continues in the next article.