Level: Intermediate Prerequisites: Basic knowledge of Azure ML Studio
Table of Contents
- Introduction to ML Use Cases
- Choosing the Right ML Technique
- AutoML vs Designer – Decision Guide
- Classification with AutoML – Complete Guide
- Clustering and Segmentation with Designer
- End-to-End Workflows with the Designer
- Model Evaluation and Metrics
- Batch Inferencing – Operationalization
- Implementation with Python SDK v2
- Model Deployment and Consumption
- Best Practices and Patterns
- Summary and Key Points
- Glossary
1. Introduction to ML Use Cases
1.1 The Fundamental Question
Imagine your manager arrives with: “We need an ML solution for fraud detection.” Or: “Can we predict next quarter’s sales?” Or: “Segment our customers into coherent groups.”
The classic problem: Managers don’t speak in ML terms. They formulate business problems. If you can’t transform a business problem into the appropriate ML type, you’ll waste weeks on the wrong approach.
Fundamental principle: Before opening Azure ML Studio, ALWAYS ask yourself: “What type of ML problem is this?“
1.2 The 3 Fundamental Types of Supervised/Unsupervised ML
flowchart TD
PROBLEM["Business problem"] --> Q1{"Is the answer\na category?"}
Q1 -->|Yes| Q2{"How many categories?"}
Q2 -->|"2 (Yes/No)"| BINARY["Binary Classification\n(Fraud/Not Fraud)"]
Q2 -->|"More than 2"| MULTI["Multi-class Classification\n(Product type)"]
Q1 -->|No| Q3{"Is the answer\na number?"}
Q3 -->|Yes| Q4{"Is time\nimportant?"}
Q4 -->|No| REGRESSION["Regression\n(Predict a price)"]
Q4 -->|Yes| FORECAST["Forecasting (Time series)\n(Predict future sales)"]
Q3 -->|No| Q5{"Looking for\nsimilar groups?"}
Q5 -->|Yes| CLUSTERING["Clustering\n(Segment customers)"]
Q5 -->|No| OTHER["Other type\n(Anomaly Detection, etc.)"]
2. Choosing the Right ML Technique
2.1 Mapping Business Problems to ML Types
| Business Problem | ML Type | Typical Algorithms | Metrics |
|---|---|---|---|
| Fraud: yes or no? | Binary classification | Logistic Regression, XGBoost, Random Forest | Precision, Recall, AUC-ROC, F1 |
| Will this customer leave? | Binary classification (churn) | Gradient Boosting, Neural Network | AUC, F1, Recall |
| What type of email is this? | Multi-class classification | SVM, Random Forest, BERT | Accuracy, F1 macro |
| Predict house price | Regression | Linear Regression, XGBoost, LightGBM | RMSE, MAE, R² |
| Sales forecast | Forecasting | ARIMA, Prophet, TCNForecaster | MAPE, RMSE |
| Segment customers | Clustering | K-Means, DBSCAN, Hierarchical | Silhouette Score, Inertia |
| Detect an anomaly | Anomaly Detection | Isolation Forest, One-class SVM | Recall, F1 |
| Recommend a product | Collaborative Filtering | ALS, Neural CF | Precision@K, NDCG |
2.2 Questions to Ask Before Modeling
# ML Problem Analysis Checklist
def analyze_business_problem(description: str) -> dict:
"""
Analysis guide to transform a business problem into an ML type.
Questions asked during analysis:
1. Is the target variable known in the historical data?
2. Is the target categorical or numerical?
3. Is there a temporal dimension?
4. How many classes?
5. What is the impact of false positives vs false negatives?
"""
questions = {
"target_variable_known": None, # Supervised or unsupervised
"target_type": None, # Categorical/Numerical
"temporal_dimension": None, # Forecasting vs Regression
"number_of_classes": None, # Binary vs Multi-class
"false_positive_impact": None, # Prioritize Precision vs Recall
"data_volume": None, # Suitable algorithms
"interpretability_required": None # Complex vs simple models
}
return {
"description": description,
"questions_to_ask": questions,
"recommendation": "Answer these questions before starting"
}
# Example application
problem = analyze_business_problem(
"Our bank wants to identify fraudulent transactions in real time"
)
# Analysis:
# - Target variable known: YES (labeled transactions fraud/normal) → Supervised
# - Target type: Categorical (fraud/normal) → Classification
# - Temporal dimension: Not critical for unit detection → Binary classification
# - Number of classes: 2 → Binary
# - False positive impact: High (block a real customer) vs false negatives (let fraud pass)
# → Find the right probability threshold, optimize Recall without sacrificing too much Precision
2.3 Azure ML Algorithms Table by Problem
mindmap
root((Azure ML\nAlgorithms))
Classification
Two-Class
Logistic Regression
Decision Forest
Boosted Decision Tree
Neural Network
SVM
Multi-Class
Decision Forest
Neural Network
One-vs-All
Regression
Linear Regression
Decision Forest
Boosted Decision Tree
Neural Network
Bayesian Linear
Clustering
K-Means
Anomaly Detection
One-Class SVM
PCA-Based
Recommendation
Train Matchbox Recommender
Forecasting
TCN Forecaster
Exponential Smoothing
ARIMA
3. AutoML vs Designer – Decision Guide
3.1 Detailed Comparison
flowchart TD
Q["Which approach to use?"] --> Q1{"Do you have\nML expertise?"}
Q1 -->|Beginner| Q2{"Need fine\ncontrol?"}
Q2 -->|No| AUTOML["✅ AutoML\n(Autopilot)"]
Q2 -->|Yes| Q3{"Need to\ncode?"}
Q1 -->|Experienced| Q4{"Need\nspeed?"}
Q4 -->|Yes POC/Baseline| AUTOML2["✅ AutoML\n(Fast baseline)"]
Q4 -->|No| Q5{"Interface\npreference?"}
Q5 -->|Visual| DESIGNER["✅ Designer\n(Drag-and-drop)"]
Q5 -->|Code| SDK["✅ Python SDK v2\n(Full control)"]
Q3 -->|No| DESIGNER2["✅ Designer\n(Low-code)"]
Q3 -->|Yes| SDK2["✅ Python SDK v2"]
3.2 When to Use AutoML?
AutoML = ML Autopilot. You provide the data, the problem type, and Azure ML automatically tries dozens of algorithms and hyperparameter combinations.
Ideal situations:
- POC / Fast Baseline: Need results in a few hours without coding
- Exploration: You don’t know which algorithm to choose → AutoML tells you which is best
- Limited resources: No data scientist available
- Forecasting: AutoML Time Series is particularly powerful
Automatically tested algorithms (classification):
LightGBM, XGBoost, Random Forest, Decision Tree,
Logistic Regression, SVM, KNN, SGD,
Gradient Boosting, Extra Trees, Voting Ensemble
3.3 When to Use the Designer?
Designer = Creative Workshop. Drag-and-drop interface for building ML pipelines visually with control over each step.
Ideal situations:
- Reusability: Pipelines to run regularly (monthly, weekly)
- Moderate complexity: Custom preprocessing, feature engineering
- Training: Visualize each step to understand
- Collaboration: Share the pipeline with non-coders
4. Classification with AutoML – Complete Guide
4.1 Use Case: Bank Subscription Prediction
Context: A bank wants to predict if a customer will subscribe to a term deposit, based on demographic data and history with the bank.
Dataset variables:
- Age, occupation, marital status, education level
- Payment default, account balance, home loan, personal loan
- Last contact, duration of last campaign
- Target (y):
yes/no(term deposit subscription)
# Bank classification with Azure ML SDK v2 + AutoML
from azure.ai.ml import MLClient
from azure.ai.ml.automl import ClassificationJob
from azure.ai.ml.constants import AssetTypes
from azure.ai.ml.entities import Data, AmlCompute
from azure.identity import DefaultAzureCredential
import os
# Connect to workspace
ml_client = MLClient(
credential=DefaultAzureCredential(),
subscription_id=os.environ["AZURE_SUBSCRIPTION_ID"],
resource_group_name=os.environ["AZURE_RESOURCE_GROUP"],
workspace_name=os.environ["AZURE_ML_WORKSPACE"]
)
# Configure and submit AutoML classification job
from azure.ai.ml.automl import (
ClassificationJob,
ClassificationPrimaryMetric
)
classification_job = ClassificationJob(
# Job configuration
experiment_name="bank-subscription-classification",
display_name="AutoML-Classification-Subscription-v1",
description="Predict if a customer will subscribe to a term deposit",
# Data
training_data={
"data": {
"type": "mltable",
"path": "azureml:bank-marketing-data:1"
},
"target_column_name": "y" # Target variable
},
# Metrics and constraints
primary_metric=ClassificationPrimaryMetric.AUC_WEIGHTED,
# Limits
limits={
"timeout_minutes": 60, # Stop after 1h
"trial_timeout_minutes": 10, # Max 10 min per trial
"max_trials": 20, # Try 20 algorithms max
"max_concurrent_trials": 4, # In parallel
"enable_early_termination": True
},
# Compute
compute="cpu-cluster-4cores",
# Validation
validation_data_size=0.2, # 20% for validation
n_cross_validations=5, # 5-fold cross-validation
# Additional features
featurization="auto", # Auto encoding, normalization
enable_model_explainability=True, # SHAP values
# Blocked algorithms (for time reasons)
blocked_training_algorithms=["TensorFlowDNN"]
)
# Submit
print("Submitting AutoML classification job...")
result = ml_client.jobs.create_or_update(classification_job)
print(f"Job created: {result.name}")
print(f"Status URL: {result.studio_url}")
# Wait for completion
ml_client.jobs.stream(result.name)
# Get results
completed_job = ml_client.jobs.get(result.name)
print(f"Status: {completed_job.status}")
print(f"Best model: {completed_job.best_child_run_id}")
4.2 Analyzing AutoML Results
# Analyze AutoML job results
from azure.ai.ml.entities import Model
import json
def analyze_automl_results(job_name: str) -> dict:
"""
Analyzes the results of an AutoML job and returns key information.
"""
job = ml_client.jobs.get(job_name)
if job.status != "Completed":
return {"status": job.status, "message": "Job not completed"}
# Get the best run
best_run_id = job.best_child_run_id
best_run = ml_client.jobs.get(best_run_id)
# Best model metrics
return {
"status": "Completed",
"best_algorithm": best_run.properties.get("algorithm_name", "N/A"),
"best_score": best_run.properties.get("score", "N/A"),
"number_of_trials": job.properties.get("iteration", "N/A"),
"total_duration_min": "~60",
"run_id": best_run_id,
"model_uri": f"azureml://jobs/{job_name}/outputs/default/model"
}
5. Clustering and Segmentation with Designer
5.1 Use Case: Restaurant Customer Segmentation
Context: Segment a restaurant’s customers into homogeneous groups based on budget, activity, weight and height to personalize offers.
flowchart LR
DATA["📊 Dataset\nRestaurant Customers"] --> SELECT["Column Selection\n(budget, activity,\nweight, height)"]
SELECT --> NORMALIZE["Normalization\n(MinMax Scaler)"]
NORMALIZE --> KMEANS["K-Means Clustering\nK=3 clusters"]
KMEANS --> ASSIGN["Assign Data to Clusters\n(Add cluster column)"]
ASSIGN --> EVALUATE["Evaluate Clustering\n(Silhouette Score)"]
EVALUATE --> VIZ["Cluster\nVisualization"]
# K-Means Clustering with Azure ML SDK v2
clustering_code = '''
import pandas as pd
import numpy as np
from sklearn.preprocessing import MinMaxScaler
from sklearn.cluster import KMeans
from sklearn.metrics import silhouette_score
import mlflow
import os
# Load data
df = pd.read_csv(os.path.join(os.environ.get("AZURE_ML_INPUT_DATA_PATH", "."), "restaurant_data.csv"))
# Select clustering features
features = ["budget", "activity", "weight", "height"]
X = df[features].copy()
# Normalization
scaler = MinMaxScaler()
X_scaled = scaler.fit_transform(X)
# Determine optimal number of clusters (Elbow Method)
silhouette_scores = []
k_range = range(2, 8)
for k in k_range:
kmeans = KMeans(n_clusters=k, random_state=42, n_init=10)
labels = kmeans.fit_predict(X_scaled)
silhouette_scores.append(silhouette_score(X_scaled, labels))
# Choose best K
best_k = k_range[np.argmax(silhouette_scores)]
print(f"Best K: {best_k} (Silhouette: {max(silhouette_scores):.4f})")
# Train final model
kmeans_final = KMeans(n_clusters=best_k, random_state=42, n_init=10)
df["cluster"] = kmeans_final.fit_predict(X_scaled)
# Log metrics with MLflow
mlflow.start_run()
mlflow.log_param("n_clusters", best_k)
mlflow.log_metric("silhouette_score", max(silhouette_scores))
# Analyze cluster profiles
print("\\n=== Cluster Profiles ===")
for cluster_id in range(best_k):
cluster_data = df[df["cluster"] == cluster_id][features]
print(f"\\nCluster {cluster_id} (n={len(cluster_data)}):")
for feat in features:
print(f" {feat}: {cluster_data[feat].mean():.2f} (±{cluster_data[feat].std():.2f})")
mlflow.end_run()
print("\\n✅ Clustering complete!")
'''
6. End-to-End Workflows with the Designer
6.1 Anatomy of a Complete ML Pipeline
flowchart TD
subgraph "Step 1: Ingestion"
DATA["📊 Raw data\n(CSV, Database,\nData Lake)"]
end
subgraph "Step 2: Preprocessing"
CLEAN["Cleaning\n(Missing values,\nDuplicates, Outliers)"]
FEATURE["Feature Engineering\n(Encoding, Normalization,\nNew features)"]
SPLIT["Train/Test Split\n(70/30 or 80/20)"]
end
subgraph "Step 3: Training"
ALGO["Algorithm\n(LR, RF, XGBoost...)"]
TRAIN["Train Model\n(Learning)"]
end
subgraph "Step 4: Evaluation"
SCORE["Score Model\n(Test set predictions)"]
EVAL["Evaluate Model\n(Metrics)"]
end
subgraph "Step 5: Delivery"
REGISTER["Register Model\n(Versioning)"]
DEPLOY["Deploy\n(REST Endpoint)"]
OUTPUT["Export results\n(CSV, DB, Power BI)"]
end
DATA --> CLEAN --> FEATURE --> SPLIT
SPLIT --> ALGO --> TRAIN
TRAIN --> SCORE --> EVAL
EVAL --> REGISTER --> DEPLOY
EVAL --> OUTPUT
6.2 End-to-End Regression Pipeline (SDK v2)
# Complete regression pipeline with Azure ML SDK v2
from azure.ai.ml import dsl, Input, Output
@dsl.component(
environment="AzureML-sklearn-1.0-ubuntu20.04-py38-cpu@latest"
)
def preprocess_data(
raw_data: Input(type="uri_file"),
test_ratio: float = 0.2,
train_data: Output(type="uri_folder") = None,
test_data: Output(type="uri_folder") = None,
) -> None:
"""Cleans and splits data into train/test."""
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder
import os
df = pd.read_csv(raw_data)
df = df.dropna().drop_duplicates()
for col in df.select_dtypes(include=['object']).columns:
if col != 'price':
df[col] = LabelEncoder().fit_transform(df[col].astype(str))
X = df.drop('price', axis=1)
y = df['price']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=test_ratio, random_state=42)
os.makedirs(train_data, exist_ok=True)
os.makedirs(test_data, exist_ok=True)
pd.concat([X_train, y_train], axis=1).to_csv(os.path.join(train_data, "train.csv"), index=False)
pd.concat([X_test, y_test], axis=1).to_csv(os.path.join(test_data, "test.csv"), index=False)
@dsl.component(
environment="AzureML-sklearn-1.0-ubuntu20.04-py38-cpu@latest"
)
def train_regression_model(
train_data: Input(type="uri_folder"),
learning_rate: float = 0.01,
n_estimators: int = 100,
model_output: Output(type="uri_folder") = None,
) -> None:
"""Trains a Gradient Boosting regression model."""
import pandas as pd
from sklearn.ensemble import GradientBoostingRegressor
from sklearn.metrics import mean_squared_error, r2_score
import mlflow
import joblib
import os
df_train = pd.read_csv(os.path.join(train_data, "train.csv"))
X_train = df_train.drop('price', axis=1)
y_train = df_train['price']
mlflow.sklearn.autolog()
with mlflow.start_run():
model = GradientBoostingRegressor(
learning_rate=learning_rate,
n_estimators=n_estimators,
random_state=42
)
model.fit(X_train, y_train)
y_pred = model.predict(X_train)
print(f"Train RMSE: {mean_squared_error(y_train, y_pred, squared=False):.2f}")
print(f"Train R²: {r2_score(y_train, y_pred):.4f}")
os.makedirs(model_output, exist_ok=True)
joblib.dump(model, os.path.join(model_output, "model.joblib"))
@dsl.pipeline(
name="price-prediction-pipeline",
description="Complete regression pipeline to predict prices"
)
def price_prediction_pipeline(
data_path: Input(type="uri_file"),
test_ratio: float = 0.2,
learning_rate: float = 0.01,
n_estimators: int = 100
):
"""Complete pipeline: Preprocessing → Training → Evaluation."""
prep_step = preprocess_data(
raw_data=data_path,
test_ratio=test_ratio
)
train_step = train_regression_model(
train_data=prep_step.outputs.train_data,
learning_rate=learning_rate,
n_estimators=n_estimators
)
return {"model": train_step.outputs.model_output}
# Submit the pipeline
pipeline_job = price_prediction_pipeline(
data_path=Input(path="azureml:automobile-price-data:1", type="uri_file"),
test_ratio=0.2,
learning_rate=0.05,
n_estimators=200
)
pipeline_job.settings.default_compute = "cpu-cluster-4cores"
result = ml_client.jobs.create_or_update(pipeline_job)
print(f"✅ Pipeline submitted: {result.name}")
7. Model Evaluation and Metrics
7.1 Metrics by Problem Type
flowchart TD
PROB_TYPE["ML Problem Type"] --> REG["Regression"]
PROB_TYPE --> CLASS["Classification"]
PROB_TYPE --> CLUST["Clustering"]
REG --> RMSE["RMSE\n√(Σ(ŷ-y)²/n)\nInterpretable in target unit\nPenalizes large errors"]
REG --> MAE["MAE\nΣ|ŷ-y|/n\nRobust to outliers\nEasy to explain"]
REG --> R2["R² (R-squared)\n1 - SS_res/SS_tot\n1=Perfect, 0=Null model\nExplained variance"]
CLASS --> ACC["Accuracy\n(TP+TN)/(TP+TN+FP+FN)\nGood for balanced classes"]
CLASS --> PREC["Precision\nTP/(TP+FP)\nMinimize false positives"]
CLASS --> RECALL["Recall\nTP/(TP+FN)\nMinimize false negatives"]
CLASS --> F1["F1-Score\n2×P×R/(P+R)\nPrecision/Recall balance"]
CLASS --> AUC["AUC-ROC\nArea under ROC curve\n1=Perfect, 0.5=Random"]
CLUST --> SIL["Silhouette Score\n-1 to 1\n1=Perfect clusters\n0=Overlap"]
CLUST --> INERT["Inertia\nSum of intra-cluster distances\nLower = Better"]
7.2 Choosing the Right Metric by Use Case
| Use Case | Primary Metric | Reason |
|---|---|---|
| Fraud detection | Recall | Minimize missed fraud (high cost) |
| Spam detection | Precision | Avoid blocking legitimate emails |
| Medical diagnosis (serious disease) | Recall | False negative = disease not detected = dangerous |
| Product recommendation | Precision@K | Recommend relevant products |
| Stock forecast | MAE | Error in same units as stock |
| Real estate price prediction | RMSE | Penalizes large price errors |
| Customer segmentation | Silhouette + Business validation | Score + Business coherence |
from sklearn.metrics import (
mean_squared_error, mean_absolute_error, r2_score,
accuracy_score, precision_score, recall_score, f1_score, roc_auc_score
)
def calculate_regression_metrics(y_true, y_pred) -> dict:
"""Calculate all regression metrics."""
rmse = mean_squared_error(y_true, y_pred, squared=False)
mae = mean_absolute_error(y_true, y_pred)
r2 = r2_score(y_true, y_pred)
interpretation = (
"✅ EXCELLENT" if r2 > 0.9 else
"✅ GOOD" if r2 > 0.75 else
"⚠️ ACCEPTABLE" if r2 > 0.5 else
"❌ INSUFFICIENT"
)
return {
"rmse": rmse,
"mae": mae,
"r2": r2,
"interpretation": f"{interpretation} - Model explains {r2*100:.1f}% of variance"
}
def calculate_classification_metrics(y_true, y_pred, y_proba=None) -> dict:
"""Calculate all classification metrics."""
metrics = {
"accuracy": accuracy_score(y_true, y_pred),
"precision": precision_score(y_true, y_pred, average='weighted', zero_division=0),
"recall": recall_score(y_true, y_pred, average='weighted', zero_division=0),
"f1": f1_score(y_true, y_pred, average='weighted', zero_division=0)
}
if y_proba is not None:
try:
metrics["auc_roc"] = roc_auc_score(y_true, y_proba, multi_class='ovr')
except Exception:
pass
return metrics
8. Batch Inferencing – Operationalization
8.1 Real-Time vs Batch Inferencing
flowchart LR
subgraph "Real-Time Inferencing"
REQ["Single request\n(1 transaction)"] --> RT_ENDPOINT["REST Endpoint\n(Always active)"]
RT_ENDPOINT --> RESP["Response\n(< 200ms)"]
end
subgraph "Batch Inferencing"
BATCH["Data file\n(Millions of rows)"] --> BATCH_PIPELINE["Batch Pipeline\n(Triggered/Scheduled)"]
BATCH_PIPELINE --> COMPUTE["Compute Cluster\n(Scale-up if needed)"]
COMPUTE --> RESULTS["Results file\n(Minutes to hours)"]
RESULTS --> STORAGE["Azure Blob/ADLS\nor Azure SQL"]
end
| Criterion | Real-Time | Batch |
|---|---|---|
| Latency | Milliseconds | Minutes to hours |
| Volume | One to a few | Millions |
| Cost | High (always active) | Low (on-demand) |
| Availability | 24/7 | Scheduled |
| Use Case | Real-time fraud | Monthly credit scoring |
8.2 Create a Batch Inference Pipeline
from azure.ai.ml.entities import (
BatchEndpoint,
BatchDeployment,
BatchRetrySettings
)
# Create batch endpoint
batch_endpoint = BatchEndpoint(
name="predictions-batch-endpoint",
description="Endpoint for batch price predictions"
)
created_endpoint = ml_client.batch_endpoints.begin_create_or_update(
batch_endpoint
).result()
print(f"✅ Batch endpoint created: {created_endpoint.name}")
# Create batch deployment
batch_deployment = BatchDeployment(
name="regression-batch-deployment",
endpoint_name="predictions-batch-endpoint",
model="azureml:price-prediction-model:1",
compute="cpu-cluster-4cores",
mini_batch_size=10,
max_concurrency_per_instance=2,
error_threshold=10,
retry_settings=BatchRetrySettings(max_retries=3, timeout=300),
output_action="append_row",
output_file_name="predictions.csv"
)
created_deployment = ml_client.batch_deployments.begin_create_or_update(
batch_deployment
).result()
print(f"✅ Deployment created: {created_deployment.name}")
9. Implementation with Python SDK v2
9.1 Workspace Connection
from azure.ai.ml import MLClient
from azure.identity import DefaultAzureCredential, InteractiveBrowserCredential
import os
# Connection method 1: DefaultAzureCredential (recommended for production)
try:
credential = DefaultAzureCredential()
ml_client = MLClient(
credential=credential,
subscription_id=os.environ["AZURE_SUBSCRIPTION_ID"],
resource_group_name=os.environ["AZURE_RESOURCE_GROUP"],
workspace_name=os.environ["AZURE_ML_WORKSPACE"]
)
print(f"✅ Connected to workspace: {ml_client.workspace_name}")
except Exception as e:
print(f"DefaultAzureCredential failed, trying interactive: {e}")
credential = InteractiveBrowserCredential()
ml_client = MLClient(
credential=credential,
subscription_id=os.environ["AZURE_SUBSCRIPTION_ID"],
resource_group_name=os.environ["AZURE_RESOURCE_GROUP"],
workspace_name=os.environ["AZURE_ML_WORKSPACE"]
)
# Verify connection
workspace = ml_client.workspaces.get(ml_client.workspace_name)
print(f"Region: {workspace.location}")
print(f"Resource Group: {workspace.resource_group}")
9.2 Creating a Compute Cluster
from azure.ai.ml.entities import AmlCompute
def get_or_create_compute(
ml_client: MLClient,
compute_name: str,
vm_size: str = "STANDARD_DS3_V2",
min_instances: int = 0,
max_instances: int = 4
) -> AmlCompute:
"""Gets or creates an Azure ML compute cluster."""
try:
compute = ml_client.compute.get(compute_name)
print(f"✅ Existing compute found: {compute_name}")
return compute
except Exception:
print(f"Creating new compute cluster: {compute_name}")
compute_config = AmlCompute(
name=compute_name,
type="amlcompute",
size=vm_size,
min_instances=min_instances,
max_instances=max_instances,
idle_time_before_scale_down=120, # 2 min idle before scale down
)
compute = ml_client.compute.begin_create_or_update(compute_config).result()
print(f"✅ Compute cluster created: {compute_name}")
return compute
# Create compute for training
training_compute = get_or_create_compute(
ml_client=ml_client,
compute_name="cpu-cluster-4cores",
vm_size="STANDARD_DS3_V2",
max_instances=4
)
10. Model Deployment and Consumption
10.1 Deploy a Real-Time Endpoint
from azure.ai.ml.entities import (
ManagedOnlineEndpoint,
ManagedOnlineDeployment,
CodeConfiguration
)
# Create endpoint
endpoint = ManagedOnlineEndpoint(
name="price-prediction-endpoint",
description="Real-time price prediction endpoint",
auth_mode="key"
)
created_endpoint = ml_client.online_endpoints.begin_create_or_update(
endpoint
).result()
# Create deployment
deployment = ManagedOnlineDeployment(
name="blue",
endpoint_name="price-prediction-endpoint",
model="azureml:price-prediction-model:1",
instance_type="Standard_DS2_v2",
instance_count=1
)
created_deployment = ml_client.online_deployments.begin_create_or_update(
deployment
).result()
# Set traffic to 100% on this deployment
ml_client.online_endpoints.begin_create_or_update(
ManagedOnlineEndpoint(
name="price-prediction-endpoint",
traffic={"blue": 100}
)
).result()
print(f"✅ Endpoint deployed: {created_endpoint.scoring_uri}")
10.2 Consuming the Endpoint
import requests
import json
# Get access key
keys = ml_client.online_endpoints.get_keys(name="price-prediction-endpoint")
api_key = keys.primary_key
# Endpoint URL
endpoint_url = "https://price-prediction-endpoint.eastus.inference.ml.azure.com/score"
# Sample data for prediction
sample_data = {
"data": [
{
"make": "toyota",
"model": "corolla",
"year": 2020,
"mileage": 25000,
"condition": "excellent"
}
]
}
# Make prediction
response = requests.post(
endpoint_url,
headers={
"Content-Type": "application/json",
"Authorization": f"Bearer {api_key}"
},
data=json.dumps(sample_data)
)
if response.status_code == 200:
predictions = response.json()
print(f"✅ Prediction: {predictions}")
else:
print(f"❌ Error {response.status_code}: {response.text}")
11. Best Practices and Patterns
11.1 Data Management Best Practices
# Register a dataset in Azure ML
from azure.ai.ml.entities import Data
from azure.ai.ml.constants import AssetTypes
def register_dataset(
ml_client: MLClient,
data_path: str,
name: str,
description: str,
version: str = "1"
) -> Data:
"""Registers a dataset in the Azure ML registry."""
data = Data(
path=data_path,
type=AssetTypes.MLTABLE,
description=description,
name=name,
version=version,
tags={
"source": "production",
"created_by": "ml-team",
"domain": "sales"
}
)
registered_data = ml_client.data.create_or_update(data)
print(f"✅ Dataset registered: {registered_data.name}:{registered_data.version}")
return registered_data
11.2 Model Registry Best Practices
from azure.ai.ml.entities import Model
def register_model(
ml_client: MLClient,
model_path: str,
model_name: str,
model_version: str,
metrics: dict,
tags: dict = None
) -> Model:
"""Registers a model in the Azure ML registry with metadata."""
model = Model(
path=model_path,
name=model_name,
version=model_version,
description=f"Model trained on {model_name} dataset",
type="mlflow_model",
tags={
"framework": "scikit-learn",
"algorithm": "gradient-boosting",
"metrics": str(metrics),
**(tags or {})
}
)
registered_model = ml_client.models.create_or_update(model)
print(f"✅ Model registered: {registered_model.name}:{registered_model.version}")
return registered_model
12. Summary and Key Points
Key Concepts
| Concept | Key Points |
|---|---|
| Problem Type | Always identify classification, regression, forecasting, or clustering before starting |
| AutoML | Best for POC, baseline, exploration, time series — Azure ML handles algorithm selection |
| Designer | Best for reusable pipelines, visual understanding, collaboration with non-coders |
| SDK Python v2 | Full control, integration with CI/CD, production code |
| Metrics | Choose based on business impact (Recall for fraud, Precision for spam) |
| Batch vs Real-Time | Batch for volume, Real-Time for latency |
| MLflow | Automatic logging of parameters, metrics, and models |
| Compute Clusters | Scale to 0 when not in use to reduce costs |
Decision Framework
flowchart TD
START["New ML problem"] --> TYPE{"Problem type?"}
TYPE -->|"Predict category"| CLASS["Classification\nBinary/Multi-class"]
TYPE -->|"Predict number"| REG_FORE{"Temporal\ndimension?"}
REG_FORE -->|"No"| REG["Regression"]
REG_FORE -->|"Yes"| FORE["Forecasting"]
TYPE -->|"Find groups"| CLUST["Clustering"]
CLASS --> TOOL{"How many\ndata points?"}
REG --> TOOL
FORE --> AUTOML_FORE["AutoML Time Series\n✅ Recommended"]
CLUST --> DESIGNER_CLUST["Designer K-Means\nor SDK"]
TOOL -->|"< 10K rows"| AUTOML_SMALL["AutoML\n(Explores all algorithms)"]
TOOL -->|"10K - 1M rows"| AUTOML_MED["AutoML + SDK v2\n(Best of both worlds)"]
TOOL -->|"> 1M rows"| SDK_LARGE["SDK v2 + Spark\n(Distributed computing)"]
13. Glossary
| Term | Definition |
|---|---|
| ABAC | Attribute-Based Access Control |
| AUC-ROC | Area Under the ROC Curve — measures binary classification performance |
| AutoML | Automated Machine Learning — automates algorithm and hyperparameter selection |
| Batch Inferencing | Running predictions on large volumes of data asynchronously |
| Classification | ML problem where the target is a category |
| Clustering | Unsupervised ML to discover natural groups in data |
| Compute Cluster | Scalable Azure ML cluster for model training |
| Data Drift | Change in data distribution over time that degrades model performance |
| Designer | Azure ML visual drag-and-drop interface for ML pipelines |
| Experiment | Azure ML container for grouping related runs |
| F1-Score | Harmonic mean of Precision and Recall |
| Feature Engineering | Process of creating and transforming input variables |
| Forecasting | Time series regression for predicting future values |
| Gradient Boosting | Ensemble algorithm combining weak learners iteratively |
| Hyperparameter | Model configuration parameter tuned before training |
| MAE | Mean Absolute Error — average prediction error |
| MLflow | Open-source ML lifecycle management platform |
| ManagedOnlineEndpoint | Azure ML endpoint for real-time inference |
| MLTABLE | Azure ML data format for structured tabular data |
| Overfitting | Model too well adapted to training data, poorly generalizes |
| Pipeline | Sequence of ML steps assembled and orchestrated |
| Precision | Proportion of true positives among predicted positives |
| R² | Coefficient of determination — proportion of variance explained |
| Random Forest | Ensemble algorithm based on multiple decision trees |
| Recall | Proportion of true positives among all actual positives |
| Regression | ML problem where the target is a continuous numerical value |
| RMSE | Root Mean Squared Error — root of mean squared errors |
| SDK v2 | Azure ML Python SDK version 2 for programming ML workflows |
| Silhouette Score | Metric measuring the quality of clustering |
| Train/Test Split | Division of data into training and evaluation sets |
| Workspace | Azure ML central environment containing all ML assets |
| XGBoost | Optimized gradient boosting algorithm — often best performer |
Search Terms
azure · ml · practical · cases · platforms · deployment · machine · data · science · automl · designer · batch · case · model · pipeline · choosing · decision · end-to-end · endpoint · fundamental · inferencing · metrics · real-time · right