Advent of 2022, Day 8 – Python SDK namespaces for workspace, experiments and models
This article is originally published at https://tomaztsql.wordpress.com
In the series of Azure Machine Learning posts:
- Dec 01: What is Azure Machine Learning?
- Dec 02: Creating Azure Machine Learning Workspace
- Dec 03: Understanding Azure Machine Learning Studio
- Dec 04: Getting data to Azure Machine Learning workspace
- Dec 05: Creating compute and cluster instances in Azure Machine Learning
- Dec 06: Environments in Azure Machine Learning
- Dec 07: Introduction to Azure CLI and Python SDK
Looking briefly into Azure CLI and Python SDK, let’s explore the power of SDK and the most important namespaces.
Create a new notebook (mine is called “Namespaces_SDK.ipynb”) and attach a compute to the notebook. We will be interacting with a Python environment, using Jupyter notebooks (or Visual Studio Code, if you prefer) and explore the SDK namespaces for:
- Exploring, preparing and managing experiments
- managing resource for the experiments
- training and configuring the models, hyperparameter settings and finding best predictions
- deploying the models (including REST)
I will go through the most important namespaces.
Workspace
The full name for the namespace is azureml.core.workspace.Workspace
with additional classes available, that I will not be covering.
One sample of a class is to create worskapce.
# Create new workspace
from azureml.core import Workspace
ws = Workspace.create(name='AML_Blogpost2022_v2',
subscription_id='{your-subscription-guid}',
resource_group='RG_AML_Blogpost2022',
create_resource_group=False,
location='eastus2'
)
And for example, store the settings in JSON file:
# Store settings to file
ws.write_config(path=".", file_name="ws_AML_Blogpost2022_v2_config.json")
To double-check, you can go to default directory and see there is another workspace created.
Experiments
The full name for the namespace is azureml.core.experiment.Experiment
. It lets you create an experiment and manage it, run it and more.
from azureml.core.experiment import Experiment
experiment = Experiment(workspace=ws, name='demo-experiment')
#list all the experiments
list_experiments = Experiment.list(ws)
list_experiments
You can also get all the runs for this experiment:
list_runs = experiment.get_runs()
for run in list_runs:
print(run.id)
Model
The namespace for a run: azureml.core.model.Model
and use model registration to store and version your models in the Azure cloud, in your workspace. Registered models are identified by name and version.
Create a simple model:
from sklearn import svm
import joblib
import numpy as np
# customer ages
X_train = np.array([50, 17, 35, 23, 28, 40, 31, 29, 19, 62])
X_train = X_train.reshape(-1, 1)
# churn y/n
y_train = ["yes", "no", "no", "no", "yes", "yes", "yes", "no", "no", "yes"]
clf = svm.SVC(gamma=0.001, C=100.)
clf.fit(X_train, y_train)
joblib.dump(value=clf, filename="churn-model.pkl")
And you can register the model:
from azureml.core.model import Model
model = Model.register(workspace=ws, model_path="churn-model.pkl", model_name="churn-model-test")
Tomorrow, we will look into creating Run, enviroments, compute target, pipelines and datasets. Hang in there.
Compete set of code, documents, notebooks, and all of the materials will be available at the Github repository: https://github.com/tomaztk/Azure-Machine-Learning
Happy Advent of 2022!
Thanks for visiting r-craft.org
This article is originally published at https://tomaztsql.wordpress.com
Please visit source website for post related comments.