Created
February 12, 2015 14:44
-
-
Save jakobkogler/97c2434b1dcc8344e0df to your computer and use it in GitHub Desktop.
Nearest Neighbour
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from scipy import spatial | |
import numpy as np | |
import time | |
def nearest_neighbour(points_a, points_b): | |
tree = spatial.cKDTree(points_b) | |
return tree.query(points_a)[1] | |
def test_random(n): | |
points_a = np.random.rand(n, 2)*100 | |
points_b = np.random.rand(n, 2)*100 | |
start_time = time.time() | |
result = nearest_neighbour(points_a, points_b) | |
end_time = time.time() | |
print("n = {:7d}, {:.3f} seconds".format(n, end_time - start_time)) | |
def test_Michiel(): | |
points_a = np.array([[1, 3],[2, 4],[3, 5],[4, 6],[5, 7]]) | |
points_b = np.array([[2, 2.5],[4, 3.1],[2, 2.0],[3, 3.0],[6, 5.0]]) | |
print('Testdata from Michiel challenge:') | |
print(points_a) | |
print(points_b) | |
print(nearest_neighbour(points_a, points_b)) | |
print() | |
if __name__ == "__main__": | |
test_Michiel() | |
for k in range(7): | |
test_random(10**k) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment