MLflow

3.10.1 · active · verified Sat Mar 28

MLflow is an open-source platform designed to manage the entire machine learning lifecycle, encompassing experiment tracking, reproducible projects, model management, and deployment. The current stable version is 3.10.1, with frequent updates including patch, minor, and major releases that introduce new features and breaking changes. [9, 16]

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to log parameters, metrics, and a scikit-learn model using the MLflow fluent API. It sets up an experiment, trains a logistic regression model on the Iris dataset, logs its hyperparameters and accuracy, infers the model signature, and registers the model in the MLflow Model Registry. To view the results, start the MLflow UI by running `mlflow ui` in your terminal and navigating to `http://localhost:5000` (or `http://127.0.0.1:5000`). [2, 5, 22]

import mlflow
import pandas as pd
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score
from mlflow.models import infer_signature

# Set MLflow tracking URI (optional, defaults to local ./mlruns)
# For a local server, run 'mlflow ui' in your terminal and point to http://127.0.0.1:5000
# os.environ['MLFLOW_TRACKING_URI'] = os.environ.get('MLFLOW_TRACKING_URI', 'http://127.0.0.1:5000')

mlflow.set_experiment("MLflow_Quickstart_Experiment")

# Load the Iris dataset
X, y = datasets.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=42)

# Define model hyperparameters
params = {"solver": "lbfgs", "max_iter": 1000, "multi_class": "auto", "random_state": 8888}

with mlflow.start_run():
    # Log hyperparameters
    mlflow.log_params(params)

    # Train the model
    lr = LogisticRegression(**params)
    lr.fit(X_train, y_train)

    # Make predictions and calculate metrics
    y_pred = lr.predict(X_test)
    accuracy = accuracy_score(y_test, y_pred)
    mlflow.log_metric("accuracy", accuracy)

    # Infer model signature
    predictions = lr.predict(X_train) # Use training data for signature inference
    signature = infer_signature(X_train, predictions)

    # Log the model
    mlflow.sklearn.log_model(
        sk_model=lr,
        artifact_path="logistic_regression_model",
        signature=signature,
        registered_model_name="IrisLogisticRegression"
    )

    print(f"Logged model with accuracy: {accuracy}")
    print(f"View runs in MLflow UI: run 'mlflow ui' in your terminal and navigate to http://127.0.0.1:5000")

view raw JSON →