scipy.spatial.KDTree.

query_ball_point#

KDTree.query_ball_point(x, r, p=2.0, eps=0, workers=1, return_sorted=None, return_length=False)[source]#

找出點(們) x 距離 r 內的所有點。

參數:
xarray_like, 形狀 tuple + (self.m,)

要搜尋鄰近點的點或多個點。

rarray_like, float

要返回的點的半徑,必須廣播到 x 的長度。

pfloat, 選擇性

要使用的 Minkowski p-範數。應在 [1, inf] 範圍內。如果發生溢位,有限大的 p 可能會導致 ValueError。

eps非負浮點數, 選擇性

近似搜尋。如果樹的分支最近點遠於 r / (1 + eps),則不會探索這些分支;如果最遠點近於 r * (1 + eps),則會批量新增分支。

workersint, 選擇性

要排程用於平行處理的任務數量。如果給定 -1,則使用所有處理器。預設值:1。

在 1.6.0 版本中新增。

return_sortedbool, 選擇性

如果為 True,則對返回的索引進行排序;如果為 False,則不排序。如果為 None,則不對單點查詢進行排序,但對多點查詢進行排序,這是新增此選項之前的行為。

在 1.6.0 版本中新增。

return_lengthbool, 選擇性

返回半徑內點的數量,而不是索引列表。

在 1.6.0 版本中新增。

返回:
results列表或列表陣列

如果 x 是單個點,則返回 x 的鄰近點的索引列表。如果 x 是點陣列,則返回一個形狀為 tuple 的物件陣列,其中包含鄰近點列表。

註解

如果您有很多點要尋找鄰近點,您可以將它們放入 KDTree 並使用 query_ball_tree,從而節省大量時間。

範例

>>> import numpy as np
>>> from scipy import spatial
>>> x, y = np.mgrid[0:5, 0:5]
>>> points = np.c_[x.ravel(), y.ravel()]
>>> tree = spatial.KDTree(points)
>>> sorted(tree.query_ball_point([2, 0], 1))
[5, 10, 11, 15]

查詢多個點並繪製結果

>>> import matplotlib.pyplot as plt
>>> points = np.asarray(points)
>>> plt.plot(points[:,0], points[:,1], '.')
>>> for results in tree.query_ball_point(([2, 0], [3, 3]), 1):
...     nearby_points = points[results]
...     plt.plot(nearby_points[:,0], nearby_points[:,1], 'o')
>>> plt.margins(0.1, 0.1)
>>> plt.show()
../../_images/scipy-spatial-KDTree-query_ball_point-1.png