scipy 1.8.0 Pypi GitHub Homepage
Other Docs
ParametersReturnsBackRef
query_ball_tree(self, other, r, p=2.0, eps=0)

Parameters

other : KDTree instance

The tree containing points to search against.

r : float

The maximum distance, has to be positive.

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) . :None:None:`eps` has to be non-negative.

Returns

results : list of lists

For each element self.data[i] of this tree, results[i] is a list of the indices of its neighbors in other.data .

Find all pairs of points between :None:None:`self` and other whose distance is at most r.

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)
... 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()
See :

Back References

The following pages refer to to this document either explicitly or contain code examples using this.

scipy.spatial._kdtree.KDTree.count_neighbors scipy.spatial._kdtree.KDTree.query_ball_tree

Local connectivity graph

Hover to see nodes names; edges to Self not shown, Caped at 50 nodes.

Using a canvas is more power efficient and can get hundred of nodes ; but does not allow hyperlinks; , arrows or text (beyond on hover)

SVG is more flexible but power hungry; and does not scale well to 50 + nodes.

All aboves nodes referred to, (or are referred from) current nodes; Edges from Self to other have been omitted (or all nodes would be connected to the central node "self" which is not useful). Nodes are colored by the library they belong to, and scaled with the number of references pointing them


GitHub : /scipy/spatial/_kdtree.py#564
type: <class 'function'>
Commit: