Implementation:Scikit learn Scikit learn ShrunkCovariance
| Knowledge Sources | |
|---|---|
| Domains | Machine Learning, Covariance Estimation |
| Last Updated | 2026-02-08 15:00 GMT |
Overview
Concrete tool for regularized covariance estimation using shrinkage methods provided by scikit-learn.
Description
This module implements several shrinkage-based covariance estimators. Shrinkage regularizes the empirical covariance by combining it with a structured target (scaled identity). The module provides ShrunkCovariance with a fixed shrinkage parameter, LedoitWolf with analytically optimal shrinkage, and OAS (Oracle Approximating Shrinkage) with an alternative shrinkage formula. It also includes standalone functions ledoit_wolf, ledoit_wolf_shrinkage, and oas for direct computation without the estimator interface.
Usage
Use shrunk covariance estimators when the number of features is large relative to the number of samples, as the empirical covariance can be poorly conditioned or singular. LedoitWolf is recommended for automatic shrinkage selection.
Code Reference
Source Location
- Repository: scikit-learn
- File: sklearn/covariance/_shrunk_covariance.py
Signature
class ShrunkCovariance(EmpiricalCovariance):
"""Covariance estimator with shrinkage."""
def __init__(self, *, store_precision=True, assume_centered=False, shrinkage=0.1):
...
class LedoitWolf(EmpiricalCovariance):
"""LedoitWolf Estimator."""
def __init__(self, *, store_precision=True, assume_centered=False, block_size=1000):
...
class OAS(EmpiricalCovariance):
"""Oracle Approximating Shrinkage Estimator."""
def __init__(self, *, store_precision=True, assume_centered=False):
...
Import
from sklearn.covariance import ShrunkCovariance, LedoitWolf, OAS
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| X | array-like of shape (n_samples, n_features) | Yes | Training data for covariance estimation |
| store_precision | bool | No | Whether to store precision matrix (default: True) |
| assume_centered | bool | No | Whether data is centered (default: False) |
| shrinkage | float | No | Shrinkage coefficient for ShrunkCovariance (default: 0.1) |
| block_size | int | No | Block size for LedoitWolf block computation (default: 1000) |
Outputs
| Name | Type | Description |
|---|---|---|
| covariance_ | ndarray of shape (n_features, n_features) | Estimated shrunk covariance matrix |
| location_ | ndarray of shape (n_features,) | Estimated location (mean) |
| precision_ | ndarray of shape (n_features, n_features) | Estimated precision matrix |
| shrinkage_ | float | Computed optimal shrinkage coefficient (LedoitWolf, OAS) |
Usage Examples
Basic Usage
import numpy as np
from sklearn.covariance import LedoitWolf
# Generate high-dimensional data
rng = np.random.RandomState(42)
X = rng.randn(50, 100) # n_features > n_samples
# Fit Ledoit-Wolf shrinkage estimator
lw = LedoitWolf().fit(X)
print("Shrinkage coefficient:", lw.shrinkage_)
print("Covariance shape:", lw.covariance_.shape)