MLflow’s tracking component can actually record more than just metrics and parameters – it’s designed to capture the entire context of an ML run, including code versions and even the data itself.

Let’s fire up a basic MLflow tracking server and log our first experiment.

First, make sure you have MLflow installed:

pip install mlflow

Now, start the tracking server. This command spins up a web server that will host your experiment runs.

mlflow ui

This will typically start the server on http://localhost:5000. Open this URL in your browser. You’ll see a blank page because we haven’t logged anything yet.

Next, let’s create a simple Python script to log an experiment. Save this as log_experiment.py:

import mlflow
import os

# Set the MLflow tracking URI to point to the server we started
# If you're running this on the same machine, this is usually sufficient.
# For remote servers, you'd use 'http://your-mlflow-server:5000'
mlflow.set_tracking_uri("http://localhost:5000")

# Define an experiment name. If it doesn't exist, MLflow will create it.
experiment_name = "My First MLflow Experiment"
mlflow.set_experiment(experiment_name)

# Start a new MLflow run
with mlflow.start_run(run_name="Basic Run"):
    # Log parameters
    mlflow.log_param("learning_rate", 0.01)
    mlflow.log_param("epochs", 10)

    # Log metrics
    mlflow.log_metric("accuracy", 0.95)
    mlflow.log_metric("loss", 0.15)

    # Log an artifact (e.g., a file)
    # Create a dummy file to log
    with open("model.txt", "w") as f:
        f.write("This is a dummy model file.")
    mlflow.log_artifact("model.txt")
    os.remove("model.txt") # Clean up the dummy file

    # Log a model (this is a more structured way to save models)
    # For this example, we'll just log a placeholder string.
    # In a real scenario, this would be a trained model object.
    mlflow.python.log_model(lambda: None, "python_model", registered_model_name="MyDummyModel")

print("Experiment logged successfully!")

Now, run this Python script from your terminal:

python log_experiment.py

Go back to your MLflow UI in the browser (http://localhost:5000). You should now see "My First MLflow Experiment" listed. Click on it, and you’ll see "Basic Run" as a sub-item. Clicking on "Basic Run" will show you the logged parameters, metrics, and artifacts.

The real power here is how MLflow structures this information. Each mlflow.start_run() block defines a distinct "run." Within a run, you can log key-value pairs for parameters (mlflow.log_param), numerical values for metrics (mlflow.log_metric), and entire files or directories as artifacts (mlflow.log_artifact). The mlflow.python.log_model is a specialized function for saving models in a format MLflow understands, allowing for easier deployment later.

The tracking server, by default, stores all this data in a local mlruns directory. This directory is what gets served by the mlflow ui command. You can configure MLflow to use remote storage like S3, Azure Blob Storage, or a database for more robust deployments.

Crucially, MLflow automatically records information about the environment, such as the Git commit hash if your code is in a Git repository. This provides reproducibility, allowing you to pinpoint the exact code that produced a specific result.

What most people miss is that mlflow.log_param and mlflow.log_metric are designed for numerical and string values, respectively. While you can log other types, MLflow’s UI and backend are optimized for these. For complex data structures, it’s often better to serialize them to JSON or another string format before logging, or to log them as artifacts.

The next step is to explore how to compare different runs side-by-side in the MLflow UI, which is fundamental for model selection.

Want structured learning?

Take the full Mlflow course →