Sub topic 7
Tracking Experiments, Parameters and Metrics
Assume you know basic Python, can train a supervised model, and understand train/test evaluation. This chapter shows how to record training runs so that you can compare results and reproduce a chosen model.
Why track experiments?
An experiment is one attempt to train a model. Each attempt has inputs and results:
- Parameters are choices made before or during training, such as learning rate, regularization strength, or random seed.
- Metrics are measured results, such as accuracy, precision, loss, or area under the ROC curve.
- Artifacts are files produced by a run, such as a trained model, confusion matrix, or feature-importance plot.
Without tracking, a notebook can become a collection of unexplained results. You may know that a model scored 94%, but not which data split, parameter values, or code produced that score.
An experiment tracker stores these details in a run. A run is a record of one execution of training and evaluation.
| Recorded item | Example | Why it matters |
|---|---|---|
| Parameter | `C = 1.0` | Explains the training choice |
| Metric | `accuracy = 0.94` | Enables comparison |
| Artifact | Saved model file | Allows later inspection or deployment |
Record a run with MLflow
MLflow is an experiment-tracking tool. The following example trains logistic regression on the Iris dataset and records its parameters and test accuracy.
Install the dependencies in an environment where you can run the example:
pip install mlflow scikit-learnThen run the script:
import mlflow
from sklearn.datasets import load_iris
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score
from sklearn.model_selection import train_test_split
SEED = 42
C = 1.0
X, y = load_iris(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=SEED
)
with mlflow.start_run():
model = LogisticRegression(C=C, max_iter=200).fit(X_train, y_train)
accuracy = accuracy_score(y_test, model.predict(X_test))
mlflow.log_param("C", C)
mlflow.log_param("seed", SEED)
mlflow.log_metric("accuracy", accuracy)
print(f"test accuracy: {accuracy:.3f}")mlflow.start_run() creates a run and closes it when the with block ends. log_param records a configuration value. log_metric records a numeric result. MLflow stores the run locally by default, so you can compare runs later with its tracking interface.
Change C or SEED, run the script again, and compare the recorded accuracy. A fair comparison changes one important factor at a time and keeps the evaluation procedure consistent.
From runs to decisions
Tracking is useful only when the team agrees on what to record. A practical run usually includes:
- the source-code revision;
- training and validation data versions;
- model and feature settings;
- random seeds;
- evaluation metrics;
- warnings or failed checks.
The best run is not always the one with the highest test score. You may prefer a model with lower latency, fewer false negatives, or a simpler explanation. Tracking gives you the evidence needed to make that trade-off explicit.
Exercise
Why should a random seed be recorded as a parameter?
Show answer
The seed can affect the data split, initialization, or sampling. Recording it helps reproduce the run and explain differences between results.