Skip to content

scriptbuzz/mlflow-local

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

43 Commits
 
 
 
 
 
 

Repository files navigation

Deploy MLflow To Your Local Workstation and Track Your ML Experiments

By Michael Bitar

MLflow logo

Introduction

Welcome to this getting started guide on deploying and utilizing MLflow locally. While this guide was created on a macOS environment, the concepts translate perfectly to Windows and Linux as well.

If you are an MLOps or Machine Learning engineer, you likely spend a vast amount of time experimenting with different parameters, hyperparameters, and preprocessing strategies on your local workstation. Historically, tracking these experiment iterations often involved manual spreadsheets—a tedious, error-prone, and unscalable approach.

The machine learning ecosystem desperately needed a toolchain to bring DevOps-like standard practices to model development, giving rise to MLOps. Among these tools, MLflow stands out as a leading open-source framework designed to organize machine learning development workflows.

Why Local MLflow?

MLflow helps orchestrate the ML lifecycle by:

  • Tracking and comparing ML experiments.
  • Packaging code into reproducible formats.
  • Versioning model metadata.
  • Facilitating easy collaboration and deployment.

While MLflow supports robust centralized, cloud-hosted configurations, you might not always need a remote tracking server. Deploying MLflow locally on your workstation is an incredibly lightweight way to begin. It allows you to reliably log metrics, compare parameters, and save artifacts—guaranteeing you never have to guess which hyperparameter combination produced your best-performing model.

This guide will walk you through a step-by-step local implementation on a laptop. Once you've mastered the basics, you'll be well-prepared to scale into advanced, cloud-hosted deployments for broader team collaboration.

Project Workflow

Here is a high-level overview of the local machine learning pipeline we will be executing:

flowchart TD
    subgraph Data Preparation
        D1[(Wine Quality Dataset)] --> D2[Load CSV via Pandas]
        D2 --> D3[Split Train & Test Data]
    end

    subgraph Model Training
        T1[Define Hyperparameters<br/><code>alpha, l1_ratio</code>] --> T2{Start MLflow Run}
        D3 --> T2
        T2 --> T3[Train ElasticNet Model]
        T3 --> T4[Predict on Test Set]
        T4 --> T5[Evaluate: RMSE, MAE, R2]
    end

    subgraph MLflow Tracking
        T5 --> M1(Log Parameters)
        M1 --> M2(Log Performance Metrics)
        M2 --> M3(Log Model Artifact)
    end
    
    subgraph UI & Analysis
        M3 --> U1[[Launch MLflow UI]]
        U1 --> U2{Analyze Results}
        U2 -->|Tune Params| T1
        U2 -->|Select Best| U3[Register Model]
    end
Loading

Let's Get Started!

1. Prerequisites and Installation

You can use the code snippets in this guide within a Jupyter Notebook or a standard Python script. First, ensure you have an active internet connection to download the required packages. We will install MLflow alongside standard data science libraries.

# Install mlflow and other necessary packages
!pip install mlflow pandas numpy scikit-learn

2. Importing Dependencies

With our environment ready, let's import the necessary libraries. We'll use pandas for data manipulation, scikit-learn for generating our model and calculating metrics, and mlflow to track our experiment runs.

# Import needed support libraries 

import os
import warnings
import sys

import pandas as pd
import numpy as np
from sklearn.metrics import mean_squared_error, mean_absolute_error, r2_score
from sklearn.model_selection import train_test_split
from sklearn.linear_model import ElasticNet
from urllib.parse import urlparse
import mlflow
import mlflow.sklearn
import logging

# configure logging

logging.basicConfig(level=logging.WARN)
logger = logging.getLogger(__name__)

3. Metric Evaluation Setup

To measure our model's performance, we've defined a helper function that evaluates the test predictions against the actual data, outputting three standard regression metrics: Root Mean Squared Error (RMSE), Mean Absolute Error (MAE), and R2 Score.

# define evaluation metrics. In this case I will select RMSE, MAE, R2 scores

def eval_metrics(actual, pred):
    rmse = np.sqrt(mean_squared_error(actual, pred))
    mae = mean_absolute_error(actual, pred)
    r2 = r2_score(actual, pred)
    return rmse, mae, r2

4. Fetching and Preparing the Dataset

Next, we download an open-source Wine Quality dataset. We'll then split this data into training sets (for model learning) and test sets (for model evaluation).

#configure warning logs
warnings.filterwarnings("ignore")

# set random seed to reproduce same results everytime 

np.random.seed(40)

# Load the sample dataset csv file from this URL

csv_url = (
    "http://archive.ics.uci.edu/ml/machine-learning-databases/wine-quality/winequality-red.csv"
)
try:
    data = pd.read_csv(csv_url, sep=";")
