8000 [MRG] Fixed bug in _log_reg_scoring_path for multinomial case (#11724) · scikit-learn/scikit-learn@e500447 · GitHub
[go: up one dir, main page]

Skip to content

Commit e500447

Browse files
NicolasHugTomDLT
authored andcommitted
[MRG] Fixed bug in _log_reg_scoring_path for multinomial case (#11724)
1 parent b3a81cf commit e500447

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
lines changed

doc/whats_new/v0.20.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,12 @@ Classifiers and regressors
587587
considered all samples to be of equal weight importance.
588588
:issue:`11464` by :user:`John Stott <JohnStott>`.
589589

590+
- Fixed a bug in :func:`logistic.logistic_regression_path` to ensure that the
591+
returned coefficients are correct when ``multiclass='multinomial'``.
592+
Previously, some of the coefficients would override each other, leading to
593+
incorrect results in :class:`logistic.LogisticRegressionCV`. :issue:`11724`
594+
by :user:`Nicolas Hug <NicolasHug>`.
595+
590596
Decomposition, manifold learning and clustering
591597

592598
- Fix for uninformative error in :class:`decomposition.IncrementalPCA`:

sklearn/linear_model/logistic.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,9 @@ def logistic_regression_path(X, y, pos_class=None, Cs=10, fit_intercept=True,
572572
coefs : ndarray, shape (n_cs, n_features) or (n_cs, n_features + 1)
573573
List of coefficients for the Logistic Regression model. If
574574
fit_intercept is set to True then the second dimension will be
575-
n_features + 1, where the last item represents the intercept.
575+
n_features + 1, where the last item represents the intercept. For
576+
``multiclass='multinomial'``, the shape is (n_classes, n_cs,
577+
n_features) or (n_classes, n_cs, n_features + 1).
576578
577579
Cs : ndarray
578580
Grid of Cs used for cross-validation.
@@ -762,13 +764,13 @@ def logistic_regression_path(X, y, pos_class=None, Cs=10, fit_intercept=True,
762764
multi_w0 = np.reshape(w0, (classes.size, -1))
763765
if classes.size == 2:
764766
multi_w0 = multi_w0[1][np.newaxis, :]
765-
coefs.append(multi_w0)
767+
coefs.append(multi_w0.copy())
766768
else:
767769
coefs.append(w0.copy())
768770

769771
n_iter[i] = n_iter_i
770772

771-
return coefs, np.array(Cs), n_iter
773+
return np.array(coefs), np.array(Cs), n_iter
772774

773775

774776
# helper function for LogisticCV

sklearn/linear_model/tests/test_logistic.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1307,3 +1307,23 @@ def test_warm_start_converge_LR():
13071307
lr_ws.fit(X, y)
13081308
lr_ws_loss = log_loss(y, lr_ws.predict_proba(X))
13091309
assert_allclose(lr_no_ws_loss, lr_ws_loss, rtol=1e-5)
1310+
1311+
1312+
def test_logistic_regression_path_coefs_multinomial():
1313+
# Make sure that the returned coefs by logistic_regression_path when
1314+
# multi_class='multinomial' don't override each other (used to be a
1315+
# bug).
1316+
X, y = make_classification(n_samples=200, n_classes=3, n_informative=2,
1317+
n_redundant=0, n_clusters_per_class=1,
1318+
random_state=0, n_features=2)
1319+
Cs = [.00001, 1, 10000]
1320+
coefs, _, _ = logistic_regression_path(X, y, penalty='l1', Cs=Cs,
1321+
647F solver='saga', random_state=0,
1322+
multi_class='multinomial')
1323+
1324+
with pytest.raises(AssertionError):
1325+
assert_array_almost_equal(coefs[0], coefs[1], decimal=1)
1326+
with pytest.raises(AssertionError):
1327+
assert_array_almost_equal(coefs[0], coefs[2], decimal=1)
1328+
with pytest.raises(AssertionError):
1329+
assert_array_almost_equal(coefs[1], coefs[2], decimal=1)

0 commit comments

Comments
 (0)
0