Repository: scikit-learn/scikit-learn
File: sklearn/linear_model/tests/test_coordinate_descent.py (Python)

Code before:
    y = X @ beta
    params = dict(fit_intercept=fit_intercept, positive=positive)

    if X_is_sparse:
        X = sparse.csc_matrix(X)
    # Test alpha_max makes coefs zero.
    reg = ElasticNetCV(alphas=1, cv=2, eps=1, **params)
    reg.fit(X, y, sample_weight=sample_weight)
    assert_allclose(reg.coef_, 0, atol=1e-5)
    alpha_max = reg.alpha_
    # Test smaller alpha makes coefs nonzero.
    reg = ElasticNet(alpha=0.99 * alpha_max, tol=1e-8, **params)
    reg.fit(X, y, sample_weight=sample_weight)
    assert_array_less(1e-3, np.max(np.abs(reg.coef_)))

    if positive:
        # Make sure that the positive constraint changes alpha_max,
        # i.e. test the meaningfulness of the test data.
        not_positive_alpha_max = (
            ElasticNetCV(alphas=1, cv=2, eps=1, **{**params, "positive": not positive})
            .fit(X, y, sample_weight=sample_weight)
            .alpha_
        )
        assert not np.isclose(alpha_max, not_positive_alpha_max), (
            f"Test data cannot distinguish alpha_max between positive=True and False "
            f"for {locals()=}"
        )


@pytest.mark.parametrize("estimator", [ElasticNetCV, LassoCV])
def test_linear_models_cv_fit_with_loky(estimator):
    # LinearModelsCV.fit performs operations on fancy-indexed memmapped
    # data when using the loky backend, causing an error due to unexpected
    # behavior of fancy indexing of read-only memmaps (cf. numpy#14132).

    # Create a problem sufficiently large to cause memmapping (1MB).
    # Unfortunately the scikit-learn and joblib APIs do not make it possible to
    # change the max_nbyte of the inner Parallel call.
    X, y = make_regression(int(1e6) // 8 + 1, 1)
    assert X.nbytes > 1e6  # 1 MB
    with joblib.parallel_backend("loky"):
        estimator(n_jobs=2, cv=3).fit(X, y)


@pytest.mark.parametrize("check_input", [True, False])
def test_enet_sample_weight_does_not_overwrite_sample_weight(check_input):
    """Check that ElasticNet does not overwrite sample_weights."""

    rng = np.random.RandomState(0)
    n_samples, n_features = 10, 5


Review comment (suggestion):
```suggestion
            f"Test data cannot distinguish alpha_max between positive=True and False."
```

Code after:
    y = X @ beta
    params = dict(fit_intercept=fit_intercept, positive=positive)

    if X_is_sparse:
        X = sparse.csc_matrix(X)
    # Test alpha_max makes coefs zero.
    reg = ElasticNetCV(alphas=1, cv=2, eps=1, **params)
    reg.fit(X, y, sample_weight=sample_weight)
    assert_allclose(reg.coef_, 0, atol=1e-5)
    alpha_max = reg.alpha_
    # Test smaller alpha makes coefs nonzero.
    reg = ElasticNet(alpha=0.99 * alpha_max, tol=1e-8, **params)
    reg.fit(X, y, sample_weight=sample_weight)
    assert_array_less(1e-3, np.max(np.abs(reg.coef_)))

    if positive:
        # Make sure that the positive constraint changes alpha_max,
        # i.e. test the meaningfulness of the test data.
        not_positive_alpha_max = (
            ElasticNetCV(alphas=1, cv=2, eps=1, **{**params, "positive": not positive})
            .fit(X, y, sample_weight=sample_weight)
            .alpha_
        )
        assert not np.isclose(alpha_max, not_positive_alpha_max), (
            "Test data cannot distinguish alpha_max between positive=True and False."
        )


@pytest.mark.parametrize("estimator", [ElasticNetCV, LassoCV])
def test_linear_models_cv_fit_with_loky(estimator):
    # LinearModelsCV.fit performs operations on fancy-indexed memmapped
    # data when using the loky backend, causing an error due to unexpected
    # behavior of fancy indexing of read-only memmaps (cf. numpy#14132).

    # Create a problem sufficiently large to cause memmapping (1MB).
    # Unfortunately the scikit-learn and joblib APIs do not make it possible to
    # change the max_nbyte of the inner Parallel call.
    X, y = make_regression(int(1e6) // 8 + 1, 1)
    assert X.nbytes > 1e6  # 1 MB
    with joblib.parallel_backend("loky"):
        estimator(n_jobs=2, cv=3).fit(X, y)


@pytest.mark.parametrize("check_input", [True, False])
def test_enet_sample_weight_does_not_overwrite_sample_weight(check_input):
    """Check that ElasticNet does not overwrite sample_weights."""

    rng = np.random.RandomState(0)
    n_samples, n_features = 10, 5

    X = rng.rand(n_samples, n_features)