Calculate covariance matrices shrunk on the diagonal.
Read more in the User Guide.
Covariance matrices to be shrunk, at least 2D ndarray.
Coefficient in the convex combination used for the computation of the shrunk estimate. Range is [0, 1].
Shrunk covariance matrices.
The regularized (shrunk) covariance is given by:
(1 - shrinkage) * cov + shrinkage * mu * np.identity(n_features)
where mu = trace(cov) / n_features.
>>> import numpy as np
>>> from sklearn.datasets import make_gaussian_quantiles
>>> from sklearn.covariance import empirical_covariance, shrunk_covariance
>>> real_cov = np.array([[.8, .3], [.3, .4]])
>>> rng = np.random.RandomState(0)
>>> X = rng.multivariate_normal(mean=[0, 0], cov=real_cov, size=500)
>>> shrunk_covariance(empirical_covariance(X))
array([[0.73..., 0.25...],
[0.25..., 0.41...]])
© 2007–2025 The scikit-learn developers
Licensed under the 3-clause BSD License.
https://scikit-learn.org/1.6/modules/generated/sklearn.covariance.shrunk_covariance.html