Skip to main content

Chapter 1: What is MLOps?

Introduction​

MLOps (Machine Learning Operations) is a set of practices that aims to deploy and maintain machine learning models in production reliably and efficiently. It combines Machine Learning, DevOps, and Data Engineering to automate and standardize the ML lifecycle.

Definition

MLOps = Machine Learning + DevOps + Data Engineering

It's the discipline of delivering ML systems with the same rigor and reliability as traditional software systems.

The Problem MLOps Solves​

The ML Development Challenge​

Traditional software development follows a well-understood path:

Code → Build → Test → Deploy → Monitor

Machine learning adds significant complexity:

Data → Feature Engineering → Training → Evaluation → Deployment → Monitoring → Retraining
↑ ↓
└──────────────────────────────────────────────────────────────────────────────┘

Why 87% of ML Projects Never Make It to Production​

According to industry research:

ChallengeImpact
Poor model reproducibilityCannot recreate results
Lack of versioningCode, data, and model drift
Manual deploymentSlow and error-prone releases
No monitoringSilent model failures
Technical debtUnmaintainable systems

Core Principles of MLOps​

1. Automation​

Everything that can be automated, should be automated:

# Example: Automated training pipeline trigger
name: ML Training Pipeline
on:
schedule:
- cron: '0 0 * * 0' # Weekly retraining
push:
paths:
- 'data/**'
- 'src/training/**'

jobs:
train:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Setup Python
uses: actions/setup-python@v4
with:
python-version: '3.10'

- name: Train model
run: |
pip install -r requirements.txt
python src/train.py

- name: Evaluate model
run: python src/evaluate.py

- name: Register model
if: success()
run: python src/register_model.py

2. Versioning Everything​

Track all components of your ML system:

# Code versioning
git add .
git commit -m "feat: add new feature engineering step"

# Data versioning with DVC
dvc add data/training_data.csv
git add data/training_data.csv.dvc
git commit -m "data: update training dataset v2.1"

# Model versioning
mlflow models log \
--model-uri runs:/<run-id>/model \
--registered-model-name "production-model"

3. Continuous Integration/Continuous Deployment (CI/CD)​

4. Monitoring and Observability​

Production models require constant monitoring:

# Example: Model monitoring metrics
from prometheus_client import Counter, Histogram, Gauge

# Track predictions
prediction_counter = Counter(
'model_predictions_total',
'Total number of predictions',
['model_version', 'prediction_class']
)

# Track latency
prediction_latency = Histogram(
'model_prediction_latency_seconds',
'Time spent processing prediction',
buckets=[0.01, 0.05, 0.1, 0.5, 1.0]
)

# Track data drift
data_drift_score = Gauge(
'model_data_drift_score',
'Current data drift score',
['feature_name']
)

5. Collaboration​

MLOps enables cross-functional team collaboration:

RoleResponsibility
Data ScientistsModel development, experimentation
ML EngineersPipeline development, optimization
DevOps EngineersInfrastructure, CI/CD
Data EngineersData pipelines, quality
Product ManagersRequirements, metrics definition

MLOps Maturity Levels​

Organizations evolve through MLOps maturity levels:

Level 0: Manual Process​

┌─────────────────────────────────────────────────────────┐
│ LEVEL 0: MANUAL │
├─────────────────────────────────────────────────────────┤
│ • Jupyter notebooks for everything │
│ • Manual model deployment │
│ • No versioning │
│ • No monitoring │
│ • Data scientists do everything │
└─────────────────────────────────────────────────────────┘

Characteristics:

  • Scripts and notebooks
  • Manual, script-driven deployments
  • Lack of continuous integration
  • Rare releases (quarterly or less)

Level 1: ML Pipeline Automation​

┌─────────────────────────────────────────────────────────┐
│ LEVEL 1: PIPELINE AUTOMATION │
├─────────────────────────────────────────────────────────┤
│ • Automated training pipeline │
│ • Experiment tracking │
│ • Model registry │
│ • Basic monitoring │
│ • Retraining requires manual trigger │
└─────────────────────────────────────────────────────────┘

Characteristics:

  • Reproducible training pipelines
  • Experiment tracking with MLflow/W&B
  • Model versioning
  • Still manual deployment triggers

Level 2: CI/CD Pipeline Automation​

┌─────────────────────────────────────────────────────────┐
│ LEVEL 2: CI/CD AUTOMATION │
├─────────────────────────────────────────────────────────┤
│ • Automated CI/CD for ML │
│ • Automated testing (data, model, integration) │
│ • Automated deployment │
│ • Comprehensive monitoring │
│ • Automatic retraining triggers │
└─────────────────────────────────────────────────────────┘

Characteristics:

  • Full CI/CD automation
  • A/B testing and canary deployments
  • Automated rollbacks
  • Data and model drift detection

The MLOps Stack​

A typical MLOps stack includes:

Practical Example: Simple MLOps Setup​

Here's a minimal MLOps setup using popular tools:

# train.py - Example training script with MLflow tracking
import mlflow
import mlflow.sklearn
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score, f1_score
import pandas as pd

# Enable autologging
mlflow.sklearn.autolog()

def train_model():
# Load data
df = pd.read_csv("data/training_data.csv")
X = df.drop("target", axis=1)
y = df["target"]

# Split data
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42
)

with mlflow.start_run():
# Log parameters
mlflow.log_param("model_type", "RandomForest")
mlflow.log_param("n_estimators", 100)

# Train model
model = RandomForestClassifier(n_estimators=100, random_state=42)
model.fit(X_train, y_train)

# Evaluate
predictions = model.predict(X_test)
accuracy = accuracy_score(y_test, predictions)
f1 = f1_score(y_test, predictions, average='weighted')

# Log metrics
mlflow.log_metric("accuracy", accuracy)
mlflow.log_metric("f1_score", f1)

# Log model
mlflow.sklearn.log_model(model, "model")

print(f"Model trained with accuracy: {accuracy:.4f}")

if __name__ == "__main__":
train_model()

Key Takeaways​

Summary
  1. MLOps bridges the gap between ML development and operations
  2. Automation is essential for reliable, scalable ML systems
  3. Version everything - code, data, models, and configurations
  4. Monitor continuously - models degrade silently without proper monitoring
  5. Collaborate effectively - MLOps requires cross-functional teams

Further Reading​


Next: Chapter 2: MLOps vs DevOps vs DataOps