class sklearn.compose.TransformedTargetRegressor(regressor=None, *, transformer=None, func=None, inverse_func=None, check_inverse=True) [source]

Meta-estimator to regress on a transformed target. Useful for applying a non-linear transformation to the target y in regression problems. This transformation can be given as a Transformer such as the QuantileTransformer or as a function and its inverse such as log and exp. The computation during fit is: regressor.fit(X, func(y))
 or: regressor.fit(X, transformer.transform(y))
 The computation during predict is: inverse_func(regressor.predict(X))
 or: transformer.inverse_transform(regressor.predict(X))
 Read more in the User Guide.  New in version 0.20.   Parameters

regressorobject, default=None

Regressor object such as derived from RegressorMixin. This regressor will automatically be cloned each time prior to fitting. If regressor is None, LinearRegression() is created and used.
transformerobject, default=None

Estimator object such as derived from TransformerMixin. Cannot be set at the same time as func and inverse_func. If transformer is None as well as func and inverse_func, the transformer will be an identity transformer. Note that the transformer will be cloned during fitting. Also, the transformer is restricting y to be a numpy array.
funcfunction, default=None

Function to apply to y before passing to fit. Cannot be set at the same time as transformer. The function needs to return a 2-dimensional array. If func is None, the function used will be the identity function.
inverse_funcfunction, default=None

Function to apply to the prediction of the regressor. Cannot be set at the same time as transformer as well. The function needs to return a 2-dimensional array. The inverse function is used to return predictions to the same space of the original training labels.
check_inversebool, default=True

Whether to check that transform followed by inverse_transform or func followed by inverse_func leads to the original targets.    Attributes

regressor_object

Fitted regressor.
transformer_object

Transformer used in fit and predict.     Notes Internally, the target y is always converted into a 2-dimensional array to be used by scikit-learn transformers. At the time of prediction, the output will be reshaped to a have the same number of dimensions as y. See examples/compose/plot_transformed_target.py. Examples >>> import numpy as np
>>> from sklearn.linear_model import LinearRegression
>>> from sklearn.compose import TransformedTargetRegressor
>>> tt = TransformedTargetRegressor(regressor=LinearRegression(),
...                                 func=np.log, inverse_func=np.exp)
>>> X = np.arange(4).reshape(-1, 1)
>>> y = np.exp(2 * X).ravel()
>>> tt.fit(X, y)
TransformedTargetRegressor(...)
>>> tt.score(X, y)
1.0
>>> tt.regressor_.coef_
array([2.])
 Methods
fit(X, y, **fit_params) Fit the model according to the given training data.
get_params([deep]) Get parameters for this estimator.
predict(X) Predict using the base regressor, applying inverse.
score(X, y[, sample_weight]) Return the coefficient of determination \(R^2\) of the prediction.
set_params(**params) Set the parameters of this estimator.
fit(X, y, **fit_params) [source]

Fit the model according to the given training data.  Parameters

X{array-like, sparse matrix} of shape (n_samples, n_features)

Training vector, where n_samples is the number of samples and n_features is the number of features.
yarray-like of shape (n_samples,)

Target values.
**fit_paramsdict

Parameters passed to the fit method of the underlying regressor.    Returns

selfobject

get_params(deep=True) [source]

Get parameters for this estimator.  Parameters

deepbool, default=True

If True, will return the parameters for this estimator and contained subobjects that are estimators.    Returns

paramsdict

Parameter names mapped to their values.

predict(X) [source]

Predict using the base regressor, applying inverse. The regressor is used to predict and the inverse_func or inverse_transform is applied before returning the prediction.  Parameters

X{array-like, sparse matrix} of shape (n_samples, n_features)

Samples.    Returns

y_hatndarray of shape (n_samples,)

Predicted values.

score(X, y, sample_weight=None) [source]

Return the coefficient of determination \(R^2\) of the prediction. The coefficient \(R^2\) is defined as \((1 - \frac{u}{v})\), where \(u\) is the residual sum of squares ((y_true - y_pred)
** 2).sum() and \(v\) is the total sum of squares ((y_true -
y_true.mean()) ** 2).sum(). The best possible score is 1.0 and it can be negative (because the model can be arbitrarily worse). A constant model that always predicts the expected value of y, disregarding the input features, would get a \(R^2\) score of 0.0.  Parameters

Xarray-like of shape (n_samples, n_features)

Test samples. For some estimators this may be a precomputed kernel matrix or a list of generic objects instead with shape (n_samples, n_samples_fitted), where n_samples_fitted is the number of samples used in the fitting for the estimator.
yarray-like of shape (n_samples,) or (n_samples, n_outputs)

True values for X.
sample_weightarray-like of shape (n_samples,), default=None

Sample weights.    Returns

scorefloat

\(R^2\) of self.predict(X) wrt. y.     Notes The \(R^2\) score used when calling score on a regressor uses multioutput='uniform_average' from version 0.23 to keep consistent with default value of r2_score. This influences the score method of all the multioutput regressors (except for MultiOutputRegressor).

set_params(**params) [source]

Set the parameters of this estimator. The method works on simple estimators as well as on nested objects (such as Pipeline). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.  Parameters

**paramsdict

Estimator parameters.    Returns

selfestimator instance

Estimator instance.


sklearn.compose.TransformedTargetRegressor
class sklearn.compose.TransformedTargetRegressor(regressor=None, *, transformer=None, func=None, inverse_func=None, check_inverse=True) [source]

Meta-estimator to regress on a transformed target. Useful for applying a non-linear transformation to the target y in regression problems. This transformation can be given as a Transformer such as the QuantileTransformer or as a function and its inverse such as log and exp. The computation during fit is: regressor.fit(X, func(y))
 or: regressor.fit(X, transformer.transform(y))
 The computation during predict is: inverse_func(regressor.predict(X))
 or: transformer.inverse_transform(regressor.predict(X))
 Read more in the User Guide.  New in version 0.20.   Parameters

regressorobject, default=None

Regressor object such as derived from RegressorMixin. This regressor will automatically be cloned each time prior to fitting. If regressor is None, LinearRegression() is created and used.
transformerobject, default=None

Estimator object such as derived from TransformerMixin. Cannot be set at the same time as func and inverse_func. If transformer is None as well as func and inverse_func, the transformer will be an identity transformer. Note that the transformer will be cloned during fitting. Also, the transformer is restricting y to be a numpy array.
funcfunction, default=None

Function to apply to y before passing to fit. Cannot be set at the same time as transformer. The function needs to return a 2-dimensional array. If func is None, the function used will be the identity function.
inverse_funcfunction, default=None

Function to apply to the prediction of the regressor. Cannot be set at the same time as transformer as well. The function needs to return a 2-dimensional array. The inverse function is used to return predictions to the same space of the original training labels.
check_inversebool, default=True

Whether to check that transform followed by inverse_transform or func followed by inverse_func leads to the original targets.    Attributes

regressor_object

Fitted regressor.
transformer_object

Transformer used in fit and predict.     Notes Internally, the target y is always converted into a 2-dimensional array to be used by scikit-learn transformers. At the time of prediction, the output will be reshaped to a have the same number of dimensions as y. See examples/compose/plot_transformed_target.py. Examples >>> import numpy as np
>>> from sklearn.linear_model import LinearRegression
>>> from sklearn.compose import TransformedTargetRegressor
>>> tt = TransformedTargetRegressor(regressor=LinearRegression(),
...                                 func=np.log, inverse_func=np.exp)
>>> X = np.arange(4).reshape(-1, 1)
>>> y = np.exp(2 * X).ravel()
>>> tt.fit(X, y)
TransformedTargetRegressor(...)
>>> tt.score(X, y)
1.0
>>> tt.regressor_.coef_
array([2.])
 Methods
fit(X, y, **fit_params) Fit the model according to the given training data.
get_params([deep]) Get parameters for this estimator.
predict(X) Predict using the base regressor, applying inverse.
score(X, y[, sample_weight]) Return the coefficient of determination \(R^2\) of the prediction.
set_params(**params) Set the parameters of this estimator.
fit(X, y, **fit_params) [source]

Fit the model according to the given training data.  Parameters

X{array-like, sparse matrix} of shape (n_samples, n_features)

Training vector, where n_samples is the number of samples and n_features is the number of features.
yarray-like of shape (n_samples,)

Target values.
**fit_paramsdict

Parameters passed to the fit method of the underlying regressor.    Returns

selfobject

get_params(deep=True) [source]

Get parameters for this estimator.  Parameters

deepbool, default=True

If True, will return the parameters for this estimator and contained subobjects that are estimators.    Returns

paramsdict

Parameter names mapped to their values.

predict(X) [source]

Predict using the base regressor, applying inverse. The regressor is used to predict and the inverse_func or inverse_transform is applied before returning the prediction.  Parameters

X{array-like, sparse matrix} of shape (n_samples, n_features)

Samples.    Returns

y_hatndarray of shape (n_samples,)

Predicted values.

score(X, y, sample_weight=None) [source]

Return the coefficient of determination \(R^2\) of the prediction. The coefficient \(R^2\) is defined as \((1 - \frac{u}{v})\), where \(u\) is the residual sum of squares ((y_true - y_pred)
** 2).sum() and \(v\) is the total sum of squares ((y_true -
y_true.mean()) ** 2).sum(). The best possible score is 1.0 and it can be negative (because the model can be arbitrarily worse). A constant model that always predicts the expected value of y, disregarding the input features, would get a \(R^2\) score of 0.0.  Parameters

Xarray-like of shape (n_samples, n_features)

