From ad9f350f729b2ed194ee04be97d1c30cc7e3bf33 Mon Sep 17 00:00:00 2001 From: mghah <31044045+mghah@users.noreply.github.com> Date: Sun, 12 Jan 2020 14:35:46 -0800 Subject: [PATCH 1/5] docstring improvement --- sklearn/covariance/_elliptic_envelope.py | 18 ++--- sklearn/covariance/_empirical_covariance.py | 16 ++--- sklearn/covariance/_graph_lasso.py | 76 ++++++++++----------- sklearn/covariance/_robust_covariance.py | 24 +++---- sklearn/covariance/_shrunk_covariance.py | 42 ++++++------ 5 files changed, 88 insertions(+), 88 deletions(-) diff --git a/sklearn/covariance/_elliptic_envelope.py b/sklearn/covariance/_elliptic_envelope.py index 5ee4cdeeef96d..b0a5c7bcb1fea 100644 --- a/sklearn/covariance/_elliptic_envelope.py +++ b/sklearn/covariance/_elliptic_envelope.py @@ -16,10 +16,10 @@ class EllipticEnvelope(OutlierMixin, MinCovDet): Parameters ---------- - store_precision : boolean, optional (default=True) + store_precision : bool, default=True Specify if the estimated precision is stored. - assume_centered : boolean, optional (default=False) + assume_centered : bool, default=False If True, the support of robust location and covariance estimates is computed, and a covariance estimate is recomputed from it, without centering the data. @@ -28,16 +28,16 @@ class EllipticEnvelope(OutlierMixin, MinCovDet): If False, the robust location and covariance are directly computed with the FastMCD algorithm without additional treatment. - support_fraction : float in (0., 1.), optional (default=None) + support_fraction : float in (0, 1), default=None The proportion of points to be included in the support of the raw MCD estimate. If None, the minimum value of support_fraction will be used within the algorithm: `[n_sample + n_features + 1] / 2`. - contamination : float in (0., 0.5), optional (default=0.1) + contamination : float in (0, 0.5), default=0.1 The amount of contamination of the data set, i.e. the proportion of outliers in the data set. - random_state : int, RandomState instance or None, optional (default=None) + random_state : int or RandomState instance, default=None The seed of the pseudo random number generator to use when shuffling the data. If int, random_state is the seed used by the random number generator; If RandomState instance, random_state is the random number @@ -46,17 +46,17 @@ class EllipticEnvelope(OutlierMixin, MinCovDet): Attributes ---------- - location_ : array-like, shape (n_features,) + location_ : array-like of shape (n_features,) Estimated robust location - covariance_ : array-like, shape (n_features, n_features) + covariance_ : array-like of shape (n_features, n_features) Estimated robust covariance matrix - precision_ : array-like, shape (n_features, n_features) + precision_ : array-like of shape (n_features, n_features) Estimated pseudo inverse matrix. (stored only if store_precision is True) - support_ : array-like, shape (n_samples,) + support_ : array-like of shape (n_samples,) A mask of the observations that have been used to compute the robust estimates of location and shape. diff --git a/sklearn/covariance/_empirical_covariance.py b/sklearn/covariance/_empirical_covariance.py index 3a76abb326a26..00058fb6de9a8 100644 --- a/sklearn/covariance/_empirical_covariance.py +++ b/sklearn/covariance/_empirical_covariance.py @@ -52,10 +52,10 @@ def empirical_covariance(X, assume_centered=False): Parameters ---------- - X : ndarray, shape (n_samples, n_features) + X : ndarray of shape (n_samples, n_features) Data from which to compute the covariance estimate - assume_centered : boolean + assume_centered : bool, default=False If True, data will not be centered before computation. Useful when working with data whose mean is almost, but not exactly zero. @@ -63,7 +63,7 @@ def empirical_covariance(X, assume_centered=False): Returns ------- - covariance : 2D ndarray, shape (n_features, n_features) + covariance : ndarray of shape (n_features, n_features) Empirical covariance (Maximum Likelihood Estimator). """ @@ -92,10 +92,10 @@ class EmpiricalCovariance(BaseEstimator): Parameters ---------- - store_precision : bool + store_precision : bool, default=True Specifies if the estimated precision is stored. - assume_centered : bool + assume_centered : bool, default=False If True, data are not centered before computation. Useful when working with data whose mean is almost, but not exactly zero. @@ -103,13 +103,13 @@ class EmpiricalCovariance(BaseEstimator): Attributes ---------- - location_ : array-like, shape (n_features,) + location_ : array-like of shape (n_features,) Estimated location, i.e. the estimated mean. - covariance_ : 2D ndarray, shape (n_features, n_features) + covariance_ : ndarray of shape (n_features, n_features) Estimated covariance matrix - precision_ : 2D ndarray, shape (n_features, n_features) + precision_ : ndarray of shape (n_features, n_features) Estimated pseudo-inverse matrix. (stored only if store_precision is True) diff --git a/sklearn/covariance/_graph_lasso.py b/sklearn/covariance/_graph_lasso.py index c282d40c826bd..a59a8b37baada 100644 --- a/sklearn/covariance/_graph_lasso.py +++ b/sklearn/covariance/_graph_lasso.py @@ -86,56 +86,56 @@ def graphical_lasso(emp_cov, alpha, cov_init=None, mode='cd', tol=1e-4, Parameters ---------- - emp_cov : 2D ndarray, shape (n_features, n_features) + emp_cov : 2D ndarray of shape (n_features, n_features) Empirical covariance from which to compute the covariance estimate. - alpha : positive float + alpha : float in (0, inf] The regularization parameter: the higher alpha, the more regularization, the sparser the inverse covariance. - cov_init : 2D array (n_features, n_features), optional + cov_init : array of shape (n_features, n_features), default=None The initial guess for the covariance. - mode : {'cd', 'lars'} + mode : {'cd', 'lars'}, default='cd' The Lasso solver to use: coordinate descent or LARS. Use LARS for very sparse underlying graphs, where p > n. Elsewhere prefer cd which is more numerically stable. - tol : positive float, optional + tol : float in (0, inf], default=1e-4 The tolerance to declare convergence: if the dual gap goes below this value, iterations are stopped. - enet_tol : positive float, optional + enet_tol : float in (0, inf], default=1e-4 The tolerance for the elastic net solver used to calculate the descent direction. This parameter controls the accuracy of the search direction for a given column update, not of the overall parameter estimate. Only used for mode='cd'. - max_iter : integer, optional + max_iter : int, default=100 The maximum number of iterations. - verbose : boolean, optional + verbose : bool, default=False If verbose is True, the objective function and dual gap are printed at each iteration. - return_costs : boolean, optional + return_costs : bool, default=Flase If return_costs is True, the objective function and dual gap at each iteration are returned. - eps : float, optional + eps : float, default=`np.finfo(np.float64).eps` The machine-precision regularization in the computation of the Cholesky diagonal factors. Increase this for very ill-conditioned systems. - return_n_iter : bool, optional + return_n_iter : bool, default=False Whether or not to return the number of iterations. Returns ------- - covariance : 2D ndarray, shape (n_features, n_features) + covariance : ndarray of shape (n_features, n_features) The estimated covariance matrix. - precision : 2D ndarray, shape (n_features, n_features) + precision : ndarray of shape (n_features, n_features) The estimated (sparse) precision matrix. costs : list of (objective, dual_gap) pairs @@ -285,33 +285,33 @@ class GraphicalLasso(EmpiricalCovariance): Parameters ---------- - alpha : positive float, default 0.01 + alpha : float in (0, inf], default=0.01 The regularization parameter: the higher alpha, the more regularization, the sparser the inverse covariance. - mode : {'cd', 'lars'}, default 'cd' + mode : {'cd', 'lars'}, default='cd' The Lasso solver to use: coordinate descent or LARS. Use LARS for very sparse underlying graphs, where p > n. Elsewhere prefer cd which is more numerically stable. - tol : positive float, default 1e-4 + tol : float in (0, inf], default=1e-4 The tolerance to declare convergence: if the dual gap goes below this value, iterations are stopped. - enet_tol : positive float, optional + enet_tol : float in (0, inf], default=1e-4 The tolerance for the elastic net solver used to calculate the descent direction. This parameter controls the accuracy of the search direction for a given column update, not of the overall parameter estimate. Only used for mode='cd'. - max_iter : integer, default 100 + max_iter : int, default=100 The maximum number of iterations. - verbose : boolean, default False + verbose : bool, default=False If verbose is True, the objective function and dual gap are plotted at each iteration. - assume_centered : boolean, default False + assume_centered : bool, default=False If True, data are not centered before computation. Useful when working with data whose mean is almost, but not exactly zero. @@ -319,13 +319,13 @@ class GraphicalLasso(EmpiricalCovariance): Attributes ---------- - location_ : array-like, shape (n_features,) + location_ : array-like of shape (n_features,) Estimated location, i.e. the estimated mean. - covariance_ : array-like, shape (n_features, n_features) + covariance_ : array-like of shape (n_features, n_features) Estimated covariance matrix - precision_ : array-like, shape (n_features, n_features) + precision_ : array-like of shape (n_features, n_features) Estimated pseudo inverse matrix. n_iter_ : int @@ -500,17 +500,17 @@ class GraphicalLassoCV(GraphicalLasso): Parameters ---------- - alphas : integer, or list positive float, optional + alphas : int or list of floats in (0, inf], default=4 If an integer is given, it fixes the number of points on the grids of alpha to be used. If a list is given, it gives the grid to be used. See the notes in the class docstring for more details. - n_refinements : strictly positive integer + n_refinements : int in (0, inf], default=4 The number of times the grid is refined. Not used if explicit values of alphas are passed. - cv : int, cross-validation generator or an iterable, optional + cv : int, cross-validation generator or iterable, default=None Determines the cross-validation splitting strategy. Possible inputs for cv are: @@ -527,36 +527,36 @@ class GraphicalLassoCV(GraphicalLasso): .. versionchanged:: 0.20 ``cv`` default value if None changed from 3-fold to 5-fold. - tol : positive float, optional + tol : float in (0, inf], default=1e-4 The tolerance to declare convergence: if the dual gap goes below this value, iterations are stopped. - enet_tol : positive float, optional + enet_tol : float in (0, inf], default=1e-4 The tolerance for the elastic net solver used to calculate the descent direction. This parameter controls the accuracy of the search direction for a given column update, not of the overall parameter estimate. Only used for mode='cd'. - max_iter : integer, optional + max_iter : int, default=100 Maximum number of iterations. - mode : {'cd', 'lars'} + mode : {'cd', 'lars'}, default='cd' The Lasso solver to use: coordinate descent or LARS. Use LARS for very sparse underlying graphs, where number of features is greater than number of samples. Elsewhere prefer cd which is more numerically stable. - n_jobs : int or None, optional (default=None) + n_jobs : int, default=None number of jobs to run in parallel. ``None`` means 1 unless in a :obj:`joblib.parallel_backend` context. ``-1`` means using all processors. See :term:`Glossary ` for more details. - verbose : boolean, optional + verbose : bool, default=False If verbose is True, the objective function and duality gap are printed at each iteration. - assume_centered : boolean + assume_centered : bool, default=False If True, data are not centered before computation. Useful when working with data whose mean is almost, but not exactly zero. @@ -564,22 +564,22 @@ class GraphicalLassoCV(GraphicalLasso): Attributes ---------- - location_ : array-like, shape (n_features,) + location_ : array-like of shape (n_features,) Estimated location, i.e. the estimated mean. - covariance_ : numpy.ndarray, shape (n_features, n_features) + covariance_ : ndarray of shape (n_features, n_features) Estimated covariance matrix. - precision_ : numpy.ndarray, shape (n_features, n_features) + precision_ : ndarray of shape (n_features, n_features) Estimated precision matrix (inverse covariance). alpha_ : float Penalization parameter selected. - cv_alphas_ : list of float + cv_alphas_ : list of floats All penalization parameters explored. - grid_scores_ : 2D numpy.ndarray (n_alphas, n_folds) + grid_scores_ : ndarray of shape (n_alphas, n_folds) Log-likelihood score on left-out data across folds. n_iter_ : int diff --git a/sklearn/covariance/_robust_covariance.py b/sklearn/covariance/_robust_covariance.py index 9c59f204a7636..b921f501c10ef 100644 --- a/sklearn/covariance/_robust_covariance.py +++ b/sklearn/covariance/_robust_covariance.py @@ -524,10 +524,10 @@ class MinCovDet(EmpiricalCovariance): Parameters ---------- - store_precision : bool + store_precision : bool, default=True Specify if the estimated precision is stored. - assume_centered : bool + assume_centered : bool, default=False If True, the support of the robust location and the covariance estimates is computed, and a covariance estimate is recomputed from it, without centering the data. @@ -536,13 +536,13 @@ class MinCovDet(EmpiricalCovariance): If False, the robust location and covariance are directly computed with the FastMCD algorithm without additional treatment. - support_fraction : float, 0 < support_fraction < 1 + support_fraction : float in (0, 1), default=None The proportion of points to be included in the support of the raw MCD estimate. Default is None, which implies that the minimum value of support_fraction will be used within the algorithm: [n_sample + n_features + 1] / 2 - random_state : int, RandomState instance or None, optional (default=None) + random_state : int or RandomState instance, default=None If int, random_state is the seed used by the random number generator; If RandomState instance, random_state is the random number generator; If None, the random number generator is the RandomState instance used @@ -550,32 +550,32 @@ class MinCovDet(EmpiricalCovariance): Attributes ---------- - raw_location_ : array-like, shape (n_features,) + raw_location_ : array-like of shape (n_features,) The raw robust estimated location before correction and re-weighting. - raw_covariance_ : array-like, shape (n_features, n_features) + raw_covariance_ : array-like of shape (n_features, n_features) The raw robust estimated covariance before correction and re-weighting. - raw_support_ : array-like, shape (n_samples,) + raw_support_ : array-like of shape (n_samples,) A mask of the observations that have been used to compute the raw robust estimates of location and shape, before correction and re-weighting. - location_ : array-like, shape (n_features,) + location_ : array-like of shape (n_features,) Estimated robust location - covariance_ : array-like, shape (n_features, n_features) + covariance_ : array-like of shape (n_features, n_features) Estimated robust covariance matrix - precision_ : array-like, shape (n_features, n_features) + precision_ : array-like of shape (n_features, n_features) Estimated pseudo inverse matrix. (stored only if store_precision is True) - support_ : array-like, shape (n_samples,) + support_ : array-like of shape (n_samples,) A mask of the observations that have been used to compute the robust estimates of location and shape. - dist_ : array-like, shape (n_samples,) + dist_ : array-like of shape (n_samples,) Mahalanobis distances of the training set (on which :meth:`fit` is called) observations. diff --git a/sklearn/covariance/_shrunk_covariance.py b/sklearn/covariance/_shrunk_covariance.py index 9b01d3e7a9041..795f57ab4d100 100644 --- a/sklearn/covariance/_shrunk_covariance.py +++ b/sklearn/covariance/_shrunk_covariance.py @@ -29,10 +29,10 @@ def shrunk_covariance(emp_cov, shrinkage=0.1): Parameters ---------- - emp_cov : array-like, shape (n_features, n_features) + emp_cov : array-like of shape (n_features, n_features) Covariance matrix to be shrunk - shrinkage : float, 0 <= shrinkage <= 1 + shrinkage : float in [0,1], default=0.1 Coefficient in the convex combination used for the computation of the shrunk estimate. @@ -67,28 +67,28 @@ class ShrunkCovariance(EmpiricalCovariance): Parameters ---------- - store_precision : boolean, default True + store_precision : bool, default=True Specify if the estimated precision is stored - assume_centered : boolean, default False + assume_centered : bool, default=False If True, data will not be centered before computation. Useful when working with data whose mean is almost, but not exactly zero. If False, data will be centered before computation. - shrinkage : float, 0 <= shrinkage <= 1, default 0.1 + shrinkage : float in [0, 1], default=0.1 Coefficient in the convex combination used for the computation of the shrunk estimate. Attributes ---------- - location_ : array-like, shape (n_features,) + location_ : array-like of shape (n_features,) Estimated location, i.e. the estimated mean. - covariance_ : array-like, shape (n_features, n_features) + covariance_ : array-like of shape (n_features, n_features) Estimated covariance matrix - precision_ : array-like, shape (n_features, n_features) + precision_ : array-like of shape (n_features, n_features) Estimated pseudo inverse matrix. (stored only if store_precision is True) @@ -262,10 +262,10 @@ def ledoit_wolf(X, assume_centered=False, block_size=1000): Parameters ---------- - X : array-like, shape (n_samples, n_features) + X : array-like of shape (n_samples, n_features) Data from which to compute the covariance estimate - assume_centered : boolean, default=False + assume_centered : bool, default=False If True, data will not be centered before computation. Useful to work with data whose mean is significantly equal to zero but is not exactly zero. @@ -277,7 +277,7 @@ def ledoit_wolf(X, assume_centered=False, block_size=1000): Returns ------- - shrunk_cov : array-like, shape (n_features, n_features) + shrunk_cov : array-like of shape (n_features, n_features) Shrunk covariance. shrinkage : float @@ -347,17 +347,17 @@ class LedoitWolf(EmpiricalCovariance): Attributes ---------- - location_ : array-like, shape (n_features,) + location_ : array-like of shape (n_features,) Estimated location, i.e. the estimated mean. - covariance_ : array-like, shape (n_features, n_features) + covariance_ : array-like of shape (n_features, n_features) Estimated covariance matrix - precision_ : array-like, shape (n_features, n_features) + precision_ : array-like of shape (n_features, n_features) Estimated pseudo inverse matrix. (stored only if store_precision is True) - shrinkage_ : float, 0 <= shrinkage <= 1 + shrinkage_ : float in [0,1] Coefficient in the convex combination used for the computation of the shrunk estimate. @@ -440,10 +440,10 @@ def oas(X, assume_centered=False): Parameters ---------- - X : array-like, shape (n_samples, n_features) + X : array-like of shape (n_samples, n_features) Data from which to compute the covariance estimate. - assume_centered : boolean + assume_centered : bool, default=False If True, data will not be centered before computation. Useful to work with data whose mean is significantly equal to zero but is not exactly zero. @@ -451,7 +451,7 @@ def oas(X, assume_centered=False): Returns ------- - shrunk_cov : array-like, shape (n_features, n_features) + shrunk_cov : array-like of shape (n_features, n_features) Shrunk covariance. shrinkage : float @@ -528,14 +528,14 @@ class OAS(EmpiricalCovariance): Attributes ---------- - covariance_ : array-like, shape (n_features, n_features) + covariance_ : array-like of shape (n_features, n_features) Estimated covariance matrix. - precision_ : array-like, shape (n_features, n_features) + precision_ : array-like of shape (n_features, n_features) Estimated pseudo inverse matrix. (stored only if store_precision is True) - shrinkage_ : float, 0 <= shrinkage <= 1 + shrinkage_ : float in [0, 1] coefficient in the convex combination used for the computation of the shrunk estimate. From fa67ed1490dab34d6725e64ce218901ee9bd3044 Mon Sep 17 00:00:00 2001 From: mghah <31044045+mghah@users.noreply.github.com> Date: Tue, 14 Jan 2020 13:16:12 -0800 Subject: [PATCH 2/5] Update _shrunk_covariance.py --- sklearn/covariance/_shrunk_covariance.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sklearn/covariance/_shrunk_covariance.py b/sklearn/covariance/_shrunk_covariance.py index 795f57ab4d100..cddeb4209bac0 100644 --- a/sklearn/covariance/_shrunk_covariance.py +++ b/sklearn/covariance/_shrunk_covariance.py @@ -32,9 +32,9 @@ def shrunk_covariance(emp_cov, shrinkage=0.1): emp_cov : array-like of shape (n_features, n_features) Covariance matrix to be shrunk - shrinkage : float in [0,1], default=0.1 + shrinkage : float, default=0.1 Coefficient in the convex combination used for the computation - of the shrunk estimate. + of the shrunk estimate. Range is [0, 1]. Returns ------- From 0d6b69e4e61a018a2ab4706234e70864d3c2882a Mon Sep 17 00:00:00 2001 From: mghah <31044045+mghah@users.noreply.github.com> Date: Tue, 14 Jan 2020 13:29:57 -0800 Subject: [PATCH 3/5] changes based on feedback --- sklearn/covariance/_elliptic_envelope.py | 7 ++-- sklearn/covariance/_graph_lasso.py | 42 +++++++++++++----------- sklearn/covariance/_robust_covariance.py | 3 +- sklearn/covariance/_shrunk_covariance.py | 8 ++--- 4 files changed, 32 insertions(+), 28 deletions(-) diff --git a/sklearn/covariance/_elliptic_envelope.py b/sklearn/covariance/_elliptic_envelope.py index b0a5c7bcb1fea..c36985801a481 100644 --- a/sklearn/covariance/_elliptic_envelope.py +++ b/sklearn/covariance/_elliptic_envelope.py @@ -28,14 +28,15 @@ class EllipticEnvelope(OutlierMixin, MinCovDet): If False, the robust location and covariance are directly computed with the FastMCD algorithm without additional treatment. - support_fraction : float in (0, 1), default=None + support_fraction : float, default=None The proportion of points to be included in the support of the raw MCD estimate. If None, the minimum value of support_fraction will be used within the algorithm: `[n_sample + n_features + 1] / 2`. + Range is (0, 1). - contamination : float in (0, 0.5), default=0.1 + contamination : float, default=0.1 The amount of contamination of the data set, i.e. the proportion - of outliers in the data set. + of outliers in the data set. Range is (0, 0.5). random_state : int or RandomState instance, default=None The seed of the pseudo random number generator to use when shuffling diff --git a/sklearn/covariance/_graph_lasso.py b/sklearn/covariance/_graph_lasso.py index a59a8b37baada..206ab85ad8977 100644 --- a/sklearn/covariance/_graph_lasso.py +++ b/sklearn/covariance/_graph_lasso.py @@ -89,9 +89,10 @@ def graphical_lasso(emp_cov, alpha, cov_init=None, mode='cd', tol=1e-4, emp_cov : 2D ndarray of shape (n_features, n_features) Empirical covariance from which to compute the covariance estimate. - alpha : float in (0, inf] + alpha : float The regularization parameter: the higher alpha, the more regularization, the sparser the inverse covariance. + Range is (0, inf]. cov_init : array of shape (n_features, n_features), default=None The initial guess for the covariance. @@ -101,15 +102,15 @@ def graphical_lasso(emp_cov, alpha, cov_init=None, mode='cd', tol=1e-4, very sparse underlying graphs, where p > n. Elsewhere prefer cd which is more numerically stable. - tol : float in (0, inf], default=1e-4 + tol : float, default=1e-4 The tolerance to declare convergence: if the dual gap goes below - this value, iterations are stopped. + this value, iterations are stopped. Range is (0, inf]. - enet_tol : float in (0, inf], default=1e-4 + enet_tol : float, default=1e-4 The tolerance for the elastic net solver used to calculate the descent direction. This parameter controls the accuracy of the search direction for a given column update, not of the overall parameter estimate. Only - used for mode='cd'. + used for mode='cd'. Range is (0, inf]. max_iter : int, default=100 The maximum number of iterations. @@ -122,10 +123,10 @@ def graphical_lasso(emp_cov, alpha, cov_init=None, mode='cd', tol=1e-4, If return_costs is True, the objective function and dual gap at each iteration are returned. - eps : float, default=`np.finfo(np.float64).eps` + eps : float, default=eps The machine-precision regularization in the computation of the Cholesky diagonal factors. Increase this for very ill-conditioned - systems. + systems. Default is `np.finfo(np.float64).eps`. return_n_iter : bool, default=False Whether or not to return the number of iterations. @@ -285,24 +286,25 @@ class GraphicalLasso(EmpiricalCovariance): Parameters ---------- - alpha : float in (0, inf], default=0.01 + alpha : float, default=0.01 The regularization parameter: the higher alpha, the more regularization, the sparser the inverse covariance. + Range is (0, inf]. mode : {'cd', 'lars'}, default='cd' The Lasso solver to use: coordinate descent or LARS. Use LARS for very sparse underlying graphs, where p > n. Elsewhere prefer cd which is more numerically stable. - tol : float in (0, inf], default=1e-4 + tol : float, default=1e-4 The tolerance to declare convergence: if the dual gap goes below - this value, iterations are stopped. + this value, iterations are stopped. Range is (0, inf]. - enet_tol : float in (0, inf], default=1e-4 + enet_tol : float, default=1e-4 The tolerance for the elastic net solver used to calculate the descent direction. This parameter controls the accuracy of the search direction for a given column update, not of the overall parameter estimate. Only - used for mode='cd'. + used for mode='cd'. Range is (0, inf]. max_iter : int, default=100 The maximum number of iterations. @@ -500,15 +502,15 @@ class GraphicalLassoCV(GraphicalLasso): Parameters ---------- - alphas : int or list of floats in (0, inf], default=4 + alphas : int or list of floats, default=4 If an integer is given, it fixes the number of points on the grids of alpha to be used. If a list is given, it gives the grid to be used. See the notes in the class docstring for - more details. + more details. Range is (0, inf] when floats given. - n_refinements : int in (0, inf], default=4 + n_refinements : int, default=4 The number of times the grid is refined. Not used if explicit - values of alphas are passed. + values of alphas are passed. Range is [1, inf). cv : int, cross-validation generator or iterable, default=None Determines the cross-validation splitting strategy. @@ -527,15 +529,15 @@ class GraphicalLassoCV(GraphicalLasso): .. versionchanged:: 0.20 ``cv`` default value if None changed from 3-fold to 5-fold. - tol : float in (0, inf], default=1e-4 + tol : float, default=1e-4 The tolerance to declare convergence: if the dual gap goes below - this value, iterations are stopped. + this value, iterations are stopped. Range is (0, inf]. - enet_tol : float in (0, inf], default=1e-4 + enet_tol : float, default=1e-4 The tolerance for the elastic net solver used to calculate the descent direction. This parameter controls the accuracy of the search direction for a given column update, not of the overall parameter estimate. Only - used for mode='cd'. + used for mode='cd'. Range is (0, inf]. max_iter : int, default=100 Maximum number of iterations. diff --git a/sklearn/covariance/_robust_covariance.py b/sklearn/covariance/_robust_covariance.py index b921f501c10ef..6957bb7bbccef 100644 --- a/sklearn/covariance/_robust_covariance.py +++ b/sklearn/covariance/_robust_covariance.py @@ -536,11 +536,12 @@ class MinCovDet(EmpiricalCovariance): If False, the robust location and covariance are directly computed with the FastMCD algorithm without additional treatment. - support_fraction : float in (0, 1), default=None + support_fraction : float, default=None The proportion of points to be included in the support of the raw MCD estimate. Default is None, which implies that the minimum value of support_fraction will be used within the algorithm: [n_sample + n_features + 1] / 2 + Range is (0, 1). random_state : int or RandomState instance, default=None If int, random_state is the seed used by the random number generator; diff --git a/sklearn/covariance/_shrunk_covariance.py b/sklearn/covariance/_shrunk_covariance.py index cddeb4209bac0..65252702676ca 100644 --- a/sklearn/covariance/_shrunk_covariance.py +++ b/sklearn/covariance/_shrunk_covariance.py @@ -76,9 +76,9 @@ class ShrunkCovariance(EmpiricalCovariance): zero. If False, data will be centered before computation. - shrinkage : float in [0, 1], default=0.1 + shrinkage : float, default=0.1 Coefficient in the convex combination used for the computation - of the shrunk estimate. + of the shrunk estimate. Range is [0, 1]. Attributes ---------- @@ -357,9 +357,9 @@ class LedoitWolf(EmpiricalCovariance): Estimated pseudo inverse matrix. (stored only if store_precision is True) - shrinkage_ : float in [0,1] + shrinkage_ : float Coefficient in the convex combination used for the computation - of the shrunk estimate. + of the shrunk estimate. Range is [0, 1]. Examples -------- From 70aa39e05c02c8563e12620a70579a849c367511 Mon Sep 17 00:00:00 2001 From: mghah <31044045+mghah@users.noreply.github.com> Date: Tue, 14 Jan 2020 13:59:10 -0800 Subject: [PATCH 4/5] linting errors fixed --- sklearn/covariance/_elliptic_envelope.py | 2 +- sklearn/covariance/_graph_lasso.py | 4 ++-- sklearn/covariance/_shrunk_covariance.py | 6 +++--- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/sklearn/covariance/_elliptic_envelope.py b/sklearn/covariance/_elliptic_envelope.py index c36985801a481..b04e82d828bf0 100644 --- a/sklearn/covariance/_elliptic_envelope.py +++ b/sklearn/covariance/_elliptic_envelope.py @@ -32,7 +32,7 @@ class EllipticEnvelope(OutlierMixin, MinCovDet): The proportion of points to be included in the support of the raw MCD estimate. If None, the minimum value of support_fraction will be used within the algorithm: `[n_sample + n_features + 1] / 2`. - Range is (0, 1). + Range is (0, 1). contamination : float, default=0.1 The amount of contamination of the data set, i.e. the proportion diff --git a/sklearn/covariance/_graph_lasso.py b/sklearn/covariance/_graph_lasso.py index 206ab85ad8977..2f3d66892decc 100644 --- a/sklearn/covariance/_graph_lasso.py +++ b/sklearn/covariance/_graph_lasso.py @@ -289,7 +289,7 @@ class GraphicalLasso(EmpiricalCovariance): alpha : float, default=0.01 The regularization parameter: the higher alpha, the more regularization, the sparser the inverse covariance. - Range is (0, inf]. + Range is (0, inf]. mode : {'cd', 'lars'}, default='cd' The Lasso solver to use: coordinate descent or LARS. Use LARS for @@ -531,7 +531,7 @@ class GraphicalLassoCV(GraphicalLasso): tol : float, default=1e-4 The tolerance to declare convergence: if the dual gap goes below - this value, iterations are stopped. Range is (0, inf]. + this value, iterations are stopped. Range is (0, inf]. enet_tol : float, default=1e-4 The tolerance for the elastic net solver used to calculate the descent diff --git a/sklearn/covariance/_shrunk_covariance.py b/sklearn/covariance/_shrunk_covariance.py index 65252702676ca..c94aa8778e193 100644 --- a/sklearn/covariance/_shrunk_covariance.py +++ b/sklearn/covariance/_shrunk_covariance.py @@ -76,7 +76,7 @@ class ShrunkCovariance(EmpiricalCovariance): zero. If False, data will be centered before computation. - shrinkage : float, default=0.1 + shrinkage : float, default=0.1 Coefficient in the convex combination used for the computation of the shrunk estimate. Range is [0, 1]. @@ -535,9 +535,9 @@ class OAS(EmpiricalCovariance): Estimated pseudo inverse matrix. (stored only if store_precision is True) - shrinkage_ : float in [0, 1] + shrinkage_ : float coefficient in the convex combination used for the computation - of the shrunk estimate. + of the shrunk estimate. Range is [0, 1]. Notes ----- From df82ecad290722823151d46336fcaa9076fbc453 Mon Sep 17 00:00:00 2001 From: Guillaume Lemaitre Date: Wed, 15 Jan 2020 14:37:51 +0100 Subject: [PATCH 5/5] update methods --- sklearn/covariance/_elliptic_envelope.py | 42 +++-- sklearn/covariance/_empirical_covariance.py | 45 +++-- sklearn/covariance/_graph_lasso.py | 75 +++++---- sklearn/covariance/_robust_covariance.py | 176 ++++++++++---------- sklearn/covariance/_shrunk_covariance.py | 62 +++---- 5 files changed, 199 insertions(+), 201 deletions(-) diff --git a/sklearn/covariance/_elliptic_envelope.py b/sklearn/covariance/_elliptic_envelope.py index b04e82d828bf0..b96831077e68a 100644 --- a/sklearn/covariance/_elliptic_envelope.py +++ b/sklearn/covariance/_elliptic_envelope.py @@ -47,17 +47,17 @@ class EllipticEnvelope(OutlierMixin, MinCovDet): Attributes ---------- - location_ : array-like of shape (n_features,) + location_ : ndarray of shape (n_features,) Estimated robust location - covariance_ : array-like of shape (n_features, n_features) + covariance_ : ndarray of shape (n_features, n_features) Estimated robust covariance matrix - precision_ : array-like of shape (n_features, n_features) + precision_ : ndarray of shape (n_features, n_features) Estimated pseudo inverse matrix. (stored only if store_precision is True) - support_ : array-like of shape (n_samples,) + support_ : ndarray of shape (n_samples,) A mask of the observations that have been used to compute the robust estimates of location and shape. @@ -103,7 +103,6 @@ class EllipticEnvelope(OutlierMixin, MinCovDet): .. [1] Rousseeuw, P.J., Van Driessen, K. "A fast algorithm for the minimum covariance determinant estimator" Technometrics 41(3), 212 (1999) - """ def __init__(self, store_precision=True, assume_centered=False, support_fraction=None, contamination=0.1, @@ -120,12 +119,11 @@ def fit(self, X, y=None): Parameters ---------- - X : numpy array or sparse matrix, shape (n_samples, n_features). - Training data + X : {array-like, sparse matrix} of shape (n_samples, n_features) + Training data. y : Ignored - not used, present for API consistency by convention. - + Not used, present for API consistency by convention. """ super().fit(X) self.offset_ = np.percentile(-self.dist_, 100. * self.contamination) @@ -136,17 +134,16 @@ def decision_function(self, X): Parameters ---------- - X : array-like, shape (n_samples, n_features) + X : array-like of shape (n_samples, n_features) + The data matrix. Returns ------- - - decision : array-like, shape (n_samples, ) + decision : ndarray of shape (n_samples, ) Decision function of the samples. It is equal to the shifted Mahalanobis distances. The threshold for being an outlier is 0, which ensures a compatibility with other outlier detection algorithms. - """ check_is_fitted(self) negative_mahal_dist = self.score_samples(X) @@ -157,11 +154,12 @@ def score_samples(self, X): Parameters ---------- - X : array-like, shape (n_samples, n_features) + X : array-like of shape (n_samples, n_features) + The data matrix. Returns ------- - negative_mahal_distances : array-like, shape (n_samples, ) + negative_mahal_distances : array-like of shape (n_samples,) Opposite of the Mahalanobis distances. """ check_is_fitted(self) @@ -174,11 +172,12 @@ def predict(self, X): Parameters ---------- - X : array-like, shape (n_samples, n_features) + X : array-like of shape (n_samples, n_features) + The data matrix. Returns ------- - is_inlier : array, shape (n_samples,) + is_inlier : ndarray of shape (n_samples,) Returns -1 for anomalies/outliers and +1 for inliers. """ X = check_array(X) @@ -197,19 +196,18 @@ def score(self, X, y, sample_weight=None): Parameters ---------- - X : array-like, shape (n_samples, n_features) + X : array-like of shape (n_samples, n_features) Test samples. - y : array-like, shape (n_samples,) or (n_samples, n_outputs) + y : array-like of shape (n_samples,) or (n_samples, n_outputs) True labels for X. - sample_weight : array-like, shape (n_samples,), optional + sample_weight : array-like of shape (n_samples,), default=None Sample weights. Returns ------- score : float - Mean accuracy of self.predict(X) wrt. y. - + Mean accuracy of self.predict(X) w.r.t. y. """ return accuracy_score(y, self.predict(X), sample_weight=sample_weight) diff --git a/sklearn/covariance/_empirical_covariance.py b/sklearn/covariance/_empirical_covariance.py index 00058fb6de9a8..91dfd85740bf8 100644 --- a/sklearn/covariance/_empirical_covariance.py +++ b/sklearn/covariance/_empirical_covariance.py @@ -29,15 +29,16 @@ def log_likelihood(emp_cov, precision): Parameters ---------- - emp_cov : 2D ndarray (n_features, n_features) - Maximum Likelihood Estimator of covariance + emp_cov : ndarray of shape (n_features, n_features) + Maximum Likelihood Estimator of covariance. - precision : 2D ndarray (n_features, n_features) - The precision matrix of the covariance model to be tested + precision : ndarray of shape (n_features, n_features) + The precision matrix of the covariance model to be tested. Returns ------- - sample mean of the log-likelihood + log_likelihood_ : float + Sample mean of the log-likelihood. """ p = precision.shape[0] log_likelihood_ = - np.sum(emp_cov * precision) + fast_logdet(precision) @@ -65,7 +66,6 @@ def empirical_covariance(X, assume_centered=False): ------- covariance : ndarray of shape (n_features, n_features) Empirical covariance (Maximum Likelihood Estimator). - """ X = np.asarray(X) if X.ndim == 1: @@ -103,7 +103,7 @@ class EmpiricalCovariance(BaseEstimator): Attributes ---------- - location_ : array-like of shape (n_features,) + location_ : ndarray of shape (n_features,) Estimated location, i.e. the estimated mean. covariance_ : ndarray of shape (n_features, n_features) @@ -144,10 +144,9 @@ def _set_covariance(self, covariance): Parameters ---------- - covariance : 2D ndarray, shape (n_features, n_features) + covariance : array-like of shape (n_features, n_features) Estimated covariance matrix to be stored, and from which precision is computed. - """ covariance = check_array(covariance) # set covariance @@ -163,9 +162,8 @@ def get_precision(self): Returns ------- - precision_ : array-like + precision_ : array-like of shape (n_features, n_features) The precision matrix associated to the current covariance object. - """ if self.store_precision: precision = self.precision_ @@ -183,13 +181,12 @@ def fit(self, X, y=None): Training data, where n_samples is the number of samples and n_features is the number of features. - y - not used, present for API consistence purpose. + y : Ignored + Not used, present for API consistence purpose. Returns ------- self : object - """ X = check_array(X) if self.assume_centered: @@ -214,15 +211,14 @@ def score(self, X_test, y=None): X_test is assumed to be drawn from the same distribution than the data used in fit (including centering). - y - not used, present for API consistence purpose. + y : Ignored + Not used, present for API consistence purpose. Returns ------- res : float The likelihood of the data set with `self.covariance_` as an estimator of its covariance matrix. - """ # compute empirical covariance of the test set test_cov = empirical_covariance( @@ -242,26 +238,26 @@ def error_norm(self, comp_cov, norm='frobenius', scaling=True, comp_cov : array-like of shape (n_features, n_features) The covariance to compare with. - norm : str + norm : {"frobenius", "spectral"}, default="frobenius" The type of norm used to compute the error. Available error types: - 'frobenius' (default): sqrt(tr(A^t.A)) - 'spectral': sqrt(max(eigenvalues(A^t.A)) where A is the error ``(comp_cov - self.covariance_)``. - scaling : bool + scaling : bool, default=True If True (default), the squared error norm is divided by n_features. If False, the squared error norm is not rescaled. - squared : bool + squared : bool, default=True Whether to compute the squared error norm or the error norm. If True (default), the squared error norm is returned. If False, the error norm is returned. Returns ------- - The Mean Squared Error (in the sense of the Frobenius norm) between - `self` and `comp_cov` covariance estimators. - + result : float + The Mean Squared Error (in the sense of the Frobenius norm) between + `self` and `comp_cov` covariance estimators. """ # compute the error error = comp_cov - self.covariance_ @@ -296,9 +292,8 @@ def mahalanobis(self, X): Returns ------- - dist : array, shape = [n_samples,] + dist : ndarray of shape (n_samples,) Squared Mahalanobis distances of the observations. - """ precision = self.get_precision() # compute mahalanobis distances diff --git a/sklearn/covariance/_graph_lasso.py b/sklearn/covariance/_graph_lasso.py index 2f3d66892decc..9dbf786839585 100644 --- a/sklearn/covariance/_graph_lasso.py +++ b/sklearn/covariance/_graph_lasso.py @@ -58,16 +58,14 @@ def alpha_max(emp_cov): Parameters ---------- - emp_cov : 2D array, (n_features, n_features) - The sample covariance matrix + emp_cov : ndarray of shape (n_features, n_features) + The sample covariance matrix. Notes ----- - This results from the bound for the all the Lasso that are solved in GraphicalLasso: each time, the row of cov corresponds to Xy. As the bound for alpha is given by `max(abs(Xy))`, the result follows. - """ A = np.copy(emp_cov) A.flat[::A.shape[0] + 1] = 0 @@ -86,7 +84,7 @@ def graphical_lasso(emp_cov, alpha, cov_init=None, mode='cd', tol=1e-4, Parameters ---------- - emp_cov : 2D ndarray of shape (n_features, n_features) + emp_cov : ndarray of shape (n_features, n_features) Empirical covariance from which to compute the covariance estimate. alpha : float @@ -158,7 +156,6 @@ def graphical_lasso(emp_cov, alpha, cov_init=None, mode='cd', tol=1e-4, One possible difference with the `glasso` R package is that the diagonal coefficients are not penalized. - """ _, n_features = emp_cov.shape if alpha == 0: @@ -321,13 +318,13 @@ class GraphicalLasso(EmpiricalCovariance): Attributes ---------- - location_ : array-like of shape (n_features,) + location_ : ndarray of shape (n_features,) Estimated location, i.e. the estimated mean. - covariance_ : array-like of shape (n_features, n_features) + covariance_ : ndarray of shape (n_features, n_features) Estimated covariance matrix - precision_ : array-like of shape (n_features, n_features) + precision_ : ndarray of shape (n_features, n_features) Estimated pseudo inverse matrix. n_iter_ : int @@ -374,9 +371,15 @@ def fit(self, X, y=None): Parameters ---------- - X : ndarray, shape (n_samples, n_features) + X : array-like of shape (n_samples, n_features) Data from which to compute the covariance estimate - y : (ignored) + + y : Ignored + Not used, present for API consistence purpose. + + Returns + ------- + self : object """ # Covariance does not make sense for a single feature X = check_array(X, ensure_min_features=2, ensure_min_samples=2, @@ -404,49 +407,53 @@ def graphical_lasso_path(X, alphas, cov_init=None, X_test=None, mode='cd', Parameters ---------- - X : 2D ndarray, shape (n_samples, n_features) + X : ndarray of shape (n_samples, n_features) Data from which to compute the covariance estimate. - alphas : list of positive floats + alphas : array-like of shape (n_alphas,) The list of regularization parameters, decreasing order. - cov_init : 2D array (n_features, n_features), optional + cov_init : array of shape (n_features, n_features), default=None The initial guess for the covariance. - X_test : 2D array, shape (n_test_samples, n_features), optional + X_test : array of shape (n_test_samples, n_features), default=None Optional test matrix to measure generalisation error. - mode : {'cd', 'lars'} + mode : {'cd', 'lars'}, default='cd' The Lasso solver to use: coordinate descent or LARS. Use LARS for very sparse underlying graphs, where p > n. Elsewhere prefer cd which is more numerically stable. - tol : positive float, optional + tol : float, default=1e-4 The tolerance to declare convergence: if the dual gap goes below - this value, iterations are stopped. + this value, iterations are stopped. The tolerance must be a positive + number. - enet_tol : positive float, optional + enet_tol : float, default=1e-4 The tolerance for the elastic net solver used to calculate the descent direction. This parameter controls the accuracy of the search direction for a given column update, not of the overall parameter estimate. Only - used for mode='cd'. + used for mode='cd'. The tolerance must be a positive number. - max_iter : integer, optional - The maximum number of iterations. + max_iter : int, default=100 + The maximum number of iterations. This parameter should be a strictly + positive integer. - verbose : integer, optional + verbose : int or bool, default=False The higher the verbosity flag, the more information is printed during the fitting. Returns ------- - covariances_ : List of 2D ndarray, shape (n_features, n_features) + covariances_ : list of shape (n_alphas,) of ndarray of shape \ + (n_features, n_features) The estimated covariance matrices. - precisions_ : List of 2D ndarray, shape (n_features, n_features) + precisions_ : list of shape (n_alphas,) of ndarray of shape \ + (n_features, n_features) The estimated (sparse) precision matrices. - scores_ : List of float + scores_ : list of shape (n_alphas,), dtype=float The generalisation error (log-likelihood) on the test data. Returned only if test data is passed. """ @@ -502,7 +509,7 @@ class GraphicalLassoCV(GraphicalLasso): Parameters ---------- - alphas : int or list of floats, default=4 + alphas : int or array-like of shape (n_alphas,), dtype=float, default=4 If an integer is given, it fixes the number of points on the grids of alpha to be used. If a list is given, it gives the grid to be used. See the notes in the class docstring for @@ -566,7 +573,7 @@ class GraphicalLassoCV(GraphicalLasso): Attributes ---------- - location_ : array-like of shape (n_features,) + location_ : ndarray of shape (n_features,) Estimated location, i.e. the estimated mean. covariance_ : ndarray of shape (n_features, n_features) @@ -578,7 +585,7 @@ class GraphicalLassoCV(GraphicalLasso): alpha_ : float Penalization parameter selected. - cv_alphas_ : list of floats + cv_alphas_ : list of shape (n_alphas,), dtype=float All penalization parameters explored. grid_scores_ : ndarray of shape (n_alphas, n_folds) @@ -641,9 +648,15 @@ def fit(self, X, y=None): Parameters ---------- - X : ndarray, shape (n_samples, n_features) + X : array-like of shape (n_samples, n_features) Data from which to compute the covariance estimate - y : (ignored) + + y : Ignored + Not used, present for API consistence purpose. + + Returns + ------- + self : object """ # Covariance does not make sense for a single feature X = check_array(X, ensure_min_features=2, estimator=self) diff --git a/sklearn/covariance/_robust_covariance.py b/sklearn/covariance/_robust_covariance.py index 6957bb7bbccef..8afac2c3c0eee 100644 --- a/sklearn/covariance/_robust_covariance.py +++ b/sklearn/covariance/_robust_covariance.py @@ -33,34 +33,36 @@ def c_step(X, n_support, remaining_iterations=30, initial_estimates=None, Parameters ---------- - X : array-like, shape (n_samples, n_features) + X : array-like of shape (n_samples, n_features) Data set in which we look for the n_support observations whose scatter matrix has minimum determinant. - n_support : int, > n_samples / 2 + n_support : int, Number of observations to compute the robust estimates of location - and covariance from. + and covariance from. This parameter must be greater than + `n_samples / 2`. - remaining_iterations : int, optional + remaining_iterations : int, default=30 Number of iterations to perform. According to [Rouseeuw1999]_, two iterations are sufficient to get close to the minimum, and we never need more than 30 to reach convergence. - initial_estimates : 2-tuple, optional + initial_estimates : tuple of shape (2,), default=None Initial estimates of location and shape from which to run the c_step procedure: - initial_estimates[0]: an initial location estimate - initial_estimates[1]: an initial covariance estimate - verbose : boolean, optional + verbose : bool, defaut=False Verbose mode. - cov_computation_method : callable, default empirical_covariance + cov_computation_method : callable, \ + default=:func:`sklearn.covariance.empirical_covariance` The function which will be used to compute the covariance. - Must return shape (n_features, n_features) + Must return array of shape (n_features, n_features). - random_state : int, RandomState instance or None, optional (default=None) + random_state : int, RandomState instance, default=None If int, random_state is the seed used by the random number generator; If RandomState instance, random_state is the random number generator; If None, the random number generator is the RandomState instance used @@ -68,13 +70,13 @@ def c_step(X, n_support, remaining_iterations=30, initial_estimates=None, Returns ------- - location : array-like, shape (n_features,) + location : ndarray of shape (n_features,) Robust location estimates. - covariance : array-like, shape (n_features, n_features) + covariance : ndarray of shape (n_features, n_features) Robust covariance estimates. - support : array-like, shape (n_samples,) + support : ndarray of shape (n_samples,) A mask for the `n_support` observations whose scatter matrix has minimum determinant. @@ -83,7 +85,6 @@ def c_step(X, n_support, remaining_iterations=30, initial_estimates=None, .. [Rouseeuw1999] A Fast Algorithm for the Minimum Covariance Determinant Estimator, 1999, American Statistical Association and the American Society for Quality, TECHNOMETRICS - """ X = np.asarray(X) random_state = check_random_state(random_state) @@ -199,15 +200,17 @@ def select_candidates(X, n_support, n_trials, select=1, n_iter=30, Parameters ---------- - X : array-like, shape (n_samples, n_features) + X : array-like of shape (n_samples, n_features) Data (sub)set in which we look for the n_support purest observations. - n_support : int, [(n + p + 1)/2] < n_support < n + n_support : int The number of samples the pure data set must contain. + This parameter must be in the range `[(n + p + 1)/2] < n_support < n`. - n_trials : int, nb_trials > 0 or 2-tuple + n_trials : int or tuple of shape (2,) Number of different initial sets of observations from which to - run the algorithm. + run the algorithm. This parameter should be a strictly positive + integer. Instead of giving a number of trials to perform, one can provide a list of initial estimates that will be used to iteratively run c_step procedures. In this case: @@ -216,21 +219,24 @@ def select_candidates(X, n_support, n_trials, select=1, n_iter=30, - n_trials[1]: array-like, shape (n_trials, n_features, n_features) is the list of `n_trials` initial covariances estimates - select : int, int > 0 - Number of best candidates results to return. + select : int, default=1 + Number of best candidates results to return. This parameter must be + a strictly positive integer. - n_iter : int, nb_iter > 0 + n_iter : int, default=30 Maximum number of iterations for the c_step procedure. (2 is enough to be close to the final solution. "Never" exceeds 20). + This parameter must be a strictly positive integer. - verbose : boolean, default False + verbose : bool, default False Control the output verbosity. - cov_computation_method : callable, default empirical_covariance + cov_computation_method : callable, \ + default=:func:`sklearn.covariance.empirical_covariance` The function which will be used to compute the covariance. - Must return shape (n_features, n_features) + Must return an array of shape (n_features, n_features). - random_state : int, RandomState instance or None, optional (default=None) + random_state : int, RandomState instance, default=None If int, random_state is the seed used by the random number generator; If RandomState instance, random_state is the random number generator; If None, the random number generator is the RandomState instance used @@ -242,15 +248,15 @@ def select_candidates(X, n_support, n_trials, select=1, n_iter=30, Returns ------- - best_locations : array-like, shape (select, n_features) + best_locations : ndarray of shape (select, n_features) The `select` location estimates computed from the `select` best supports found in the data set (`X`). - best_covariances : array-like, shape (select, n_features, n_features) + best_covariances : ndarray of shape (select, n_features, n_features) The `select` covariance estimates computed from the `select` best supports found in the data set (`X`). - best_supports : array-like, shape (select, n_samples) + best_supports : ndarray of shape (select, n_samples) The `select` best supports found in the data set (`X`). References @@ -258,7 +264,6 @@ def select_candidates(X, n_support, n_trials, select=1, n_iter=30, .. [RV] A Fast Algorithm for the Minimum Covariance Determinant Estimator, 1999, American Statistical Association and the American Society for Quality, TECHNOMETRICS - """ random_state = check_random_state(random_state) @@ -312,25 +317,39 @@ def fast_mcd(X, support_fraction=None, Parameters ---------- - X : array-like, shape (n_samples, n_features) - The data matrix, with p features and n samples. + X : array-like of shape (n_samples, n_features) + The data matrix, with p features and n samples. - support_fraction : float, 0 < support_fraction < 1 - The proportion of points to be included in the support of the raw - MCD estimate. Default is None, which implies that the minimum - value of support_fraction will be used within the algorithm: - `[n_sample + n_features + 1] / 2`. + support_fraction : float, default=None + The proportion of points to be included in the support of the raw + MCD estimate. Default is `None`, which implies that the minimum + value of `support_fraction` will be used within the algorithm: + `(n_sample + n_features + 1) / 2`. This parameter must be in the + range (0, 1). - cov_computation_method : callable, default empirical_covariance + cov_computation_method : callable, \ + default=:func:`sklearn.covariance.empirical_covariance` The function which will be used to compute the covariance. - Must return shape (n_features, n_features) + Must return an array of shape (n_features, n_features). - random_state : int, RandomState instance or None, optional (default=None) + random_state : int, RandomState instance, default=None If int, random_state is the seed used by the random number generator; If RandomState instance, random_state is the random number generator; If None, the random number generator is the RandomState instance used by `np.random`. + Returns + ------- + location : ndarray of shape (n_features,) + Robust location of the data. + + covariance : ndarray of shape (n_features, n_features) + Robust covariance of the features. + + support : ndarray of shape (n_samples,), dtype=bool + A mask of the observations that have been used to compute + the robust location and covariance estimates of the data set. + Notes ----- The FastMCD algorithm has been introduced by Rousseuw and Van Driessen @@ -356,19 +375,6 @@ def fast_mcd(X, support_fraction=None, .. [Butler1993] R. W. Butler, P. L. Davies and M. Jhun, Asymptotics For The Minimum Covariance Determinant Estimator, The Annals of Statistics, 1993, Vol. 21, No. 3, 1385-1400 - - Returns - ------- - location : array-like, shape (n_features,) - Robust location of the data. - - covariance : array-like, shape (n_features, n_features) - Robust covariance of the features. - - support : array-like, type boolean, shape (n_samples,) - A mask of the observations that have been used to compute - the robust location and covariance estimates of the data set. - """ random_state = check_random_state(random_state) @@ -540,8 +546,8 @@ class MinCovDet(EmpiricalCovariance): The proportion of points to be included in the support of the raw MCD estimate. Default is None, which implies that the minimum value of support_fraction will be used within the algorithm: - [n_sample + n_features + 1] / 2 - Range is (0, 1). + `(n_sample + n_features + 1) / 2`. The parameter must be in the range + (0, 1). random_state : int or RandomState instance, default=None If int, random_state is the seed used by the random number generator; @@ -551,32 +557,32 @@ class MinCovDet(EmpiricalCovariance): Attributes ---------- - raw_location_ : array-like of shape (n_features,) + raw_location_ : ndarray of shape (n_features,) The raw robust estimated location before correction and re-weighting. - raw_covariance_ : array-like of shape (n_features, n_features) + raw_covariance_ : ndarray of shape (n_features, n_features) The raw robust estimated covariance before correction and re-weighting. - raw_support_ : array-like of shape (n_samples,) + raw_support_ : ndarray of shape (n_samples,) A mask of the observations that have been used to compute the raw robust estimates of location and shape, before correction and re-weighting. - location_ : array-like of shape (n_features,) - Estimated robust location + location_ : ndarray of shape (n_features,) + Estimated robust location. - covariance_ : array-like of shape (n_features, n_features) - Estimated robust covariance matrix + covariance_ : ndarray of shape (n_features, n_features) + Estimated robust covariance matrix. - precision_ : array-like of shape (n_features, n_features) + precision_ : ndarray of shape (n_features, n_features) Estimated pseudo inverse matrix. (stored only if store_precision is True) - support_ : array-like of shape (n_samples,) + support_ : ndarray of shape (n_samples,) A mask of the observations that have been used to compute the robust estimates of location and shape. - dist_ : array-like of shape (n_samples,) + dist_ : ndarray of shape (n_samples,) Mahalanobis distances of the training set (on which :meth:`fit` is called) observations. @@ -609,7 +615,6 @@ class MinCovDet(EmpiricalCovariance): .. [ButlerDavies] R. W. Butler, P. L. Davies and M. Jhun, Asymptotics For The Minimum Covariance Determinant Estimator, The Annals of Statistics, 1993, Vol. 21, No. 3, 1385-1400 - """ _nonrobust_covariance = staticmethod(empirical_covariance) @@ -626,16 +631,15 @@ def fit(self, X, y=None): Parameters ---------- X : array-like of shape (n_samples, n_features) - Training data, where n_samples is the number of samples - and n_features is the number of features. + Training data, where `n_samples` is the number of samples + and `n_features` is the number of features. - y - not used, present for API consistence purpose. + y: Ignored + Not used, present for API consistence purpose. Returns ------- self : object - """ X = check_array(X, ensure_min_samples=2, estimator='MinCovDet') random_state = check_random_state(self.random_state) @@ -677,23 +681,22 @@ def correct_covariance(self, data): Parameters ---------- - data : array-like, shape (n_samples, n_features) + data : array-like of shape (n_samples, n_features) The data matrix, with p features and n samples. The data set must be the one which was used to compute the raw estimates. + Returns + ------- + covariance_corrected : ndarray of shape (n_features, n_features) + Corrected robust covariance estimate. + References ---------- .. [RVD] A Fast Algorithm for the Minimum Covariance Determinant Estimator, 1999, American Statistical Association and the American Society for Quality, TECHNOMETRICS - - Returns - ------- - covariance_corrected : array-like, shape (n_features, n_features) - Corrected robust covariance estimate. - """ # Check that the covariance of the support data is not equal to 0. @@ -718,30 +721,29 @@ def reweight_covariance(self, data): Parameters ---------- - data : array-like, shape (n_samples, n_features) + data : array-like of shape (n_samples, n_features) The data matrix, with p features and n samples. The data set must be the one which was used to compute the raw estimates. - References - ---------- - - .. [RVDriessen] A Fast Algorithm for the Minimum Covariance - Determinant Estimator, 1999, American Statistical Association - and the American Society for Quality, TECHNOMETRICS - Returns ------- - location_reweighted : array-like, shape (n_features, ) + location_reweighted : ndarray of shape (n_features,) Re-weighted robust location estimate. - covariance_reweighted : array-like, shape (n_features, n_features) + covariance_reweighted : ndarray of shape (n_features, n_features) Re-weighted robust covariance estimate. - support_reweighted : array-like, type boolean, shape (n_samples,) + support_reweighted : ndarray of shape (n_samples,), dtype=bool A mask of the observations that have been used to compute the re-weighted robust location and covariance estimates. + References + ---------- + + .. [RVDriessen] A Fast Algorithm for the Minimum Covariance + Determinant Estimator, 1999, American Statistical Association + and the American Society for Quality, TECHNOMETRICS """ n_samples, n_features = data.shape mask = self.dist_ < chi2(n_features).isf(0.025) diff --git a/sklearn/covariance/_shrunk_covariance.py b/sklearn/covariance/_shrunk_covariance.py index c94aa8778e193..61907274ad823 100644 --- a/sklearn/covariance/_shrunk_covariance.py +++ b/sklearn/covariance/_shrunk_covariance.py @@ -38,7 +38,7 @@ def shrunk_covariance(emp_cov, shrinkage=0.1): Returns ------- - shrunk_cov : array-like + shrunk_cov : ndarray of shape (n_features, n_features) Shrunk covariance. Notes @@ -48,7 +48,6 @@ def shrunk_covariance(emp_cov, shrinkage=0.1): (1 - shrinkage) * cov + shrinkage * mu * np.identity(n_features) where mu = trace(cov) / n_features - """ emp_cov = check_array(emp_cov) n_features = emp_cov.shape[0] @@ -82,13 +81,13 @@ class ShrunkCovariance(EmpiricalCovariance): Attributes ---------- - location_ : array-like of shape (n_features,) + location_ : ndarray of shape (n_features,) Estimated location, i.e. the estimated mean. - covariance_ : array-like of shape (n_features, n_features) + covariance_ : ndarray of shape (n_features, n_features) Estimated covariance matrix - precision_ : array-like of shape (n_features, n_features) + precision_ : ndarray of shape (n_features, n_features) Estimated pseudo inverse matrix. (stored only if store_precision is True) @@ -117,7 +116,6 @@ class ShrunkCovariance(EmpiricalCovariance): (1 - shrinkage) * cov + shrinkage * mu * np.identity(n_features) where mu = trace(cov) / n_features - """ def __init__(self, store_precision=True, assume_centered=False, shrinkage=0.1): @@ -126,8 +124,8 @@ def __init__(self, store_precision=True, assume_centered=False, self.shrinkage = shrinkage def fit(self, X, y=None): - """ Fits the shrunk covariance model - according to the given training data and parameters. + """Fit the shrunk covariance model according to the given training data + and parameters. Parameters ---------- @@ -135,13 +133,12 @@ def fit(self, X, y=None): Training data, where n_samples is the number of samples and n_features is the number of features. - y + y: Ignored not used, present for API consistence purpose. Returns ------- self : object - """ X = check_array(X) # Not calling the parent object to fit, to avoid a potential @@ -167,16 +164,16 @@ def ledoit_wolf_shrinkage(X, assume_centered=False, block_size=1000): Parameters ---------- - X : array-like, shape (n_samples, n_features) + X : array-like of shape (n_samples, n_features) Data from which to compute the Ledoit-Wolf shrunk covariance shrinkage. - assume_centered : bool + assume_centered : bool, default=False If True, data will not be centered before computation. Useful to work with data whose mean is significantly equal to zero but is not exactly zero. If False, data will be centered before computation. - block_size : int + block_size : int, default=1000 Size of the blocks into which the covariance matrix will be split. Returns @@ -192,7 +189,6 @@ def ledoit_wolf_shrinkage(X, assume_centered=False, block_size=1000): (1 - shrinkage) * cov + shrinkage * mu * np.identity(n_features) where mu = trace(cov) / n_features - """ X = np.asarray(X) # for only one feature, the result is the same whatever the shrinkage @@ -277,7 +273,7 @@ def ledoit_wolf(X, assume_centered=False, block_size=1000): Returns ------- - shrunk_cov : array-like of shape (n_features, n_features) + shrunk_cov : ndarray of shape (n_features, n_features) Shrunk covariance. shrinkage : float @@ -291,7 +287,6 @@ def ledoit_wolf(X, assume_centered=False, block_size=1000): (1 - shrinkage) * cov + shrinkage * mu * np.identity(n_features) where mu = trace(cov) / n_features - """ X = np.asarray(X) # for only one feature, the result is the same whatever the shrinkage @@ -347,13 +342,13 @@ class LedoitWolf(EmpiricalCovariance): Attributes ---------- - location_ : array-like of shape (n_features,) + location_ : ndarray of shape (n_features,) Estimated location, i.e. the estimated mean. - covariance_ : array-like of shape (n_features, n_features) - Estimated covariance matrix + covariance_ : ndarray of shape (n_features, n_features) + Estimated covariance matrix. - precision_ : array-like of shape (n_features, n_features) + precision_ : ndarray of shape (n_features, n_features) Estimated pseudo inverse matrix. (stored only if store_precision is True) @@ -392,7 +387,6 @@ class LedoitWolf(EmpiricalCovariance): "A Well-Conditioned Estimator for Large-Dimensional Covariance Matrices", Ledoit and Wolf, Journal of Multivariate Analysis, Volume 88, Issue 2, February 2004, pages 365-411. - """ def __init__(self, store_precision=True, assume_centered=False, block_size=1000): @@ -401,21 +395,20 @@ def __init__(self, store_precision=True, assume_centered=False, self.block_size = block_size def fit(self, X, y=None): - """ Fits the Ledoit-Wolf shrunk covariance model - according to the given training data and parameters. + """Fit the Ledoit-Wolf shrunk covariance model according to the given + training data and parameters. Parameters ---------- X : array-like of shape (n_samples, n_features) - Training data, where n_samples is the number of samples - and n_features is the number of features. - y + Training data, where `n_samples` is the number of samples + and `n_features` is the number of features. + y : Ignored not used, present for API consistence purpose. Returns ------- self : object - """ # Not calling the parent object to fit, to avoid computing the # covariance matrix (and potentially the precision) @@ -468,7 +461,6 @@ def oas(X, assume_centered=False): The formula we used to implement the OAS is slightly modified compared to the one given in the article. See :class:`OAS` for more details. - """ X = np.asarray(X) # for only one feature, the result is the same whatever the shrinkage @@ -528,10 +520,10 @@ class OAS(EmpiricalCovariance): Attributes ---------- - covariance_ : array-like of shape (n_features, n_features) + covariance_ : ndarray of shape (n_features, n_features) Estimated covariance matrix. - precision_ : array-like of shape (n_features, n_features) + precision_ : ndarray of shape (n_features, n_features) Estimated pseudo inverse matrix. (stored only if store_precision is True) @@ -552,25 +544,23 @@ class OAS(EmpiricalCovariance): ---------- "Shrinkage Algorithms for MMSE Covariance Estimation" Chen et al., IEEE Trans. on Sign. Proc., Volume 58, Issue 10, October 2010. - """ def fit(self, X, y=None): - """ Fits the Oracle Approximating Shrinkage covariance model + """Fit the Oracle Approximating Shrinkage covariance model according to the given training data and parameters. Parameters ---------- X : array-like of shape (n_samples, n_features) - Training data, where n_samples is the number of samples - and n_features is the number of features. - y + Training data, where `n_samples` is the number of samples + and `n_features` is the number of features. + y : Ignored not used, present for API consistence purpose. Returns ------- self : object - """ X = check_array(X) # Not calling the parent object to fit, to avoid computing the