[MRG] value error when distance matrix is not square and affinity=precomputed#16257
Conversation
| # the distance matrix. | ||
| if len(X.shape) != 2 or X.shape[0] != X.shape[1]: | ||
| raise ValueError( | ||
| 'Matrix should be square, as returned by pdist. ' |
There was a problem hiding this comment.
pairwise_distances would be more familiar to our users than pdist
There was a problem hiding this comment.
Ok, thanks. Let me change the Error Message.
…e_distances. Update test accordingly.
| # containing the upper triangular of the distance matrix. | ||
| if len(X.shape) != 2 or X.shape[0] != X.shape[1]: | ||
| raise ValueError( | ||
| 'Matrix should be square, ' |
There was a problem hiding this comment.
I am a bit confused here. I have to say I am not sure what shape X should have when affinity == 'precomputed'.
The thing is at the moment, the error message says that X should be square and the comment above says that X should be flat ...
There was a problem hiding this comment.
@jeremiedbb thinks that the matrix should be square because of the code below:
i, j = np.triu_indices(X.shape[0], k=1)
X = X[i, j] # now X is a flat array containing the upper triangular of the distance matrix So the comment needs to be changed.
There was a problem hiding this comment.
Rather than
provide as first argument an ndarray of the shape returned
by pdist: it is a flat array containing the upper triangular of
by sklearn.metrics.pairwise_distances: it is a flat array
say something like:
X should be a distance matrix as returned by sklearn.metrics.pairwise_distances
There was a problem hiding this comment.
I removed the comment. The input matrix should be squared. And for sure it shouldn't be flat, as there is another check somewhere else asking the user to reshape the potential 1D input as a 2D matrix (n,1).
jeremiedbb
left a comment
There was a problem hiding this comment.
Thanks @simonamaggio. A few comments below.
| 'Matrix should be square, ' | ||
| 'as returned by pairwise_distances. ' | ||
| 'Found dimensionality %s' % str(X.shape) |
There was a problem hiding this comment.
I would not say as return by pairwise_distances, because pairwise distances can return non square matrix (if called on X and Y with different shapes). I would just say
'Matrix should be squared. Found matrix of shape {}'.format(X.shape)There was a problem hiding this comment.
Done. Just added 'Distance matrix' instead of 'Matrix' to be more specific.
There was a problem hiding this comment.
Please use the .format syntax.
jeremiedbb
left a comment
There was a problem hiding this comment.
Besides my last comment, lgtm !
| if len(X.shape) != 2 or X.shape[0] != X.shape[1]: | ||
| raise ValueError( | ||
| 'Distance matrix should be square, ' | ||
| 'Found dimensionality %s' % str(X.shape) |
There was a problem hiding this comment.
| 'Found dimensionality %s' % str(X.shape) | |
| 'Got shape %s' % str(X.shape) |
glemaitre
left a comment
There was a problem hiding this comment.
We will need an entry in the whats new as well.
|
@simonamaggio could you please add an entry in the what's new ( |
Co-Authored-By: Guillaume Lemaitre <g.lemaitre58@gmail.com>
glemaitre
left a comment
There was a problem hiding this comment.
Sorry I did not see the typo the first time.
LGTM otherwise
| # by sklearn.metrics.pairwise_distances. | ||
| if X.shape[0] != X.shape[1]: | ||
| raise ValueError( | ||
| 'Distance matrix should be square, ' |
There was a problem hiding this comment.
| 'Distance matrix should be square, ' | |
| 'Distance matrix should be squared, ' |
There was a problem hiding this comment.
Are you sure? I think it's "square matrix", not "squared matrix". This doesn't seem right, but if you insist, I'll change it.
| if X.shape[0] != X.shape[1]: | ||
| raise ValueError( | ||
| 'Distance matrix should be square, ' | ||
| 'Got matrix of shape {}'.format(X.shape) |
There was a problem hiding this comment.
| 'Got matrix of shape {}'.format(X.shape) | |
| f'Got matrix of shape {X.shape}.' |
| # and a non square matrix is passed (PR #16257). | ||
| rng = np.random.RandomState(0) | ||
| X = rng.rand(5, 3) | ||
| with pytest.raises(ValueError, match="Distance matrix should be square, "): |
There was a problem hiding this comment.
| with pytest.raises(ValueError, match="Distance matrix should be square, "): | |
| with pytest.raises(ValueError, match="Distance matrix should be squared, "): |
There was a problem hiding this comment.
Are you sure? I think it's "square matrix", not "squared matrix". This doesn't seem right, but if you insist, I'll change it.
|
@simonamaggio Thank you for finishing the PR. Do not hesitate to take another one :) |
Reference Issues/PRs
Closes #4901
What does this implement/fix? Explain your changes.
Added a check in AgglomerativeClustering, to make sure the input matrix of fit() is square, when affinity=precomputed.
This raises a more clear error when a non square matrix is used as input (instead of out of bounds IndexError).
Any other comments?