π Day 06 β Experiment Tracking & MLflow: Supercharge Your MLOps Workflow

I am Bittu Sharma, a DevOps & AI Engineer with a keen interest in building intelligent, automated systems. My goal is to bridge the gap between software engineering and data science, ensuring scalable deployments and efficient model operations in production.! ππ²π'π ππΌπ»π»π²π°π I would love the opportunity to connect and contribute. Feel free to DM me on LinkedIn itself or reach out to me at bittush9534@gmail.com. I look forward to connecting and networking with people in this exciting Tech World.
Welcome back to the MLOps learning series!
In the previous blogs, we explored Docker, Git, and version control for ML workflows.
Today, we dive into one of the most essential tools in MLOps β MLflow, a powerful open-source platform for managing the end-to-end machine learning lifecycle.
Letβs understand how you can track, reproduce, and manage ML experiments efficiently using MLflow.
π§ What is MLflow?
MLflow is an open-source platform designed to manage the complete lifecycle of a Machine Learning (ML) model β from experiment tracking to deployment.
It provides 4 major components:
MLflow Tracking β Log parameters, metrics, and artifacts for each experiment.
MLflow Projects β Package ML code in a reusable and reproducible format.
MLflow Models β Manage and serve trained models.
MLflow Registry β Version control and manage the lifecycle of models.
βοΈ Why You Need MLflow in MLOps
In real-world ML projects, you often train hundreds of models with different:
Hyperparameters
Feature sets
Algorithms
Data versions
Without tracking tools, comparing these models is chaotic.
MLflow helps you:
β
Keep a record of all experiments
β
Compare runs automatically
β
Log metrics, parameters, and artifacts
β
Reproduce any previous experiment easily
π§© MLflow Components in Detail
1οΈβ£ MLflow Tracking
It helps log experiments β model parameters, metrics, and outputs.
You can log:
Parameters β Learning rate, batch size, etc.
Metrics β Accuracy, loss, F1-score
Artifacts β Model files, plots, confusion matrices
Source code β Code version for reproducibility
2οΈβ£ MLflow Projects
MLflow Projects help package your code in a standardized format using a simple MLproject file.
π Example structure:
mlflow_project/
β
βββ MLproject
βββ train.py
βββ conda.yaml
π MLproject file example:
name: mlflow-demo-project
conda_env: conda.yaml
entry_points:
main:
parameters:
alpha: {type: float, default: 0.5}
l1_ratio: {type: float, default: 0.1}
command: "python train.py --alpha {alpha} --l1_ratio {l1_ratio}"
3οΈβ£ MLflow Models
It helps manage models after training β you can save, load, and deploy models easily.
MLflow supports multiple model formats:
mlflow.sklearnmlflow.tensorflowmlflow.pytorchmlflow.xgboost
Each saved model can be served through REST API or deployed to cloud services.
π§ͺ Hands-on: Track ML Experiments using MLflow
Letβs now do a practical example of tracking your ML experiments.
β Step 1: Install MLflow
pip install mlflow
β Step 2: Import Required Libraries
import mlflow
import mlflow.sklearn
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score
from sklearn.model_selection import train_test_split
from sklearn.datasets import load_iris
β Step 3: Prepare Dataset
data = load_iris()
X_train, X_test, y_train, y_test = train_test_split(
data.data, data.target, test_size=0.2, random_state=42
)
β Step 4: Initialize MLflow Experiment
mlflow.set_experiment("Iris_Classifier_Experiment")
β Step 5: Train Model and Log Parameters
with mlflow.start_run():
clf = RandomForestClassifier(n_estimators=100, max_depth=3, random_state=42)
clf.fit(X_train, y_train)
preds = clf.predict(X_test)
acc = accuracy_score(y_test, preds)
# Logging parameters and metrics
mlflow.log_param("n_estimators", 100)
mlflow.log_param("max_depth", 3)
mlflow.log_metric("accuracy", acc)
# Log model
mlflow.sklearn.log_model(clf, "model")
print("Model accuracy:", acc)
β Step 6: Run MLflow UI
After running the script, start MLflow UI to visualize your experiments:
mlflow ui
Then open http://127.0.0.1:5000 in your browser.
Youβll see:
All experiment runs
Their accuracy, parameters, and metrics
Downloadable artifacts (like model.pkl)
π§° Step 7: Compare Multiple Experiments
Change the parameters (like max_depth or n_estimators) and rerun the code.
Each run will be automatically tracked β you can compare them in the MLflow UI to choose the best model.
π§± Step 8: Save and Load Model from MLflow
model_uri = "runs:/<run_id>/model"
loaded_model = mlflow.sklearn.load_model(model_uri)
predictions = loaded_model.predict(X_test)
You can get <run_id> from the MLflow UI.
π Step 9: Deploy the Model
You can easily deploy the trained model as a REST API using:
mlflow models serve -m runs:/<run_id>/model -p 1234
Then send a request to test:
curl -X POST -H "Content-Type: application/json" \
--data '{"dataframe_split": {"columns": [...], "data": [...]}}' \
http://127.0.0.1:1234/invocations
π Key Takeaways
| Concept | Description |
| Tracking | Logs experiments (metrics, parameters, models) |
| Projects | Makes your ML code reproducible |
| Models | Simplifies model management and deployment |
| Registry | Version control for models |
π§© Real-World Use Cases of MLflow
Tracking 100s of experiments in production ML pipelines
Comparing model versions for A/B testing
Managing model lifecycle (train β test β deploy)
Collaborating among data science teams
π§ Final Thoughts
MLflow is an essential MLOps tool that helps teams build scalable, reproducible, and traceable ML workflows.
Once you integrate MLflow in your pipeline, youβll never lose track of your experiments again!




