Deploy a simple supervised machine learning algorithm in Azure as a function.
Published:
This post outlines the development of a supervised machine learning model using the DecisionTreeClassifier algorithm. We will cover training the model on a specific dataset, fine-tuning its parameters, and evaluating accuracy. After achieving proficiency, weโll deploy the model as an Azure Function for serverless execution and expose it as a REST API for real-time predictions. This approach offers insights into machine learning and cloud-based solutions.
Note:Azure provides machine learning services through Azure ML Designer, which is a visual drag-and-drop interface for building and deploying machine learning pipelines, and Azure ML SDK, which allows for programmatic control and customization.
๐ Features
- Machine Learning Model: DecisionTreeClassifier trained on the famous Iris dataset
- REST API: FastAPI-powered endpoints with automatic documentation
- Cloud Deployment: Serverless deployment on Azure Functions
- Containerization: Docker support for consistent environments
๐โโ๏ธ Quick Start
Prerequisites
- Python 3.8+
- pip or conda
- Docker (optional)
- Azure CLI (for Azure deployment)
Local Installation
- Clone the repository
git clone https://github.com/uday160386/ml-model-as-rest-api cd ml-model-deployment
- Install dependencies
pip install -r requirements.txt
- Run the application
uvicorn main:app --reload
- Access the application
- API: http://localhost:8000
- Interactive docs: http://localhost:8000/docs
- Alternative docs: http://localhost:8000/redoc
๐บ Dataset Information
Iris Dataset from scikit-learn:
- Samples: 150
- Features: 4 (sepal length, sepal width, petal length, petal width)
- Classes: 3 (Setosa, Versicolor, Virginica)
- Type: Multiclass classification
The dataset is automatically loaded and preprocessed when training the model.
๐ Project Structure
ml-model-deployment/
โโโ main.py # FastAPI application
โโโ model/
โ โโโ __init__.py
โ โโโ train.py # Model training script
โ โโโ iris_model.joblib # Trained model file
โโโ requirements.txt # Python dependencies
โโโ Dockerfile # Docker configuration
โโโ README.md # This file
๐งช Testing
Manual Testing
Use the provided examples in the interactive documentation or test with curl:
curl -X POST "http://localhost:8000/predict" \
-H "Content-Type: application/json" \
-d '{
"sepal_length": 6.3,
"sepal_width": 2.5,
"petal_length": 5.0,
"petal_width": 1.9
}'
๐ Model Performance
- Accuracy: ~96% on test set
- Training Time: < 1 second
- Prediction Time: < 10ms per request
- Model Size: ~2KB
๐ API Documentation
Endpoints
POST /predict
Predict the Iris species based on flower measurements.
Request Body:
{
"sepal_length": 5.1,
"sepal_width": 3.5,
"petal_length": 1.4,
"petal_width": 0.2
}
Response:
{
"prediction": "setosa",
"confidence": 0.95,
"all_probabilities": {
"setosa": 0.95,
"versicolor": 0.03,
"virginica": 0.02
}
}
๐ณ Docker Deployment
docker run -p 8000:8000 venmaum/ml-simple-app
โ๏ธ Azure Functions Deployment
Prerequisites
- Azure CLI installed and configured
- Azure Functions Core Tools
Deployment Steps
- Login to Azure
az login
- Create Resource Group
az group create --name ml-app-rg --location eastus
- Deploy Function App
func azure functionapp publish <function-app-name>
- Test the deployed function
curl -X POST https://<function-app-name>.azurewebsites.net/api/predict \ -H "Content-Type: application/json" \ -d '{"sepal_length": 5.1, "sepal_width": 3.5, "petal_length": 1.4, "petal_width": 0.2}'
๐ References
๐ท๏ธ Tags
machine-learning
fastapi
azure-functions
docker
iris-dataset
decision-tree
rest-api
python
scikit-learn