except Exception as e:
    logger.exception(
        "Unable to download training & test CSV, check your internet connection. Error: %s", e
    )
    
# Split the data into training and test sets using (0.75, 0.25) split.
train, test = train_test_split(data)

# The predicted column is "quality" which is a scalar from [3, 9]

train_x = train.drop(["quality"], axis=1)
test_x = test.drop(["quality"], axis=1)
train_y = train[["quality"]]
test_y = test[["quality"]]

5. Training the Model and Tracking with MLflow

Now comes the core of the MLOps process. We will train an ElasticNet model, which is a linear regression model combining L1 and L2 regularizations.

Notice the with mlflow.start_run(): context block below. By executing our training within this block, MLflow will automatically monitor the run. We can use methods like log_param for hyperparameters and log_metric for our evaluation scores.

Try running the cell below multiple times, experimenting with different values between 0 and 1 for alpha and l1_ratio. Every execution will automatically be logged to your local MLflow tracking database.

# Hyperparameters for ElasticNet
# We will be using the ElasticNet model in this example. 
# It's a linear regression model with combined L1 and L2 priors as regularizers

alpha =  .35 # change this value for each run. This is used to multiply penalty terms.
l1_ratio =  .45  # change this value for each run. This var is a penalty value.


with mlflow.start_run():
    lr = ElasticNet(alpha=alpha, l1_ratio=l1_ratio, random_state=42)
    lr.fit(train_x, train_y)

    predicted_qualities = lr.predict(test_x)

    (rmse, mae, r2) = eval_metrics(test_y, predicted_qualities)

    print("Elasticnet model (alpha=%f, l1_ratio=%f):" % (alpha, l1_ratio))
    print("  RMSE: %s" % rmse)
    print("  MAE: %s" % mae)
    print("  R2: %s" % r2)
    
    # log vars of interest to be tracked and listed by MLflow
    mlflow.log_param("alpha", alpha)
    mlflow.log_param("l1_ratio", l1_ratio)
    mlflow.log_metric("rmse", rmse)
    mlflow.log_metric("r2", r2)
    mlflow.log_metric("mae", mae)

    tracking_url_type_store = urlparse(mlflow.get_tracking_uri()).scheme

    if tracking_url_type_store != "file":

        # Register the model
        # There are other ways to use the Model Registry, which depends on the use case,
        # please refer to docs for more information:
        #
        # https://mlflow.org/docs/latest/model-registry.html#api-workflow
        mlflow.sklearn.log_model(lr, "model", registered_model_name="ElasticnetWineModel")
    else:
        mlflow.sklearn.log_model(lr, "model")


# for each run, above metrics will be saved in model's local directory where it will be picked up by MLflow
Elasticnet model (alpha=0.350000, l1_ratio=0.450000):
  RMSE: 0.7616514499663437
  MAE: 0.5936841528680933
  R2: 0.17804834226795552

6. Starting the Local MLflow Dashboard

After executing the training cell a few times with varying hyperparameters, it's time to visualize our tracking data! MLflow comes with an intuitive, built-in graphical dashboard.

To launch it, open your terminal, navigate to the directory where you ran your script, and execute the following command:

mlflow ui

Once the server spins up, open your web browser and navigate to:

http://localhost:5000

Analyzing Your Experiments

In the MLflow dashboard (pictured below), you'll find a visual record of every run you executed.

What you can do here:

  • Compare Performace: Sort the grid by metrics like rmse or r2 to instantly identify the best-performing iteration.
  • Inspect Metadata: Click into an experiment to review the discrete combinations of hyperparameters that produced those results.
  • Manage Models: Look at rich artifact logs and register models of interest for future deployment or sharing.

MLflow Dashboard Overview

When you click on one of the experiment runs, you can see more detailed views of its parameters and related artifacts.

Experiment Details View

In the same view, you can track structural artifact metadata such as the registered model schema and its environment dependencies.

Artifact Metadata View

Summary and Next Steps

By leveraging MLflow directly on your local workstation, you can rapidly prototype models and systematically track every change without relying on spreadsheets or disparate notes. Even when working independently, this discipline brings immediate productivity gains and structure to your Machine Learning initiatives.

Throughout this guide we have:

  1. Setup a local MLflow environment.
  2. Embedded metrics and parameter logging into a standard scikit-learn training pipeline.
  3. Used the MLflow UI to compare models and investigate runs visually.

Once you feel comfortable integrating these basics into your regular ML routine, you'll be well-positioned to scale your workflow.

What's Next? My upcoming MLOps guides will cover remote MLflow deployments—diving into the platform's more advanced features (like centralized tracking servers, persistent backend stores, and remote artifact storage) to fully support cloud pipelines and cross-functional team collaboration.

Thank you for following along!

About

Deploy MLflow locally to track model development

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors