Skip to content

[MRG] Paired haversine distances - #17258

Closed
acharles7 wants to merge 3 commits into
scikit-learn:mainfrom
acharles7:paired_distance
Closed

acharles7 wants to merge 3 commits into
scikit-learn:mainfrom
acharles7:paired_distance

Conversation

@acharles7

@acharles7 acharles7 commented May 18, 2020 •

Copy link
Copy Markdown
Contributor

Reference Issues/PRs

Added feature to calculate haversine distance between 2 paired arrays with equal dimension as mentioned in #17212

What does this implement/fix? Explain your changes.

Added a new feature in the metrics module to calculate haversine distance b/w 2 arrays
Currently, paired_distances supports the following options:

PAIRED_DISTANCES = {
    'cosine': paired_cosine_distances,
    'euclidean': paired_euclidean_distances,
    'l2': paired_euclidean_distances,
    'l1': paired_manhattan_distances,
    'manhattan': paired_manhattan_distances,
    'cityblock': paired_manhattan_distances}

Now, using this PR, we can calculate haversine distance between two paired arrays of shape (n_samples, 2), haversine: paired_haversine_distances in paired_distances

Any other comments?

It seems like this would be a useful distance measure to have in sklearn

Comment thread sklearn/metrics/pairwise.py Resolved
Comment thread sklearn/metrics/pairwise.py Resolved
@jona-sassenhagen

jona-sassenhagen commented May 18, 2020 •

Copy link
Copy Markdown
Contributor

Tests are failing because the paired metrics expect data of the shape n_samples, n_features, and is used with n_features > 2 in some tests. The new function accepts only n_samples, 2.

@acharles7

Copy link
Copy Markdown
Contributor Author

@jona-sassenhagen Yes, the paired metrics expect data of shape n_samples, n_features but, the haversine distance only calculates distance b/w two paired array of shape n_samples, 2 (longitude, latitude). Right? Should I remove haversine distance from paired metrics and leave standalone paired function, paired_haversine_distances like paired_euclidean_distances and paired_manhattan_distances

@jona-sassenhagen

Copy link
Copy Markdown
Contributor

Maybe you could check if it is only the unit tests which loop over all paired metrics and just fix it for the unit tests by looping over a new collection PAIRED_DISTANCES_WITHOUT_RESTRICTIONS or something like that?

@acharles7

Copy link
Copy Markdown
Contributor Author

@jona-sassenhagen I'll create a new collection called PAIRED_DISTANCES_WITHOUT_RESTRICTIONS and place haversine in it and change the paired_distances method accordingly and its tests, then see the results of the tests. thank you for this.

@acharles7 acharles7 changed the title Paired haversine distance [MRG] Paired haversine distance May 19, 2020
@acharles7 acharles7 changed the title [MRG] Paired haversine distance [MRG] Paired haversine distances May 19, 2020
@acharles7

Copy link
Copy Markdown
Contributor Author

@jnothman Please have a look at this PR when you get a chance. Thanks

@zachmayer

Copy link
Copy Markdown
Contributor

@jnothman tests are green on this PR— could you review it?

Comment thread sklearn/metrics/pairwise.py Outdated
a = np.sin(dlat / 2.0) ** 2 + np.cos(lat1) * np.cos(lat2) * \
np.sin(dlon / 2.0) ** 2
c = 2 * np.arcsin(np.sqrt(a))
km = 6367 * c

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 would maybe explain this magic number

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.

Ok, I'll add some description

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

lgtm

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

Thanks for the ping

Comment thread sklearn/metrics/tests/test_pairwise.py Outdated
# Test the pairwise_distance helper function.
rng = np.random.RandomState(0)

if metric == "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.

I think you still need to compare to the pairwise distance implementation, as with other metrics below. It would be clearer, in my opinion, to use the conditional block only to change X and Y. To handle the lack of sparse support, check below that an appropriate error is raised if the sparse case doesn't just work.

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.

Would you mind giving me an example of how to do that if you have some time?

# Test paired haversine distance
# The data should be valid latitude and longitude

X = rng.random_sample((5, 2))

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.

Please generate data with a more appropriate range for haversine

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.

Do you mean more data? i.e X = rng.random_sample((20, 2))
Can you give me some examples?

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.

added new fake data below

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.

I mean that this input ranges from 0 to 1, unlike haversine input

assert_array_almost_equal(D, [1., 2.])


def test_paired_haversine_distances():

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 shouldn't be needed if the above comprise to pairwise_distances

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.

added new data.
I'll have this remove if you want

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.

X = np.array([[-34.83333, -58.5166646], [-43.63284, -63.9876712]])
Y = np.array([[49.0083899664, 2.53844117956], [57.23313, 4.993234]])

Comment thread sklearn/metrics/pairwise.py Resolved
Comment thread sklearn/metrics/pairwise.py Resolved
Comment thread sklearn/metrics/pairwise.py Resolved
@jnothman

jnothman commented Jul 2, 2020

Copy link
Copy Markdown
Member

Test failures

@acharles7
acharles7 requested a review from jnothman July 3, 2020 18:18
Parameters
----------
X : ndarray (n_samples, n_features)
X : ndarray (n_samples, n_features) or, (n_samples, 2)

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.

I don't think this needs changing. For haversine, n_features=2, but that doesn't mean the general case is insufficient

>>> paired_distances(X, Y)
array([0., 1.])

The haversine metric only accept array of shape (n_samples, 2)

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.

Suggested change
The haversine metric only accept array of shape (n_samples, 2)
The haversine metric only accepts array of shape (n_samples, 2)

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.

I'm not sure that another example here was needed

'manhattan': paired_manhattan_distances,
'cityblock': paired_manhattan_distances}

PAIRED_DISTANCES_WITHOUT_RESTRICTIONS = {

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.

I don't understand why this was introduced in the public API. I understand if haversine needs distinguishing from others in testing, but why here? I also don't understand what "without restrictions" intends, when haversine is restricted to dense and 2 features

Base automatically changed from master to main January 22, 2021 10:52
@jona-sassenhagen

Copy link
Copy Markdown
Contributor

What's preventing this one from being merged? @acharles7 seems like this shouldn't be too hard?

@jona-sassenhagen

Copy link
Copy Markdown
Contributor

@acharles7 ping

@jona-sassenhagen

Copy link
Copy Markdown
Contributor

@jnothman I would be interested in taking over this PR. Should I make a new PR based on this?

@jnothman

jnothman commented Mar 6, 2022

Copy link
Copy Markdown
Member

Yes that would be fine, @jona-sassenhagen

@cmarmo cmarmo added the Superseded PR has been replace by a newer PR label Mar 10, 2022
@cmarmo

cmarmo commented Nov 26, 2022

Copy link
Copy Markdown
Contributor

Closing this one as superseded by #22705.

@cmarmo cmarmo closed this Nov 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

module:metrics Superseded PR has been replace by a newer PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants