8000 [MRG+1] Fixes incorrect output when input is precomputed sparse matrix in DBSCAN. by Akshay0724 · Pull Request #8339 · scikit-learn/scikit-learn · GitHub
[go: up one dir, main page]

Skip to content

[MRG+1] Fixes incorrect output when input is precomputed sparse matrix in DBSCAN. #8339

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
Feb 23, 2017
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
4 changes: 4 additions & 0 deletions doc/whats_new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,10 @@ Enhancements

Bug fixes
.........
- Fixed a bug where :class:`sklearn.cluster.DBSCAN` gives incorrect
result when input is a precomputed sparse matrix with initial
rows all zero.
:issue:`8306` by :user:`Akshay Gupta <Akshay0724>`

- Fixed a bug where :func:`sklearn.datasets.make_moons` gives an
incorrect result when ``n_samples`` is odd.
Expand Down
3 changes: 2 additions & 1 deletion sklearn/cluster/dbscan_.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@ def dbscan(X, eps=0.5, min_samples=5, metric='minkowski', metric_params=None,
X.sum_duplicates() # XXX: modifies X's internals in-place
X_mask = X.data <= eps
masked_indices = astype(X.indices, np.intp, copy=False)[X_mask]
masked_indptr = np.cumsum(X_mask)[X.indptr[1:] - 1]
masked_indptr = np.concatenate(([0], np.cumsum(X_mask)))[X.indptr[1:]]

# insert the diagonal: a point is its own neighbor, but 0 distance
# means absence from sparse matrix data
masked_indices = np.insert(masked_indices, masked_indptr,
Expand Down
17 changes: 17 additions & 0 deletions sklearn/cluster/tests/test_dbscan.py
Original file line number Diff line number Diff line change
Expand Up @@ -350,3 +350,20 @@ def test_dbscan_precomputed_metric_with_degenerate_input_arrays():
X = np.zeros((10, 10))
labels = DBSCAN(eps=0.5, metric='precomputed').fit(X).labels_
assert_equal(len(set(labels)), 1)


def test_dbscan_precomputed_metric_with_initial_rows_zero():
# sample matrix with initial two row all zero
ar = np.array([
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
[0.0, 0.0, 0.0, 0.0, 0.1, 0.0, 0.0],
[0.0, 0.0, 0.0, 0.0, 0.1, 0.0, 0.0],
[0.0, 0.0, 0.1, 0.1, 0.0, 0.0, 0.3],
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.1],
[0.0, 0.0, 0.0, 0.0, 0.3, 0.1, 0.0]
])
matrix = sparse.csr_matrix(ar)
labels = DBSCAN(eps=0.2, metric='precomputed',
min_samples=2).fit(matrix).labels_
assert_array_equal(labels, [-1, -1, 0, 0, 0, 1, 1])
0