bundles / scipy 2.0.0.dev0+git20260907.41eeb59 / scipy / spatial / _kdtree / KDTree / query_ball_tree
function
scipy.spatial._kdtree:KDTree.query_ball_tree
source: build-install/usr/lib/python3.14/site-packages/scipy/spatial/_kdtree.py:641
Signature
def query_ball_tree(self, other, r, p = 2.0, eps = 0.0)Summary
Find all pairs of points between self and other whose distance is at most r.
Parameters
other: KDTree instanceThe tree containing points to search against.
r: floatThe maximum distance, has to be positive.
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.
Returns
results: list of listsFor each element
self.data[i]of this tree,results[i]is a list of the indices of its neighbors inother.data.
Examples
You can search all pairs of points between two kd-trees within a distance: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)✗
[<matplotlib.lines.Line2D object at 0x7f3f43ebe510>]
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")✗
[<matplotlib.lines.Line2D object at 0x7f3f43ebf230>] [<matplotlib.lines.Line2D object at 0x7f3f43ebe7b0>] [<matplotlib.lines.Line2D object at 0x7f3f43ebf380>] [<matplotlib.lines.Line2D object at 0x7f3f43ebc050>] [<matplotlib.lines.Line2D object at 0x7f3f43ebf620>] [<matplotlib.lines.Line2D object at 0x7f3f43ebf770>] [<matplotlib.lines.Line2D object at 0x7f3f43ebc1a0>] [<matplotlib.lines.Line2D object at 0x7f3f43ebc2f0>] [<matplotlib.lines.Line2D object at 0x7f3f43ebc440>] [<matplotlib.lines.Line2D object at 0x7f3f43ebc590>] [<matplotlib.lines.Line2D object at 0x7f3f43ebc6e0>] [<matplotlib.lines.Line2D object at 0x7f3f43ebc830>] [<matplotlib.lines.Line2D object at 0x7f3f43ebc980>] [<matplotlib.lines.Line2D object at 0x7f3f43ebcad0>] [<matplotlib.lines.Line2D object at 0x7f3f43ebcc20>] [<matplotlib.lines.Line2D object at 0x7f3f43ebcd70>] [<matplotlib.lines.Line2D object at 0x7f3f43ebcec0>] [<matplotlib.lines.Line2D object at 0x7f3f43ebd010>] [<matplotlib.lines.Line2D object at 0x7f3f43ebd160>] [<matplotlib.lines.Line2D object at 0x7f3f43ebd2b0>] [<matplotlib.lines.Line2D object at 0x7f3f43ebd400>] [<matplotlib.lines.Line2D object at 0x7f3f43ebf4d0>] [<matplotlib.lines.Line2D object at 0x7f3f43ebe120>]
plt.show()
✓
Aliases
scipy.spatial.KDTree.query_ball_tree