Skip to content

[MRG] DOC covariance doctest examples #12124

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Sep 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion sklearn/covariance/elliptic_envelope.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
# License: BSD 3 clause

import numpy as np
import scipy as sp
import warnings
from . import MinCovDet
from ..utils.validation import check_is_fitted, check_array
Expand Down
21 changes: 21 additions & 0 deletions sklearn/covariance/empirical_covariance_.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,34 @@ class EmpiricalCovariance(BaseEstimator):

Attributes
----------
location_ : array-like, shape (n_features,)
Estimated location, i.e. the estimated mean.

covariance_ : 2D ndarray, shape (n_features, n_features)
Estimated covariance matrix

precision_ : 2D ndarray, shape (n_features, n_features)
Estimated pseudo-inverse matrix.
(stored only if store_precision is True)

Examples
--------
>>> import numpy as np
>>> from sklearn.covariance import EmpiricalCovariance
>>> from sklearn.datasets import make_gaussian_quantiles
>>> real_cov = np.array([[.8, .3],
... [.3, .4]])
>>> np.random.seed(0)
>>> X = np.random.multivariate_normal(mean=[0, 0],
... cov=real_cov,
... size=500)
>>> cov = EmpiricalCovariance().fit(X)
>>> cov.covariance_ # doctest: +ELLIPSIS
array([[0.7569..., 0.2818...],
[0.2818..., 0.3928...]])
>>> cov.location_
array([0.0622..., 0.0193...])

"""
def __init__(self, store_precision=True, assume_centered=False):
self.store_precision = store_precision
Expand Down
18 changes: 18 additions & 0 deletions sklearn/covariance/robust_covariance.py
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,24 @@ class MinCovDet(EmpiricalCovariance):
Mahalanobis distances of the training set (on which `fit` is called)
observations.

Examples
--------
>>> import numpy as np
>>> from sklearn.covariance import MinCovDet
>>> from sklearn.datasets import make_gaussian_quantiles
>>> real_cov = np.array([[.8, .3],
... [.3, .4]])
>>> np.random.seed(0)
>>> X = np.random.multivariate_normal(mean=[0, 0],
... cov=real_cov,
... size=500)
>>> cov = MinCovDet(random_state=0).fit(X)
>>> cov.covariance_ # doctest: +ELLIPSIS
array([[0.7411..., 0.2535...],
[0.2535..., 0.3053...]])
>>> cov.location_
array([0.0813... , 0.0427...])

References
----------

Expand Down
21 changes: 21 additions & 0 deletions sklearn/covariance/shrunk_covariance_.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ class ShrunkCovariance(EmpiricalCovariance):

Attributes
----------
location_ : array-like, shape (n_features,)
Estimated location, i.e. the estimated mean.

covariance_ : array-like, shape (n_features, n_features)
Estimated covariance matrix

Expand All @@ -95,6 +98,24 @@ class ShrunkCovariance(EmpiricalCovariance):
Coefficient in the convex combination used for the computation
of the shrunk estimate.

Examples
--------
>>> import numpy as np
>>> from sklearn.covariance import ShrunkCovariance
>>> from sklearn.datasets import make_gaussian_quantiles
>>> real_cov = np.array([[.8, .3],
... [.3, .4]])
>>> np.random.seed(0)
>>> X = np.random.multivariate_normal(mean=[0, 0],
... cov=real_cov,
... size=500)
>>> cov = ShrunkCovariance().fit(X)
>>> cov.covariance_ # doctest: +ELLIPSIS
array([[0.7387..., 0.2536...],
[0.2536..., 0.4110...]])
>>> cov.location_
array([0.0622..., 0.0193...])

Notes
-----
The regularized covariance is given by:
Expand Down