Thanks to visit codestin.com
Credit goes to github.com

Skip to content

MNT Improve robustness of sparse test in HDBSCAN #26889

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
Jul 27, 2023
Merged
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
33 changes: 24 additions & 9 deletions sklearn/cluster/tests/test_hdbscan.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,22 +287,37 @@ def test_hdbscan_precomputed_non_brute(tree):
def test_hdbscan_sparse():
"""
Tests that HDBSCAN works correctly when passing sparse feature data.
Evaluates correctness by comparing against the same data passed as a dense
array.
"""
sparse_X = sparse.csr_matrix(X)

labels = HDBSCAN().fit(sparse_X).labels_
n_clusters = len(set(labels) - OUTLIER_SET)
dense_labels = HDBSCAN().fit(X).labels_
n_clusters = len(set(dense_labels) - OUTLIER_SET)
assert n_clusters == 3

sparse_X_nan = sparse_X.copy()
sparse_X_nan[0, 0] = np.nan
labels = HDBSCAN().fit(sparse_X_nan).labels_
n_clusters = len(set(labels) - OUTLIER_SET)
assert n_clusters == 3
_X_sparse = sparse.csr_matrix(X)
X_sparse = _X_sparse.copy()
sparse_labels = HDBSCAN().fit(X_sparse).labels_
assert_array_equal(dense_labels, sparse_labels)

# Compare that the sparse and dense non-precomputed routines return the same labels
# where the 0th observation contains the outlier.
for outlier_val, outlier_type in ((np.inf, "infinite"), (np.nan, "missing")):
X_dense = X.copy()
X_dense[0, 0] = outlier_val
dense_labels = HDBSCAN().fit(X_dense).labels_
n_clusters = len(set(dense_labels) - OUTLIER_SET)
assert n_clusters == 3
assert dense_labels[0] == _OUTLIER_ENCODING[outlier_type]["label"]

X_sparse = _X_sparse.copy()
X_sparse[0, 0] = outlier_val
sparse_labels = HDBSCAN().fit(X_sparse).labels_
assert_array_equal(dense_labels, sparse_labels)

msg = "Sparse data matrices only support algorithm `brute`."
with pytest.raises(ValueError, match=msg):
HDBSCAN(metric="euclidean", algorithm="balltree").fit(sparse_X)
HDBSCAN(metric="euclidean", algorithm="balltree").fit(X_sparse)


@pytest.mark.parametrize("algorithm", ALGORITHMS)
Expand Down