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

Code before:
    assert_allclose(expected, W)


@pytest.mark.parametrize("solver", SOLVERS)
@pytest.mark.parametrize("csr_container", CSR_CONTAINERS)
def test_large_sparse_matrix(solver, global_random_seed, csr_container):
    # Solvers either accept large sparse matrices, or raise helpful error.
    # Non-regression test for pull-request #21093.

    # generate sparse matrix with int64 indices
    X = csr_container(sparse.rand(20, 10, random_state=global_random_seed))
    for attr in ["indices", "indptr"]:
        setattr(X, attr, getattr(X, attr).astype("int64"))
    rng = np.random.RandomState(global_random_seed)
    y = rng.randint(2, size=X.shape[0])

    if solver in ["liblinear", "sag", "saga"]:
        msg = "Only sparse matrices with 32-bit integer indices"
        with pytest.raises(ValueError, match=msg):
            LogisticRegression(solver=solver).fit(X, y)
    else:
        LogisticRegression(solver=solver).fit(X, y)


def test_liblinear_with_large_values():
    # Liblinear raises error when data size is more than 1e100.

    # generate sparse matrix with int64 indices
    X = np.array([0, 1e100]).reshape(-1, 1)
    y = np.array([0, 1])

    msg = (
        "Using the 'liblinear' solver while X contains a maximum "
        "value>1e30 results in a frozen fit. Please choose another solver."
    )
    with pytest.raises(ValueError, match=msg):
        LogisticRegression(solver="liblinear").fit(X, y)


def test_single_feature_newton_cg():
    # Test that Newton-CG works with a single feature and intercept.
    # Non-regression test for issue #23605.

    X = np.array([[0.5, 0.65, 1.1, 1.25, 0.8, 0.54, 0.95, 0.7]]).T
    y = np.array([1, 1, 0, 0, 1, 1, 0, 1])
    assert X.shape[1] == 1
    LogisticRegression(solver="newton-cg", fit_intercept=True).fit(X, y)


def test_liblinear_not_stuck(global_random_seed):
    # Non-regression https://github.com/scikit-learn/scikit-learn/issues/18264

Review comment (suggestion):
```suggestion
    # Liblinear freezes when X.max() ~ 1e100, see issue #7486.
    # We preemptively raise an error when X.max() > 1e30.
```

Code after:
    assert_allclose(expected, W)


@pytest.mark.parametrize("solver", SOLVERS)
@pytest.mark.parametrize("csr_container", CSR_CONTAINERS)
def test_large_sparse_matrix(solver, global_random_seed, csr_container):
    # Solvers either accept large sparse matrices, or raise helpful error.
    # Non-regression test for pull-request #21093.

    # generate sparse matrix with int64 indices
    X = csr_container(sparse.rand(20, 10, random_state=global_random_seed))
    for attr in ["indices", "indptr"]:
        setattr(X, attr, getattr(X, attr).astype("int64"))
    rng = np.random.RandomState(global_random_seed)
    y = rng.randint(2, size=X.shape[0])

    if solver in ["liblinear", "sag", "saga"]:
        msg = "Only sparse matrices with 32-bit integer indices"
        with pytest.raises(ValueError, match=msg):
            LogisticRegression(solver=solver).fit(X, y)
    else:
        LogisticRegression(solver=solver).fit(X, y)


def test_liblinear_with_large_values():
    # Liblinear freezes when X.max() ~ 1e100, see issue #7486.
    # We preemptively raise an error when X.max() > 1e30.

    # generate sparse matrix with int64 indices
    X = np.array([0, 1e100]).reshape(-1, 1)
    y = np.array([0, 1])

    msg = (
        "Using the 'liblinear' solver while X contains a maximum "
        "value > 1e30 results in a frozen fit. Please choose another "
        "solver or rescale the input X."
    )
    with pytest.raises(ValueError, match=msg):
        LogisticRegression(solver="liblinear").fit(X, y)


def test_single_feature_newton_cg():
    # Test that Newton-CG works with a single feature and intercept.
    # Non-regression test for issue #23605.

    X = np.array([[0.5, 0.65, 1.1, 1.25, 0.8, 0.54, 0.95, 0.7]]).T
    y = np.array([1, 1, 0, 0, 1, 1, 0, 1])
    assert X.shape[1] == 1
    LogisticRegression(solver="newton-cg", fit_intercept=True).fit(X, y)