Skip to content

Commit

Permalink
MAINT Parameters validation for sklearn.linear_model.lars_path (sciki…
Browse files Browse the repository at this point in the history
…t-learn#26381)

Co-authored-by: Guillaume Lemaitre <[email protected]>
  • Loading branch information
jiawei-zhang-a and glemaitre committed Jul 6, 2023
1 parent 77d4bc1 commit 3444673
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 13 deletions.
41 changes: 30 additions & 11 deletions sklearn/linear_model/_least_angle.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,32 @@

# mypy error: Module 'sklearn.utils' has no attribute 'arrayfuncs'
from ..utils import arrayfuncs, as_float_array, check_random_state # type: ignore
from ..utils._param_validation import Hidden, Interval, StrOptions
from ..utils._param_validation import Hidden, Interval, StrOptions, validate_params
from ..utils.parallel import Parallel, delayed
from ._base import LinearModel, LinearRegression, _deprecate_normalize, _preprocess_data

SOLVE_TRIANGULAR_ARGS = {"check_finite": False}


@validate_params(
{
"X": [np.ndarray, None],
"y": [np.ndarray, None],
"Xy": [np.ndarray, None],
"Gram": [StrOptions({"auto"}), "boolean", np.ndarray, None],
"max_iter": [Interval(Integral, 0, None, closed="left")],
"alpha_min": [Interval(Real, 0, None, closed="left")],
"method": [StrOptions({"lar", "lasso"})],
"copy_X": ["boolean"],
"eps": [Interval(Real, 0, None, closed="neither"), None],
"copy_Gram": ["boolean"],
"verbose": ["verbose"],
"return_path": ["boolean"],
"return_n_iter": ["boolean"],
"positive": ["boolean"],
},
prefer_skip_nested_validation=True,
)
def lars_path(
X,
y,
Expand Down Expand Up @@ -60,21 +79,21 @@ def lars_path(
Parameters
----------
X : None or array-like of shape (n_samples, n_features)
X : None or ndarray of shape (n_samples, n_features)
Input data. Note that if X is `None` then the Gram matrix must be
specified, i.e., cannot be `None` or `False`.
y : None or array-like of shape (n_samples,)
y : None or ndarray of shape (n_samples,)
Input targets.
Xy : array-like of shape (n_features,) or (n_features, n_targets), \
default=None
`Xy = np.dot(X.T, y)` that can be precomputed. It is useful
`Xy = X.T @ y` that can be precomputed. It is useful
only when the Gram matrix is precomputed.
Gram : None, 'auto', array-like of shape (n_features, n_features), \
Gram : None, 'auto', bool, ndarray of shape (n_features, n_features), \
default=None
Precomputed Gram matrix (X' * X), if `'auto'`, the Gram
Precomputed Gram matrix `X.T @ X`, if `'auto'`, the Gram
matrix is precomputed from the given X, if there are more samples
than features.
Expand Down Expand Up @@ -123,20 +142,20 @@ def lars_path(
Returns
-------
alphas : array-like of shape (n_alphas + 1,)
alphas : ndarray of shape (n_alphas + 1,)
Maximum of covariances (in absolute value) at each iteration.
`n_alphas` is either `max_iter`, `n_features`, or the
number of nodes in the path with `alpha >= alpha_min`, whichever
is smaller.
active : array-like of shape (n_alphas,)
active : ndarray of shape (n_alphas,)
Indices of active variables at the end of the path.
coefs : array-like of shape (n_features, n_alphas + 1)
coefs : ndarray of shape (n_features, n_alphas + 1)
Coefficients along the path.
n_iter : int
Number of iterations run. Returned only if return_n_iter is set
Number of iterations run. Returned only if `return_n_iter` is set
to True.
See Also
Expand Down Expand Up @@ -1361,7 +1380,7 @@ def _lars_path_residues(
y_test,
Gram=None,
copy=True,
method="lars",
method="lar",
verbose=False,
fit_intercept=True,
normalize=False,
Expand Down
4 changes: 2 additions & 2 deletions sklearn/linear_model/tests/test_least_angle.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ def test_lars_path_gram_equivalent(method, return_path):
def test_x_none_gram_none_raises_value_error():
# Test that lars_path with no X and Gram raises exception
Xy = np.dot(X.T, y)
with pytest.raises(ValueError):
with pytest.raises(ValueError, match="X and Gram cannot both be unspecified"):
linear_model.lars_path(None, y, Gram=None, Xy=Xy)


Expand Down Expand Up @@ -804,7 +804,7 @@ def test_lars_with_jitter(est):

def test_X_none_gram_not_none():
with pytest.raises(ValueError, match="X cannot be None if Gram is not None"):
lars_path(X=None, y=[1], Gram="not None")
lars_path(X=None, y=np.array([1]), Gram=True)


def test_copy_X_with_auto_gram():
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 @@ -201,6 +201,7 @@ def _check_function_param_validation(
"sklearn.isotonic.check_increasing",
"sklearn.isotonic.isotonic_regression",
"sklearn.linear_model.enet_path",
"sklearn.linear_model.lars_path",
"sklearn.linear_model.lasso_path",
"sklearn.linear_model.orthogonal_mp",
"sklearn.linear_model.orthogonal_mp_gram",
Expand Down

0 comments on commit 3444673

Please sign in to comment.