MLOps (Machine Learning Operations) applies DevOps principles to ML systems. It aims to automate and streamline the end-to-end ML lifecycle — from data preparation and model training to deployment, monitoring, and retraining — ensuring reliable and scalable ML in production.
Automated ML workflows orchestrate the steps of model development: data ingestion, validation, preprocessing, training, evaluation, and deployment. Tools like Apache Airflow, Kubeflow Pipelines, and Azure ML Pipelines define these as directed acyclic graphs (DAGs) of tasks.
# Conceptual ML pipeline using pseudo-code
# def ml_pipeline(data_source):
# raw_data = ingest(data_source)
# validated_data = validate(raw_data)
# processed_data = preprocess(validated_data)
# X_train, X_test, y_train, y_test = split(processed_data)
# model = train(X_train, y_train)
# metrics = evaluate(model, X_test, y_test)
# if metrics['accuracy'] > threshold:
# register_model(model, metrics)
# deploy(model, staging)
# return metrics
Several strategies exist for deploying ML models: Batch inference processes data in bulk periodically. Real-time inference serves predictions via REST API endpoints with low latency. Edge deployment runs models on IoT devices. Shadow deployment runs a new model alongside the old one without serving its results.
from flask import Flask, request, jsonify
import joblib
import numpy as np
# app = Flask(__name__)
# model = joblib.load('model.pkl')
#
# @app.route('/predict', methods=['POST'])
# def predict():
# data = request.get_json()
# features = np.array(data['features']).reshape(1, -1)
# prediction = model.predict(features)[0]
# probability = model.predict_proba(features)[0].tolist()
# return jsonify({
# 'prediction': int(prediction),
# 'probability': probability
# })
#
# if __name__ == '__main__':
# app.run(port=5000)
Production ML models require continuous monitoring for data drift (changes in input distribution), concept drift (changes in the target relationship), and performance degradation. Alerts should trigger retraining pipelines when metrics fall below thresholds.
import numpy as np
def detect_data_drift(reference_data, production_data, threshold=0.05):
from scipy.stats import ks_2samp
drift_scores = {}
for col in reference_data.columns:
stat, p_value = ks_2samp(reference_data[col], production_data[col])
drift_scores[col] = {'statistic': stat, 'p_value': p_value}
drifted_features = [col for col, scores in drift_scores.items()
if scores['p_value'] < threshold]
return drifted_features, drift_scores
Data leakage occurs when information from outside the training set accidentally influences the model, leading to overoptimistic performance estimates. Common sources include: scaling before splitting, using future data in time series, and duplicate samples across train and test sets.
Pipeline in scikit-learn to chain transformers and estimators, ensuring each cross-validation fold is processed independently.
Model versioning tracks the code, data, hyperparameters, and artifacts for each model iteration. Tools like DVC (Data Version Control), MLflow, and Weights & Biases record experiments, making it easy to reproduce results and compare model versions.
Continuous Integration (CI) for ML validates code changes by running tests, linting, and quick training checks. Continuous Delivery (CD) automates deployment to staging and production after successful validation. Feature stores provide consistent features across training and serving.
A feature store is a centralized repository for managing, sharing, and serving ML features consistently across training and inference. It handles feature computation, storage, and serving, ensuring that the same feature transformations are applied in both environments.
Model registries store metadata, versioning, lineage, and deployment status for trained models. They support staging, production, and archived models. MLflow Model Registry and Azure ML Model Registry are popular choices.
Monetization strategies include: offering predictions as API services (pay-per-call), embedding models in SaaS products, selling insights from trained models, licensing proprietary models, and creating data products that combine raw data with ML-generated features.
joblib. Create a simple Flask API that loads the model and serves predictions. Dockerize the application. Write a CI/CD pipeline using GitHub Actions that runs tests, builds the Docker image, and deploys to a cloud platform (e.g., Azure Container Apps).