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,3 @@
- Fix tie-breaking behavior in :class:`multiclass.OneVsRestClassifier` to match
`np.argmax` tie-breaking behavior.
By :user:`Lakshmi Krishnan <lakrish>`.
6 changes: 4 additions & 2 deletions sklearn/multiclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -499,10 +499,12 @@ def predict(self, X):
maxima = np.empty(n_samples, dtype=float)
maxima.fill(-np.inf)
argmaxima = np.zeros(n_samples, dtype=int)
for i, e in enumerate(self.estimators_):
n_classes = len(self.estimators_)
# Iterate in reverse order to match np.argmax tie-breaking behavior
for i, e in enumerate(reversed(self.estimators_)):
pred = _predict_binary(e, X)
np.maximum(maxima, pred, out=maxima)
argmaxima[maxima == pred] = i
argmaxima[maxima == pred] = n_classes - i - 1
return self.classes_[argmaxima]
else:
thresh = _threshold_for_binary_predict(self.estimators_[0])
Expand Down
19 changes: 19 additions & 0 deletions sklearn/tests/test_multiclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,25 @@ def test_check_classification_targets():
check_classification_targets(y)


def test_ovr_ties():
"""Check that ties-breaking matches np.argmax behavior

Non-regression test for issue #14124
"""

class Dummy(BaseEstimator):
def fit(self, X, y):
return self

def decision_function(self, X):
return np.zeros(len(X))

X = np.array([[0], [0], [0], [0]])
y = np.array([0, 1, 2, 3])
clf = OneVsRestClassifier(Dummy()).fit(X, y)
assert_array_equal(clf.predict(X), np.argmax(clf.decision_function(X), axis=1))


def test_ovr_fit_predict():
# A classifier which implements decision_function.
ovr = OneVsRestClassifier(LinearSVC(random_state=0))
Expand Down