Skip to main content
Hello and welcome back. In this lesson, we demonstrate how to run an experiment locally and store its results in an MLflow service configured earlier. We’ll use scikit-learn for this example and learn how to log key metrics, store models, and view experiments in the MLflow UI. Let’s jump into our VS Code editor.

Preparing Your Environment

Before running any experiment, ensure you choose an appropriate use case and data science package. In this example, we use scikit-learn.
If you install scikit-learn in the same terminal session running the MLflow UI, the UI will stop. For example, executing:
will halt the UI. Always open a new terminal for installing additional packages.

Creating the Experiment File

Create a new file named example_mlflow.py in your VS Code editor and paste the code below. This script sets the MLflow tracking URI, creates synthetic regression data, splits it into training and testing sets, and defines a helper function to train models, make predictions, log metrics, and store models in MLflow.

Installing Required Packages

Make sure you have scikit-learn installed. Open a new terminal session and run the following command:
You might see output similar to:
Once installed, run the example file with:
Remember: Your MLflow UI must be running (in a separate terminal) so that the experiment data logs correctly.

Analyzing Experiment Results in MLflow

After the execution, open the MLflow UI and navigate to the experiments section to find a new experiment titled “ML Model Experiment”. Here, you will see three runs corresponding to the following models:
  • Linear Regression
  • Decision Tree Regressor
  • Random Forest Regressor
The image shows an MLflow interface displaying an experiment with three model runs: Random Forest Regressor, Decision Tree Regressor, and Linear Regression, each with details like creation time, duration, and source.
By selecting this experiment, you can view details such as run duration and input data. Use the evaluation section and select all the model runs, then click “Compare” to analyze key metrics side by side.
The image shows an MLflow interface comparing three runs from one experiment, with a focus on a parallel coordinates plot for RMSE metrics. It includes details of each run, such as run ID, name, start and end times, and duration.
This comparison view provides valuable insights into the performance metrics of each model. Another useful visualization is the contour plot, which helps compare metrics like explained variance, mean absolute error (MAE), and mean squared error (MSE) across runs.
The image shows a contour plot from an MLflow experiment comparing three runs, with axes labeled for explained variance, mean absolute error (mae), and mean squared error (mse). Below the plot, there are details of the runs, including IDs, names, start and end times, and durations.
This interface is invaluable for data science experiments, as it simplifies the process of selecting the best model based on performance metrics.

Next Steps

That concludes this lesson. In our next article, we will discuss how to store the model file in the model registry. Thank you, and see you in the next lesson!

Watch Video