Test samples. For some estimators this may be a precomputed kernel matrix or a list of generic objects instead with shape (n_samples, n_samples_fitted), where n_samples_fitted is the number of samples used in the fitting for the estimator.
yarray-like of shape (n_samples,) or (n_samples, n_outputs)

True values for X.
sample_weightarray-like of shape (n_samples,), default=None

Sample weights.    Returns

scorefloat

\(R^2\) of self.predict(X) wrt. y.     Notes The \(R^2\) score used when calling score on a regressor uses multioutput='uniform_average' from version 0.23 to keep consistent with default value of r2_score. This influences the score method of all the multioutput regressors (except for MultiOutputRegressor).

set_params(**params) [source]

Set the parameters of this estimator. The method works on simple estimators as well as on nested objects (such as Pipeline). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.  Parameters

**paramsdict

Estimator parameters.    Returns

selfestimator instance

Estimator instance.

 Examples using sklearn.compose.TransformedTargetRegressor

  Poisson regression and non-normal loss

  Common pitfalls in interpretation of coefficients of linear models

  Effect of transforming the targets in regression model


fit(X, y, **fit_params) [source]

Fit the model according to the given training data.  Parameters

X{array-like, sparse matrix} of shape (n_samples, n_features)

Training vector, where n_samples is the number of samples and n_features is the number of features.
yarray-like of shape (n_samples,)

Target values.
**fit_paramsdict

Parameters passed to the fit method of the underlying regressor.    Returns

selfobject


get_params(deep=True) [source]

Get parameters for this estimator.  Parameters

deepbool, default=True

If True, will return the parameters for this estimator and contained subobjects that are estimators.    Returns

paramsdict

Parameter names mapped to their values.


predict(X) [source]

Predict using the base regressor, applying inverse. The regressor is used to predict and the inverse_func or inverse_transform is applied before returning the prediction.  Parameters

X{array-like, sparse matrix} of shape (n_samples, n_features)

Samples.    Returns

y_hatndarray of shape (n_samples,)

Predicted values.


score(X, y, sample_weight=None) [source]

Return the coefficient of determination \(R^2\) of the prediction. The coefficient \(R^2\) is defined as \((1 - \frac{u}{v})\), where \(u\) is the residual sum of squares ((y_true - y_pred)
** 2).sum() and \(v\) is the total sum of squares ((y_true -
y_true.mean()) ** 2).sum(). The best possible score is 1.0 and it can be negative (because the model can be arbitrarily worse). A constant model that always predicts the expected value of y, disregarding the input features, would get a \(R^2\) score of 0.0.  Parameters

Xarray-like of shape (n_samples, n_features)

Test samples. For some estimators this may be a precomputed kernel matrix or a list of generic objects instead with shape (n_samples, n_samples_fitted), where n_samples_fitted is the number of samples used in the fitting for the estimator.
yarray-like of shape (n_samples,) or (n_samples, n_outputs)

True values for X.
sample_weightarray-like of shape (n_samples,), default=None

Sample weights.    Returns

scorefloat

\(R^2\) of self.predict(X) wrt. y.     Notes The \(R^2\) score used when calling score on a regressor uses multioutput='uniform_average' from version 0.23 to keep consistent with default value of r2_score. This influences the score method of all the multioutput regressors (except for MultiOutputRegressor).


set_params(**params) [source]

Set the parameters of this estimator. The method works on simple estimators as well as on nested objects (such as Pipeline). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.  Parameters

**paramsdict

Estimator parameters.    Returns

selfestimator instance

Estimator instance.


sklearn.config_context(**new_config) [source]

Context manager for global scikit-learn configuration  Parameters

assume_finitebool, default=False

If True, validation for finiteness will be skipped, saving time, but leading to potential crashes. If False, validation for finiteness will be performed, avoiding error. Global default: False.
working_memoryint, default=1024

If set, scikit-learn will attempt to limit the size of temporary arrays to this number of MiB (per job when parallelised), often saving both computation time and memory on expensive operations that can be performed in chunks. Global default: 1024.
print_changed_onlybool, default=True

If True, only the parameters that were set to non-default values will be printed when printing an estimator. For example, print(SVC()) while True will only print ‘SVC()’, but would print ‘SVC(C=1.0, cache_size=200, …)’ with all the non-changed parameters when False. Default is True.  Changed in version 0.23: Default changed from False to True.
display{‘text’, ‘diagram’}, default=’text’

If ‘diagram’, estimators will be displayed as a diagram in a Jupyter lab or notebook context. If ‘text’, estimators will be displayed as text. Default is ‘text’.  New in version 0.23.       See also
set_config

Set global scikit-learn configuration.
get_config

Retrieve current values of the global configuration.    Notes All settings, not just those presently modified, will be returned to their previous values when the context manager is exited. This is not thread-safe. Examples >>> import sklearn
>>> from sklearn.utils.validation import assert_all_finite
>>> with sklearn.config_context(assume_finite=True):
...     assert_all_finite([float('nan')])
>>> with sklearn.config_context(assume_finite=True):
...     with sklearn.config_context(assume_finite=False):
...         assert_all_finite([float('nan')])
Traceback (most recent call last):
...
ValueError: Input contains NaN, ...


class sklearn.covariance.EllipticEnvelope(*, store_precision=True, assume_centered=False, support_fraction=None, contamination=0.1, random_state=None) [source]

An object for detecting outliers in a Gaussian distributed dataset. Read more in the User Guide.  Parameters

store_precisionbool, default=True

Specify if the estimated precision is stored.
assume_centeredbool, 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. Useful to work with data whose mean is significantly equal to zero but is not exactly zero. If False, the robust location and covariance are directly computed with the FastMCD algorithm without additional treatment.
support_fractionfloat, 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).
contaminationfloat, default=0.1

The amount of contamination of the data set, i.e. the proportion of outliers in the data set. Range is (0, 0.5).
random_stateint, RandomState instance or None, default=None

Determines the pseudo random number generator for shuffling the data. Pass an int for reproducible results across multiple function calls. See :term: Glossary <random_state>.    Attributes

location_ndarray of shape (n_features,)

Estimated robust location.
covariance_ndarray of shape (n_features, n_features)

Estimated robust covariance matrix.
precision_ndarray of shape (n_features, n_features)

Estimated pseudo inverse matrix. (stored only if store_precision is True)
support_ndarray of shape (n_samples,)

A mask of the observations that have been used to compute the robust estimates of location and shape.
offset_float

Offset used to define the decision function from the raw scores. We have the relation: decision_function = score_samples - offset_. The offset depends on the contamination parameter and is defined in such a way we obtain the expected number of outliers (samples with decision function < 0) in training.  New in version 0.20.
raw_location_ndarray of shape (n_features,)

The raw robust estimated location before correction and re-weighting.
raw_covariance_ndarray of shape (n_features, n_features)

The raw robust estimated covariance before correction and re-weighting.
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.
dist_ndarray of shape (n_samples,)

Mahalanobis distances of the training set (on which fit is called) observations.      See also
EmpiricalCovariance, MinCovDet

  Notes Outlier detection from covariance estimation may break or not perform well in high-dimensional settings. In particular, one will always take care to work with n_samples > n_features ** 2. References
1
Rousseeuw, P.J., Van Driessen, K. “A fast algorithm for the minimum covariance determinant estimator” Technometrics 41(3), 212 (1999)   Examples >>> import numpy as np
>>> from sklearn.covariance import EllipticEnvelope
>>> true_cov = np.array([[.8, .3],
...                      [.3, .4]])
>>> X = np.random.RandomState(0).multivariate_normal(mean=[0, 0],
...                                                  cov=true_cov,
...                                                  size=500)
>>> cov = EllipticEnvelope(random_state=0).fit(X)
>>> # predict returns 1 for an inlier and -1 for an outlier
>>> cov.predict([[0, 0],
...              [3, 3]])
array([ 1, -1])
>>> cov.covariance_
array([[0.7411..., 0.2535...],
       [0.2535..., 0.3053...]])
>>> cov.location_
array([0.0813... , 0.0427...])
 Methods
correct_covariance(data) Apply a correction to raw Minimum Covariance Determinant estimates.
decision_function(X) Compute the decision function of the given observations.
error_norm(comp_cov[, norm, scaling, squared]) Computes the Mean Squared Error between two covariance estimators.
fit(X[, y]) Fit the EllipticEnvelope model.
fit_predict(X[, y]) Perform fit on X and returns labels for X.
get_params([deep]) Get parameters for this estimator.
get_precision() Getter for the precision matrix.
mahalanobis(X) Computes the squared Mahalanobis distances of given observations.
predict(X) Predict the labels (1 inlier, -1 outlier) of X according to the fitted model.
reweight_covariance(data) Re-weight raw Minimum Covariance Determinant estimates.
score(X, y[, sample_weight]) Returns the mean accuracy on the given test data and labels.
score_samples(X) Compute the negative Mahalanobis distances.
set_params(**params) Set the parameters of this estimator.
correct_covariance(data) [source]

Apply a correction to raw Minimum Covariance Determinant estimates. Correction using the empirical correction factor suggested by Rousseeuw and Van Driessen in [RVD].  Parameters

dataarray-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_correctedndarray 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

decision_function(X) [source]

Compute the decision function of the given observations.  Parameters

Xarray-like of shape (n_samples, n_features)

The data matrix.    Returns

decisionndarray 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.

error_norm(comp_cov, norm='frobenius', scaling=True, squared=True) [source]

Computes the Mean Squared Error between two covariance estimators. (In the sense of the Frobenius norm).  Parameters

comp_covarray-like of shape (n_features, n_features)

The covariance to compare with.
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_).
scalingbool, default=True

If True (default), the squared error norm is divided by n_features. If False, the squared error norm is not rescaled.
squaredbool, 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

resultfloat

The Mean Squared Error (in the sense of the Frobenius norm) between self and comp_cov covariance estimators.

fit(X, y=None) [source]

Fit the EllipticEnvelope model.  Parameters

X{array-like, sparse matrix} of shape (n_samples, n_features)

Training data.
yIgnored

Not used, present for API consistency by convention.

fit_predict(X, y=None) [source]

Perform fit on X and returns labels for X. Returns -1 for outliers and 1 for inliers.  Parameters

X{array-like, sparse matrix, dataframe} of shape (n_samples, n_features)

yIgnored

Not used, present for API consistency by convention.    Returns

yndarray of shape (n_samples,)

1 for inliers, -1 for outliers.

get_params(deep=True) [source]

Get parameters for this estimator.  Parameters

deepbool, default=True

If True, will return the parameters for this estimator and contained subobjects that are estimators.    Returns

paramsdict

Parameter names mapped to their values.

get_precision() [source]

Getter for the precision matrix.  Returns

precision_array-like of shape (n_features, n_features)

The precision matrix associated to the current covariance object.

mahalanobis(X) [source]

Computes the squared Mahalanobis distances of given observations.  Parameters

Xarray-like of shape (n_samples, n_features)

The observations, the Mahalanobis distances of the which we compute. Observations are assumed to be drawn from the same distribution than the data used in fit.    Returns

distndarray of shape (n_samples,)

Squared Mahalanobis distances of the observations.

predict(X) [source]

Predict the labels (1 inlier, -1 outlier) of X according to the fitted model.  Parameters

Xarray-like of shape (n_samples, n_features)

The data matrix.    Returns

is_inlierndarray of shape (n_samples,)

Returns -1 for anomalies/outliers and +1 for inliers.

reweight_covariance(data) [source]

Re-weight raw Minimum Covariance Determinant estimates. Re-weight observations using Rousseeuw’s method (equivalent to deleting outlying observations from the data set before computing location and covariance estimates) described in [RVDriessen].  Parameters

dataarray-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

location_reweightedndarray of shape (n_features,)

Re-weighted robust location estimate.
covariance_reweightedndarray of shape (n_features, n_features)

Re-weighted robust covariance estimate.
support_reweightedndarray 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

score(X, y, sample_weight=None) [source]

Returns the mean accuracy on the given test data and labels. In multi-label classification, this is the subset accuracy which is a harsh metric since you require for each sample that each label set be correctly predicted.  Parameters

Xarray-like of shape (n_samples, n_features)

Test samples.
yarray-like of shape (n_samples,) or (n_samples, n_outputs)

True labels for X.
sample_weightarray-like of shape (n_samples,), default=None

Sample weights.    Returns

scorefloat

Mean accuracy of self.predict(X) w.r.t. y.

score_samples(X) [source]

Compute the negative Mahalanobis distances.  Parameters

Xarray-like of shape (n_samples, n_features)

The data matrix.    Returns

negative_mahal_distancesarray-like of shape (n_samples,)

Opposite of the Mahalanobis distances.

set_params(**params) [source]

Set the parameters of this estimator. The method works on simple estimators as well as on nested objects (such as Pipeline). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.  Parameters

**paramsdict

Estimator parameters.    Returns

selfestimator instance

Estimator instance.


sklearn.covariance.EllipticEnvelope
class sklearn.covariance.EllipticEnvelope(*, store_precision=True, assume_centered=False, support_fraction=None, contamination=0.1, random_state=None) [source]

An object for detecting outliers in a Gaussian distributed dataset. Read more in the User Guide.  Parameters

store_precisionbool, default=True

Specify if the estimated precision is stored.
assume_centeredbool, 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. Useful to work with data whose mean is significantly equal to zero but is not exactly zero. If False, the robust location and covariance are directly computed with the FastMCD algorithm without additional treatment.
support_fractionfloat, 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).
contaminationfloat, default=0.1

The amount of contamination of the data set, i.e. the proportion of outliers in the data set. Range is (0, 0.5).
random_stateint, RandomState instance or None, default=None

Determines the pseudo random number generator for shuffling the data. Pass an int for reproducible results across multiple function calls. See :term: Glossary <random_state>.    Attributes

location_ndarray of shape (n_features,)

Estimated robust location.
covariance_ndarray of shape (n_features, n_features)

Estimated robust covariance matrix.
precision_ndarray of shape (n_features, n_features)

Estimated pseudo inverse matrix. (stored only if store_precision is True)
support_ndarray of shape (n_samples,)

A mask of the observations that have been used to compute the robust estimates of location and shape.
offset_float

Offset used to define the decision function from the raw scores. We have the relation: decision_function = score_samples - offset_. The offset depends on the contamination parameter and is defined in such a way we obtain the expected number of outliers (samples with decision function < 0) in training.  New in version 0.20.
raw_location_ndarray of shape (n_features,)

The raw robust estimated location before correction and re-weighting.
raw_covariance_ndarray of shape (n_features, n_features)

The raw robust estimated covariance before correction and re-weighting.
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.
dist_ndarray of shape (n_samples,)

Mahalanobis distances of the training set (on which fit is called) observations.      See also
EmpiricalCovariance, MinCovDet

  Notes Outlier detection from covariance estimation may break or not perform well in high-dimensional settings. In particular, one will always take care to work with n_samples > n_features ** 2. References
1
Rousseeuw, P.J., Van Driessen, K. “A fast algorithm for the minimum covariance determinant estimator” Technometrics 41(3), 212 (1999)   Examples >>> import numpy as np
>>> from sklearn.covariance import EllipticEnvelope
>>> true_cov = np.array([[.8, .3],
...                      [.3, .4]])
>>> X = np.random.RandomState(0).multivariate_normal(mean=[0, 0],
...                                                  cov=true_cov,
...                                                  size=500)
>>> cov = EllipticEnvelope(random_state=0).fit(X)
>>> # predict returns 1 for an inlier and -1 for an outlier
>>> cov.predict([[0, 0],
...              [3, 3]])
array([ 1, -1])
>>> cov.covariance_
array([[0.7411..., 0.2535...],
       [0.2535..., 0.3053...]])
>>> cov.location_
array([0.0813... , 0.0427...])
 Methods
correct_covariance(data) Apply a correction to raw Minimum Covariance Determinant estimates.
decision_function(X) Compute the decision function of the given observations.
error_norm(comp_cov[, norm, scaling, squared]) Computes the Mean Squared Error between two covariance estimators.
fit(X[, y]) Fit the EllipticEnvelope model.
fit_predict(X[, y]) Perform fit on X and returns labels for X.
get_params([deep]) Get parameters for this estimator.
get_precision() Getter for the precision matrix.
mahalanobis(X) Computes the squared Mahalanobis distances of given observations.
predict(X) Predict the labels (1 inlier, -1 outlier) of X according to the fitted model.
reweight_covariance(data) Re-weight raw Minimum Covariance Determinant estimates.
score(X, y[, sample_weight]) Returns the mean accuracy on the given test data and labels.
score_samples(X) Compute the negative Mahalanobis distances.
set_params(**params) Set the parameters of this estimator.
correct_covariance(data) [source]

Apply a correction to raw Minimum Covariance Determinant estimates. Correction using the empirical correction factor suggested by Rousseeuw and Van Driessen in [RVD].  Parameters

dataarray-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_correctedndarray 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

decision_function(X) [source]

Compute the decision function of the given observations.  Parameters

Xarray-like of shape (n_samples, n_features)

The data matrix.    Returns

decisionndarray 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.

error_norm(comp_cov, norm='frobenius', scaling=True, squared=True) [source]

Computes the Mean Squared Error between two covariance estimators. (In the sense of the Frobenius norm).  Parameters

comp_covarray-like of shape (n_features, n_features)

The covariance to compare with.
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_).
scalingbool, default=True

If True (default), the squared error norm is divided by n_features. If False, the squared error norm is not rescaled.
squaredbool, 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

resultfloat

The Mean Squared Error (in the sense of the Frobenius norm) between self and comp_cov covariance estimators.

fit(X, y=None) [source]

Fit the EllipticEnvelope model.  Parameters

X{array-like, sparse matrix} of shape (n_samples, n_features)

Training data.
yIgnored

Not used, present for API consistency by convention.

fit_predict(X, y=None) [source]

Perform fit on X and returns labels for X. Returns -1 for outliers and 1 for inliers.  Parameters

X{array-like, sparse matrix, dataframe} of shape (n_samples, n_features)

yIgnored

Not used, present for API consistency by convention.    Returns

yndarray of shape (n_samples,)

1 for inliers, -1 for outliers.

get_params(deep=True) [source]

Get parameters for this estimator.  Parameters

deepbool, default=True

If True, will return the parameters for this estimator and contained subobjects that are estimators.    Returns

paramsdict

Parameter names mapped to their values.

get_precision() [source]

Getter for the precision matrix.  Returns

precision_array-like of shape (n_features, n_features)

The precision matrix associated to the current covariance object.

mahalanobis(X) [source]

Computes the squared Mahalanobis distances of given observations.  Parameters

Xarray-like of shape (n_samples, n_features)

The observations, the Mahalanobis distances of the which we compute. Observations are assumed to be drawn from the same distribution than the data used in fit.    Returns

distndarray of shape (n_samples,)

Squared Mahalanobis distances of the observations.

predict(X) [source]

Predict the labels (1 inlier, -1 outlier) of X according to the fitted model.  Parameters

Xarray-like of shape (n_samples, n_features)

The data matrix.    Returns

is_inlierndarray of shape (n_samples,)

Returns -1 for anomalies/outliers and +1 for inliers.

