scipy.spatial.KDTree.
query_pairs#
- KDTree.query_pairs(r, p=2.0, eps=0, output_type='set')[原始碼]#
找出 self 中所有距離至多為 r 的點對。
- 參數:
- r正浮點數
最大距離。
- p浮點數,選用
要使用的 Minkowski 範數。p 必須符合條件
1 <= p <= infinity
。- eps浮點數,選用
近似搜尋。如果樹狀結構的分支最近點的距離大於
r/(1+eps)
,則不會探索這些分支;如果最遠點的距離小於r * (1+eps)
,則會批量新增分支。eps 必須是非負數。- output_type字串,選用
選擇輸出容器,'set' 或 'ndarray'。預設值:'set'
版本 1.6.0 新增。
- 返回:
- resultsset 或 ndarray
成對集合
(i,j)
,其中i < j
,對應位置彼此接近。如果 output_type 為 'ndarray',則會返回 ndarray 而非集合。
範例
您可以搜尋 kd 樹中距離內的所有點對
>>> import matplotlib.pyplot as plt >>> import numpy as np >>> from scipy.spatial import KDTree >>> rng = np.random.default_rng() >>> points = rng.random((20, 2)) >>> plt.figure(figsize=(6, 6)) >>> plt.plot(points[:, 0], points[:, 1], "xk", markersize=14) >>> kd_tree = KDTree(points) >>> pairs = kd_tree.query_pairs(r=0.2) >>> for (i, j) in pairs: ... plt.plot([points[i, 0], points[j, 0]], ... [points[i, 1], points[j, 1]], "-r") >>> plt.show()