Skip to content

Latest commit

 

History

History
95 lines (64 loc) · 3.73 KB

File metadata and controls

95 lines (64 loc) · 3.73 KB

MLflow Integration Guide

1. Introduction to MLflow

MLflow is an open-source platform designed to manage the end-to-end machine learning (ML) lifecycle. Developed by Databricks, it addresses key challenges in ML development, including experiment tracking, reproducibility, and model deployment. MLflow provides a set of tools that help data scientists and machine learning engineers streamline their workflows and collaborate more effectively.

Key Components of MLflow:

  • MLflow Tracking: Allows you to record and query experiments, including code, data, configuration, and results. It enables you to track parameters, metrics, and artifacts (e.g., models, plots) for each run.
  • MLflow Projects: Provides a standard format for packaging ML code in a reusable and reproducible way. This enables sharing of ML code across different platforms and ensures consistent execution.
  • MLflow Models: Offers a convention for packaging ML models in a standard format that can be used by various downstream tools (e.g., for real-time serving or batch inference). It supports different flavors of models (e.g., scikit-learn, TensorFlow, PyTorch).
  • MLflow Model Registry: A centralized model store that allows you to manage the lifecycle of MLflow Models, including versioning, stage transitions (e.g., Staging, Production), and annotations.

2. Basic Installation Process

MLflow can be easily installed using pip. Before installation, it's recommended to create a virtual environment to manage dependencies.

Step 1: Create a Virtual Environment (Recommended)

python3 -m venv mlflow-env
source mlflow-env/bin/activate  # On Linux/macOS
# For Windows: .\mlflow-env\Scripts\activate

Step 2: Install MLflow

Once your virtual environment is activated, you can install MLflow using pip:

pip install mlflow

Step 3: Verify Installation

To verify that MLflow has been installed correctly, you can run the following command:

mlflow --version

This should output the installed MLflow version.

Step 4: Start the MLflow UI (Optional)

MLflow comes with a web-based user interface that allows you to visualize, search, and compare ML runs. To start the UI, navigate to your project directory and run:

mlflow ui

By default, the MLflow UI will be accessible at http://localhost:5000. You can then navigate to this URL in your web browser to view your experiments.

Step 5: Basic Usage Example (Python)

Here's a simple Python example demonstrating how to log parameters and metrics using MLflow Tracking:

import mlflow
import mlflow.sklearn
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score

# Enable autologging for scikit-learn
mlflow.sklearn.autolog()

# Start an MLflow run
with mlflow.start_run():
    # Log parameters
    mlflow.log_param("learning_rate", 0.01)
    mlflow.log_param("n_estimators", 100)

    # Train a simple model
    model = LogisticRegression(solver="liblinear", random_state=42)
    # In a real scenario, you would train on actual data
    # For demonstration, we'll just simulate a training process
    # model.fit(X_train, y_train)

    # Simulate predictions and metrics
    y_true = [0, 1, 0, 1, 0]
    y_pred = [0, 1, 1, 1, 0]
    accuracy = accuracy_score(y_true, y_pred)

    # Log metrics
    mlflow.log_metric("accuracy", accuracy)

    # Log the model (optional, autologging might handle this)
    # mlflow.sklearn.log_model(model, "logistic_regression_model")

    print(f"MLflow Run ID: {mlflow.active_run().info.run_id}")
    print(f"Accuracy: {accuracy}")

After running this script, you can open the MLflow UI (mlflow ui) to see the logged experiment run, including parameters, metrics, and potentially the model artifact.