reweight_covariance(data) [source]

Re-weight raw Minimum Covariance Determinant estimates. Re-weight observations using Rousseeuw’s method (equivalent to deleting outlying observations from the data set before computing location and covariance estimates) described in [RVDriessen].  Parameters

dataarray-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

location_reweightedndarray of shape (n_features,)

Re-weighted robust location estimate.
covariance_reweightedndarray of shape (n_features, n_features)

Re-weighted robust covariance estimate.
support_reweightedndarray 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

score(X, y, sample_weight=None) [source]

Returns the mean accuracy on the given test data and labels. In multi-label classification, this is the subset accuracy which is a harsh metric since you require for each sample that each label set be correctly predicted.  Parameters

Xarray-like of shape (n_samples, n_features)

Test samples.
yarray-like of shape (n_samples,) or (n_samples, n_outputs)

True labels for X.
sample_weightarray-like of shape (n_samples,), default=None

Sample weights.    Returns

scorefloat

Mean accuracy of self.predict(X) w.r.t. y.

score_samples(X) [source]

Compute the negative Mahalanobis distances.  Parameters

Xarray-like of shape (n_samples, n_features)

The data matrix.    Returns

negative_mahal_distancesarray-like of shape (n_samples,)

Opposite of the Mahalanobis distances.

set_params(**params) [source]

Set the parameters of this estimator. The method works on simple estimators as well as on nested objects (such as Pipeline). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.  Parameters

**paramsdict

Estimator parameters.    Returns

selfestimator instance

Estimator instance.

 Examples using sklearn.covariance.EllipticEnvelope

  Outlier detection on a real data set

  Comparing anomaly detection algorithms for outlier detection on toy datasets


correct_covariance(data) [source]

Apply a correction to raw Minimum Covariance Determinant estimates. Correction using the empirical correction factor suggested by Rousseeuw and Van Driessen in [RVD].  Parameters

dataarray-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_correctedndarray 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


decision_function(X) [source]

Compute the decision function of the given observations.  Parameters

Xarray-like of shape (n_samples, n_features)

The data matrix.    Returns

decisionndarray 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.


error_norm(comp_cov, norm='frobenius', scaling=True, squared=True) [source]

Computes the Mean Squared Error between two covariance estimators. (In the sense of the Frobenius norm).  Parameters

comp_covarray-like of shape (n_features, n_features)

The covariance to compare with.
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_).
scalingbool, default=True

If True (default), the squared error norm is divided by n_features. If False, the squared error norm is not rescaled.
squaredbool, 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

resultfloat

The Mean Squared Error (in the sense of the Frobenius norm) between self and comp_cov covariance estimators.


fit(X, y=None) [source]

Fit the EllipticEnvelope model.  Parameters

X{array-like, sparse matrix} of shape (n_samples, n_features)

Training data.
yIgnored

Not used, present for API consistency by convention.


fit_predict(X, y=None) [source]

Perform fit on X and returns labels for X. Returns -1 for outliers and 1 for inliers.  Parameters

X{array-like, sparse matrix, dataframe} of shape (n_samples, n_features)

yIgnored

Not used, present for API consistency by convention.    Returns

yndarray of shape (n_samples,)

1 for inliers, -1 for outliers.


get_precision() [source]

Getter for the precision matrix.  Returns

precision_array-like of shape (n_features, n_features)

The precision matrix associated to the current covariance object.


mahalanobis(X) [source]

Computes the squared Mahalanobis distances of given observations.  Parameters

Xarray-like of shape (n_samples, n_features)

The observations, the Mahalanobis distances of the which we compute. Observations are assumed to be drawn from the same distribution than the data used in fit.    Returns

distndarray of shape (n_samples,)

Squared Mahalanobis distances of the observations.


predict(X) [source]

Predict the labels (1 inlier, -1 outlier) of X according to the fitted model.  Parameters

Xarray-like of shape (n_samples, n_features)

The data matrix.    Returns

is_inlierndarray of shape (n_samples,)

Returns -1 for anomalies/outliers and +1 for inliers.


reweight_covariance(data) [source]

Re-weight raw Minimum Covariance Determinant estimates. Re-weight observations using Rousseeuw’s method (equivalent to deleting outlying observations from the data set before computing location and covariance estimates) described in [RVDriessen].  Parameters

dataarray-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

location_reweightedndarray of shape (n_features,)

Re-weighted robust location estimate.
covariance_reweightedndarray of shape (n_features, n_features)

Re-weighted robust covariance estimate.
support_reweightedndarray 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


score(X, y, sample_weight=None) [source]

Returns the mean accuracy on the given test data and labels. In multi-label classification, this is the subset accuracy which is a harsh metric since you require for each sample that each label set be correctly predicted.  Parameters

Xarray-like of shape (n_samples, n_features)

Test samples.
yarray-like of shape (n_samples,) or (n_samples, n_outputs)

True labels for X.
sample_weightarray-like of shape (n_samples,), default=None

Sample weights.    Returns

scorefloat

Mean accuracy of self.predict(X) w.r.t. y.


score_samples(X) [source]

Compute the negative Mahalanobis distances.  Parameters

Xarray-like of shape (n_samples, n_features)

The data matrix.    Returns

negative_mahal_distancesarray-like of shape (n_samples,)

Opposite of the Mahalanobis distances.


class sklearn.covariance.EmpiricalCovariance(*, store_precision=True, assume_centered=False) [source]

Maximum likelihood covariance estimator Read more in the User Guide.  Parameters

store_precisionbool, default=True

Specifies if the estimated precision is stored.
assume_centeredbool, default=False

If True, data are not centered before computation. Useful when working with data whose mean is almost, but not exactly zero. If False (default), data are centered before computation.    Attributes

location_ndarray of shape (n_features,)

Estimated location, i.e. the estimated mean.
covariance_ndarray of shape (n_features, n_features)

Estimated covariance matrix
precision_ndarray of 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]])
>>> rng = np.random.RandomState(0)
>>> X = rng.multivariate_normal(mean=[0, 0],
...                             cov=real_cov,
...                             size=500)
>>> cov = EmpiricalCovariance().fit(X)
>>> cov.covariance_
array([[0.7569..., 0.2818...],
       [0.2818..., 0.3928...]])
>>> cov.location_
array([0.0622..., 0.0193...])
 Methods
error_norm(comp_cov[, norm, scaling, squared]) Computes the Mean Squared Error between two covariance estimators.
fit(X[, y]) Fits the Maximum Likelihood Estimator covariance model according to the given training data and parameters.
get_params([deep]) Get parameters for this estimator.
get_precision() Getter for the precision matrix.
mahalanobis(X) Computes the squared Mahalanobis distances of given observations.
score(X_test[, y]) Computes the log-likelihood of a Gaussian data set with self.covariance_ as an estimator of its covariance matrix.
set_params(**params) Set the parameters of this estimator.
error_norm(comp_cov, norm='frobenius', scaling=True, squared=True) [source]

Computes the Mean Squared Error between two covariance estimators. (In the sense of the Frobenius norm).  Parameters

comp_covarray-like of shape (n_features, n_features)

The covariance to compare with.
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_).
scalingbool, default=True

If True (default), the squared error norm is divided by n_features. If False, the squared error norm is not rescaled.
squaredbool, 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

resultfloat

The Mean Squared Error (in the sense of the Frobenius norm) between self and comp_cov covariance estimators.

fit(X, y=None) [source]

Fits the Maximum Likelihood Estimator covariance model according to the given training data and parameters.  Parameters

Xarray-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.
yIgnored

Not used, present for API consistency by convention.    Returns

selfobject

get_params(deep=True) [source]

Get parameters for this estimator.  Parameters

deepbool, default=True

If True, will return the parameters for this estimator and contained subobjects that are estimators.    Returns

paramsdict

Parameter names mapped to their values.

get_precision() [source]

Getter for the precision matrix.  Returns

precision_array-like of shape (n_features, n_features)

The precision matrix associated to the current covariance object.

mahalanobis(X) [source]

Computes the squared Mahalanobis distances of given observations.  Parameters

Xarray-like of shape (n_samples, n_features)

The observations, the Mahalanobis distances of the which we compute. Observations are assumed to be drawn from the same distribution than the data used in fit.    Returns

distndarray of shape (n_samples,)

Squared Mahalanobis distances of the observations.

score(X_test, y=None) [source]

Computes the log-likelihood of a Gaussian data set with self.covariance_ as an estimator of its covariance matrix.  Parameters

X_testarray-like of shape (n_samples, n_features)

Test data of which we compute the likelihood, where n_samples is the number of samples and n_features is the number of features. X_test is assumed to be drawn from the same distribution than the data used in fit (including centering).
yIgnored

Not used, present for API consistency by convention.    Returns

resfloat

The likelihood of the data set with self.covariance_ as an estimator of its covariance matrix.

set_params(**params) [source]

Set the parameters of this estimator. The method works on simple estimators as well as on nested objects (such as Pipeline). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.  Parameters

**paramsdict

Estimator parameters.    Returns

selfestimator instance

Estimator instance.


sklearn.covariance.EmpiricalCovariance
class sklearn.covariance.EmpiricalCovariance(*, store_precision=True, assume_centered=False) [source]

Maximum likelihood covariance estimator Read more in the User Guide.  Parameters

store_precisionbool, default=True

Specifies if the estimated precision is stored.
assume_centeredbool, default=False

If True, data are not centered before computation. Useful when working with data whose mean is almost, but not exactly zero. If False (default), data are centered before computation.    Attributes

location_ndarray of shape (n_features,)

Estimated location, i.e. the estimated mean.
covariance_ndarray of shape (n_features, n_features)

