bundles / scipy 1.17.1 / scipy / spatial / _kdtree / KDTree / query
function
scipy.spatial._kdtree:KDTree.query
source: /scipy/spatial/_kdtree.py :369
Signature
def query ( self , x , k = 1 , eps = 0.0 , p = 2.0 , distance_upper_bound = inf , workers = 1 ) Summary
Query the kd-tree for nearest neighbors.
Parameters
x: array_like, last dimension self.mAn array of points to query.
k: int or Sequence[int], optionalEither the number of nearest neighbors to return, or a list of the k-th nearest neighbors to return, starting from 1.
eps: nonnegative float, optionalReturn approximate nearest neighbors; the kth returned value is guaranteed to be no further than (1+eps) times the distance to the real kth nearest neighbor.
p: float, 1<=p<=infinity, optionalWhich Minkowski p-norm to use. 1 is the sum-of-absolute-values distance ("Manhattan" distance). 2 is the usual Euclidean distance. infinity is the maximum-coordinate-difference distance. A large, finite p may cause a ValueError if overflow can occur.
distance_upper_bound: nonnegative float, optionalReturn only neighbors within this distance. This is used to prune tree searches, so if you are doing a series of nearest-neighbor queries, it may help to supply the distance to the nearest neighbor of the most recent point.
workers: int, optionalNumber of workers to use for parallel processing. If -1 is given all CPU threads are used. Default: 1.
Returns
d: float or array of floatsThe distances to the nearest neighbors. If
xhas shapetuple+(self.m,), thendhas shapetuple+(k,). When k == 1, the last dimension of the output is squeezed. Missing neighbors are indicated with infinite distances. Hits are sorted by distance (nearest first).i: integer or array of integersThe index of each neighbor in
self.data.iis the same shape as d. Missing neighbors are indicated withself.n.
Examples
import numpy as np from scipy.spatial import KDTree x, y = np.mgrid[0:5, 2:8] tree = KDTree(np.c_[x.ravel(), y.ravel()])✓
dd, ii = tree.query([[0, 0], [2.2, 2.9]], k=1)
✓print(dd, ii, sep='\n')
✗dd, ii = tree.query([[0, 0], [2.2, 2.9]], k=[1])
✓print(dd, ii, sep='\n')
✗dd, ii = tree.query([[0, 0], [2.2, 2.9]], k=[2])
✓print(dd, ii, sep='\n')
✗dd, ii = tree.query([[0, 0], [2.2, 2.9]], k=2)
✓print(dd, ii, sep='\n')
✗dd, ii = tree.query([[0, 0], [2.2, 2.9]], k=[1, 2])
✓print(dd, ii, sep='\n')
✗Aliases
-
scipy.spatial.KDTree.query