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

Skip to content

FIX: Change limits of power_t param to [0, inf) #31474

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 8 commits into from
Jun 10, 2025
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,6 @@
- :class:`linear_model.SGDClassifier`, :class:`linear_model.SGDRegressor`, and
:class:`linear_model.SGDOneClassSVM` now deprecate negative values for the
`power_t` parameter. Using a negative value will raise a warning in version 1.8
and will raise an error in version 1.10. A value in the range [0.0, inf) must be used
instead.
By :user:`Ritvi Alagusankar <ritvi-alagusankar>`
43 changes: 40 additions & 3 deletions sklearn/linear_model/_stochastic_gradient.py
Original file line number Diff line number Diff line change
Expand Up @@ -731,6 +731,15 @@ def _fit(
),
ConvergenceWarning,
)

if self.power_t < 0:
warnings.warn(
"Negative values for `power_t` are deprecated in version 1.8 "
"and will raise an error in 1.10. "
"Use values in the range [0.0, inf) instead.",
FutureWarning,
)

return self

def _fit_binary(self, X, y, alpha, C, sample_weight, learning_rate, max_iter):
Expand Down Expand Up @@ -1082,7 +1091,11 @@ class SGDClassifier(BaseSGDClassifier):

power_t : float, default=0.5
The exponent for inverse scaling learning rate.
Values must be in the range `(-inf, inf)`.
Values must be in the range `[0.0, inf)`.

.. deprecated:: 1.8
Negative values for `power_t` are deprecated in version 1.8 and will raise
an error in 1.10. Use values in the range [0.0, inf) instead.

early_stopping : bool, default=False
Whether to use early stopping to terminate training when validation
Expand Down Expand Up @@ -1585,6 +1598,14 @@ def _fit(
ConvergenceWarning,
)

if self.power_t < 0:
warnings.warn(
"Negative values for `power_t` are deprecated in version 1.8 "
"and will raise an error in 1.10. "
"Use values in the range [0.0, inf) instead.",
FutureWarning,
)

return self

@_fit_context(prefer_skip_nested_validation=True)
Expand Down Expand Up @@ -1880,7 +1901,11 @@ class SGDRegressor(BaseSGDRegressor):

power_t : float, default=0.25
The exponent for inverse scaling learning rate.
Values must be in the range `(-inf, inf)`.
Values must be in the range `[0.0, inf)`.

.. deprecated:: 1.8
Negative values for `power_t` are deprecated in version 1.8 and will raise
an error in 1.10. Use values in the range [0.0, inf) instead.

early_stopping : bool, default=False
Whether to use early stopping to terminate training when validation
Expand Down Expand Up @@ -2118,7 +2143,11 @@ class SGDOneClassSVM(OutlierMixin, BaseSGD):

power_t : float, default=0.5
The exponent for inverse scaling learning rate.
Values must be in the range `(-inf, inf)`.
Values must be in the range `[0.0, inf)`.

.. deprecated:: 1.8
Negative values for `power_t` are deprecated in version 1.8 and will raise
an error in 1.10. Use values in the range [0.0, inf) instead.

warm_start : bool, default=False
When set to True, reuse the solution of the previous call to fit as
Expand Down Expand Up @@ -2490,6 +2519,14 @@ def _fit(
ConvergenceWarning,
)

if self.power_t < 0:
warnings.warn(
"Negative values for `power_t` are deprecated in version 1.8 "
"and will raise an error in 1.10. "
"Use values in the range [0.0, inf) instead.",
FutureWarning,
)

return self

@_fit_context(prefer_skip_nested_validation=True)
Expand Down
30 changes: 30 additions & 0 deletions sklearn/linear_model/tests/test_sgd.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pickle
import warnings
from unittest.mock import Mock

import joblib
Expand Down Expand Up @@ -507,6 +508,35 @@ def test_sgd_failing_penalty_validation(Estimator):
clf.fit(X, Y)


# TODO(1.10): remove this test
@pytest.mark.parametrize(
"klass",
[
SGDClassifier,
SparseSGDClassifier,
SGDRegressor,
SparseSGDRegressor,
SGDOneClassSVM,
SparseSGDOneClassSVM,
],
)
def test_power_t_limits(klass):
"""Check that a warning is raised when `power_t` is negative."""

# Check that negative values of `power_t` raise a warning
clf = klass(power_t=-1.0)
with pytest.warns(
FutureWarning, match="Negative values for `power_t` are deprecated"
):
clf.fit(X, Y)

# Check that values of 'power_t in range [0, inf) do not raise a warning
with warnings.catch_warnings(record=True) as w:
clf = klass(power_t=0.5)
clf.fit(X, Y)
assert len(w) == 0


###############################################################################
# Classification Test Case

Expand Down