Estimated covariance matrix
precision_ndarray of 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]])
>>> rng = np.random.RandomState(0)
>>> X = rng.multivariate_normal(mean=[0, 0],
...                             cov=real_cov,
...                             size=500)
>>> cov = EmpiricalCovariance().fit(X)
>>> cov.covariance_
array([[0.7569..., 0.2818...],
       [0.2818..., 0.3928...]])
>>> cov.location_
array([0.0622..., 0.0193...])
 Methods
error_norm(comp_cov[, norm, scaling, squared]) Computes the Mean Squared Error between two covariance estimators.
fit(X[, y]) Fits the Maximum Likelihood Estimator covariance model according to the given training data and parameters.
get_params([deep]) Get parameters for this estimator.
get_precision() Getter for the precision matrix.
mahalanobis(X) Computes the squared Mahalanobis distances of given observations.
score(X_test[, y]) Computes the log-likelihood of a Gaussian data set with self.covariance_ as an estimator of its covariance matrix.
set_params(**params) Set the parameters of this estimator.
error_norm(comp_cov, norm='frobenius', scaling=True, squared=True) [source]

Computes the Mean Squared Error between two covariance estimators. (In the sense of the Frobenius norm).  Parameters

comp_covarray-like of shape (n_features, n_features)

The covariance to compare with.
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_).
scalingbool, default=True

If True (default), the squared error norm is divided by n_features. If False, the squared error norm is not rescaled.
squaredbool, 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

resultfloat

The Mean Squared Error (in the sense of the Frobenius norm) between self and comp_cov covariance estimators.

fit(X, y=None) [source]

Fits the Maximum Likelihood Estimator covariance model according to the given training data and parameters.  Parameters

Xarray-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.
yIgnored

Not used, present for API consistency by convention.    Returns

selfobject

get_params(deep=True) [source]

Get parameters for this estimator.  Parameters

deepbool, default=True

If True, will return the parameters for this estimator and contained subobjects that are estimators.    Returns

paramsdict

Parameter names mapped to their values.

get_precision() [source]

Getter for the precision matrix.  Returns

precision_array-like of shape (n_features, n_features)

The precision matrix associated to the current covariance object.

mahalanobis(X) [source]

Computes the squared Mahalanobis distances of given observations.  Parameters

Xarray-like of shape (n_samples, n_features)

The observations, the Mahalanobis distances of the which we compute. Observations are assumed to be drawn from the same distribution than the data used in fit.    Returns

distndarray of shape (n_samples,)

Squared Mahalanobis distances of the observations.

score(X_test, y=None) [source]

Computes the log-likelihood of a Gaussian data set with self.covariance_ as an estimator of its covariance matrix.  Parameters

X_testarray-like of shape (n_samples, n_features)

Test data of which we compute the likelihood, where n_samples is the number of samples and n_features is the number of features. X_test is assumed to be drawn from the same distribution than the data used in fit (including centering).
yIgnored

Not used, present for API consistency by convention.    Returns

resfloat

The likelihood of the data set with self.covariance_ as an estimator of its covariance matrix.

set_params(**params) [source]

Set the parameters of this estimator. The method works on simple estimators as well as on nested objects (such as Pipeline). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.  Parameters

**paramsdict

Estimator parameters.    Returns

selfestimator instance

Estimator instance.

 Examples using sklearn.covariance.EmpiricalCovariance

  Shrinkage covariance estimation: LedoitWolf vs OAS and max-likelihood

  Robust covariance estimation and Mahalanobis distances relevance

  Robust vs Empirical covariance estimate


fit(X, y=None) [source]

Fits the Maximum Likelihood Estimator covariance model according to the given training data and parameters.  Parameters

Xarray-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.
yIgnored

Not used, present for API consistency by convention.    Returns

selfobject


get_params(deep=True) [source]

Get parameters for this estimator.  Parameters

deepbool, default=True

If True, will return the parameters for this estimator and contained subobjects that are estimators.    Returns

paramsdict

Parameter names mapped to their values.


score(X_test, y=None) [source]

Computes the log-likelihood of a Gaussian data set with self.covariance_ as an estimator of its covariance matrix.  Parameters

X_testarray-like of shape (n_samples, n_features)

Test data of which we compute the likelihood, where n_samples is the number of samples and n_features is the number of features. X_test is assumed to be drawn from the same distribution than the data used in fit (including centering).
yIgnored

Not used, present for API consistency by convention.    Returns

resfloat

The likelihood of the data set with self.covariance_ as an estimator of its covariance matrix.


set_params(**params) [source]

Set the parameters of this estimator. The method works on simple estimators as well as on nested objects (such as Pipeline). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.  Parameters

**paramsdict

Estimator parameters.    Returns

selfestimator instance

Estimator instance.


sklearn.covariance.empirical_covariance(X, *, assume_centered=False) [source]

Computes the Maximum likelihood covariance estimator  Parameters

Xndarray of shape (n_samples, n_features)

Data from which to compute the covariance estimate
assume_centeredbool, 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.    Returns

covariancendarray of shape (n_features, n_features)

Empirical covariance (Maximum Likelihood Estimator).     Examples >>> from sklearn.covariance import empirical_covariance
>>> X = [[1,1,1],[1,1,1],[1,1,1],
...      [0,0,0],[0,0,0],[0,0,0]]
>>> empirical_covariance(X)
array([[0.25, 0.25, 0.25],
       [0.25, 0.25, 0.25],
       [0.25, 0.25, 0.25]])


class sklearn.covariance.GraphicalLasso(alpha=0.01, *, mode='cd', tol=0.0001, enet_tol=0.0001, max_iter=100, verbose=False, assume_centered=False) [source]

Sparse inverse covariance estimation with an l1-penalized estimator. Read more in the User Guide.  Changed in version v0.20: GraphLasso has been renamed to GraphicalLasso   Parameters

alphafloat, 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.
tolfloat, default=1e-4

The tolerance to declare convergence: if the dual gap goes below this value, iterations are stopped. Range is (0, inf].
enet_tolfloat, 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’. Range is (0, inf].
max_iterint, default=100

The maximum number of iterations.
verbosebool, default=False

If verbose is True, the objective function and dual gap are plotted at each iteration.
assume_centeredbool, default=False

If True, data are not centered before computation. Useful when working with data whose mean is almost, but not exactly zero. If False, data are centered before computation.    Attributes

location_ndarray of shape (n_features,)

Estimated location, i.e. the estimated mean.
covariance_ndarray of shape (n_features, n_features)

Estimated covariance matrix
precision_ndarray of shape (n_features, n_features)

Estimated pseudo inverse matrix.
n_iter_int

Number of iterations run.      See also
graphical_lasso, GraphicalLassoCV

  Examples >>> import numpy as np
>>> from sklearn.covariance import GraphicalLasso
>>> true_cov = np.array([[0.8, 0.0, 0.2, 0.0],
...                      [0.0, 0.4, 0.0, 0.0],
...                      [0.2, 0.0, 0.3, 0.1],
...                      [0.0, 0.0, 0.1, 0.7]])
>>> np.random.seed(0)
>>> X = np.random.multivariate_normal(mean=[0, 0, 0, 0],
...                                   cov=true_cov,
...                                   size=200)
>>> cov = GraphicalLasso().fit(X)
>>> np.around(cov.covariance_, decimals=3)
array([[0.816, 0.049, 0.218, 0.019],
       [0.049, 0.364, 0.017, 0.034],
       [0.218, 0.017, 0.322, 0.093],
       [0.019, 0.034, 0.093, 0.69 ]])
>>> np.around(cov.location_, decimals=3)
array([0.073, 0.04 , 0.038, 0.143])
 Methods
error_norm(comp_cov[, norm, scaling, squared]) Computes the Mean Squared Error between two covariance estimators.
fit(X[, y]) Fits the GraphicalLasso model to X.
get_params([deep]) Get parameters for this estimator.
get_precision() Getter for the precision matrix.
mahalanobis(X) Computes the squared Mahalanobis distances of given observations.
score(X_test[, y]) Computes the log-likelihood of a Gaussian data set with self.covariance_ as an estimator of its covariance matrix.
set_params(**params) Set the parameters of this estimator.
error_norm(comp_cov, norm='frobenius', scaling=True, squared=True) [source]

Computes the Mean Squared Error between two covariance estimators. (In the sense of the Frobenius norm).  Parameters

comp_covarray-like of shape (n_features, n_features)

The covariance to compare with.
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_).
scalingbool, default=True

If True (default), the squared error norm is divided by n_features. If False, the squared error norm is not rescaled.
squaredbool, 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

resultfloat

The Mean Squared Error (in the sense of the Frobenius norm) between self and comp_cov covariance estimators.

fit(X, y=None) [source]

Fits the GraphicalLasso model to X.  Parameters

Xarray-like of shape (n_samples, n_features)

Data from which to compute the covariance estimate
yIgnored

Not used, present for API consistency by convention.    Returns

selfobject

get_params(deep=True) [source]

Get parameters for this estimator.  Parameters

deepbool, default=True

If True, will return the parameters for this estimator and contained subobjects that are estimators.    Returns

paramsdict

Parameter names mapped to their values.

get_precision() [source]

Getter for the precision matrix.  Returns

precision_array-like of shape (n_features, n_features)

The precision matrix associated to the current covariance object.

mahalanobis(X) [source]

Computes the squared Mahalanobis distances of given observations.  Parameters

Xarray-like of shape (n_samples, n_features)

The observations, the Mahalanobis distances of the which we compute. Observations are assumed to be drawn from the same distribution than the data used in fit.    Returns

distndarray of shape (n_samples,)

