MLflow Tracking & Experiments

Databricks · Machine Learning & AI

Theory

MLflow is the open-source ML lifecycle tool built into Databricks; current workspaces use MLflow 3. Tracking records each training run: parameters (such as max_depth), metrics (such as accuracy or RMSE), tags and artifacts like plots, plus the model itself as a logged model. Runs are grouped into experiments, which you browse under Experiments in the sidebar to sort, filter and compare runs side by side. mlflow.autolog() captures most of this automatically for libraries like scikit-learn, XGBoost and Spark ML. MLflow 3 also adds GenAI features: tracing of LLM and agent calls and evaluation with judges and scorers.

Example: Asha trains three churn models with different max_depth values on main.sales data. In the experiment view she sorts by val_auc, sees max_depth=6 scored 0.87, and picks that run to register.

Why we use it

ML work involves many trial runs; without tracking, nobody remembers which data, code and settings produced the best model. MLflow keeps a searchable, reproducible record and connects runs to registered models and serving.

When to use it

Use MLflow tracking for every model training, from notebooks or jobs. Use autolog for quick coverage and explicit log_param/log_metric for custom values. For GenAI apps and agents, use MLflow Tracing and evaluation rather than classic metrics alone.

How it works

  1. mlflow.set_experiment(...) chooses where runs go
  2. Start a run and train the model
  3. Log params, metrics and the model
  4. More settings to try? Yes: Start another run with new params. No: Compare runs in the Experiments UI.
  5. Pick the best run and register its model in Unity Catalog

Syntax

import mlflow
mlflow.set_experiment("/Users/<you>/<experiment>")
mlflow.autolog()
with mlflow.start_run(run_name="..."):
    mlflow.log_param("k", v); mlflow.log_metric("m", x)
    mlflow.sklearn.log_model(model, name="model")
mlflow.search_runs(order_by=["metrics.val_auc DESC"])
UI: Experiments → <experiment> → Runs table → select runs → Compare

Example

import mlflow
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import roc_auc_score

mlflow.set_experiment("/Users/asha@techtown.in/churn")

df = spark.table("main.sales.customer_features").toPandas()   # small feature table
X, y = df.drop(columns=["customer_id", "churned"]), df["churned"]

for depth in [4, 6, 8]:
    with mlflow.start_run(run_name=f"rf_depth_{depth}"):
        model = RandomForestClassifier(max_depth=depth).fit(X, y)
        auc = roc_auc_score(y, model.predict_proba(X)[:, 1])
        mlflow.log_param("max_depth", depth)
        mlflow.log_metric("val_auc", auc)
        mlflow.sklearn.log_model(model, name="model", input_example=X.head(5))

best = mlflow.search_runs(order_by=["metrics.val_auc DESC"]).iloc[0]

Key points

Databricks topics

Databricks Fundamentals

Compute

Notebooks & Development

Apache Spark on Databricks

Delta Lake

Unity Catalog & Governance

Data Ingestion

Declarative Pipelines (Lakeflow)

Lakeflow Jobs

Databricks SQL & BI

Architecture, Performance & Cost

Machine Learning & AI

Certification & Interview

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Master Databricks

The lakehouse platform from the ground up: Apache Spark, Delta Lake, Unity Catalog, Lakeflow pipelines and jobs, Databricks SQL and MLflow. Click any topic in the sidebar to open it right here.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice SQL / Spark SQL Sharpen the SQL you use in Databricks SQL and Spark SQL: 150 query problems in a live editor with instant checking. Start practising →
Section {{ sec.num }}

{{ sec.label }}

Databricks  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

{{ sec.h }}

{{ sec.body }}

{{ sec.body }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
On screen — where to click

{{ rich.ui.title }}

{{ u.text }}
  1. {{ st.n }}{{ st.text }}
{{ rich.ui.title }}
{{ u.text }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
Want to run this yourself? Databricks needs a real workspace — there's no in-browser engine for it. Click Copy above, then paste it into a notebook or the SQL editor in Databricks Free Edition (free, no credit card) and run it on serverless compute.
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic