This is a pre-release version (2.0.0.dev0+git20260907.41eeb59).Go to latest (1.18.0)
{ } Raw JSON

bundles / scipy 2.0.0.dev0+git20260907.41eeb59 / scipy / spatial / _kdtree / KDTree / query_pairs

function

scipy.spatial._kdtree:KDTree.query_pairs

source: build-install/usr/lib/python3.14/site-packages/scipy/spatial/_kdtree.py:692

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 float

The maximum distance.

p : float, optional

Which Minkowski norm to use. p has to meet the condition 1 <= p <= infinity.

eps : float, optional

Approximate 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 than r * (1+eps). eps has to be non-negative.

output_type : str, optional

Choose the output container, 'set' or 'ndarray'. Default: 'set'

Returns

results : set or ndarray

Set of pairs (i,j), with i < 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)
✗
[<matplotlib.lines.Line2D object at 0x7f3f68aecd70>]
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")
✗
[<matplotlib.lines.Line2D object at 0x7f3f68aec2f0>]
[<matplotlib.lines.Line2D object at 0x7f3f68aec830>]
[<matplotlib.lines.Line2D object at 0x7f3f68aedbe0>]
[<matplotlib.lines.Line2D object at 0x7f3f68aed160>]
[<matplotlib.lines.Line2D object at 0x7f3f68aee510>]
[<matplotlib.lines.Line2D object at 0x7f3f68aedfd0>]
[<matplotlib.lines.Line2D object at 0x7f3f68aef0e0>]
[<matplotlib.lines.Line2D object at 0x7f3f68aeecf0>]
[<matplotlib.lines.Line2D object at 0x7f3f68aef380>]
[<matplotlib.lines.Line2D object at 0x7f3f68aee660>]
[<matplotlib.lines.Line2D object at 0x7f3f68aefe00>]
[<matplotlib.lines.Line2D object at 0x7f3f68aee3c0>]
[<matplotlib.lines.Line2D object at 0x7f3f68aec6e0>]
[<matplotlib.lines.Line2D object at 0x7f3f68aef230>]
[<matplotlib.lines.Line2D object at 0x7f3f68aefb60>]
[<matplotlib.lines.Line2D object at 0x7f3f68aed400>]
[<matplotlib.lines.Line2D object at 0x7f3f68aecc20>]
[<matplotlib.lines.Line2D object at 0x7f3f59b11d30>]
[<matplotlib.lines.Line2D object at 0x7f3f59b13b60>]
[<matplotlib.lines.Line2D object at 0x7f3f59b10440>]
[<matplotlib.lines.Line2D object at 0x7f3f59b12120>]
[<matplotlib.lines.Line2D object at 0x7f3f59b11550>]
[<matplotlib.lines.Line2D object at 0x7f3f59b13620>]
plt.show()
✓
fig-ad99bf26f296f93c.png

Aliases

  • scipy.spatial.KDTree.query_pairs