Squared Mahalanobis distances of the observations.

score(X_test, y=None) [source]

Computes the log-likelihood of a Gaussian data set with self.covariance_ as an estimator of its covariance matrix.  Parameters

X_testarray-like of shape (n_samples, n_features)

Test data of which we compute the likelihood, where n_samples is the number of samples and n_features is the number of features. X_test is assumed to be drawn from the same distribution than the data used in fit (including centering).
yIgnored

Not used, present for API consistency by convention.    Returns

resfloat

The likelihood of the data set with self.covariance_ as an estimator of its covariance matrix.

set_params(**params) [source]

Set the parameters of this estimator. The method works on simple estimators as well as on nested objects (such as Pipeline). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.  Parameters

**paramsdict

Estimator parameters.    Returns

selfestimator instance

Estimator instance.


sklearn.covariance.GraphicalLasso
class sklearn.covariance.GraphicalLasso(alpha=0.01, *, mode='cd', tol=0.0001, enet_tol=0.0001, max_iter=100, verbose=False, assume_centered=False) [source]

Sparse inverse covariance estimation with an l1-penalized estimator. Read more in the User Guide.  Changed in version v0.20: GraphLasso has been renamed to GraphicalLasso   Parameters

alphafloat, 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.
tolfloat, default=1e-4

The tolerance to declare convergence: if the dual gap goes below this value, iterations are stopped. Range is (0, inf].
enet_tolfloat, 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’. Range is (0, inf].
max_iterint, default=100

The maximum number of iterations.
verbosebool, default=False

If verbose is True, the objective function and dual gap are plotted at each iteration.
assume_centeredbool, default=False

If True, data are not centered before computation. Useful when working with data whose mean is almost, but not exactly zero. If False, data are centered before computation.    Attributes

location_ndarray of shape (n_features,)

Estimated location, i.e. the estimated mean.
covariance_ndarray of shape (n_features, n_features)

Estimated covariance matrix
precision_ndarray of shape (n_features, n_features)

Estimated pseudo inverse matrix.
n_iter_int

Number of iterations run.      See also
graphical_lasso, GraphicalLassoCV

  Examples >>> import numpy as np
>>> from sklearn.covariance import GraphicalLasso
>>> true_cov = np.array([[0.8, 0.0, 0.2, 0.0],
...                      [0.0, 0.4, 0.0, 0.0],
...                      [0.2, 0.0, 0.3, 0.1],
...                      [0.0, 0.0, 0.1, 0.7]])
>>> np.random.seed(0)
>>> X = np.random.multivariate_normal(mean=[0, 0, 0, 0],
...                                   cov=true_cov,
...                                   size=200)
>>> cov = GraphicalLasso().fit(X)
>>> np.around(cov.covariance_, decimals=3)
array([[0.816, 0.049, 0.218, 0.019],
       [0.049, 0.364, 0.017, 0.034],
       [0.218, 0.017, 0.322, 0.093],
       [0.019, 0.034, 0.093, 0.69 ]])
>>> np.around(cov.location_, decimals=3)
array([0.073, 0.04 , 0.038, 0.143])
 Methods
error_norm(comp_cov[, norm, scaling, squared]) Computes the Mean Squared Error between two covariance estimators.
fit(X[, y]) Fits the GraphicalLasso model to X.
get_params([deep]) Get parameters for this estimator.
get_precision() Getter for the precision matrix.
mahalanobis(X) Computes the squared Mahalanobis distances of given observations.
score(X_test[, y]) Computes the log-likelihood of a Gaussian data set with self.covariance_ as an estimator of its covariance matrix.
set_params(**params) Set the parameters of this estimator.
error_norm(comp_cov, norm='frobenius', scaling=True, squared=True) [source]

Computes the Mean Squared Error between two covariance estimators. (In the sense of the Frobenius norm).  Parameters

comp_covarray-like of shape (n_features, n_features)

The covariance to compare with.
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_).
scalingbool, default=True

If True (default), the squared error norm is divided by n_features. If False, the squared error norm is not rescaled.
squaredbool, 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

resultfloat

The Mean Squared Error (in the sense of the Frobenius norm) between self and comp_cov covariance estimators.

fit(X, y=None) [source]

Fits the GraphicalLasso model to X.  Parameters

Xarray-like of shape (n_samples, n_features)

Data from which to compute the covariance estimate
yIgnored

Not used, present for API consistency by convention.    Returns

selfobject

get_params(deep=True) [source]

Get parameters for this estimator.  Parameters

deepbool, default=True

If True, will return the parameters for this estimator and contained subobjects that are estimators.    Returns

paramsdict

Parameter names mapped to their values.

get_precision() [source]

Getter for the precision matrix.  Returns

precision_array-like of shape (n_features, n_features)

The precision matrix associated to the current covariance object.

mahalanobis(X) [source]

Computes the squared Mahalanobis distances of given observations.  Parameters

Xarray-like of shape (n_samples, n_features)

The observations, the Mahalanobis distances of the which we compute. Observations are assumed to be drawn from the same distribution than the data used in fit.    Returns

distndarray of shape (n_samples,)

Squared Mahalanobis distances of the observations.

score(X_test, y=None) [source]

Computes the log-likelihood of a Gaussian data set with self.covariance_ as an estimator of its covariance matrix.  Parameters

X_testarray-like of shape (n_samples, n_features)

Test data of which we compute the likelihood, where n_samples is the number of samples and n_features is the number of features. X_test is assumed to be drawn from the same distribution than the data used in fit (including centering).
yIgnored

Not used, present for API consistency by convention.    Returns

resfloat

The likelihood of the data set with self.covariance_ as an estimator of its covariance matrix.

set_params(**params) [source]

Set the parameters of this estimator. The method works on simple estimators as well as on nested objects (such as Pipeline). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.  Parameters

**paramsdict

Estimator parameters.    Returns

selfestimator instance

Estimator instance.


error_norm(comp_cov, norm='frobenius', scaling=True, squared=True) [source]

Computes the Mean Squared Error between two covariance estimators. (In the sense of the Frobenius norm).  Parameters

comp_covarray-like of shape (n_features, n_features)

The covariance to compare with.
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_).
scalingbool, default=True

If True (default), the squared error norm is divided by n_features. If False, the squared error norm is not rescaled.
squaredbool, 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

resultfloat

The Mean Squared Error (in the sense of the Frobenius norm) between self and comp_cov covariance estimators.


fit(X, y=None) [source]

Fits the GraphicalLasso model to X.  Parameters

Xarray-like of shape (n_samples, n_features)

Data from which to compute the covariance estimate
yIgnored

Not used, present for API consistency by convention.    Returns

selfobject


get_precision() [source]

Getter for the precision matrix.  Returns

precision_array-like of shape (n_features, n_features)

The precision matrix associated to the current covariance object.


mahalanobis(X) [source]

Computes the squared Mahalanobis distances of given observations.  Parameters

Xarray-like of shape (n_samples, n_features)

The observations, the Mahalanobis distances of the which we compute. Observations are assumed to be drawn from the same distribution than the data used in fit.    Returns

distndarray of shape (n_samples,)

Squared Mahalanobis distances of the observations.


class sklearn.covariance.GraphicalLassoCV(*, alphas=4, n_refinements=4, cv=None, tol=0.0001, enet_tol=0.0001, max_iter=100, mode='cd', n_jobs=None, verbose=False, assume_centered=False) [source]

Sparse inverse covariance w/ cross-validated choice of the l1 penalty. See glossary entry for cross-validation estimator. Read more in the User Guide.  Changed in version v0.20: GraphLassoCV has been renamed to GraphicalLassoCV   Parameters

alphasint 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 more details. Range is (0, inf] when floats given.
n_refinementsint, default=4

The number of times the grid is refined. Not used if explicit values of alphas are passed. Range is [1, inf).
cvint, cross-validation generator or iterable, default=None

Determines the cross-validation splitting strategy. Possible inputs for cv are:  None, to use the default 5-fold cross-validation, integer, to specify the number of folds.
CV splitter, An iterable yielding (train, test) splits as arrays of indices.  For integer/None inputs KFold is used. Refer User Guide for the various cross-validation strategies that can be used here.  Changed in version 0.20: cv default value if None changed from 3-fold to 5-fold.
tolfloat, default=1e-4

The tolerance to declare convergence: if the dual gap goes below this value, iterations are stopped. Range is (0, inf].
enet_tolfloat, 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’. Range is (0, inf].
max_iterint, default=100

Maximum number of iterations.
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_jobsint, default=None

number of jobs to run in parallel. None means 1 unless in a joblib.parallel_backend context. -1 means using all processors. See Glossary for more details.  Changed in version v0.20: n_jobs default changed from 1 to None
verbosebool, default=False

If verbose is True, the objective function and duality gap are printed at each iteration.
assume_centeredbool, default=False

If True, data are not centered before computation. Useful when working with data whose mean is almost, but not exactly zero. If False, data are centered before computation.    Attributes

location_ndarray of shape (n_features,)

Estimated location, i.e. the estimated mean.
covariance_ndarray of shape (n_features, n_features)

Estimated covariance matrix.
precision_ndarray of shape (n_features, n_features)

Estimated precision matrix (inverse covariance).
alpha_float

Penalization parameter selected.
cv_alphas_list of shape (n_alphas,), dtype=float

All penalization parameters explored.  Deprecated since version 0.24: The cv_alphas_ attribute is deprecated in version 0.24 in favor of cv_results_['alphas'] and will be removed in version 1.1 (renaming of 0.26).
grid_scores_ndarray of shape (n_alphas, n_folds)

