8000 [WIP] Make knn kernel undirected. by musically-ut · Pull Request #9439 · scikit-learn/scikit-learn · GitHub
[go: up one dir, main page]

Skip to content

[WIP] Make knn kernel undirected. #9439

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

Closed
wants to merge 2 commits into from
Closed
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
13 changes: 10 additions & 3 deletions sklearn/semi_supervised/label_propagation.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,16 @@ def _get_kernel(self, X, y=None):
self.nn_fit = NearestNeighbors(self.n_neighbors,
n_jobs=self.n_jobs).fit(X)
if y is None:
return self.nn_fit.kneighbors_graph(self.nn_fit._fit_X,
self.n_neighbors,
mode='connectivity')
# Nearest neighbors returns a directed matrix.
dir_graph = self.nn_fit.kneighbors_graph(self.nn_fit._fit_X,
self.n_neighbors,
mode='connectivity')
# Making the matrix symmetric
un_graph = dir_graph + dir_graph.T
# Since it is a connectivity matrix, all values should be
# either 0 or 1
un_graph[un_graph > 1] = 1
return un_graph
else:
return self.nn_fit.kneighbors(y, return_distance=False)
elif callable(self.kernel):
Expand Down
41 changes: 22 additions & 19 deletions sklearn/semi_supervised/tests/test_label_propagation.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,8 @@ def test_distribution():
labels = [0, 1, -1]
for estimator, parameters in ESTIMATORS:
clf = estimator(**parameters).fit(samples, labels)
if parameters['kernel'] == 'knn':
continue # unstable test; changes in k-NN ordering break it
assert_array_almost_equal(clf.predict_proba([[1., 0.0]]),
np.array([[1., 0.]]), 2)
else:
assert_array_almost_equal(np.asarray(clf.label_distributions_[2]),
np.array([.5, .5]), 2)
assert_array_almost_equal(np.asarray(clf.label_distributions_[2]),
np.array([.5, .5]), decimal=3)


def test_predict():
Expand All @@ -62,20 +57,23 @@ def test_predict_proba():
for estimator, parameters in ESTIMATORS:
clf = estimator(**parameters).fit(samples, labels)
assert_array_almost_equal(clf.predict_proba([[1., 1.]]),
np.array([[0.5, 0.5]]))
np.array([[0.5, 0.5]]), decimal=3)


def test_alpha_deprecation():
X, y = make_classification(n_samples=100)
y[::3] = -1

lp_default = label_propagation.LabelPropagation(kernel='rbf', gamma=0.1)
lp_default_y = assert_no_warnings(lp_default.fit, X, y).transduction_
for kernel in ['rbf', 'knn']:
lp_default = label_propagation.LabelPropagation(kernel=kernel,
gamma=0.1)
lp_default_y = assert_no_warnings(lp_default.fit, X, y).transduction_

lp_0 = label_propagation.LabelPropagation(alpha=0, kernel='rbf', gamma=0.1)
lp_0_y = assert_warns(DeprecationWarning, lp_0.fit, X, y).transduction_
lp_0 = label_propagation.LabelPropagation(alpha=0, kernel=kernel,
gamma=0.1)
lp_0_y = assert_warns(DeprecationWarning, lp_0.fit, X, y).transduction_

assert_array_equal(lp_default_y, lp_0_y)
assert_array_equal(lp_default_y, lp_0_y)


def test_label_spreading_closed_form():
Expand All @@ -94,7 +92,8 @@ def test_label_spreading_closed_form():
expected /= expected.sum(axis=1)[:, np.newaxis]
clf = label_propagation.LabelSpreading(max_iter=10000, alpha=alpha)
clf.fit(X, y)
assert_array_almost_equal(expected, clf.label_distributions_, 4)
assert_array_almost_equal(expected, clf.label_distributions_,
decimal=4)


def test_label_propagation_closed_form():
Expand Down Expand Up @@ -139,9 +138,13 @@ def test_convergence_speed():
# This is a non-regression test for #5774
X = np.array([[1., 0.], [0., 1.], [1., 2.5]])
y = np.array([0, 1, -1])
mdl = label_propagation.LabelSpreading(kernel='rbf', max_iter=5000)
mdl.fit(X, y)

# this should converge quickly:
assert mdl.n_iter_ < 10
assert_array_equal(mdl.predict(X), [0, 1, 1])
for kernel in ['rbf', 'knn']:
mdl = label_propagation.LabelSpreading(kernel=kernel, max_iter=5000,
n_neighbors=2)
mdl.fit(X, y)

# this should converge quickly:
assert mdl.n_iter_ < 10
assert_array_almost_equal(mdl.predict_proba([[0.5, 0.5]]),
[[0.5, 0.5]], decimal=3)
0