-
-
Notifications
You must be signed in to change notification settings - Fork 26.2k
FIX don't modify X in-place when precomputed in DBSCAN #27651
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
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
624c69c
FIX: Copy to avoid in-place modify
tataganesh 817eab2
Merge branch 'main' into dbscan_matrix_copy
tataganesh 7ecd6ef
Suggestions from code review - Better comments
tataganesh 34dc9de
DOCS: Changelog updated
tataganesh 11da1ef
FIX: Linter error
tataganesh dbbc46b
Merge branch 'main' into dbscan_matrix_copy
tataganesh 9a644d2
Merge branch 'main' into dbscan_matrix_copy
tataganesh 6c823c5
CHORE: Assert for implicit zeroes
tataganesh 0bc0a2c
Merge branch 'dbscan_matrix_copy' of github.com:tataganesh/scikit-lea…
tataganesh 595399e
Merge branch 'main' into dbscan_matrix_copy
tataganesh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -122,6 +122,28 @@ def test_dbscan_input_not_modified(metric, csr_container): | |
assert_array_equal(X, X_copy) | ||
|
||
|
||
@pytest.mark.parametrize("csr_container", CSR_CONTAINERS) | ||
def test_dbscan_input_not_modified_precomputed_sparse_nodiag(csr_container): | ||
"""Check that we don't modify in-place the pre-computed sparse matrix. | ||
|
||
Non-regression test for: | ||
https://github.com/scikit-learn/scikit-learn/issues/27508 | ||
""" | ||
X = np.random.RandomState(0).rand(10, 10) | ||
# Add zeros on the diagonal that will be implicit when creating | ||
# the sparse matrix. If `X` is modified in-place, the zeros from | ||
# the diagonal will be made explicit. | ||
np.fill_diagonal(X, 0) | ||
X = csr_container(X) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could make an assert just below this line to be sure that we have implicit zeros. |
||
assert all(row != col for row, col in zip(*X.nonzero())) | ||
X_copy = X.copy() | ||
dbscan(X, metric="precomputed") | ||
# Make sure that we did not modify `X` in-place even by creating | ||
# explicit 0s values. | ||
assert X.nnz == X_copy.nnz | ||
tataganesh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
assert_array_equal(X.toarray(), X_copy.toarray()) | ||
|
||
|
||
@pytest.mark.parametrize("csr_container", CSR_CONTAINERS) | ||
def test_dbscan_no_core_samples(csr_container): | ||
rng = np.random.RandomState(0) | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.