Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Scikit learn Scikit learn GraphicalLasso

From Leeroopedia


Knowledge Sources
Domains Machine Learning, Covariance Estimation
Last Updated 2026-02-08 15:00 GMT

Overview

Concrete tool for sparse inverse covariance estimation with L1 penalty provided by scikit-learn.

Description

This module implements the GraphicalLasso and GraphicalLassoCV estimators for sparse inverse covariance (precision) matrix estimation using an L1 penalty. The L1 penalty encourages sparsity in the precision matrix, revealing conditional independence structure between features. GraphicalLassoCV adds cross-validated selection of the regularization parameter alpha. The module uses coordinate descent and LARS solvers internally for optimization.

Usage

Use GraphicalLasso to estimate sparse precision matrices when you want to discover the conditional independence structure in your data, such as in gene regulatory network inference or financial risk modeling.

Code Reference

Source Location

Signature

class GraphicalLasso(BaseGraphicalLasso):
    """Sparse inverse covariance estimation with an l1-penalized estimator."""

    def __init__(
        self,
        alpha=0.01,
        *,
        mode="cd",
        tol=1e-4,
        enet_tol=1e-4,
        max_iter=100,
        verbose=False,
        assume_centered=False,
    ):
        ...

class GraphicalLassoCV(BaseGraphicalLasso):
    """Sparse inverse covariance w/ cross-validated choice of the l1 penalty."""

    def __init__(
        self,
        *,
        alphas=4,
        n_refinements=4,
        cv=None,
        tol=1e-4,
        enet_tol=1e-4,
        max_iter=100,
        mode="cd",
        n_jobs=None,
        verbose=False,
        assume_centered=False,
    ):
        ...

Import

from sklearn.covariance import GraphicalLasso, GraphicalLassoCV

I/O Contract

Inputs

Name Type Required Description
X array-like of shape (n_samples, n_features) Yes Training data for covariance estimation
alpha float No Regularization parameter for L1 penalty (default: 0.01)
mode str No Solver: 'cd' for coordinate descent or 'lars' (default: 'cd')
tol float No Convergence tolerance (default: 1e-4)
max_iter int No Maximum number of iterations (default: 100)
assume_centered bool No Whether data is centered (default: False)

Outputs

Name Type Description
covariance_ ndarray of shape (n_features, n_features) Estimated covariance matrix
precision_ ndarray of shape (n_features, n_features) Estimated sparse precision matrix
alpha_ float Selected alpha value (GraphicalLassoCV only)
n_iter_ int Number of iterations run

Usage Examples

Basic Usage

import numpy as np
from sklearn.covariance import GraphicalLassoCV

# Generate sample data with known structure
rng = np.random.RandomState(42)
n_samples, n_features = 100, 20
X = rng.randn(n_samples, n_features)

# Fit GraphicalLasso with cross-validated alpha
model = GraphicalLassoCV(cv=5).fit(X)
print("Selected alpha:", model.alpha_)
print("Precision matrix sparsity:", (model.precision_ == 0).mean())

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment