MLOps Basics
10 examples to get you started with MLOps - 7 basic and 3 intermediate.
Prerequisites
uv venv && source .venv/bin/activate
uv pip install "mlflow>=2.20" "scikit-learn>=1.5" "fastapi>=0.115" "joblib"Basic Examples
1. Log an Experiment with MLflow
import mlflow
import mlflow.sklearn
from sklearn.ensemble import RandomForestClassifier
with mlflow.start_run():
clf = RandomForestClassifier(n_estimators=100, random_state=42)
clf.fit(X_train, y_train)
mlflow.log_param("n_estimators", 100)
mlflow.log_metric("accuracy", clf.score(X_test, y_test))
mlflow.sklearn.log_model(clf, "model")Related: Experiment Tracking
2. Save a Model Artifact
import joblib
joblib.dump(pipeline, "models/churn_v1.joblib")3. Load and Predict
pipeline = joblib.load("models/churn_v1.joblib")
prediction = pipeline.predict(new_data)4. Version Training Data Hash
import hashlib
data_hash = hashlib.sha256(open("train.csv", "rb").read()).hexdigest()[:12]5. Script Training (Not Notebooks)
# train.py - run with: python train.py --data train.csv
def main(data_path: str) -> None:
...
if __name__ == "__main__":
main()6. Serve with FastAPI
from fastapi import FastAPI
app = FastAPI()
@app.post("/predict")
def predict(features: dict):
return {"prediction": int(model.predict([list(features.values())])[0])}Related: Serving with FastAPI
7. Smoke Test Inference
def test_predict():
assert model.predict(sample_input).shape == (1,)Intermediate Examples
8. Track Data with DVC
dvc add data/train.csv
git add data/train.csv.dvcRelated: Data & Model Versioning (DVC)
9. Promote Model to Staging
client = mlflow.tracking.MlflowClient()
client.transition_model_version_stage("churn", version=3, stage="Staging")Related: Model Registries & Versioning
10. Log Prediction Distribution
import logging
logging.info(f"prediction={pred} features_hash={hash}")Related: Monitoring and Drift
Stack versions: This page was written for Python 3.14.0 (stable 3.14, maintenance 3.13), FastAPI 0.115+, Django 5.2, Flask 3.1, Pydantic 2, PyTorch 2.6+, pandas 2.2+, Polars 1.x, ruff 0.9+, and uv 0.6+.