Skip to content

Jaccard distance in trees very different from pairwise_distances jaccard distance. #4523

Closed

Description

@amueller

As observed in #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])

[[ 0. 1. 1. 1. 1.]
[ 0. 1. 1. 1. 1.]
[ 0. 1. 1. 1. 1.]
[ 0. 1. 1. 1. 1.]
[ 0. 1. 1. 1. 1.]
[ 0. 1. 1. 1. 1.]]
[[ 0. 0. 0. 0. 0.]
[ 0. 0. 0. 0. 0.]
[ 0. 0. 0. 0. 0.]
[ 0. 0. 0. 0. 0.]
[ 0. 0. 0. 0. 0.]
[ 0. 0. 0. 0. 0.]]

Activity

  1. jnothman commented on Apr 6, 2015

    @jnothman
    Member

    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:

    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])

    [[ 0. 1. 1. 1. 1.]
    [ 0. 1. 1. 1. 1.]
    [ 0. 1. 1. 1. 1.]
    [ 0. 1. 1. 1. 1.]
    [ 0. 1. 1. 1. 1.]
    [ 0. 1. 1. 1. 1.]]
    [[ 0. 0. 0. 0. 0.]
    [ 0. 0. 0. 0. 0.]
    [ 0. 0. 0. 0. 0.]
    [ 0. 0. 0. 0. 0.]
    [ 0. 0. 0. 0. 0.]
    [ 0. 0. 0. 0. 0.]]

    —
    Reply to this email directly or view it on GitHub
    #4523.

  2. amueller commented on Apr 6, 2015

    @amueller
    MemberAuthor

    yeah. Well pairwise_distances is calling scipy.spatial.distance here, but dist_metrics does something else.

  3. amueller commented on Apr 6, 2015

    @amueller
    MemberAuthor

    See the tests in #4522

  4. jakevdp commented on May 5, 2015

    @jakevdp
    Member

    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.

  5. amueller commented on May 5, 2015

    @amueller
    MemberAuthor

    @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?

  6. added this to the 0.16.2 milestone on May 5, 2015
  7. jakevdp commented on May 5, 2015

    @jakevdp
    Member

    Maybe we should just implement or own boolean brute metric?

    I think that's probably best. I do believe that the scipy implementation is technically not correct.

  8. jakevdp commented on May 5, 2015

    @jakevdp
    Member

    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.

  9. ogrisel commented on May 7, 2015

    @ogrisel
    Member

    +1 for at least raising a warning, maybe even an exception.

  10. amueller commented on May 7, 2015

    @amueller
    MemberAuthor

    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?

  11. jakevdp commented on May 7, 2015

    @jakevdp
    Member

    Rather than re-implement, we could add a validation check for all the boolean metrics. Basically something like

    if np.any((X != 1) | (X != 0)):
        warnings.warn("casting data to boolean for {0} metric".format(metric))
        X = (X != 0).astype(float)

    Then the current code should be consistent between scipy and scikit-learn (and is tested as such with our current unit tests).

  12. amueller commented on May 7, 2015

    @amueller
    MemberAuthor

    Why would you convert back to float in the end? Because the trees need that?

  13. jakevdp commented on May 7, 2015

    @jakevdp
    Member

    Hmm, I can't remember what input type they assume. Let me check

  14. amueller commented on May 7, 2015

    @amueller
    MemberAuthor

    Ok, lets raise a DataConversionWarning. As the previous behavior was inconsistent, I think the behavior change is "ok".

  15. 3 remaining items

  16. jakevdp commented on May 7, 2015

    @jakevdp
    Member

    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.

  17. amueller commented on May 7, 2015

    @amueller
    MemberAuthor

    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.

  18. jakevdp commented on May 7, 2015

    @jakevdp
    Member

    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.

  19. amueller commented on May 19, 2015

    @amueller
    MemberAuthor

    @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.

  20. modified the milestones: 0.16.2, 0.17 on Sep 8, 2015
  21. tomMoral commented on Oct 19, 2015

    @tomMoral
    Contributor

    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?

  22. amueller commented on Oct 21, 2015

    @amueller
    MemberAuthor

    @tomMoral yeah I think that is right (your summary of the consensus)

  23. modified the milestones: 0.17, 0.18 on Nov 2, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions