### Navigation

- index
- modules |
- modules |
- previous |
- SciPy v0.9 Reference Guide (DRAFT) »
- Spatial algorithms and data structures (`scipy.spatial`) »

# scipy.spatial.KDTree

class `scipy.spatial.``KDTree`(data, leafsize=10)

  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 tree, 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.

  The tree also supports all-neighbors queries, both with arrays of points and with other kd-trees. These do use a reasonably efficient algorithm, but the kd-tree is not necessarily the best data structure for this sort of calculation.

  Methods

  `count_neighbors` |
  `innernode` |
  `leafnode` |
  `node` |
  `query` |
  `query_ball_point` |
  `query_ball_tree` |
  `query_pairs` |
  `sparse_distance_matrix` |

### Navigation

- index
- modules |
- modules |
- previous |
- SciPy v0.9 Reference Guide (DRAFT) »
- Spatial algorithms and data structures (`scipy.spatial`) »