你可以使用一個scipy.spatial.cKDTree
:
import numpy as np
import scipy.spatial as spatial
points = np.array([(1, 2), (3, 4), (4, 5)])
point_tree = spatial.cKDTree(points)
# This finds the index of all points within distance 1 of [1.5,2.5].
print(point_tree.query_ball_point([1.5, 2.5], 1))
# [0]
# This gives the point in the KDTree which is within 1 unit of [1.5, 2.5]
print(point_tree.data[point_tree.query_ball_point([1.5, 2.5], 1)])
# [[1 2]]
# More than one point is within 3 units of [1.5, 1.6].
print(point_tree.data[point_tree.query_ball_point([1.5, 1.6], 3)])
# [[1 2]
# [3 4]]
這裏是你展示如何 找到所有最近的鄰居點的數組,一個電話 到point_tree.query_ball_point
一個例子:
import numpy as np
import scipy.spatial as spatial
import matplotlib.pyplot as plt
np.random.seed(2015)
centers = [(1, 2), (3, 4), (4, 5)]
points = np.concatenate([pt+np.random.random((10, 2))*0.5
for pt in centers])
point_tree = spatial.cKDTree(points)
cmap = plt.get_cmap('copper')
colors = cmap(np.linspace(0, 1, len(centers)))
for center, group, color in zip(centers, point_tree.query_ball_point(centers, 0.5), colors):
cluster = point_tree.data[group]
x, y = cluster[:, 0], cluster[:, 1]
plt.scatter(x, y, c=color, s=200)
plt.show()
你有沒有試圖自己做到這一點呢?你的代碼現在是什麼樣的?你能舉一個你想要計算什麼的例子(即3米是什麼意思)?這些GPS座標? – reynoldsnlp
'從SciPy的進口空間 myTreeName = spatial.cKDTree(座標,leafsize = 100)在座標 爲項: TheResult = myTreeName.query(項目中,k = 20,distance_upper_bound = 3)' 是我之前,但嘗試過在這裏我必須指定我想找到多少個最近的鄰居。是的,這些都是GPS座標(X,Y),我想爲數據集中的每個點找到半徑爲3米範圍內的所有NN。 – Kitumijasi