Implementation:Scikit learn Scikit learn EmpiricalCovariance
| Knowledge Sources | |
|---|---|
| Domains | Machine Learning, Covariance Estimation |
| Last Updated | 2026-02-08 15:00 GMT |
Overview
Concrete tool for maximum likelihood covariance estimation provided by scikit-learn.
Description
This module implements the EmpiricalCovariance estimator, which computes the maximum likelihood estimate of the covariance matrix from data. It serves as the base class for all covariance estimators in scikit-learn. The module also provides utility functions including log_likelihood for computing the sample mean log-likelihood under a covariance model, and empirical_covariance for computing the covariance matrix directly. The estimator supports both centered and non-centered data assumptions.
Usage
Use EmpiricalCovariance when you need a simple maximum likelihood covariance estimate and your data is well-behaved (not corrupted by outliers and with sufficient samples relative to features).
Code Reference
Source Location
- Repository: scikit-learn
- File: sklearn/covariance/_empirical_covariance.py
Signature
class EmpiricalCovariance(BaseEstimator):
"""Maximum likelihood covariance estimator."""
def __init__(self, *, store_precision=True, assume_centered=False):
...
def log_likelihood(emp_cov, precision):
...
def empirical_covariance(X, *, assume_centered=False):
...
Import
from sklearn.covariance import EmpiricalCovariance, empirical_covariance, log_likelihood
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 the precision matrix (default: True) |
| assume_centered | bool | No | Whether data is already centered (default: False) |
Outputs
| Name | Type | Description |
|---|---|---|
| covariance_ | ndarray of shape (n_features, n_features) | Estimated covariance matrix |
| location_ | ndarray of shape (n_features,) | Estimated location (mean) |
| precision_ | ndarray of shape (n_features, n_features) | Estimated precision matrix (inverse covariance) |
Usage Examples
Basic Usage
import numpy as np
from sklearn.covariance import EmpiricalCovariance
# Generate sample data
rng = np.random.RandomState(42)
X = rng.multivariate_normal(mean=[0, 0], cov=[[1, 0.5], [0.5, 1]], size=100)
# Fit the covariance estimator
cov = EmpiricalCovariance().fit(X)
print("Covariance:\n", cov.covariance_)
print("Log-likelihood:", cov.score(X))