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
7 changes: 7 additions & 0 deletions doc/whats_new/v1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,13 @@ Changelog
for non-English characters. :pr:`18959` by :user:`Zero <Zeroto521>`
and :user:`wstates <wstates>`.

:mod:`sklearn.semi_supervised`
.................................

- |Fix| Avoid NaN during label propagation in
:class:`~sklearn.semi_supervised.LabelPropagation`.
:pr:`19271` by :user:`Zhaowei Wang <ThuWangzw>`.

Code and Documentation Contributors
-----------------------------------

Expand Down
1 change: 1 addition & 0 deletions sklearn/semi_supervised/_label_propagation.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ def fit(self, X, y):
if self._variant == 'propagation':
normalizer = np.sum(
self.label_distributions_, axis=1)[:, np.newaxis]
normalizer[normalizer == 0] = 1
self.label_distributions_ /= normalizer
self.label_distributions_ = np.where(unlabeled,
self.label_distributions_,
Expand Down
12 changes: 8 additions & 4 deletions sklearn/semi_supervised/tests/test_label_propagation.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,15 +157,19 @@ def test_convergence_warning():
assert_no_warnings(mdl.fit, X, y)


def test_label_propagation_non_zero_normalizer():
@pytest.mark.parametrize("LabelPropagationCls",
[label_propagation.LabelSpreading,
label_propagation.LabelPropagation])
def test_label_propagation_non_zero_normalizer(LabelPropagationCls):
# check that we don't divide by zero in case of null normalizer
# non-regression test for
# https://github.com/scikit-learn/scikit-learn/pull/15946
# https://github.com/scikit-learn/scikit-learn/issues/9292
X = np.array([[100., 100.], [100., 100.], [0., 0.], [0., 0.]])
y = np.array([0, 1, -1, -1])
mdl = label_propagation.LabelSpreading(kernel='knn',
max_iter=100,
n_neighbors=1)
mdl = LabelPropagationCls(kernel='knn',
max_iter=100,
n_neighbors=1)
assert_no_warnings(mdl.fit, X, y)


Expand Down