Skip to content

Label spreading bug fix #15946

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 12 commits into from
Jan 9, 2020
1 change: 1 addition & 0 deletions sklearn/semi_supervised/_label_propagation.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,7 @@ def fit(self, X, y):
self.n_iter_ += 1

normalizer = np.sum(self.label_distributions_, axis=1)[:, np.newaxis]
normalizer[normalizer == 0] = 1
self.label_distributions_ /= normalizer

# set the transduction item
Expand Down
12 changes: 12 additions & 0 deletions sklearn/semi_supervised/tests/test_label_propagation.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,18 @@ def test_convergence_warning():
assert_no_warnings(mdl.fit, X, y)


def test_label_propagation_non_zero_normalizer():
# 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
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)
assert_no_warnings(mdl.fit, X, y)


def test_predict_sparse_callable_kernel():
# This is a non-regression test for #15866

Expand Down