scipy.spatial.KDTree.
query_ball_tree#
- KDTree.query_ball_tree(other, r, p=2.0, eps=0)[source]#
找出 self 和 other 之間距離至多為 r 的所有點對。
- 參數:
- otherKDTree 實例
包含要搜尋點的樹。
- rfloat
最大距離,必須為正數。
- pfloat,選用
要使用的 Minkowski 範數。p 必須符合條件
1 <= p <= infinity
。- epsfloat,選用
近似搜尋。如果樹的分支最近點遠於
r/(1+eps)
,則不會探索這些分支;如果樹的最遠點近於r * (1+eps)
,則會批量加入這些分支。eps 必須為非負數。
- 回傳:
- results列表的列表
對於此樹的每個元素
self.data[i]
,results[i]
是other.data
中其鄰居索引的列表。
範例
您可以搜尋兩個 kd 樹之間在一定距離內的所有點對
>>> import matplotlib.pyplot as plt >>> import numpy as np >>> from scipy.spatial import KDTree >>> rng = np.random.default_rng() >>> points1 = rng.random((15, 2)) >>> points2 = rng.random((15, 2)) >>> plt.figure(figsize=(6, 6)) >>> plt.plot(points1[:, 0], points1[:, 1], "xk", markersize=14) >>> plt.plot(points2[:, 0], points2[:, 1], "og", markersize=14) >>> kd_tree1 = KDTree(points1) >>> kd_tree2 = KDTree(points2) >>> indexes = kd_tree1.query_ball_tree(kd_tree2, r=0.2) >>> for i in range(len(indexes)): ... for j in indexes[i]: ... plt.plot([points1[i, 0], points2[j, 0]], ... [points1[i, 1], points2[j, 1]], "-r") >>> plt.show()