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
Show all changes
41 commits
Select commit Hold shift + click to select a range
78e2155
DEP penalty in LogisticRegression
lorentzenchr Nov 5, 2025
3986510
DOC add whatsnew entry
lorentzenchr Nov 6, 2025
37eeb63
DOC/FIX develop.rst
lorentzenchr Nov 6, 2025
69b11b5
TST take care of warnings in tests
lorentzenchr Nov 7, 2025
1cc08fb
CLN adapt deprecation of penalty throught codebase
lorentzenchr Nov 7, 2025
b0b243a
Merge branch 'main' into dep_logistic_penalty
lorentzenchr Nov 7, 2025
f85d664
CLN docstring
lorentzenchr Nov 7, 2025
dbbdb07
MNT add deprecation warning for l1_ratio=None and review comments
lorentzenchr Nov 10, 2025
eab936a
DEP change default of l1_ratios from "warn" to None, deprecate None a…
lorentzenchr Nov 10, 2025
c745f77
DOC docstring of l1_ratio and table of solver compatibility
lorentzenchr Nov 17, 2025
b3b50fa
CLN review comments
lorentzenchr Nov 17, 2025
f0f181a
Merge branch 'main' into dep_logistic_penalty
lorentzenchr Nov 21, 2025
06f6d18
TST fix warnings in tests
lorentzenchr Nov 21, 2025
2fe94f6
Merge branch 'main' into dep_logistic_penalty
lorentzenchr Nov 21, 2025
cacb942
CLN address review comments
lorentzenchr Nov 24, 2025
ff7df3c
CLN nitpick
lorentzenchr Nov 24, 2025
2043b52
trigger CD
lorentzenchr Nov 24, 2025
8eaa810
MNT l1_ratios="warn"
lorentzenchr Nov 24, 2025
85a325f
CLN fix warnings in tests
lorentzenchr Nov 25, 2025
9f63e57
[azure parallel] sprinkle with a few more l1_ratios
lesteve Nov 25, 2025
f988165
[azure parallel] one more l1_ratios
lesteve Nov 25, 2025
527de21
This does not seem necessary
lesteve Nov 25, 2025
b002c22
[azure parallel] tweak changelog
lesteve Nov 25, 2025
e9ba34f
[azure parallel] an attempt at tweaking the changelog
lesteve Nov 25, 2025
8fcb264
[azure parallel] yet another changelog tweak
lesteve Nov 25, 2025
5991522
Revert "This does not seem necessary"
lesteve Nov 25, 2025
7a71a56
[azure parallel] tweak
lesteve Nov 25, 2025
370823f
[azure parallel] keep tweaking
lesteve Nov 25, 2025
7e28999
tweak
lesteve Nov 25, 2025
f079c5d
tweaks in test
lesteve Nov 25, 2025
36533a8
tweak warning wording
lesteve Nov 25, 2025
32c26e2
Tweak wording of l1_ratios deprecation + default change
lesteve Nov 25, 2025
ffc7c97
[azure parallel] trigger CI
lesteve Nov 25, 2025
0ed5277
[azure parallel] tweak changelog again
lesteve Nov 25, 2025
aea4cef
Update sklearn/svm/_bounds.py
jeremiedbb Nov 25, 2025
3646004
Update sklearn/linear_model/_logistic.py
jeremiedbb Nov 25, 2025
297b1cf
Apply suggestion from @jeremiedbb
jeremiedbb Nov 25, 2025
bdec717
lint
jeremiedbb Nov 25, 2025
49ed659
warn for inconsistent penalty and l1_ratio values
jeremiedbb Nov 25, 2025
983d6a5
lint
jeremiedbb Nov 25, 2025
689dbee
ignore other warning in test
jeremiedbb Nov 25, 2025
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
4 changes: 2 additions & 2 deletions asv_benchmarks/benchmarks/linear_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ def make_data(self, params):
def make_estimator(self, params):
representation, solver, n_jobs = params

penalty = "l2" if solver == "lbfgs" else "l1"
l1_ratio = 0 if solver == "lbfgs" else 1

estimator = LogisticRegression(
solver=solver,
penalty=penalty,
l1_ratio=l1_ratio,
tol=0.01,
n_jobs=n_jobs,
random_state=0,
Expand Down
4 changes: 3 additions & 1 deletion benchmarks/bench_saga.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,12 @@ def fit_single(
times = [0]

if penalty == "l2":
l1_ratio = 0
alpha = 1.0 / (C * n_samples)
beta = 0
lightning_penalty = None
else:
l1_ratio = 1
alpha = 0.0
beta = 1.0 / (C * n_samples)
lightning_penalty = "l1"
Expand Down Expand Up @@ -97,7 +99,7 @@ def fit_single(
lr = LogisticRegression(
solver=solver,
C=C,
penalty=penalty,
l1_ratio=l1_ratio,
fit_intercept=False,
tol=0,
max_iter=this_max_iter,
Expand Down
4 changes: 2 additions & 2 deletions doc/developers/develop.rst
Original file line number Diff line number Diff line change
Expand Up @@ -381,10 +381,10 @@ The parameter `deep` controls whether or not the parameters of the
subestimator__dual -> False
subestimator__fit_intercept -> True
subestimator__intercept_scaling -> 1
subestimator__l1_ratio -> None
subestimator__l1_ratio -> 0.0
subestimator__max_iter -> 100
subestimator__n_jobs -> None
subestimator__penalty -> l2
subestimator__penalty -> deprecated
subestimator__random_state -> None
subestimator__solver -> lbfgs
subestimator__tol -> 0.0001
Expand Down
67 changes: 33 additions & 34 deletions doc/modules/linear_model.rst
Original file line number Diff line number Diff line change
Expand Up @@ -999,20 +999,20 @@ specific training sample (the vector :math:`s` is formed by element-wise
multiplication of the class weights and sample weights),
and the sum :math:`S = \sum_{i=1}^n s_i`.

We currently provide four choices for the regularization term :math:`r(w)` via
the `penalty` argument:

+----------------+-------------------------------------------------+
| penalty | :math:`r(w)` |
+================+=================================================+
| `None` | :math:`0` |
+----------------+-------------------------------------------------+
| :math:`\ell_1` | :math:`\|w\|_1` |
+----------------+-------------------------------------------------+
| :math:`\ell_2` | :math:`\frac{1}{2}\|w\|_2^2 = \frac{1}{2}w^T w` |
+----------------+-------------------------------------------------+
| `ElasticNet` | :math:`\frac{1 - \rho}{2}w^T w + \rho \|w\|_1` |
+----------------+-------------------------------------------------+
We currently provide four choices for the regularization or penalty term :math:`r(w)`
via the arguments `C` and `l1_ratio`:

+-------------------------------+-------------------------------------------------+
| penalty | :math:`r(w)` |
+===============================+=================================================+
| none (`C=np.inf`) | :math:`0` |
+-------------------------------+-------------------------------------------------+
| :math:`\ell_1` (`l1_ratio=1`) | :math:`\|w\|_1` |
+-------------------------------+-------------------------------------------------+
| :math:`\ell_2` (`l1_ratio=0`) | :math:`\frac{1}{2}\|w\|_2^2 = \frac{1}{2}w^T w` |
+-------------------------------+-------------------------------------------------+
| ElasticNet (`0<l1_ratio<1`) | :math:`\frac{1 - \rho}{2}w^T w + \rho \|w\|_1` |
+-------------------------------+-------------------------------------------------+

For ElasticNet, :math:`\rho` (which corresponds to the `l1_ratio` parameter)
controls the strength of :math:`\ell_1` regularization vs. :math:`\ell_2`
Expand Down Expand Up @@ -1063,21 +1063,20 @@ logistic regression, see also `log-linear model
Again, :math:`s_{ik}` are the weights assigned by the user (multiplication of sample
weights and class weights) with their sum :math:`S = \sum_{i=1}^n \sum_{k=0}^{K-1} s_{ik}`.

We currently provide four choices
for the regularization term :math:`r(W)` via the `penalty` argument, where :math:`m`
is the number of features:

+----------------+----------------------------------------------------------------------------------+
| penalty | :math:`r(W)` |
+================+==================================================================================+
| `None` | :math:`0` |
+----------------+----------------------------------------------------------------------------------+
| :math:`\ell_1` | :math:`\|W\|_{1,1} = \sum_{i=1}^m\sum_{j=1}^{K}|W_{i,j}|` |
+----------------+----------------------------------------------------------------------------------+
| :math:`\ell_2` | :math:`\frac{1}{2}\|W\|_F^2 = \frac{1}{2}\sum_{i=1}^m\sum_{j=1}^{K} W_{i,j}^2` |
+----------------+----------------------------------------------------------------------------------+
| `ElasticNet` | :math:`\frac{1 - \rho}{2}\|W\|_F^2 + \rho \|W\|_{1,1}` |
+----------------+----------------------------------------------------------------------------------+
We currently provide four choices for the regularization or penalty term :math:`r(W)`
via the arguments `C` and `l1_ratio`, where :math:`m` is the number of features:

+-------------------------------+----------------------------------------------------------------------------------+
| penalty | :math:`r(W)` |
+===============================+==================================================================================+
| none (`C=np.inf`) | :math:`0` |
+-------------------------------+----------------------------------------------------------------------------------+
| :math:`\ell_1` (`l1_ratio=1`) | :math:`\|W\|_{1,1} = \sum_{i=1}^m\sum_{j=1}^{K}|W_{i,j}|` |
+-------------------------------+----------------------------------------------------------------------------------+
| :math:`\ell_2` (`l1_ratio=0`) | :math:`\frac{1}{2}\|W\|_F^2 = \frac{1}{2}\sum_{i=1}^m\sum_{j=1}^{K} W_{i,j}^2` |
+-------------------------------+----------------------------------------------------------------------------------+
| ElasticNet (`0<l1_ratio<1`) | :math:`\frac{1 - \rho}{2}\|W\|_F^2 + \rho \|W\|_{1,1}` |
+-------------------------------+----------------------------------------------------------------------------------+

.. _logistic_regression_solvers:

Expand All @@ -1100,7 +1099,7 @@ The following table summarizes the penalties and multinomial multiclass supporte
+------------------------------+-------------+-----------------+-----------------+-----------------------+-----------+------------+
| Elastic-Net (L1 + L2) | no | no | no | no | no | yes |
+------------------------------+-------------+-----------------+-----------------+-----------------------+-----------+------------+
| No penalty ('none') | yes | no | yes | yes | yes | yes |
| No penalty | yes | no | yes | yes | yes | yes |
+------------------------------+-------------+-----------------+-----------------+-----------------------+-----------+------------+
| **Multiclass support** | |
+------------------------------+-------------+-----------------+-----------------+-----------------------+-----------+------------+
Expand Down Expand Up @@ -1164,10 +1163,10 @@ zero, is likely to be an underfit, bad model and you are advised to set
than other solvers for large datasets, when both the number of samples and the
number of features are large.

* The "saga" solver [7]_ is a variant of "sag" that also supports the
non-smooth `penalty="l1"`. This is therefore the solver of choice for sparse
multinomial logistic regression. It is also the only solver that supports
`penalty="elasticnet"`.
* The "saga" solver [7]_ is a variant of "sag" that also supports the non-smooth
:math:`\ell_1` penalty (`l1_ratio=1`). This is therefore the solver of choice for
sparse multinomial logistic regression. It is also the only solver that supports
Elastic-Net (`0 < l1_ratio < 1`).

* The "lbfgs" is an optimization algorithm that approximates the
Broyden–Fletcher–Goldfarb–Shanno algorithm [8]_, which belongs to
Expand Down
27 changes: 27 additions & 0 deletions doc/whats_new/upcoming_changes/sklearn.linear_model/32659.api.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
- Parameter `penalty` of :class:`linear_model.LogisticRegression` and
:class:`linear_model.LogisticRegressionCV` is deprecated and will be removed in
version 1.10. The equivalent behaviour can be obtained as follows:

- for :class:`linear_model.LogisticRegression`

- use `l1_ratio=0` instead of `penalty="l2"`
- use `l1_ratio=1` instead of `penalty="l1"`
- use `0<l1_ratio<1` instead of `penalty="elasticnet"`
- use `C=np.inf` instead of `penalty=None`

- for :class:`linear_model.LogisticRegressionCV`

- use `l1_ratios=(0,)` instead of `penalty="l2"`
- use `l1_ratios=(1,)` instead of `penalty="l1"`
- the equivalent of `penalty=None` is to have `np.inf` as an element of the `Cs` parameter

For :class:`linear_model.LogisticRegression`, the default value of `l1_ratio`
has changed from `None` to `0.0`. Setting `l1_ratio=None` is deprecated and
will raise an error in version 1.10

For :class:`linear_model.LogisticRegressionCV`, the default value of `l1_ratios`
has changed from `None` to `"warn"`. It will be changed to `(0,)` in version
1.10. Setting `l1_ratios=None` is deprecated and will raise an error in
version 1.10.

By :user:`Christian Lorentzen <lorentzenchr>`.
8 changes: 3 additions & 5 deletions examples/linear_model/plot_logistic_l1_l2_sparsity.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,9 @@
# Set regularization parameter
for i, (C, axes_row) in enumerate(zip((1, 0.1, 0.01), axes)):
# Increase tolerance for short training time
clf_l1_LR = LogisticRegression(C=C, penalty="l1", tol=0.01, solver="saga")
clf_l2_LR = LogisticRegression(C=C, penalty="l2", tol=0.01, solver="saga")
clf_en_LR = LogisticRegression(
C=C, penalty="elasticnet", solver="saga", l1_ratio=l1_ratio, tol=0.01
)
clf_l1_LR = LogisticRegression(C=C, l1_ratio=1, tol=0.01, solver="saga")
clf_l2_LR = LogisticRegression(C=C, l1_ratio=0, tol=0.01, solver="saga")
clf_en_LR = LogisticRegression(C=C, l1_ratio=l1_ratio, tol=0.01, solver="saga")
clf_l1_LR.fit(X, y)
clf_l2_LR.fit(X, y)
clf_en_LR.fit(X, y)
Expand Down
2 changes: 1 addition & 1 deletion examples/linear_model/plot_logistic_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
clf = make_pipeline(
StandardScaler(),
LogisticRegression(
penalty="l1",
l1_ratio=1,
solver="liblinear",
tol=1e-6,
max_iter=int(1e6),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@
% (model_params["name"], solver, this_max_iter)
)
clf = LogisticRegression(
l1_ratio=1,
solver=solver,
penalty="l1",
max_iter=this_max_iter,
random_state=42,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
X_test = scaler.transform(X_test)

# Turn up tolerance for faster convergence
clf = LogisticRegression(C=50.0 / train_samples, penalty="l1", solver="saga", tol=0.1)
clf = LogisticRegression(C=50.0 / train_samples, l1_ratio=1, solver="saga", tol=0.1)
clf.fit(X_train, y_train)
sparsity = np.mean(clf.coef_ == 0) * 100
score = clf.score(X_test, y_test)
Expand Down
2 changes: 1 addition & 1 deletion examples/miscellaneous/plot_estimator_representation.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
# values when displayed as a string. This reduces the visual noise and makes it
# easier to spot what the differences are when comparing instances.

lr = LogisticRegression(penalty="l1")
lr = LogisticRegression(l1_ratio=1)
print(lr)

# %%
Expand Down
9 changes: 9 additions & 0 deletions sklearn/feature_selection/_from_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,20 @@ def _calculate_threshold(estimator, importances, threshold):
is_elasticnetcv_l1_penalized = est_name == "ElasticNetCV" and (
hasattr(estimator, "l1_ratio_") and np.isclose(estimator.l1_ratio_, 1.0)
)
is_logreg_l1_penalized = est_name == "LogisticRegression" and (
hasattr(estimator, "l1_ratio") and np.isclose(estimator.l1_ratio, 1.0)
)
is_logregcv_l1_penalized = est_name == "LogisticRegressionCV" and (
hasattr(estimator, "l1_ratio_")
and np.all(np.isclose(estimator.l1_ratio_, 1.0))
)
if (
is_l1_penalized
or is_lasso
or is_elasticnet_l1_penalized
or is_elasticnetcv_l1_penalized
or is_logreg_l1_penalized
or is_logregcv_l1_penalized
):
# the natural default threshold is 0 when l1 penalty was used
threshold = 1e-5
Expand Down
Loading
Loading