Experiment Tracking with MLflow
mlflow.start_run(), log_param(), log_metric(), log_artifact(), viewing the MLflow UI.
Experiment Tracking with MLflow is a free Learn AI with Python lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Learn AI with Python learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Why Track Experiments?
ML work means dozens of runs with different parameters and data. Without tracking you lose which settings produced your best model. MLflow records parameters, metrics, and artifacts for every run so results are reproducible and comparable.
Installing and Importing
MLflow is a single pip install and import.
# pip install mlflow
import mlflow
import mlflow.sklearnThe start_run Context
mlflow.start_run() opens a run as a context manager. Everything logged inside the with block belongs to that run, and the run closes automatically on exit.
with mlflow.start_run():
# log params, metrics, model here
passLogging Parameters
log_param records a single hyperparameter (an input you chose). Log everything that defines the run so you can reproduce it later.
with mlflow.start_run():
mlflow.log_param("n_estimators", 200)
mlflow.log_param("max_depth", 8)Logging Metrics
log_metric records a numeric result like accuracy or loss (an output). Metrics can be logged repeatedly with a step to track training curves.
mlflow.log_metric("accuracy", 0.93)
mlflow.log_metric("val_loss", 0.21, step=epoch)Params vs Metrics
- Parameters: inputs you set (learning rate, depth). Logged once.
- Metrics: outputs you measure (accuracy, RMSE). Can change over steps.
Keeping them separate lets MLflow compare runs in tables and charts.
Logging Artifacts
log_artifact stores any file with the run: plots, confusion matrices, data samples, config files. Artifacts are how you attach supporting evidence to a run.
mlflow.log_artifact("confusion_matrix.png")
mlflow.log_artifact("feature_importance.csv")Logging the Model
mlflow.sklearn.log_model serializes a trained scikit-learn model as an artifact, packaged with its dependencies so it can be reloaded or deployed later. Each framework has its own flavor (mlflow.keras, mlflow.pytorch).
mlflow.sklearn.log_model(model, artifact_path="model")The run_id
Every run gets a unique run_id. You use it to reference, reload, or compare a specific run programmatically.
with mlflow.start_run() as run:
mlflow.log_metric("accuracy", 0.93)
print("Run ID:", run.info.run_id)A Complete Run
Putting it together: open a run, log params, train, log metrics, log the model, all in one block.
with mlflow.start_run():
mlflow.log_param("max_depth", 8)
model.fit(X_train, y_train)
acc = model.score(X_test, y_test)
mlflow.log_metric("accuracy", acc)
mlflow.sklearn.log_model(model, "model")Viewing Experiments
Run mlflow ui in your terminal to launch a local web dashboard (default http://localhost:5000) where you browse runs, sort by metric, and compare parameters side by side.
# terminal
mlflow uiQuick Check
Test your MLflow tracking knowledge.
Recap
You tracked experiments with MLflow: start_run() opens a run, log_param/log_metric record inputs and outputs, log_artifact stores files, and mlflow.sklearn.log_model saves the model. Each run has a run_id, and mlflow ui shows everything. Next: the model registry and versioning.
Frequently asked questions
Is the “Experiment Tracking with MLflow” lesson free?
Yes — the full text of “Experiment Tracking with MLflow” is free to read here on the web, and the Learn AI with Python course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Learn AI with Python course, upgrade to CoddyKit PRO.
What will I learn in “Experiment Tracking with MLflow”?
mlflow.start_run(), log_param(), log_metric(), log_artifact(), viewing the MLflow UI. You practise Learn AI with Python with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Learn AI with Python?
No prior experience is required. Learn AI with Python on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Experiment Tracking with MLflow” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Learn AI with Python lesson?
Yes. Every Learn AI with Python lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Experiment Tracking with MLflow
- Model Registry and Versioning
- Building Reproducible ML Pipelines
- Monitoring Model Performance in Production