Sub topic 3
Querying and Promoting MLflow Runs
Assume you know Python context managers, scikit-learn training, and basic MLflow logging. This chapter shows how to find runs, group tuning trials, and promote a model in Unity Catalog.
Querying runs programmatically
mlflow.search_runs() returns run data as a pandas DataFrame. Filter on logged values and sort by the metric you care about.
import mlflow
runs = mlflow.search_runs(
experiment_names=["/Users/<your-email>/iris-tracking"],
filter_string="metrics.test_accuracy > 0.90",
order_by=["metrics.test_accuracy DESC"],
max_results=5,
)
print(runs[["run_id", "params.C", "metrics.test_accuracy"]])The experiment name must match the experiment that received the runs. Metric and parameter names use the metrics. and params. prefixes in filter expressions.
Grouping tuning trials with nested runs
A nested run is a child run created inside a parent run. Use a parent run for one tuning job and child runs for its trials. The Databricks experiment UI then groups the trials together.
with mlflow.start_run(run_name="logistic-regression-sweep"):
for c in [0.01, 0.1, 1.0, 10.0]:
with mlflow.start_run(run_name=f"C={c}", nested=True):
model = LogisticRegression(C=c, max_iter=200).fit(X_train, y_train)
mlflow.log_param("C", c)
mlflow.log_metric("test_accuracy", model.score(X_test, y_test))Registering a model in Unity Catalog
Unity Catalog is Databricks’ governed catalog for data and machine-learning assets. Register a model with a three-level name: catalog.schema.model.
import mlflow
mlflow.set_registry_uri("databricks-uc")
model_name = "main.default.iris_classifier"
version = mlflow.register_model(
f"runs:/{run.info.run_id}/model",
model_name,
)
print(version.version)The referenced run must already contain a model artifact at model.
Assigning aliases and loading a registered model
An alias is a stable name for a model version. Use an alias such as champion instead of embedding a version number in application code.
from mlflow import MlflowClient
client = MlflowClient()
client.set_registered_model_alias(model_name, "champion", version.version)
loaded = mlflow.pyfunc.load_model(f"models:/{model_name}@champion")
predictions = loaded.predict(X_test[:3])Reproducibility and access control
A run can record parameters, metrics, tags, artifacts, the model, source information, library dependencies, and timestamps. Log the data split or dataset version and random seed when they affect results. A model signature and input example also document the expected input and output shape.
Experiment permissions control who can view, log to, or manage runs. Use workspace groups and least-privilege permissions. Keep production models in governed Unity Catalog locations rather than relying on an individual notebook path.
> callout note > An alias changes which model version clients load without changing client code. Treat alias changes as deployment actions and restrict who can make them.
Exercise
Why is loading models:/main.default.iris_classifier@champion safer than loading a fixed version such as models:/main.default.iris_classifier/3?
Show answer
The alias identifies the approved version while allowing authorized operators to promote a newer version without editing application code.