bundles / scipy 1.17.1 / scipy / spatial / _ckdtree / cKDTree / query
cython_function_or_method
scipy.spatial._ckdtree:cKDTree.query
Summary
Query the kd-tree for nearest neighbors
Parameters
x: array_like, last dimension self.mAn array of points to query.
k: list of integer or integerThe list of k-th nearest neighbors to return. If k is an integer it is treated as a list of [1, ... k] (range(1, k+1)). Note that the counting starts from 1.
eps: non-negative floatReturn approximate nearest neighbors; the k-th returned value is guaranteed to be no further than (1+eps) times the distance to the real k-th nearest neighbor.
p: float, 1<=p<=infinityWhich Minkowski p-norm to use. 1 is the sum-of-absolute-values "Manhattan" distance 2 is the usual Euclidean distance infinity is the maximum-coordinate-difference distance A finite large p may cause a ValueError if overflow can occur.
distance_upper_bound: nonnegative floatReturn 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: 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.i: ndarray of intsThe index of each neighbor in
self.data. Ifxhas shapetuple+(self.m,), thenihas shapetuple+(k,). When k == 1, the last dimension of the output is squeezed. Missing neighbors are indicated withself.n.
Notes
If the KD-Tree is periodic, the position x is wrapped into the box.
When the input k is a list, a query for arange(max(k)) is performed, but only columns that store the requested values of k are preserved. This is implemented in a manner that reduces memory usage.
Examples
import numpy as np from scipy.spatial import cKDTree x, y = np.mgrid[0:5, 2:8] tree = cKDTree(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.cKDTree.query