liuslnlp / libkdtree Goto Github PK
View Code? Open in Web Editor NEWA KD-Tree model implemented by C++(with C and Python interface)
License: MIT License
A KD-Tree model implemented by C++(with C and Python interface)
License: MIT License
printf("K-Nearest: \n");
for (size_t i = 0; i < 5; ++i) {
printf("ID %lu, Dist %.2f\n", args[i], dists[i]);
}
ID 44, Dist 13.04
ID 13, Dist 12.08
ID 8, Dist 11.66
ID 39, Dist 7.00
ID 47, Dist 3.61
the distances is desc , which I think is an error.
std::tuple<size_t, float> FindNearest(const float *coor, size_t k) { return FindKNearests(coor, k)[0]; }
this function also will return the largest distance in k node, not the smallest one.
I believe I have found a bug. For some data-sets find_k_nearests() doesn't seems to return the right result. Using the repo example with different data for instance :
#include "kdtree.h"
#include <stdio.h>
#include <stdlib.h>
int main() {
float datas[108] = { 0, 0, 0,
0.333333, 0, 0,
0.333333, 0.333333, 0,
0, 0.333333, 0,
0.333333, 0, 0,
0.666667, 0, 0,
0.666667, 0.333333, 0,
0.333333, 0.333333, 0,
0.666667, 0, 0,
1, 0, 0,
1, 0.333333, 0,
0.666667, 0.333333, 0,
0, 0.333333, 0,
0.333333, 0.333333, 0,
0.333333, 0.666667, 0,
0, 0.666667, 0,
0.333333, 0.333333, 0,
0.666667, 0.333333, 0,
0.666667, 0.666667, 0,
0.333333, 0.666667, 0,
0.666667, 0.333333, 0,
1, 0.333333, 0,
1, 0.666667, 0,
0.666667, 0.666667, 0,
0, 0.666667, 0,
0.333333, 0.666667, 0,
0.333333, 1, 0,
0, 1, 0,
0.333333, 0.666667, 0,
0.666667, 0.666667, 0,
0.666667, 1, 0,
0.333333, 1, 0,
0.666667, 0.666667, 0,
1, 0.666667, 0,
1, 1, 0,
0.666667, 1, 0};
float labels[100];
for (size_t i = 0; i < 36; ++i)
labels[i] = (float)i;
tree_model *model = build_kdtree(datas, labels, 12, 3, 2);
float test[3] = {0, 0.666667, 0};
size_t args[100] = { 0 };
float dists[100] = {-1};
find_k_nearests(model, test, 1, args, dists); // This only searches for K neighbors of (3,3,3)
printf("K-Nearest: \n");
for (size_t i = 0; i < 5; ++i) {
printf("ID %d, Dist %.2f\n", args[i], dists[i]);
}
// tree_node *root = model->root;
free(ans);
free_tree_memory(model->root);
return 0;
}
outputs:
K-Nearest:
ID 11, Dist 0.333334
ID 0, Dist 0
ID 0, Dist 0
ID 0, Dist 0
ID 0, Dist 0
While it should return:
ID 15, Dist 0
ID 0, Dist 0
ID 0, Dist 0
ID 0, Dist 0
ID 0, Dist 0
or:
ID 24, Dist 0
ID 0, Dist 0
ID 0, Dist 0
ID 0, Dist 0
ID 0, Dist 0
p = coor[p->split] <= GetDimVal(p->id, p->split) ? p = p->left : p = p->right;
应该是这样吧: p = coor[p->split] <= GetDimVal(p->id, p->split) ? p->left : p->right;
虽然那么写执行的结果是一样的,但是多了两次赋值,而且阅读代码时会让人感到困惑。
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.