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

Code before:
@pytest.mark.filterwarnings("ignore:With alpha=0, this algorithm:UserWarning")
@pytest.mark.parametrize("precompute", [False, True])
@pytest.mark.parametrize("effective_rank", [None, 10])
def test_enet_ols_consistency(precompute, effective_rank, global_random_seed):
    """Test that ElasticNet(alpha=0) converges to the same solution as OLS."""
    rng = np.random.RandomState(global_random_seed)
    n_samples = 300
    X, y = make_regression(
        n_samples=n_samples,
        n_features=100,
        effective_rank=effective_rank,
        n_informative=50,
        random_state=rng,
    )
    sw = rng.uniform(low=0.01, high=10, size=X.shape[0])

    ols = LinearRegression().fit(X, y, sample_weight=sw)
    enet = ElasticNet(alpha=0, precompute=precompute, tol=1e-15).fit(
        X, y, sample_weight=sw
    )

    # Might be a singular problem, so check for same predictions
    assert_allclose(enet.predict(X), ols.predict(X))
    # and for similar objective function (squared error)
    se_ols = np.sum((y - ols.predict(X)) ** 2)
    se_enet = np.sum((y - enet.predict(X)) ** 2)
    assert se_ols <= 1.2e-20
    assert se_enet <= 1.2e-20
    # We check equal coefficients, but "only" with absolute tolerance.
    assert_allclose(enet.coef_, ols.coef_, atol=1e-11)
    assert_allclose(enet.intercept_, ols.intercept_, atol=1.2e-12)


@pytest.mark.parametrize(
    "estimator",
    [
        Lasso(alpha=1.0),
        ElasticNet(alpha=1.0, l1_ratio=0.1),
    ],
)
def test_sample_weight_invariance(estimator):
    rng = np.random.RandomState(42)
    X, y = make_regression(
        n_samples=100,
        n_features=300,
        effective_rank=10,
        n_informative=50,
        random_state=rng,
    )
    sw = rng.uniform(low=0.01, high=2, size=X.shape[0])
    params = dict(tol=1e-12)

Review comment (suggestion):
```suggestion
se_ols = np.sum(sw * (y - ols.predict(X)) ** 2)
se_enet = np.sum(sw * (y - enet.predict(X)) ** 2)
```

Code after:
@pytest.mark.filterwarnings("ignore:With alpha=0, this algorithm:UserWarning")
@pytest.mark.parametrize("precompute", [False, True])
@pytest.mark.parametrize("effective_rank", [None, 10])
def test_enet_ols_consistency(precompute, effective_rank, global_random_seed):
    """Test that ElasticNet(alpha=0) converges to the same solution as OLS."""
    rng = np.random.RandomState(global_random_seed)
    n_samples = 300
    X, y = make_regression(
        n_samples=n_samples,
        n_features=100,
        effective_rank=effective_rank,
        n_informative=50,
        random_state=rng,
    )
    sw = rng.uniform(low=0.01, high=10, size=X.shape[0])

    ols = LinearRegression().fit(X, y, sample_weight=sw)
    enet = ElasticNet(alpha=0, precompute=precompute, tol=1e-15).fit(
        X, y, sample_weight=sw
    )

    # Might be a singular problem, so check for same predictions
    assert_allclose(enet.predict(X), ols.predict(X))
    # and for similar objective function (squared error)
    se_ols = np.sum(sw * (y - ols.predict(X)) ** 2)
    se_enet = np.sum(sw * (y - enet.predict(X)) ** 2)
    assert se_ols <= 1e-19
    assert se_enet <= 1e-19
    # We check equal coefficients, but "only" with absolute tolerance.
    assert_allclose(enet.coef_, ols.coef_, atol=1e-11)
    assert_allclose(enet.intercept_, ols.intercept_, atol=1e-11)


@pytest.mark.parametrize(
    "estimator",
    [
        Lasso(alpha=1.0),
        ElasticNet(alpha=1.0, l1_ratio=0.1),
    ],
)
def test_sample_weight_invariance(estimator):
    rng = np.random.RandomState(42)
    X, y = make_regression(
        n_samples=100,
        n_features=300,
        effective_rank=10,
        n_informative=50,
        random_state=rng,
    )
    sw = rng.uniform(low=0.01, high=2, size=X.shape[0])
    params = dict(tol=1e-12)