This page demonstrates how to train a simple linear regression model using Python on the Azure Machine Learning platform with a free Azure account. It includes the Python code to train the model, deploy it, and steps to set it up in Azure ML Studio.
We use a tiny dataset (x,y
pairs where y = 2x
) stored in Azure Blob Storage, train a linear regression model using scikit-learn, register it in Azure ML, and deploy it as a web service. The process uses Azure ML Studio's free tier compute and storage.
Create a CSV file named simple_data.csv
with the following content and upload it to Azure Blob Storage via Azure ML Studio:
x,y
1,2
2,4
3,6
4,8
5,10
This Python script loads the dataset from Azure storage, trains a linear regression model, and registers it. Run it in a Jupyter notebook in Azure ML Studio.
import pandas as pd
from azureml.core import Workspace, Dataset
from sklearn.linear_model import LinearRegression
import joblib
from azureml.core.model import Model
# Connect to Azure ML workspace
ws = Workspace.from_config()
# Load data from Azure Blob Storage
dataset = Dataset.get_by_name(ws, name='simple-dataset')
df = dataset.to_pandas_dataframe()
X = df[['x']].values
y = df['y'].values
print("Data loaded from Azure Blob Storage:", df.head())
# Train the model
model = LinearRegression()
model.fit(X, y)
print("Model trained. Coefficient:", model.coef_[0])
# Save and register the model
joblib.dump(model, 'linear_model.pkl')
registered_model = Model.register(workspace=ws,
model_path='linear_model.pkl',
model_name='simple-linear-model',
description='Simple linear regression model')
print("Model registered:", registered_model.name)
This script (score.py
) is used when deploying the model as a real-time endpoint in Azure ML.
import joblib
import json
def init():
global model
model = joblib.load('model.pkl')
def run(data):
input_data = json.loads(data)['data']
prediction = model.predict([[input_data]])
return {'prediction': prediction[0]}
simple_data.csv
to the default Blob Storage and register it as a dataset named "simple-dataset".score.py
.{'data': 6}
to predict y=12
) using tools like Postman or Python's requests
.Use this Python code locally to test the deployed endpoint:
import requests
import json
endpoint_url = 'YOUR_ENDPOINT_URL' # From Azure ML Studio
scoring_uri = endpoint_url + '/score'
headers = {'Content-Type': 'application/json'}
data = json.dumps({'data': 6})
response = requests.post(scoring_uri, data=data, headers=headers)
print("Prediction:", response.json())