Why kNNsearch and kNNclassify don't give the same result??
1 次查看(过去 30 天)
显示 更早的评论
I'm using knnclassify to do a kNN classification in Matlab and it's working well. Now, I need to know the distances to the neighbors and it seems that knnsearch funcion gives me that.
The problem is the results are not the same. I am quite sure knnsearch is not working properly, but I don't know the reason.
This is my code:
k=1;
distance='euclidean';
rule='nearest';
%kNN classification
result = knnclassify(sample_matrix, training_matrix, label_matrix,k,distance,rule);
%showing which element is recognized and the distance to it
[recognition,distances] = knnsearch(training_matrix, sample_matrix,'k',k);
So, result and recognition should be the same, and then I can see the distances.
This is result:
4 1 1 2 4 1 1 4 1 1 2 2 2 2 2 2 2 2 2 2 3 3 3 2 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5 5 5 6 6 6 6 6 6 6 6 6 7 7 7 7 7 7 7 7 7 7 8 8 8 8 8 8 8 8 8 8 9 9 9 9 9 9 9 9 9 9
And this is recognition:
3 1 1 2 3 1 1 3 1 1 2 2 1 2 2 2 1 2 2 2 2 2 2 2 2 2 2 3 2 2 4 3 3 4 3 4 4 3 3 3 4 4 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5 5 5 6 6 6 5 5 6 6 6 7 6 6 6 6 7 6 6 6 7 8 7 7 7 7 7 7 7 7 7
(They're two vectors). The desired result is
1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5 5 6 6 6 6 6 6 6 6 6 6 7 7 7 7 7 7 7 7 7 7 8 8 8 8 8 8 8 8 8 8 9 9 9 9 9 9 9 9 9 9
So, result is almost well (good enough) and recognition is a disaster. As you can see the beginning is much better than the end.
I don't understand because a simple example does work:
sample = [.9 .8;.1 .3;.2 .6]; training=[0 0;.5 .5;1 1]; group = [1;2;3]; foo_classify = knnclassify(sample, training, group); [foo_search,dist] = knnsearch(training, sample); The results are: foo_search = 3 1 2; foo_classify = 3 1 2; dist = 0.2236 0.3162 0.3162;
Anybody can help me?? Thank you very much.
0 个评论
回答(1 个)
Tom Lane
2015-2-9
As I understand this, knnclassify returns a classification so result is a value from label_matrix.
knnsearch just searches for nearest neighbors with no classification involved, so recognition would be a row number to index into training_matrix.
If label_matrix is just a row number, then maybe that's okay. In that case I'd check to see if the nearest neighbor is a tie, and the two functions are just choosing two different equidistant neighbors.
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!