MLflow Tutorial: Track, Reproduce, and Deploy ML Models with Python
Learn how to use MLflow to manage the full machine learning lifecycle-track experiments, log metrics, register models, and deploy seamlessly. Step-by-step Python example with clear explanations and visuals. Ideal for data scientists and MLOps teams.
Building machine learning models is only half the battle. Managing the entire lifecycle-from training and tuning to tracking and deployment – is where many projects fall apart. MLflow, a powerful open-source platform that helps machine learning practitioners streamline their workflows and reduce chaos.
In this guide, you will learn exactly what MLflow is, why it matters, and how to use it step-by-step with Python code. We will walk through a complete example using scikit-learn to show how to log experiments, track metrics, and save models effectively.
What is MLflow?
MLflow is a machine learning lifecycle management tool that offers four main components:
MLflow Tracking: Logs parameters, metrics, artifacts, and models.
MLflow Projects: Packages ML code in a reproducible format.
MLflow Models: Manages and serves models in multiple formats.
MLflow Model Registry: Organizes and version-controls your models.
This modular design lets you use what you need, when you need it.
Why Use MLflow?
Here is why MLflow is essential in machine learning pipelines:
.Reproducibility: Everything is logged. You can trace back every run.
.Experiment Tracking: Compare models and tweak parameters easily.
.Version Control: Models are versioned like software.
.Deployment-Ready: Serve models with one command.
.Library-Agnostic: Works with scikit-learn, TensorFlow, PyTorch, XGBoost, and more.
Step-by-Step Guide with Code
Let’s go hands-on. We shall train a simple RandomForest classifier on the Iris dataset and use MLflow to track the experiment.
Step 1: Install MLflow
First, install MLflow using pip:
pip install mlflow
Step 2: Load and Prepare Data
We use the classic Iris dataset for simplicity.
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
# Load Iris dataset
iris = load_iris()
X = iris.data
y = iris.target
# Split into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42
)
What’s happening here?
load_iris()
loads the dataset.train_test_split()
splits it 80/20 into training and testing sets.
Step 3: Train a Model and Start an MLflow Run
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score
import mlflow
import mlflow.sklearn
# Start logging with MLflow
with mlflow.start_run():
# Define and train model
model = RandomForestClassifier(n_estimators=100, max_depth=3)
model.fit(X_train, y_train)
# Make predictions
predictions = model.predict(X_test)
acc = accuracy_score(y_test, predictions)
# Log hyperparameters
mlflow.log_param("n_estimators", 100)
mlflow.log_param("max_depth", 3)
# Log accuracy
mlflow.log_metric("accuracy", acc)
# Log model
mlflow.sklearn.log_model(model, "model")
Breakdown:
mlflow.start_run()
: Starts a new experiment run.log_param()
: Logs model hyperparameters (n_estimators
andmax_depth
).log_metric()
: Logs the accuracy of the model.log_model()
: Saves the trained model so it can be reloaded or deployed later.
Step 4: View the MLflow UI
To see all your runs visually, launch the MLflow UI:
mlflow ui
Then go to http://localhost:5000
in your browser.
Here you’ll find:
A list of all experiment runs
Parameters, metrics, and artifacts logged
Comparison tools to evaluate different runs.
Step 5: Register and Version Your Model
Use MLflow’s Model Registry to manage and version your models.
from mlflow.tracking import MlflowClient
client = MlflowClient()
# Register the model under a unique name
client.create_registered_model("IrisClassifier")
# Create a new version linked to an existing run
client.create_model_version(
name="IrisClassifier",
source="mlruns/0/<run_id>/artifacts/model", # Replace <run_id> with your run ID
run_id="<run_id>"
)
What this does:
Registers your model in a central registry.
Tracks versions as you iterate and retrain.
Step 6: Serve the Model via REST API
MLflow makes deployment fast. Serve your model locally:
mlflow models serve -m runs:/<run_id>/model --port 1234
Now your model is live at http://localhost:1234/invocations
. You can make HTTP POST requests with input data to get predictions.
Real-World Applications of MLflow
Teams: Coordinate model development across team members.
Startups: Quickly iterate and deploy models to production.
Enterprises: Track, audit, and manage compliance with ease.
Conclusion: MLflow Is Essential for Scalable ML
Machine learning without structure is unsustainable. MLflow brings order and power to your workflows. With features for tracking, versioning, and deployment, it acts like Git + Docker + CI/CD for your models.
Whether you’re a solo data scientist or part of a large ML team, MLflow keeps you productive and in control.
Try it today and never lose track of your models again.