-
-
Notifications
You must be signed in to change notification settings - Fork 25.8k
Fix Kd-tree time complexity problem. #11103
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
82eb949
Use std::nth_element from C++ standard library.
jiefangxuanyan 076069a
Add a benchmark to expose time complexity degeneration problem of the…
jiefangxuanyan 3c34c7c
Added a general case for binary tree partition algorithm benchmark.
jiefangxuanyan 5c1f40e
Merge branch 'master' into cpp-nth-element
jiefangxuanyan cd112a8
Multiple changes on the nth_element extension:
jiefangxuanyan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
from sklearn.neighbors import KDTree | ||
import numpy as np | ||
from time import time | ||
|
||
|
||
def main(): | ||
test_cases = [ | ||
("random", np.random.rand(50000)), | ||
("ordered", np.arange(50000, dtype=float)), | ||
("reverse ordered", np.arange(50000, 0, -1, dtype=float)), | ||
("duplicated", np.zeros([50000], dtype=float)) | ||
] | ||
for name, case in test_cases: | ||
expanded_case = np.expand_dims(case, -1) | ||
begin = time() | ||
tree = KDTree(expanded_case, leaf_size=1) | ||
end = time() | ||
del tree | ||
print("{name}: {time}s".format(name=name, time=end - begin)) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would just write the benchmark script directly without defining the |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from ._typedefs cimport DTYPE_t, ITYPE_t | ||
|
||
cdef int partition_node_indices( | ||
DTYPE_t *data, | ||
ITYPE_t *node_indices, | ||
ITYPE_t split_dim, | ||
ITYPE_t split_index, | ||
ITYPE_t n_features, | ||
ITYPE_t n_points) except -1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
cdef extern from "_nth_element_inner.h": | ||
void partition_node_indices_inner[D, I]( | ||
D *data, | ||
I *node_indices, | ||
I split_dim, | ||
I split_index, | ||
I n_features, | ||
I n_points) except + | ||
|
||
|
||
cdef int partition_node_indices( | ||
DTYPE_t *data, | ||
ITYPE_t *node_indices, | ||
ITYPE_t split_dim, | ||
ITYPE_t split_index, | ||
ITYPE_t n_features, | ||
ITYPE_t n_points) except -1: | ||
partition_node_indices_inner( | ||
data, | ||
node_indices, | ||
split_dim, | ||
split_index, | ||
n_features, | ||
n_points) | ||
return 0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#include <algorithm> | ||
|
||
template<class D, class I> | ||
class IndexComparator { | ||
private: | ||
const D *data; | ||
I split_dim, n_features; | ||
public: | ||
IndexComparator(const D *data, const I &split_dim, const I &n_features): | ||
data(data), split_dim(split_dim), n_features(n_features) {} | ||
|
||
bool operator()(const I &a, const I &b) const { | ||
D a_value = data[a * n_features + split_dim]; | ||
D b_value = data[b * n_features + split_dim]; | ||
return a_value == b_value ? a < b : a_value < b_value; | ||
} | ||
}; | ||
|
||
template<class D, class I> | ||
void partition_node_indices_inner( | ||
const D *data, | ||
I *node_indices, | ||
const I &split_dim, | ||
const I &split_index, | ||
const I &n_features, | ||
const I &n_points) { | ||
IndexComparator<D, I> index_comparator(data, split_dim, n_features); | ||
std::nth_element( | ||
node_indices, | ||
node_indices + split_index, | ||
node_indices + n_points, | ||
index_comparator); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
garbage collector will take care of this, so, I think, we can remove it.