-
-
Notifications
You must be signed in to change notification settings - Fork 26.2k
[MRG] Fixes sklearn.metrics.silhouette_samples for sparse matrices #18723
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
Closed
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
6b39cc3
Updated silhouette reduce function
56c7bce
Fixed lint tests
d4f17da
Codecov coverage failing test
5083c85
Fixed indentation to conform to PEP style
deffdfb
Fixed coding style
bb2d464
Merge branch 'master' of https://github.com/scikit-learn/scikit-learn…
76e7140
Unit test for changes
4e18cbf
Avoid converting sprase matrix to dense matrix
476e331
Fixed line spaces in test file:
15e400f
Merge branch 'master' of https://github.com/scikit-learn/scikit-learn…
d1706a5
Unit test for sparse input implementation
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
import scipy.sparse as sp | ||
import pytest | ||
from scipy.sparse import csr_matrix | ||
from scipy.sparse import csc_matrix | ||
|
||
from sklearn import datasets | ||
from sklearn.utils._testing import assert_array_equal | ||
|
@@ -184,6 +185,29 @@ def test_silhouette_nonzero_diag(dtype): | |
silhouette_samples(dists, labels, metric='precomputed') | ||
|
||
|
||
@pytest.mark.parametrize('to_sparse', (csr_matrix, csc_matrix)) | ||
def test_silhouette_sparse_input(to_sparse): | ||
""" Ensure that silhouette_samples works for sparse matrix inputs """ | ||
X = np.array([[0, 0], [1, 0], [10, 10], [10, 11]], dtype=np.float32) | ||
y = np.array([1, 1, 1, 0]) | ||
pdist = pairwise_distances(X) | ||
sX = to_sparse(pdist) | ||
silhouette_samples(sX, y, metric="precomputed") | ||
|
||
|
||
def test_silhouette_sparse_implementation(): | ||
""" Ensure implementation for sparse matrix works correctly""" | ||
X = np.array([[0, 0], [1, 0], [10, 10], [10, 11]], dtype=np.float32) | ||
y = np.array([1, 1, 1, 0]) | ||
pdist = pairwise_distances(X) | ||
sX = csr_matrix(pdist) | ||
sparse_out = silhouette_samples(sX, y, metric="precomputed") | ||
dense_out = silhouette_samples(pdist, y, metric="precomputed") | ||
|
||
for out in zip(sparse_out, dense_out): | ||
assert out[0] == out[1] | ||
Comment on lines
+198
to
+208
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. To test a sparse pdist that have some non-diagonal zeros. from numpy.testing import assert_allclose
def test_silhouette_sparse_implementation():
""" Ensure implementation for sparse matrix works correctly"""
X = np.array([[0.2, 0.1, 0.1, 0.2, 0.1, 1.6, 0.2, 0.1]],
dtype=np.float32).T
y = [0, 0, 0, 0, 1, 1, 1, 1]
pdist_dense = pairwise_distances(X)
pdist_sparse = csr_matrix(pdist_dense)
sparse_out = silhouette_samples(pdist_sparse, y, metric="precomputed")
dense_out = silhouette_samples(pdist_dense, y, metric="precomputed")
assert_allclose(sparse_out, dense_out) |
||
|
||
|
||
def assert_raises_on_only_one_label(func): | ||
"""Assert message when there is only one label""" | ||
rng = np.random.RandomState(seed=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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks better.
I think we would need a test to make sure that
sample_weights
work as expected. A simple test would be to runsilhouette_samples
on a sparse dataset and compare it to the result with the same dataset but densified.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@thomasjpfan I have added a test for this