Skip to content

FIX use safe_sparse_dot for callable kernel in LabelSpreading (#15866) #15868

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 6 commits into from
Dec 27, 2019
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
8 changes: 8 additions & 0 deletions doc/whats_new/v0.22.rst
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ Changelog
- |Fix| :func:`metrics.classification_report` does no longer ignore the
value of the ``zero_division`` keyword argument. :pr:`15879`
by :user:`Bibhash Chandra Mitra <Bibyutatsu>`.

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

- |Fix| :class:`semi_supervised.LabelPropagation` and
:class:`semi_supervised.LabelSpreading` now allow callable kernel function to
return sparse weight matrix.
:pr:`15868` by :user:`Niklas Smedemark-Margulies <nik-sm>`.

:mod:`sklearn.utils`
....................
Expand Down
3 changes: 2 additions & 1 deletion sklearn/semi_supervised/_label_propagation.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,8 @@ class labels
for weight_matrix in weight_matrices])
else:
weight_matrices = weight_matrices.T
probabilities = np.dot(weight_matrices, self.label_distributions_)
probabilities = safe_sparse_dot(
weight_matrices, self.label_distributions_)
normalizer = np.atleast_2d(np.sum(probabilities, axis=1)).T
probabilities /= normalizer
return probabilities
Expand Down
39 changes: 39 additions & 0 deletions sklearn/semi_supervised/tests/test_label_propagation.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
import numpy as np
import pytest

from scipy.sparse import issparse
from sklearn.utils._testing import assert_warns
from sklearn.utils._testing import assert_no_warnings
from sklearn.semi_supervised import _label_propagation as label_propagation
from sklearn.metrics.pairwise import rbf_kernel
from sklearn.model_selection import train_test_split
from sklearn.neighbors import NearestNeighbors
from sklearn.datasets import make_classification
from sklearn.exceptions import ConvergenceWarning
from numpy.testing import assert_array_almost_equal
Expand Down Expand Up @@ -152,3 +155,39 @@ def test_convergence_warning():

mdl = label_propagation.LabelPropagation(kernel='rbf', max_iter=500)
assert_no_warnings(mdl.fit, X, y)


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

# Custom sparse kernel (top-K RBF)
def topk_rbf(X, Y=None, n_neighbors=10, gamma=1e-5):
nn = NearestNeighbors(n_neighbors=10, metric='euclidean', n_jobs=-1)
nn.fit(X)
W = -1 * nn.kneighbors_graph(Y, mode='distance').power(2) * gamma
np.exp(W.data, out=W.data)
assert issparse(W)
return W.T

n_classes = 4
n_samples = 500
n_test = 10
X, y = make_classification(n_classes=n_classes,
n_samples=n_samples,
n_features=20,
n_informative=20,
n_redundant=0,
n_repeated=0,
random_state=0)

X_train, X_test, y_train, y_test = train_test_split(X, y,
test_size=n_test,
random_state=0)

model = label_propagation.LabelSpreading(kernel=topk_rbf)
model.fit(X_train, y_train)
assert model.score(X_test, y_test) >= 0.9

model = label_propagation.LabelPropagation(kernel=topk_rbf)
model.fit(X_train, y_train)
assert model.score(X_test, y_test) >= 0.9