Skip to content

MAINT parameter validation for covariance.graphical_lasso #25911

New issue

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

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

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions sklearn/covariance/_graph_lasso.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
check_scalar,
)
from ..utils.parallel import delayed, Parallel
from ..utils._param_validation import Interval, StrOptions
from ..utils._param_validation import validate_params, Interval, StrOptions

# mypy error: Module 'sklearn.linear_model' has no attribute '_cd_fast'
from ..linear_model import _cd_fast as cd_fast # type: ignore
Expand Down Expand Up @@ -78,6 +78,21 @@ def alpha_max(emp_cov):


# The g-lasso algorithm
@validate_params(
{
"emp_cov": ["array-like"],
"alpha": [Real],
"cov_init": ["array-like", None],
"mode": [StrOptions({"cd", "lars"})],
"tol": [Interval(Real, 0, None, closed="right")],
"enet_tol": [Interval(Real, 0, None, closed="right")],
"max_iter": [Interval(Integral, 0, None, closed="left")],
"verbose": ["verbose"],
"return_costs": ["boolean"],
"eps": [Real],
"return_n_iter": ["boolean"],
}
)
def graphical_lasso(
emp_cov,
alpha,
Expand All @@ -101,15 +116,15 @@ def graphical_lasso(

Parameters
----------
emp_cov : ndarray of shape (n_features, n_features)
emp_cov : array-like of shape (n_features, n_features)
Empirical covariance from which to compute the covariance estimate.

alpha : float
The regularization parameter: the higher alpha, the more
regularization, the sparser the inverse covariance.
Range is (0, inf].

cov_init : array of shape (n_features, n_features), default=None
cov_init : array-like of shape (n_features, n_features), default=None
The initial guess for the covariance. If None, then the empirical
covariance is used.

Expand Down Expand Up @@ -149,10 +164,10 @@ def graphical_lasso(

Returns
-------
covariance : ndarray of shape (n_features, n_features)
covariance : array-like of shape (n_features, n_features)
The estimated covariance matrix.

precision : ndarray of shape (n_features, n_features)
precision : array-like of shape (n_features, n_features)
The estimated (sparse) precision matrix.

costs : list of (objective, dual_gap) pairs
Expand Down
1 change: 1 addition & 0 deletions sklearn/tests/test_public_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ def _check_function_param_validation(
"sklearn.cluster.cluster_optics_xi",
"sklearn.cluster.ward_tree",
"sklearn.covariance.empirical_covariance",
"sklearn.covariance.graphical_lasso",
"sklearn.covariance.shrunk_covariance",
"sklearn.datasets.dump_svmlight_file",
"sklearn.datasets.fetch_20newsgroups",
Expand Down