Sub topic 2
Logging artifacts: files, plots and data
This chapter assumes you can start an MLflow tracking server, open a run with mlflow.start_run(), and train a scikit-learn classifier. It also assumes you know what a train/test split is. Parameters and metrics are numbers and short strings. Artifacts are everything else: files such as plots, CSV extracts, notes and saved models. MLflow stores them in the run's artifact store, which is a directory on disk or in cloud storage, and links them to the run ID.
Logging files, plots and data
Use mlflow.log_artifact() to upload an existing file, and mlflow.log_text() or mlflow.log_figure() to save content directly from Python. The example below logs a confusion matrix plot and the test split used for evaluation.
import matplotlib.pyplot as plt
import mlflow
from sklearn.datasets import load_iris
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import ConfusionMatrixDisplay
from sklearn.model_selection import train_test_split
mlflow.set_tracking_uri("http://127.0.0.1:5000")
mlflow.set_experiment("iris-artifacts")
iris = load_iris(as_frame=True)
X_train, X_test, y_train, y_test = train_test_split(
iris.data, iris.target, test_size=0.2, random_state=42
)
with mlflow.start_run(run_name="logreg-artifacts"):
model = LogisticRegression(max_iter=200).fit(X_train, y_train)
fig, ax = plt.subplots()
ConfusionMatrixDisplay.from_estimator(model, X_test, y_test, ax=ax)
mlflow.log_figure(fig, "plots/confusion_matrix.png")
plt.close(fig)
X_test.assign(label=y_test).to_csv("test_split.csv", index=False)
mlflow.log_artifact("test_split.csv", artifact_path="data")
mlflow.log_text("No feature scaling applied.", "notes/preprocessing.txt")Artifacts can be grouped by passing a subdirectory, such as artifact_path="data". Keep large raw datasets out of artifacts. Log a pointer, such as a file path or hash, and store the data itself in a data versioning system.
> The log_figure() function accepts Matplotlib figures. Call plt.close(fig) afterward so that repeated runs do not hold open figures in memory.
Autologging and its limits
Autologging is a feature that records parameters, training metrics and the fitted model without explicit calls. Enable it with mlflow.sklearn.autolog() before calling fit.
Autologging does not capture everything. It records the estimator's constructor parameters, the training score that scikit-learn reports, and the model. It does not record:
- Test-set metrics you compute yourself, such as the accuracy in the example above.
- Plots, data splits or notes you create.
- Preprocessing steps that sit outside the estimator, such as a scaler you fit separately.
Log those items explicitly with the functions shown earlier.
Comparing runs in the UI
Open the tracking server at http://127.0.0.1:5000 and select an experiment. Tick two or more runs and choose Compare. The comparison view puts parameters, metrics and tags side by side. Open a run to browse its artifacts, and preview images such as the confusion matrix.
Exercise
Why does the example log the test split as an artifact instead of only logging the test accuracy?
Show answer
The split lets you recheck the evaluation later. You can confirm which rows were held out and reproduce the metric. A single number alone does not show which data produced it.