-
-
Notifications
You must be signed in to change notification settings - Fork 26.6k
[MRG + 1] Implement haversine metric in pairwise #4458
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,8 +11,10 @@ | |
| from sklearn.utils.testing import assert_equal | ||
| from sklearn.utils.testing import assert_true | ||
| from sklearn.utils.testing import assert_warns | ||
| from sklearn.utils.testing import assert_raise_message | ||
| from sklearn.utils.testing import ignore_warnings | ||
| from sklearn.utils.validation import check_random_state | ||
|
|
||
| from sklearn.metrics.pairwise import pairwise_distances | ||
| from sklearn import neighbors, datasets | ||
|
|
||
|
|
@@ -797,7 +799,7 @@ def test_neighbors_badargs(): | |
| neighbors.NearestNeighbors, | ||
| algorithm='blah') | ||
|
|
||
| X = rng.random_sample((10, 2)) | ||
| X = rng.random_sample((10, 3)) | ||
| Xsparse = csr_matrix(X) | ||
| y = np.ones(10) | ||
|
|
||
|
|
@@ -812,13 +814,6 @@ def test_neighbors_badargs(): | |
| cls, p=-1) | ||
| assert_raises(ValueError, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This still crashes, right? Why did you remove the test?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So you got "error not raised" here?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry this was my suggestion.
Yes. Travis failed owing to this. I assumed since haversine was not previously implemented, this test was introduced.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, but I thought it is still not implemented for the ball_tree. Is it?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, ok. The test was slightly confusing to me. |
||
| cls, algorithm='blah') | ||
| nbrs = cls(algorithm='ball_tree', metric='haversine') | ||
| assert_raises(ValueError, | ||
| nbrs.predict, | ||
| X) | ||
| assert_raises(ValueError, | ||
| ignore_warnings(nbrs.fit), | ||
| Xsparse, y) | ||
| nbrs = cls() | ||
| assert_raises(ValueError, | ||
| nbrs.fit, | ||
|
|
@@ -830,6 +825,12 @@ def test_neighbors_badargs(): | |
| assert_raises(ValueError, | ||
| nbrs.predict, | ||
| []) | ||
| nbrs = cls(metric='haversine', algorithm='brute') | ||
| nbrs.fit(X, y) | ||
| assert_raise_message(ValueError, | ||
| "Haversine distance only valid in 2 dimensions", | ||
| nbrs.predict, | ||
| X) | ||
|
|
||
| nbrs = neighbors.NearestNeighbors().fit(X) | ||
|
|
||
|
|
@@ -857,7 +858,8 @@ def test_neighbors_metrics(n_samples=20, n_features=3, | |
| ('chebyshev', {}), | ||
| ('seuclidean', dict(V=rng.rand(n_features))), | ||
| ('wminkowski', dict(p=3, w=rng.rand(n_features))), | ||
| ('mahalanobis', dict(VI=VI))] | ||
| ('mahalanobis', dict(VI=VI)), | ||
| ('haversine', {})] | ||
| algorithms = ['brute', 'ball_tree', 'kd_tree'] | ||
| X = rng.rand(n_samples, n_features) | ||
|
|
||
|
|
@@ -880,8 +882,13 @@ def test_neighbors_metrics(n_samples=20, n_features=3, | |
| algorithm=algorithm, | ||
| metric=metric, p=p, | ||
| metric_params=metric_params) | ||
| neigh.fit(X) | ||
| results.append(neigh.kneighbors(test, return_distance=True)) | ||
| if metric == 'haversine': | ||
| neigh.fit(X[:, :2]) | ||
| results.append(neigh.kneighbors(test[:, :2], | ||
| return_distance=True)) | ||
| else: | ||
| neigh.fit(X) | ||
| results.append(neigh.kneighbors(test, return_distance=True)) | ||
|
|
||
| assert_array_almost_equal(results[0][0], results[1][0]) | ||
| assert_array_almost_equal(results[0][1], results[1][1]) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add a reference (to wikipedia?)