Skip to content

ENH Paired haversine - #22705

Open
jona-sassenhagen wants to merge 41 commits into
scikit-learn:mainfrom
jona-sassenhagen:jona/paired_haversine_take_2
Open

jona-sassenhagen wants to merge 41 commits into
scikit-learn:mainfrom
jona-sassenhagen:jona/paired_haversine_take_2

Conversation

@jona-sassenhagen

@jona-sassenhagen jona-sassenhagen commented Mar 6, 2022 •

Copy link
Copy Markdown
Contributor

Closes #17212

There is a PR in #17258 , but it has grown stale.

(I also realised there was an issue in the original implementation, in that the existing haversine expects the input to be in radians.)

WIP because I cannot build sklearn to test locally right now.

Comment thread sklearn/metrics/tests/test_pairwise.py Resolved
@jona-sassenhagen jona-sassenhagen changed the title [WIP] Paired haversine Paired haversine Mar 6, 2022
@jona-sassenhagen jona-sassenhagen changed the title Paired haversine ENH Paired haversine Mar 14, 2022
@jona-sassenhagen

Copy link
Copy Markdown
Contributor Author

@jnothman would you have time to review?

@cmarmo cmarmo left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @jona-sassenhagen for your work.
I would like to see tested the error about the dimensions.

Comment thread sklearn/metrics/pairwise.py Outdated
Comment on lines +905 to +910
if not (len(X.shape) == len(Y.shape) == 2) or not (X.shape[1] == Y.shape[1] == 2):
raise ValueError(
"For paired distances and the metric `haversine`, "
"X and Y should both be of shape (n_samples, 2), "
"but X.shape is {0} and Y.shape is {1}".format(X.shape, Y.shape)
)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mind adding a test verifying that the ValueError is raised?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I see now that you added it already... I'd rather have an independent test checking the error message too... but I guess this is not essential...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cmarmo Does that mean you're ok with the test as is?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find more readable to have a separate test. But I think it is better to have another opinion, just to avoid double back and forth.
I'm labeling this one as "Waiting for review": let's hope someone will jump in quickly... :)

@jona-sassenhagen

Copy link
Copy Markdown
Contributor Author

ping @jnothman

@jona-sassenhagen

Copy link
Copy Markdown
Contributor Author

@agramfort maybe you can take a look?

@agramfort agramfort left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jona-sassenhagen I am mostly concerned about the organization of the tests

Comment thread sklearn/metrics/tests/test_pairwise.py Outdated
Y = np.tile(X[0, :], (5, 1))
S = haversine_distances(X, Y[0].reshape(1, -1)).ravel()
S2 = paired_haversine_distances(X, Y).ravel()
assert_array_almost_equal(S, S2)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test should test the function pairwise_distances with parameter haversine

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You see that no paired_xxx function are tested in this test function. What I would suggest is that you don’t pop the haversine function from the global but amend the test so that you only have 2 features if haversine is used. You could for example just take the first two columns so haversine is tested like all others.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

D'uh. That's an obvious mistake.

@jona-sassenhagen jona-sassenhagen Sep 14, 2022 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I don't pop it, I get a bunch of errors like this:

test_agglomerative_clustering
ValueError: For paired distances and the metric `haversine`, X and Y should both be of shape (n_samples, 2), but X.shape is (9900, 50) and Y.shape is (9900, 50)

from places like this

         for metric in PAIRED_DISTANCES.keys():
            # Compare our (structured) implementation to scipy
            clustering = AgglomerativeClustering(
                n_clusters=10,
                connectivity=np.ones((n_samples, n_samples)),
                metric=metric,
                linkage="complete",
            )

Should I manually exempt all of them? I.e.,

        for metric in PAIRED_DISTANCES.keys():
            if metric == 'paired_haversine':
                continue
            # Compare our (structured) implementation to scipy
            clustering = AgglomerativeClustering(
                n_clusters=10,
                connectivity=np.ones((n_samples, n_samples)),
                metric=metric,
                linkage="complete",
            )

@cmarmo

cmarmo commented Jul 7, 2022

Copy link
Copy Markdown
Contributor

Hi @jona-sassenhagen are you interested in finalizing this pull request? Thanks for your work so far.

@jona-sassenhagen

Copy link
Copy Markdown
Contributor Author

Hi @cmarmo - yeah sorry, got distracted. I will finish it.

@jona-sassenhagen

Copy link
Copy Markdown
Contributor Author

I'm getting this test failure:

E       AssertionError: assert dtype('float64') == <class 'numpy.float32'>

I'm not sure how to resolve it. Should I just coerce to global_dtype /np.float32?

@Micky774

Copy link
Copy Markdown
Contributor

Not sure I understand.

The point of this PR is so that you can do sklearn.metrics.pairwise.paired_distances(X, Y, metric='haversine'), which currently gives ValueError: Unknown distance haversine.

A couple things going on in this PR:

  1. We enable pairwise.paired_distances(X, Y, metric='haversine'), but we do this by
  2. Creating and exposing a new public function pairwise.paired_haversine_distances

