8000 FIX improve error message for large sparse matrix input in LogisticRegression by TomDLT · Pull Request #21093 · scikit-learn/scikit-learn · GitHub
[go: up one dir, main page]

Skip to content

FIX improve error message for large sparse matrix input in LogisticRegression #21093

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

Merged
merged 2 commits into from
Sep 24, 2021
Merged
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
7 changes: 7 additions & 0 deletions doc/whats_new/v1.1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ Changelog
:pr:`123456` by :user:`Joe Bloggs <joeongithub>`.
where 123456 is the *pull request* number, not the issue number.

:mod:`sklearn.linear_model`
...........................

- |Fix| :class:`linear_model.LogisticRegression` now raises a better error
message when the solver does not support sparse matrices with int64 indices.
:pr:`21093` by `Tom Dupre la Tour`_.

:mod:`sklearn.utils`
....................

Expand Down
6 changes: 3 additions & 3 deletions sklearn/linear_model/_logistic.py
Original file line number Diff line number Diff line change
Expand Up @@ -663,7 +663,7 @@ def _logistic_regression_path(
X,
accept_sparse="csr",
dtype=np.float64,
accept_large_sparse=solver != "liblinear",
accept_large_sparse=solver not in ["liblinear", "sag", "saga"],
)
y = check_array(y, ensure_2d=False, dtype=None)
check_consistent_length(X, y)
Expand Down Expand Up @@ -1511,7 +1511,7 @@ def fit(self, X, y, sample_weight=None):
accept_sparse="csr",
dtype=_dtype,
order="C",
accept_large_sparse=solver != "liblinear",
accept_large_sparse=solver not in ["liblinear", "sag", "saga"],
)
check_classification_targets(y)
self.classes_ = np.unique(y)
Expand Down Expand Up @@ -2080,7 +2080,7 @@ def fit(self, X, y, sample_weight=None):
accept_sparse="csr",
dtype=np.float64,
order="C",
accept_large_sparse=solver != "liblinear",
accept_large_sparse=solver not in ["liblinear", "sag", "saga"],
)
check_classification_targets(y)

Expand Down
19 changes: 19 additions & 0 deletions sklearn/linear_model/tests/test_logistic.py
Original file line number Diff line number Diff line change
Expand Up @@ -2237,3 +2237,22 @@ def test_sample_weight_not_modified(multi_class, class_weight):
)
clf.fit(X, y, sample_weight=W)
assert_allclose(expected, W)


@pytest.mark.parametrize("solver", ["liblinear", "lbfgs", "newton-cg", "sag", "saga"])
def test_large_sparse_matrix(solver):
# Solvers either accept large sparse matrices, or raise helpful error.
# Non-regression test for pull-request #21093.

# generate sparse matrix with int64 indices
X = sp.rand(20, 10, format="csr")
for attr in ["indices", "indptr"]:
setattr(X, attr, getattr(X, attr).astype("int64"))
y = np.random.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)
0