Thanks to visit codestin.com
Credit goes to github.com

Skip to content
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
- Raising error in :class:`sklearn.linear_model.LogisticRegression` when
liblinear solver is used and input X values are larger than 1e30,
the liblinear solver freezes otherwise.
By :user:`Shruti Nath <snath-xoc>`.
6 changes: 6 additions & 0 deletions sklearn/linear_model/_logistic.py
Original file line number Diff line number Diff line change
Expand Up @@ -1295,6 +1295,12 @@ def fit(self, X, y, sample_weight=None):
multi_class = _check_multi_class(multi_class, solver, len(self.classes_))

if solver == "liblinear":
if np.max(X) > 1e30:
raise ValueError(
"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."
)
if len(self.classes_) > 2:
warnings.warn(
"Using the 'liblinear' solver for multiclass classification is "
Expand Down
17 changes: 17 additions & 0 deletions sklearn/linear_model/tests/test_logistic.py
Original file line number Diff line number Diff line change
Expand Up @@ -2319,6 +2319,23 @@ def test_large_sparse_matrix(solver, global_random_seed, csr_container):
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.
Expand Down
Loading