bundles / scipy latest / scipy / spatial / _kdtree / KDTree / query_pairs
function
scipy.spatial._kdtree:KDTree.query_pairs
source: /scipy/spatial/_kdtree.py :614
Signature
def query_pairs ( self , r , p = 2.0 , eps = 0.0 , output_type = set ) Summary
Find all pairs of points in self whose distance is at most r.
Parameters
r: positive floatThe maximum distance.
p: float, optionalWhich Minkowski norm to use.
phas to meet the condition1 <= p <= infinity.eps: float, optionalApproximate search. Branches of the tree are not explored if their nearest points are further than
r/(1+eps), and branches are added in bulk if their furthest points are nearer thanr * (1+eps).epshas to be non-negative.output_type: string, optionalChoose the output container, 'set' or 'ndarray'. Default: 'set'
Returns
results: set or ndarraySet of pairs
(i,j), withi < j, for which the corresponding positions are close. If output_type is 'ndarray', an ndarray is returned instead of a set.
Examples
You can search all pairs of points in a kd-tree within a distance: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()
✓
Aliases
-
scipy.spatial.KDTree.query_pairs