The point here is that we don't know if we want to keep on maintaining public paired_*_distances functions. Each of these are specializations that we're not convinced belong in scikit-learn in the first place.

In fact, it is relatively easy to construction your own paired_distances function provided your favorite scipy distance metric. Is that something that we, as a library, need to provide? We use it very sparsely internally, but its public exposure is costly in terms of maintenance. Something like pairwise_distances (note pairwise vs paired) makes more sense to maintain because we actually use it in many core computations.

And even if we do choose to continue maintaining paired_distances, should we maintain each individual paired_*_distances function, and keep that as our canonical pattern for when next we have a request to add a new metric? We could, for example, rely on our internal DistanceMetric objects and make the entire thing generic. This would allow is to deprecate and remove the paired_*_distances functions without abandoning paired_distances.

At the end of the day, the questions are:

  1. Do we want to maintain a public paired_distances function?
  2. Do we want to maintain public paired_*_distances functions?

I am -0 on the first, and -1 on the second.

@jjerphan

Copy link
Copy Markdown
Member

Do we want to maintain a public paired_distances function?

I think we must since users are using it.

Do we want to maintain public paired_*_distances functions?

I think we better remove them and point users to paired_distances with support for haversine which is currently missing.

@Micky774

Copy link
Copy Markdown
Contributor

I think we better remove them and point users to paired_distances with support for haversine which is currently missing.

In that case, do we want to potentially opt for a generic function which leverages DistanceMetric objects? That way we don't have individual specializations?

@jona-sassenhagen

Copy link
Copy Markdown
Contributor Author

I don't feel qualified to speak to the larger question of how the pairwise/paired API should look like. But wouldn't it be orthogonal to that to add the function in this PR as private? And continue the discussion of how to continue with paired elsewhere?

@jjerphan

Copy link
Copy Markdown
Member

In that case, do we want to potentially opt for a generic function which leverages DistanceMetric objects? That way we don't have individual specializations?

Potentially if we get the exact same results numerically and if there are no performance regression. To me, this decision goes beyond the scope of this PR; what do you think of discussing the implementation of paired_distances in a dedicated issue?

@Micky774

Micky774 commented Jul 28, 2023 •

Copy link
Copy Markdown
Contributor

But wouldn't it be orthogonal to that to add the function in this PR as private? And continue the discussion of how to continue with paired elsewhere?

Yes, I think that is at least fine 👍

To me, this decision goes beyond the scope of this PR; what do you think of discussing the implementation of paired_distances in a dedicated issue?

I will create an issue for it on Monday, but if anyone else prefers to do so before then, feel free.

@jjerphan

Copy link
Copy Markdown
Member

@jona-sassenhagen: I would proceed as you propose. 👍

Comment thread doc/whats_new/v1.4.rst Resolved
Comment thread doc/whats_new/v1.3.rst Resolved
@haiatn

haiatn commented Jul 29, 2023

Copy link
Copy Markdown
Contributor

Thank you for your work! Can't wait for it to merge

@jjerphan jjerphan left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

Comment thread doc/whats_new/v1.4.rst Resolved
Comment thread sklearn/metrics/pairwise.py Resolved
Comment thread sklearn/metrics/pairwise.py Resolved
jona-sassenhagen and others added 4 commits July 29, 2023 12:50
@jona-sassenhagen

Copy link
Copy Markdown
Contributor Author

Thx everyone, I accepted the docstring suggestions, please merge when green.

@jona-sassenhagen

Copy link
Copy Markdown
Contributor Author

@cmarmo I think you're a member now, could you approve and merge?

@adrinjalali adrinjalali left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since in a discussion we had in our drafting meeting, we were not sure if this should be included or weather paired_distances should be deprecated altogether, I'm putting a "request changes" here to mark that for now.

@jona-sassenhagen

Copy link
Copy Markdown
Contributor Author

Hi @adrinjalali , I think this was discussed and resolved above? E.g. around here #22705 (comment)

@adrinjalali

Copy link
Copy Markdown
Member

I don't see any resolution in the discussion. I'm proposing paired_distances to be deprecated and removed, and I don't see why we wouldn't do that.

@Micky774

Copy link
Copy Markdown
Contributor

@adrinjalali @jjerphan Do we have an idea of how widely both paired_distances and paired_*_distances are used? The website stats indicate 247 page hits for paired_distances in the last 30 days, but that's not a perfect indicator by any means.

@adrinjalali Would your opinion changed if we could make it a generic and thin wrapper around using DistanceMetric.dist?

@jona-sassenhagen

Copy link
Copy Markdown
Contributor Author

At this time there is no change to the API from this PR and I thought discussion of the API should happen in a separate and dedicated issue?

@adrinjalali

Copy link
Copy Markdown
Member

Would the wrapper be anything other than return np.array([f(x, y) for x, y in zip(a, b)]) plus input data checks? I don't think that warrants a helper method. It's faster if users write it on their own since then they won't need to do input type checks and it'll faster for them to run.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

module:metrics Needs Decision Requires decision Waiting for Second Reviewer First reviewer is done, need a second one!

Projects

None yet

Development

Successfully merging this pull request may close these issues.

"Paired" distance function for haversine distance

7 participants