Skip to main content
  1. Blog/

Building Data Science Workflows with Azure ML Studio

·7 mins·
Data Science Azure Azure Machine Learning Python Pandas scikit-learn
Kyriakos Antoniadis
Author
Kyriakos Antoniadis
AI SYSTEMS TODAY
Table of Contents
Empowering Data Science with AI

Introduction
#

In today’s rapidly evolving data landscape, harnessing the power of data science and artificial intelligence (AI) is essential for organizations seeking to remain competitive. From optimizing business processes to unlocking new revenue streams, data science provides the foundation for impactful decision-making. However, building and managing end-to-end data science workflows is a complex task that requires the right tools and practices.

Azure Machine Learning Studio, a comprehensive cloud-based platform provided by Microsoft, simplifies the journey of creating, training, and deploying machine learning models. By offering an integrated development environment (IDE) that incorporates machine learning tools, compute resources, and workflow orchestration, Azure ML Studio helps data scientists and developers streamline their workflows and achieve better results.

This blog will guide you through creating data science workflows with Azure ML Studio, from data preparation and model development to deployment and monitoring.

Key Features of Azure ML Studio
#

Azure Machine Learning Studio provides several features that empower data scientists and developers to build and manage workflows efficiently:

  1. Integrated Development Environment (IDE): Azure ML Studio provides a user-friendly interface for managing data science projects, datasets, and models.
  2. Automated Machine Learning (AutoML): Automatically train and tune machine learning models with minimal intervention.
  3. Designer: Drag-and-drop tools to build, train, and deploy models without writing code.
  4. Compute Targets: Access to powerful compute resources like Azure Databricks, GPU-enabled virtual machines, and Kubernetes clusters.
  5. Pipelines: Create reusable, automated ML workflows for data preparation, training, and deployment.
  6. Model Management: Register, version, and track models in a central repository.
  7. Monitoring and Debugging: Monitor experiments and deployed models using built-in tools.

Prerequisites
#

Before starting with Azure Machine Learning Studio, ensure that you have the following prerequisites:

  • An Azure Subscription. If you don’t have one, you can create a free account at azure.com/free.
  • A Workspace in Azure Machine Learning Studio.
  • Familiarity with Python programming and basic data science concepts.

Setting Up Azure ML Studio Workspace
#

  1. Create an Azure ML Workspace:
    • Log in to the Azure Portal.
    • Click on “Create a resource” and search for “Machine Learning.”
    • Click “Create” and provide the required information (subscription, resource group, workspace name, etc.).
    • Click “Review + Create” and then “Create” to deploy the workspace.
  2. Launch Azure ML Studio:
    • After deployment, navigate to the workspace and click “Launch studio.”

Building Data Science Workflows in Azure ML Studio
#

1. Data Preparation and Exploration
#

The first step in any data science workflow is data preparation and exploration. Azure ML Studio offers different tools to import and preprocess data:

Importing Data
#

  1. Dataset Creation:
    • In the Azure ML Studio workspace, navigate to the “Datasets” tab.
    • Click on “Create Dataset” and choose the type of dataset (From local files, From Datastore, etc.).
    • Follow the prompts to upload and register your dataset.
  2. Explore Data in Jupyter Notebooks:
    • Launch a new or existing Jupyter Notebook.
    • Use the following code to load the dataset into a Pandas DataFrame:
from azureml.core import Workspace, Dataset

# Load the workspace
ws = Workspace.from_config()

# Retrieve the dataset
dataset = Dataset.get_by_name(ws, name='your_dataset_name')
df = dataset.to_pandas_dataframe()

# Display the first few rows
df.head()
  1. Data Cleaning and Transformation:
    • Perform data cleaning and transformation using Pandas or the Designer drag-and-drop tools.
    • Example transformation using Pandas:
# Drop rows with missing values
df_clean = df.dropna()

# Convert categorical variables to numerical
df_clean['category'] = df_clean['category'].astype('category').cat.codes

2. Model Development and Training
#

Azure ML Studio supports various model training techniques, including Automated ML and custom training scripts.

Automated ML (AutoML)
#

  1. Create an AutoML Experiment:
    • In the Studio, navigate to the “Automated ML” tab.
    • Click “New Automated ML run” and select your dataset.
    • Configure the experiment by specifying the target column and the type of problem (classification, regression, etc.).
    • Choose a compute cluster and click “Finish.”
  2. Monitor the Experiment:
    • After submitting the experiment, you can monitor its progress in the Experiments tab.
    • Review the leaderboard to identify the best-performing model.

Custom Model Training
#

  1. Create and Register an Environment:
    • Define a custom Python environment for your training script:
from azureml.core import Environment
from azureml.core.conda_dependencies import CondaDependencies

# Create an environment
env = Environment(name="custom-env")
conda_dep = CondaDependencies()

# Add packages
conda_dep.add_conda_package("scikit-learn")
conda_dep.add_pip_package("pandas")
env.python.conda_dependencies = conda_dep

# Register the environment
env.register(workspace=ws)
  1. Write a Training Script:
    • Create a Python training script (train.py) with your desired ML model:
# train.py
import argparse
import joblib
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier

# Parse command-line arguments
parser = argparse.ArgumentParser()
parser.add_argument('--input-data', type=str, help='Path to the input data')
parser.add_argument('--output-model', type=str, help='Path to save the trained model')
args = parser.parse_args()

# Load the data
df = pd.read_csv(args.input_data)

# Preprocess and split the data
X = df.drop(columns='target')
y = df['target']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Train a RandomForest model
clf = RandomForestClassifier(n_estimators=100)
clf.fit(X_train, y_train)

# Save the trained model
joblib.dump(clf, args.output_model)
  1. Create a Training Pipeline:
    • Define the training pipeline using the Pipeline API:
from azureml.core import Experiment, ScriptRunConfig, Dataset
from azureml.pipeline.core import Pipeline, PipelineData
from azureml.pipeline.steps import PythonScriptStep

# Create an output directory for the trained model
output_dir = PipelineData("model_output", datastore=ws.get_default_datastore())

# Create a script run configuration
src = ScriptRunConfig(source_directory=".", script="train.py", arguments=[
    "--input-data", Dataset.get_by_name(ws, "your_dataset").as_mount(),
    "--output-model", output_dir
], environment=env)

# Create a Python script step
train_step = PythonScriptStep(
    name="Train Model",
    source_directory=".",
    script_name="train.py",
    arguments=["--input-data", Dataset.get_by_name(ws, "your_dataset").as_mount(), "--output-model", output_dir],
    outputs=[output_dir],
    compute_target="your-compute-cluster",
    runconfig=src.run_config
)

# Create a pipeline and submit the experiment
pipeline = Pipeline(workspace=ws, steps=[train_step])
experiment = Experiment(ws, "train-model-experiment")
pipeline_run = experiment.submit(pipeline)

3. Model Deployment
#

Once a model is trained and registered, it can be deployed as a web service using Azure ML Studio.

Deploy to Azure Container Instance (ACI)
#

  1. Register the Model:
    • Register the trained model in the workspace:
from azureml.core import Model

model = Model.register(workspace=ws, model_name="random_forest_model", model_path="outputs/random_forest_model.pkl")
  1. Create an Inference Environment:
    • Create a new environment with inference dependencies:
env = Environment(name="inference-env")
conda_dep = CondaDependencies()
conda_dep.add_conda_package("scikit-learn")
conda_dep.add_pip_package("joblib")
env.python.conda_dependencies = conda_dep
env.register(workspace=ws)
  1. Create an Inference Configuration:
    • Write a scoring script (score.py) to handle inference requests:
# score.py
import joblib
import json
import numpy as np
from sklearn.ensemble import RandomForestClassifier

# Initialize the model
def init():
    global model
    model_path = "your_model_path"
    model = joblib.load(model_path)

# Run predictions
def run(raw_data):
    data = np.array(json.loads(raw_data))
    predictions = model.predict(data)
    return json.dumps(predictions.tolist())
  1. Deploy the Model:
    • Create an inference configuration and deploy the model:
from azureml.core import InferenceConfig
from azureml.core.webservice import AciWebservice

# Create an inference configuration
inference_config = InferenceConfig(entry_script="score.py", environment=env)

# Define the deployment configuration
aci_config = AciWebservice.deploy_configuration(cpu_cores=1, memory_gb=1)

# Deploy the model as a web service
service = Model.deploy(
    workspace=ws,
    name="random-forest-service",
    models=[model],
    inference_config=inference_config,
    deployment_config=aci_config
)

# Wait for deployment completion
service.wait_for_deployment(show_output=True)

Test the Web Service
#

Once deployed, the web service can be tested by sending HTTP requests:

import requests
import json

# Prepare test data
test_data = json.dumps({"data": [[5.1, 3.5, 1.4, 0.2], [6.7, 3.0, 5.2, 2.3]]})
headers = {"Content-Type": "application/json"}

# Send POST request to the web service
url = service.scoring_uri
response = requests.post(url, data=test_data, headers=headers)

# Display predictions
print(response.json())

4. Monitoring and Management
#

Monitoring deployed models is crucial for ensuring optimal performance and detecting issues. Azure ML Studio provides built-in tools for monitoring and managing services.

  1. Monitor Service Logs:
    • View service logs directly in the Azure ML Studio workspace.
  2. Model Drift Monitoring:
    • Use Azure Monitor to track model drift over time by comparing prediction outputs with ground truth.
  3. Retraining and Redeployment:
    • Create automated pipelines that periodically retrain and redeploy models based on new data.

Conclusion
#

Building data science workflows with Azure ML Studio empowers organizations to harness the potential of AI efficiently. With features like Automated ML, the Designer, and powerful compute resources, Azure ML Studio simplifies the end-to-end process of developing, training, and deploying machine learning models.

By following the best practices and leveraging the rich toolset provided by Azure ML Studio, businesses can significantly reduce the time to value and unlock the full potential of their data.

Next Steps
#

  • Learn More: Dive deeper into Azure ML Studio with the official documentation.
  • Experiment: Try out different features like Automated ML and the Designer for your data science workflows.
  • Expand Your Skills: Take an advanced course in Azure Machine Learning or explore other Azure AI Services.

Happy experimenting!

Related

Fine-tuning BERT Models for NLP Tasks in Azure
·6 mins
Data Science Deep Learning Azure Azure Machine Learning BERT Transformers Hugging Face
Implementing ChatGPT-like Models with Azure OpenAI Service
·5 mins
Data Science Generative AI Azure Azure OpenAI GPT4 Transformers Hugging Face
AI Consultancy
5 mins
Consultancy
AI Custom Bots
4 mins
Custom Bots
AI Integrations
5 mins
Integrations