8000 [MRG] DOC covariance doctest examples (#12124) · lithuak/scikit-learn@673218c · GitHub
[go: up one dir, main page]

Skip to content

Commit 673218c

Browse files
adrinjalaliamueller
authored andcommitted
[MRG] DOC covariance doctest examples (scikit-learn#12124)
1 parent 9476808 commit 673218c

File tree

4 files changed

+60
-1
lines changed

4 files changed

+60
-1
lines changed

sklearn/covariance/elliptic_envelope.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
# License: BSD 3 clause
44

55
import numpy as np
6-
import scipy as sp
76
import warnings
87
from . import MinCovDet
98
from ..utils.validation import check_is_fitted, check_array

sklearn/covariance/empirical_covariance_.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,34 @@ class EmpiricalCovariance(BaseEstimator):
104104
105105
Attributes
106106
----------
107+
location_ : array-like, shape (n_features,)
108+
Estimated location, i.e. the estimated mean.
109+
107110
covariance_ : 2D ndarray, shape (n_features, n_features)
108111
Estimated covariance matrix
109112
110113
precision_ : 2D ndarray, shape (n_features, n_features)
111114
Estimated pseudo-inverse matrix.
112115
(stored only if store_precision is True)
113116
117+
Examples
118+
--------
119+
>>> import numpy as np
120+
>>> from sklearn.covariance import EmpiricalCovariance
121+
>>> from sklearn.datasets import make_gaussian_quantiles
122+
>>> real_cov = np.array([[.8, .3],
123+
... [.3, .4]])
124+
>>> np.random.seed(0)
125+
>>> X = np.random.multivariate_normal(mean=[0, 0],
126+
... cov=real_cov,
127+
... size=500)
128+
>>> cov = EmpiricalCovariance().fit(X)
129+
>>> cov.covariance_ # doctest: +ELLIPSIS
130+
array([[0.7569..., 0.2818...],
131+
[0.2818..., 0.3928...]])
132+
>>> cov.location_
133+
array([0.0622..., 0.0193...])
134+
114135
"""
115136
def __init__(self, store_precision=True, assume_centered=False):
116137
self.store_precision = store_precision

sklearn/covariance/robust_covariance.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,24 @@ class MinCovDet(EmpiricalCovariance):
581581
Mahalanobis distances of the training set (on which `fit` is called)
582582
observations.
583583
584+
Examples
585+
--------
586+
>>> import numpy as np
587+
>>> from sklearn.covariance import MinCovDet
588+
>>> from sklearn.datasets import make_gaussian_quantiles
589+
>>> real_cov = np.array([[.8, .3],
590+
... [.3, .4]])
591+
>>> np.random.seed(0)
592+
>>> X = np.random.multivariate_normal(mean=[0, 0],
593+
... cov=real_cov,
594+
... size=500)
595+
>>> cov = MinCovDet(random_state=0).fit(X)
596+
>>> cov.covariance_ # doctest: +ELLIPSIS
597+
array([[0.7411..., 0.2535...],
598+
[0.2535..., 0.3053...]])
599+
>>> cov.location_
600+
array([0.0813... , 0.0427...])
601+
584602
References
585603
----------
586604

sklearn/covariance/shrunk_covariance_.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ class ShrunkCovariance(EmpiricalCovariance):
8484
8585
Attributes
8686
----------
87+
location_ : array-like, shape (n_features,)
88+
Estimated location, i.e. the estimated mean.
89+
8790
covariance_ : array-like, shape (n_features, n_features)
8891
Estimated covariance matrix
8992
@@ -95,6 +98,24 @@ class ShrunkCovariance(EmpiricalCovariance):
9598
Coefficient in the convex combination used for the computation
9699
of the shrunk estimate.
97100
101+
Examples
102+
--------
103+
>>> import numpy as np
104+
>>> from sklearn.covariance import ShrunkCovariance
105+
>>> from sklearn.datasets import make_gaussian_quantiles
106+
>>> real_cov = np.array([[.8, .3],
107+
... [.3, .4]])
108+
>>> np.random.seed(0)
109+
>>> X = np.random.multivariate_normal(mean=[0, 0],
110+
... cov=real_cov,
111+
... size=500)
112+
>>> cov = ShrunkCovariance().fit(X)
113+
>>> cov.covariance_ # doctest: +ELLIPSIS
114+
array([[0.7387..., 0.2536...],
115+
[0.2536..., 0.4110...]])
116+
>>> cov.location_
117+
array([0.0622..., 0.0193...])
118+
98119
Notes
99120
-----
100121
The regularized covariance is given by:

0 commit comments

Comments
 (0)
0