GraphicalLasso
- class sklearn.covariance.GraphicalLasso(alpha=0.01, *, mode='cd', covariance=None, tol=0.0001, enet_tol=0.0001, max_iter=, , ,
Sparse inverse covariance estimation with an l1-penalized estimator.
For a usage example see Visualizing the stock market structure.
Read more in the User Guide.
The regularization parameter: the higher alpha, the more regularization, the sparser the inverse covariance. Range is (0, inf].
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.
If covariance is “precomputed”, the input data in fit is assumed
to be the covariance matrix. If None, the empirical covariance
is estimated from the data X.
The tolerance to declare convergence: if the dual gap goes below this value, iterations are stopped. Range is (0, inf].
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].
The maximum number of iterations.
If verbose is True, the objective function and dual gap are plotted at each iteration.
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.
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.
Estimated location, i.e. the estimated mean.
Estimated covariance matrix
Estimated pseudo inverse matrix.
Number of iterations run.
The list of values of the objective function and the dual gap at each iteration. Returned only if return_costs is True.
>>> 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,
Compute the Mean Squared Error between two covariance estimators.
The covariance to compare with.
Fit the GraphicalLasso model to X.
Data from which to compute the covariance estimate.
Not used, present for API consistency by convention.
Get metadata routing of this object.
Please check User Guide on how the routing mechanism works.
A MetadataRequest encapsulating
routing information.
Get parameters for this estimator.
If True, will return the parameters for this estimator and contained subobjects that are estimators.
Parameter names mapped to their values.
Getter for the precision matrix.
The precision matrix associated to the current covariance object.
Compute the squared Mahalanobis distances of given observations.
For a detailed example of how outliers affects the Mahalanobis distance, see Robust covariance estimation and Mahalanobis distances relevance.
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.
Compute the log-likelihood of X_test under the estimated Gaussian model.
The Gaussian model is defined by its mean and covariance matrix which are
represented respectively by self.location_ and self.covariance_.
Test data of which we compute the likelihood, where is
the number of samples and is the number of features.
is assumed to be drawn from the same distribution than
the data used in fit (including centering).
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.
Estimator parameters.
Number of features seen during fit.
n_features_in_,)Names of features seen during fit. Defined only when X
has feature names that are all strings.
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_).
If True (default), the squared error norm is divided by n_features. If False, the squared error norm is not rescaled.
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.
The Mean Squared Error (in the sense of the Frobenius norm) between
self and comp_cov covariance estimators.
Returns the instance itself.
Squared Mahalanobis distances of the observations.
n_featuresX_testNot used, present for API consistency by convention.
The log-likelihood of X_test with self.location_ and self.covariance_
as estimators of the Gaussian model mean and covariance matrix respectively.
Estimator instance.