Skip to content

Commit ffb81e4

Browse files
committed
Merge pull request scikit-learn#4650 from lesteve/fix-4641
[MRG + 1] Fix DBSCAN fit with precomputed matrix in edge cases
2 parents 259e983 + af1b652 commit ffb81e4

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

sklearn/cluster/dbscan_.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,8 @@ def dbscan(X, eps=0.5, min_samples=5, metric='minkowski',
115115
# neighborhood of point i. While True, its useless information)
116116
if metric == 'precomputed':
117117
D = pairwise_distances(X, metric=metric)
118-
neighborhoods = np.array([np.where(x <= eps)[0] for x in D])
118+
neighborhoods = np.empty(X.shape[0], dtype=object)
119+
neighborhoods[:] = [np.where(x <= eps)[0] for x in D]
119120
else:
120121
neighbors_model = NearestNeighbors(radius=eps, algorithm=algorithm,
121122
leaf_size=leaf_size,

sklearn/cluster/tests/test_dbscan.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,3 +293,15 @@ def test_dbscan_core_samples_toy():
293293
min_samples=4)
294294
assert_array_equal(core_samples, [])
295295
assert_array_equal(labels, -np.ones(n_samples))
296+
297+
298+
def test_dbscan_precomputed_metric_with_degenerate_input_arrays():
299+
# see https://github.com/scikit-learn/scikit-learn/issues/4641 for
300+
# more details
301+
X = np.ones((10, 2))
302+
labels = DBSCAN(eps=0.5, metric='precomputed').fit(X).labels_
303+
assert_equal(len(set(labels)), 1)
304+
305+
X = np.zeros((10, 2))
306+
labels = DBSCAN(eps=0.5, metric='precomputed').fit(X).labels_
307+
assert_equal(len(set(labels)), 1)

0 commit comments

Comments
 (0)