scipy.spatial.cKDTree¶
- class scipy.spatial.cKDTree¶
kd-tree for quick nearest-neighbor lookup
This class provides an index into a set of k-dimensional points which can be used to rapidly look up the nearest neighbors of any point.
The algorithm used is described in Maneewongvatana and Mount 1999. The general idea is that the kd-tree is a binary trie, each of whose nodes represents an axis-aligned hyperrectangle. Each node specifies an axis and splits the set of points based on whether their coordinate along that axis is greater than or less than a particular value.
During construction, the axis and splitting point are chosen by the “sliding midpoint” rule, which ensures that the cells do not all become long and thin.
The tree can be queried for the r closest neighbors of any given point (optionally returning only those within some maximum distance of the point). It can also be queried, with a substantial gain in efficiency, for the r approximate closest neighbors.
For large dimensions (20 is already large) do not expect this to run significantly faster than brute force. High-dimensional nearest-neighbor queries are a substantial open problem in computer science.
Parameters : data : array-like, shape (n,m)
The n data points of dimension mto be indexed. This array is not copied unless this is necessary to produce a contiguous array of doubles, and so modifying this data will result in bogus results.
leafsize : positive integer
The number of points at which the algorithm switches over to brute-force.
Attributes
data leafsize m maxes mins n Methods
count_neighbors(self, other, r, p) Count how many nearby pairs can be formed. query(self, x[, k, eps, p, distance_upper_bound]) Query the kd-tree for nearest neighbors query_ball_point(self, x, r, p, eps) Find all points within distance r of point(s) x. query_ball_tree(self, other, r, p, eps) Find all pairs of points whose distance is at most r query_pairs(self, r, p, eps) Find all pairs of points whose distance is at most r. sparse_distance_matrix(self, max_distance, p) Compute a sparse distance matrix