Log-likelihood score on left-out data across folds.  Deprecated since version 0.24: The grid_scores_ attribute is deprecated in version 0.24 in favor of cv_results_ and will be removed in version 1.1 (renaming of 0.26).
cv_results_dict of ndarrays

A dict with keys:
alphasndarray of shape (n_alphas,)

All penalization parameters explored.
split(k)_scorendarray of shape (n_alphas,)

Log-likelihood score on left-out data across (k)th fold.
mean_scorendarray of shape (n_alphas,)

Mean of scores over the folds.
std_scorendarray of shape (n_alphas,)

Standard deviation of scores over the folds.    New in version 0.24.
n_iter_int

Number of iterations run for the optimal alpha.      See also
graphical_lasso, GraphicalLasso

  Notes The search for the optimal penalization parameter (alpha) is done on an iteratively refined grid: first the cross-validated scores on a grid are computed, then a new refined grid is centered around the maximum, and so on. One of the challenges which is faced here is that the solvers can fail to converge to a well-conditioned estimate. The corresponding values of alpha then come out as missing values, but the optimum may be close to these missing values. Examples >>> import numpy as np
>>> from sklearn.covariance import GraphicalLassoCV
>>> true_cov = np.array([[0.8, 0.0, 0.2, 0.0],
...                      [0.0, 0.4, 0.0, 0.0],
...                      [0.2, 0.0, 0.3, 0.1],
...                      [0.0, 0.0, 0.1, 0.7]])
>>> np.random.seed(0)
>>> X = np.random.multivariate_normal(mean=[0, 0, 0, 0],
...                                   cov=true_cov,
...                                   size=200)
>>> cov = GraphicalLassoCV().fit(X)
>>> np.around(cov.covariance_, decimals=3)
array([[0.816, 0.051, 0.22 , 0.017],
       [0.051, 0.364, 0.018, 0.036],
       [0.22 , 0.018, 0.322, 0.094],
       [0.017, 0.036, 0.094, 0.69 ]])
>>> np.around(cov.location_, decimals=3)
array([0.073, 0.04 , 0.038, 0.143])
 Methods
error_norm(comp_cov[, norm, scaling, squared]) Computes the Mean Squared Error between two covariance estimators.
fit(X[, y]) Fits the GraphicalLasso covariance model to X.
get_params([deep]) Get parameters for this estimator.
get_precision() Getter for the precision matrix.
mahalanobis(X) Computes the squared Mahalanobis distances of given observations.
score(X_test[, y]) Computes the log-likelihood of a Gaussian data set with self.covariance_ as an estimator of its covariance matrix.
set_params(**params) Set the parameters of this estimator.
error_norm(comp_cov, norm='frobenius', scaling=True, squared=True) [source]

Computes the Mean Squared Error between two covariance estimators. (In the sense of the Frobenius norm).  Parameters

comp_covarray-like of shape (n_features, n_features)

The covariance to compare with.
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_).
scalingbool, default=True

If True (default), the squared error norm is divided by n_features. If False, the squared error norm is not rescaled.
squaredbool, 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

resultfloat

The Mean Squared Error (in the sense of the Frobenius norm) between self and comp_cov covariance estimators.

fit(X, y=None) [source]

Fits the GraphicalLasso covariance model to X.  Parameters

Xarray-like of shape (n_samples, n_features)

Data from which to compute the covariance estimate
yIgnored

Not used, present for API consistency by convention.    Returns

selfobject

get_params(deep=True) [source]

Get parameters for this estimator.  Parameters

deepbool, default=True

If True, will return the parameters for this estimator and contained subobjects that are estimators.    Returns

paramsdict

Parameter names mapped to their values.

get_precision() [source]

Getter for the precision matrix.  Returns

precision_array-like of shape (n_features, n_features)

The precision matrix associated to the current covariance object.

mahalanobis(X) [source]

Computes the squared Mahalanobis distances of given observations.  Parameters

Xarray-like of shape (n_samples, n_features)

The observations, the Mahalanobis distances of the which we compute. Observations are assumed to be drawn from the same distribution than the data used in fit.    Returns

distndarray of shape (n_samples,)

Squared Mahalanobis distances of the observations.

score(X_test, y=None) [source]

Computes the log-likelihood of a Gaussian data set with self.covariance_ as an estimator of its covariance matrix.  Parameters

X_testarray-like of shape (n_samples, n_features)

Test data of which we compute the likelihood, where n_samples is the number of samples and n_features is the number of features. X_test is assumed to be drawn from the same distribution than the data used in fit (including centering).
yIgnored

Not used, present for API consistency by convention.    Returns

resfloat

The likelihood of the data set with self.covariance_ as an estimator of its covariance matrix.

set_params(**params) [source]

Set the parameters of this estimator. The method works on simple estimators as well as on nested objects (such as Pipeline). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.  Parameters

**paramsdict

Estimator parameters.    Returns

selfestimator instance

Estimator instance.


sklearn.covariance.GraphicalLassoCV
class sklearn.covariance.GraphicalLassoCV(*, alphas=4, n_refinements=4, cv=None, tol=0.0001, enet_tol=0.0001, max_iter=100, mode='cd', n_jobs=None, verbose=False, assume_centered=False) [source]

Sparse inverse covariance w/ cross-validated choice of the l1 penalty. See glossary entry for cross-validation estimator. Read more in the User Guide.  Changed in version v0.20: GraphLassoCV has been renamed to GraphicalLassoCV   Parameters

alphasint 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 more details. Range is (0, inf] when floats given.
n_refinementsint, default=4

The number of times the grid is refined. Not used if explicit values of alphas are passed. Range is [1, inf).
cvint, cross-validation generator or iterable, default=None

Determines the cross-validation splitting strategy. Possible inputs for cv are:  None, to use the default 5-fold cross-validation, integer, to specify the number of folds.
CV splitter, An iterable yielding (train, test) splits as arrays of indices.  For integer/None inputs KFold is used. Refer User Guide for the various cross-validation strategies that can be used here.  Changed in version 0.20: cv default value if None changed from 3-fold to 5-fold.
tolfloat, default=1e-4

The tolerance to declare convergence: if the dual gap goes below this value, iterations are stopped. Range is (0, inf].
enet_tolfloat, 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’. Range is (0, inf].
max_iterint, default=100

Maximum number of iterations.
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_jobsint, default=None

number of jobs to run in parallel. None means 1 unless in a joblib.parallel_backend context. -1 means using all processors. See Glossary for more details.  Changed in version v0.20: n_jobs default changed from 1 to None
verbosebool, default=False

If verbose is True, the objective function and duality gap are printed at each iteration.
assume_centeredbool, default=False

If True, data are not centered before computation. Useful when working with data whose mean is almost, but not exactly zero. If False, data are centered before computation.    Attributes

location_ndarray of shape (n_features,)

Estimated location, i.e. the estimated mean.
covariance_ndarray of shape (n_features, n_features)

Estimated covariance matrix.
precision_ndarray of shape (n_features, n_features)

Estimated precision matrix (inverse covariance).
alpha_float

Penalization parameter selected.
cv_alphas_list of shape (n_alphas,), dtype=float

All penalization parameters explored.  Deprecated since version 0.24: The cv_alphas_ attribute is deprecated in version 0.24 in favor of cv_results_['alphas'] and will be removed in version 1.1 (renaming of 0.26).
grid_scores_ndarray of shape (n_alphas, n_folds)

Log-likelihood score on left-out data across folds.  Deprecated since version 0.24: The grid_scores_ attribute is deprecated in version 0.24 in favor of cv_results_ and will be removed in version 1.1 (renaming of 0.26).
cv_results_dict of ndarrays

A dict with keys:
alphasndarray of shape (n_alphas,)

All penalization parameters explored.
split(k)_scorendarray of shape (n_alphas,)

Log-likelihood score on left-out data across (k)th fold.
mean_scorendarray of shape (n_alphas,)

Mean of scores over the folds.
std_scorendarray of shape (n_alphas,)

Standard deviation of scores over the folds.    New in version 0.24.
n_iter_int

Number of iterations run for the optimal alpha.      See also
graphical_lasso, GraphicalLasso

  Notes The search for the optimal penalization parameter (alpha) is done on an iteratively refined grid: first the cross-validated scores on a grid are computed, then a new refined grid is centered around the maximum, and so on. One of the challenges which is faced here is that the solvers can fail to converge to a well-conditioned estimate. The corresponding values of alpha then come out as missing values, but the optimum may be close to these missing values. Examples >>> import numpy as np
>>> from sklearn.covariance import GraphicalLassoCV
>>> true_cov = np.array([[0.8, 0.0, 0.2, 0.0],
...                      [0.0, 0.4, 0.0, 0.0],
...                      [0.2, 0.0, 0.3, 0.1],
...                      [0.0, 0.0, 0.1, 0.7]])
>>> np.random.seed(0)
>>> X = np.random.multivariate_normal(mean=[0, 0, 0, 0],
...                                   cov=true_cov,
...                                   size=200)
>>> cov = GraphicalLassoCV().fit(X)
>>> np.around(cov.covariance_, decimals=3)
array([[0.816, 0.051, 0.22 , 0.017],
       [0.051, 0.364, 0.018, 0.036],
       [0.22 , 0.018, 0.322, 0.094],
       [0.017, 0.036, 0.094, 0.69 ]])
>>> np.around(cov.location_, decimals=3)
array([0.073, 0.04 , 0.038, 0.143])
 Methods
error_norm(comp_cov[, norm, scaling, squared]) Computes the Mean Squared Error between two covariance estimators.
fit(X[, y]) Fits the GraphicalLasso covariance model to X.
get_params([deep]) Get parameters for this estimator.
get_precision() Getter for the precision matrix.
mahalanobis(X) Computes the squared Mahalanobis distances of given observations.
score(X_test[, y]) Computes the log-likelihood of a Gaussian data set with self.covariance_ as an estimator of its covariance matrix.
set_params(**params) Set the parameters of this estimator.
error_norm(comp_cov, norm='frobenius', scaling=True, squared=True) [source]

Computes the Mean Squared Error between two covariance estimators. (In the sense of the Frobenius norm).  Parameters

comp_covarray-like of shape (n_features, n_features)

The covariance to compare with.
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_).
scalingbool, default=True

If True (default), the squared error norm is divided by n_features. If False, the squared error norm is not rescaled.
squaredbool, 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

resultfloat

The Mean Squared Error (in the sense of the Frobenius norm) between self and comp_cov covariance estimators.

fit(X, y=None) [source]

Fits the GraphicalLasso covariance model to X.  Parameters

Xarray-like of shape (n_samples, n_features)

Data from which to compute the covariance estimate
yIgnored

Not used, present for API consistency by convention.    Returns

selfobject

get_params(deep=True) [source]

Get parameters for this estimator.  Parameters

deepbool, default=True

If True, will return the parameters for this estimator and contained subobjects that are estimators.    Returns

paramsdict

Parameter names mapped to their values.

get_precision() [source]

Getter for the precision matrix.  Returns

precision_array-like of shape (n_features, n_features)

The precision matrix associated to the current covariance object.

mahalanobis(X) [source]

Computes the squared Mahalanobis distances of given observations.  Parameters

Xarray-like of shape (n_samples, n_features)

The observations, the Mahalanobis distances of the which we compute. Observations are assumed to be drawn from the same distribution than the data used in fit.    Returns

distndarray of shape (n_samples,)

Squared Mahalanobis distances of the observations.

score(X_test, y=None) [source]

Computes the log-likelihood of a Gaussian data set with self.covariance_ as an estimator of its covariance matrix.  Parameters

X_testarray-like of shape (n_samples, n_features)

Test data of which we compute the likelihood, where n_samples is the number of samples and n_features is the number of features. X_test is assumed to be drawn from the same distribution than the data used in fit (including centering).
yIgnored

Not used, present for API consistency by convention.    Returns

resfloat

The likelihood of the data set with self.covariance_ as an estimator of its covariance matrix.

set_params(**params) [source]

Set the parameters of this estimator. The method works on simple estimators as well as on nested objects (such as Pipeline). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.  Parameters

**paramsdict

Estimator parameters.    Returns

selfestimator instance

Estimator instance.

 Examples using sklearn.covariance.GraphicalLassoCV

  Sparse inverse covariance estimation

  Visualizing the stock market structure


fit(X, y=None) [source]

Fits the GraphicalLasso covariance model to X.  Parameters

Xarray-like of shape (n_samples, n_features)

Data from which to compute the covariance estimate
yIgnored

Not used, present for API consistency by convention.    Returns

selfobject


sklearn.covariance.graphical_lasso(emp_cov, alpha, *, cov_init=None, mode='cd', tol=0.0001, enet_tol=0.0001, max_iter=100, verbose=False, return_costs=False, eps=2.220446049250313e-16, return_n_iter=False) [source]

l1-penalized covariance estimator Read more in the User Guide.  Changed in version v0.20: graph_lasso has been renamed to graphical_lasso   Parameters

emp_covndarray of shape (n_features, n_features)

Empirical covariance from which to compute the covariance estimate.
alphafloat

The regularization parameter: the higher alpha, the more regularization, the sparser the inverse covariance. Range is (0, inf].
cov_initarray of shape (n_features, n_features), default=None

The initial guess for the covariance. If None, then the empirical covariance is used.
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.
tolfloat, default=1e-4

The tolerance to declare convergence: if the dual gap goes below this value, iterations are stopped. Range is (0, inf].
enet_tolfloat, 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’. Range is (0, inf].
max_iterint, default=100

The maximum number of iterations.
verbosebool, default=False

If verbose is True, the objective function and dual gap are printed at each iteration.
return_costsbool, default=Flase

If return_costs is True, the objective function and dual gap at each iteration are returned.
epsfloat, default=eps

The machine-precision regularization in the computation of the Cholesky diagonal factors. Increase this for very ill-conditioned systems. Default is np.finfo(np.float64).eps.
return_n_iterbool, default=False

Whether or not to return the number of iterations.    Returns

covariancendarray of shape (n_features, n_features)

The estimated covariance matrix.
precisionndarray of shape (n_features, n_features)

The estimated (sparse) precision matrix.
costslist of (objective, dual_gap) pairs

The list of values of the objective function and the dual gap at each iteration. Returned only if return_costs is True.
n_iterint

Number of iterations. Returned only if return_n_iter is set to True.      See also
GraphicalLasso, GraphicalLassoCV

  Notes The algorithm employed to solve this problem is the GLasso algorithm, from the Friedman 2008 Biostatistics paper. It is the same algorithm as in the R glasso package. One possible difference with the glasso R package is that the diagonal coefficients are not penalized.


class sklearn.covariance.LedoitWolf(*, store_precision=True, assume_centered=False, block_size=1000) [source]

LedoitWolf Estimator Ledoit-Wolf is a particular form of shrinkage, where the shrinkage coefficient is computed using O. Ledoit and M. Wolf’s formula as described in “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. Read more in the User Guide.  Parameters

store_precisionbool, default=True

Specify if the estimated precision is stored.
assume_centeredbool, 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 (default), data will be centered before computation.
block_sizeint, default=1000

Size of blocks into which the covariance matrix will be split during its Ledoit-Wolf estimation. This is purely a memory optimization and does not affect results.    Attributes

covariance_ndarray of shape (n_features, n_features)

Estimated covariance matrix.
location_ndarray of shape (n_features,)

Estimated location, i.e. the estimated mean.
precision_ndarray of shape (n_features, n_features)

Estimated pseudo inverse matrix. (stored only if store_precision is True)
shrinkage_float

Coefficient in the convex combination used for the computation of the shrunk estimate. Range is [0, 1].     Notes The regularised covariance is: (1 - shrinkage) * cov + shrinkage * mu * np.identity(n_features) where mu = trace(cov) / n_features and shrinkage is given by the Ledoit and Wolf formula (see References) References “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. Examples >>> import numpy as np
>>> from sklearn.covariance import LedoitWolf
>>> real_cov = np.array([[.4, .2],
...                      [.2, .8]])
>>> np.random.seed(0)
>>> X = np.random.multivariate_normal(mean=[0, 0],
...                                   cov=real_cov,
...                                   size=50)
>>> cov = LedoitWolf().fit(X)
>>> cov.covariance_
array([[0.4406..., 0.1616...],
       [0.1616..., 0.8022...]])
>>> cov.location_
array([ 0.0595... , -0.0075...])
 Methods
error_norm(comp_cov[, norm, scaling, squared]) Computes the Mean Squared Error between two covariance estimators.
fit(X[, y]) Fit the Ledoit-Wolf shrunk covariance model according to the given training data and parameters.
get_params([deep]) Get parameters for this estimator.
get_precision() Getter for the precision matrix.
mahalanobis(X) Computes the squared Mahalanobis distances of given observations.
score(X_test[, y]) Computes the log-likelihood of a Gaussian data set with self.covariance_ as an estimator of its covariance matrix.
set_params(**params) Set the parameters of this estimator.
error_norm(comp_cov, norm='frobenius', scaling=True, squared=True) [source]

Computes the Mean Squared Error between two covariance estimators. (In the sense of the Frobenius norm).  Parameters

comp_covarray-like of shape (n_features, n_features)

The covariance to compare with.
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_).
scalingbool, default=True

If True (default), the squared error norm is divided by n_features. If False, the squared error norm is not rescaled.
squaredbool, 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

resultfloat

The Mean Squared Error (in the sense of the Frobenius norm) between self and comp_cov covariance estimators.

fit(X, y=None) [source]

Fit the Ledoit-Wolf shrunk covariance model according to the given training data and parameters.  Parameters

Xarray-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.
yIgnored

Not used, present for API consistency by convention.    Returns

selfobject

get_params(deep=True) [source]

Get parameters for this estimator.  Parameters

deepbool, default=True

If True, will return the parameters for this estimator and contained subobjects that are estimators.    Returns

paramsdict

Parameter names mapped to their values.

get_precision() [source]

Getter for the precision matrix.  Returns

precision_array-like of shape (n_features, n_features)

The precision matrix associated to the current covariance object.

mahalanobis(X) [source]

Computes the squared Mahalanobis distances of given observations.  Parameters

Xarray-like of shape (n_samples, n_features)

The observations, the Mahalanobis distances of the which we compute. Observations are assumed to be drawn from the same distribution than the data used in fit.    Returns

distndarray of shape (n_samples,)

Squared Mahalanobis distances of the observations.

score(X_test, y=None) [source]

Computes the log-likelihood of a Gaussian data set with self.covariance_ as an estimator of its covariance matrix.  Parameters

X_testarray-like of shape (n_samples, n_features)

Test data of which we compute the likelihood, where n_samples is the number of samples and n_features is the number of features. X_test is assumed to be drawn from the same distribution than the data used in fit (including centering).
yIgnored

Not used, present for API consistency by convention.    Returns

resfloat

The likelihood of the data set with self.covariance_ as an estimator of its covariance matrix.

set_params(**params) [source]

Set the parameters of this estimator. The method works on simple estimators as well as on nested objects (such as Pipeline). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.  Parameters

**paramsdict

Estimator parameters.    Returns

selfestimator instance

Estimator instance.