KNN returns unexpected class (Bug #4217)
Description
Let's assume the following code:
KNearest knn; Mat samples = (Mat_<float>(4,1) << 1, 1.5, 2, 2.5); Mat responses = (Mat_<int>(4,1) << 4, 3, 2, 1); knn.train(samples, responses); Mat test_sample = (Mat_<float>(1,1) << 1.1); cv::Mat results, neighbours, dists; knn.find_nearest(test_sample, 4, results, neighbours, dists); cout << results << " - " << neighbours << ", " << dists << endl;
As the result is displayed class "1". Which is, in fact, the farthest one. I believe, the "4" should be returned instead.
Associated revisions
Merge pull request #4217 from wangyan42164:ocl_pyrlk_mul
History
Updated by Nisarg Thakkar about 10 years ago
I think this is working according to the algorithm. The output is 1 because on considering k=4, we get, 1 member from each class. And so, we can choose any class as the result for that particular node. We could return the class of the nearest neighbor if there multiple classes have the same count, but that would just lead to extra computation and it is unlikely that such a situation would occur if the data set is large and distributed.
Updated by Maksim Shabunin over 9 years ago
Issue has been transferred to GitHub: https://github.com/Itseez/opencv/issues/4942