Difference between scipy.spatial.KDTree and scipy.spatial.cKDTree

Question:

What is the difference between these two algorithms?

Asked By: Benjamin

||

Answers:

cKDTree is a subset of KDTree, implemented in C++ wrapped in Cython, so therefore faster.

Each of them 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.

but KDTree

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.

Answered By: agf

In a use case (5D nearest neighbor look ups in a KDTree with approximately 100K points) cKDTree is around 12x faster than KDTree.

Answered By: Newmu

Currently, both have almost same APIs, and cKDTree is faster than KDTree.
So, In the near future, SciPy developers are planning to remove KDTree, and cKDTree will be renamed to KDTree in a backwards-compatible way.

Ref:
Detailed SciPy Roadmap — SciPy v1.6.0.dev Reference Guide
https://docs.scipy.org/doc/scipy/reference/roadmap-detailed.html#spatial

Answered By: Atsushi Sakai

Update 2022: cKDTree is deprecated

The current (v1.8) SciPy documentation states that scipy.spatial.cKDTree is now deprecated and was replaced by the functionally-identical scipy.spatial.KDTree.

Here is the note:

cKDTree is functionally identical to KDTree. Prior to SciPy v1.6.0, cKDTree had better performance and slightly different functionality but now the two names exist only for backward-compatibility reasons. If compatibility with SciPy < 1.6 is not a concern, prefer KDTree.

Answered By: divenex
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.