You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Jaccard distance in trees very different from pairwise_distances jaccard distance. #4523
As observed in #4522 #4522, BallTree and
pairwise_distances have very different results for metric="jaccard":
from sklearn.neighbors import NearestNeighbors
X = np.random.uniform(size=(6, 5))
nn = NearestNeighbors(metric="jaccard", algorithm='brute').fit(X)print(nn.kneighbors(X)[0])
nn = NearestNeighbors(metric="jaccard", algorithm='ball_tree').fit(X)print(nn.kneighbors(X)[0])
I think the "extended" Jaccard distance used by scipy is not actually a true metric, which is why BallTree casts to bools and uses the true (binary) Jaccard metric. Under the scipy definition, the BallTree search would fail.
@jakevdp Do you have a good idea to resolve this? I find the current state pretty catastrophic (different metrics are used depending on the size of the data).
Maybe we should just implement or own boolean brute metric?
Note that the unit tests in the sklearn.neighbors module explicitly only test boolean metrics with boolean values, which is why this wasn't caught by the tests. It's a simple case of GIGO, so perhaps we should be more explicit in the documentation, and/or raise a warning if the user passes non-boolean data to a boolean metrics.
OK, all of a sudden I'm confused about where this should go. Do we want all pairwise_distance calculations to do this for boolean metrics, or are we just worried about routines with e.g. algorithm='brute' and algorithm='ball_tree'?
I'm worried that there may be use cases for the non-boolean appliactions of the boolean metrics that I don't know about.
It's a bug if you expect the results to conform to the definition of a metric. But there may be cases where the extended non-metric definition is useful; I just don't know of any.
@TomDLT if you like you can have a look at this one. The consensus seems to be to cast float data to boolean data when a boolean metric is requested and raise a warning.
We looked into it today. Our understanding of the concensus is:
We should add a list PAIRWISE_BOOLEAN_METRIC in pairwise.py and check for those metrics that the input array is boolean in pairwise_distances before pdist.
Else we raise a warning and cast the array to boolean.
Is that correct? Shouldn't we switch to using the dist_metrics implementation of the metric?
Activity
jnothman commentedon Apr 6, 2015
Oh, that doesn't look good. Both pairwise_distances and
sklearn.neighbors.dist_metrics should be tested against
scipy.spatial.distance where possible.
On 6 April 2015 at 06:28, Andreas Mueller [email protected] wrote:
amueller commentedon Apr 6, 2015
yeah. Well
pairwise_distancesis calling scipy.spatial.distance here, butdist_metricsdoes something else.amueller commentedon Apr 6, 2015
See the tests in #4522
jakevdp commentedon May 5, 2015
Relevant discussion on the SciPy-dev mailing list: http://mail.scipy.org/pipermail/scipy-dev/2012-December/018129.html
I think the "extended" Jaccard distance used by scipy is not actually a true metric, which is why BallTree casts to bools and uses the true (binary) Jaccard metric. Under the scipy definition, the BallTree search would fail.
amueller commentedon May 5, 2015
@jakevdp Do you have a good idea to resolve this? I find the current state pretty catastrophic (different metrics are used depending on the size of the data).
Maybe we should just implement or own boolean brute metric?
jakevdp commentedon May 5, 2015
I think that's probably best. I do believe that the scipy implementation is technically not correct.
jakevdp commentedon May 5, 2015
Note that the unit tests in the
sklearn.neighborsmodule explicitly only test boolean metrics with boolean values, which is why this wasn't caught by the tests. It's a simple case of GIGO, so perhaps we should be more explicit in the documentation, and/or raise a warning if the user passes non-boolean data to a boolean metrics.ogrisel commentedon May 7, 2015
+1 for at least raising a warning, maybe even an exception.
amueller commentedon May 7, 2015
If we raise an exception, we will not have to reimplement. But it will break peoples code. Reimplement +warn seems like a good idea, maybe?
jakevdp commentedon May 7, 2015
Rather than re-implement, we could add a validation check for all the boolean metrics. Basically something like
Then the current code should be consistent between scipy and scikit-learn (and is tested as such with our current unit tests).
amueller commentedon May 7, 2015
Why would you convert back to float in the end? Because the trees need that?
jakevdp commentedon May 7, 2015
Hmm, I can't remember what input type they assume. Let me check
amueller commentedon May 7, 2015
Ok, lets raise a
DataConversionWarning. As the previous behavior was inconsistent, I think the behavior change is "ok".3 remaining items
jakevdp commentedon May 7, 2015
OK, all of a sudden I'm confused about where this should go. Do we want all
pairwise_distancecalculations to do this for boolean metrics, or are we just worried about routines with e.g.algorithm='brute'andalgorithm='ball_tree'?I'm worried that there may be use cases for the non-boolean appliactions of the boolean metrics that I don't know about.
amueller commentedon May 7, 2015
I thought you claimed that was a bug ;)
I think we should have a consistent behavior everywhere, so it should probably go in
pairwise_distance.jakevdp commentedon May 7, 2015
It's a bug if you expect the results to conform to the definition of a metric. But there may be cases where the extended non-metric definition is useful; I just don't know of any.
amueller commentedon May 19, 2015
@TomDLT if you like you can have a look at this one. The consensus seems to be to cast float data to boolean data when a boolean metric is requested and raise a warning.
tomMoral commentedon Oct 19, 2015
We looked into it today. Our understanding of the concensus is:
Is that correct? Shouldn't we switch to using the dist_metrics implementation of the metric?
amueller commentedon Oct 21, 2015
@tomMoral yeah I think that is right (your summary of the consensus)