Clear Filters
Clear Filters

K nearest neighbour predict() and knnsearch() not giving same result

1 view (last 30 days)
Hi experts,
I have a ClassificationKNN object called KNNMdl which I would like to use to predict new data from my table called test_data. When I make the prediction I would also like to see the nearest neighbours used to make the prediction. However, the results of the predict and knnsearch functions yield different results
predict_row = 3176;
predicted = predict(KNNMdl, test_data{predict_row, :})
% Yields : '706'
However, when I use the knnsearch function, the highest number of closest neighbours are not from the '706' class, but from a different class:
knn_search = knnsearch(table2array(KNNMdl.X), test_data{predict_row, :},'K',20);
%knn_search returns indicies of nearest k datapoints. From this, get class labels:
nearest_classes = KNNMdl.Y(knn_search);
The nearest_classes variable shows that from the 20 closest neighbours, only 4 are in the '706' class, and the remaining 16 are in the '999' class.
What am I doing wrong? Or have I misunderstood the functionality of the knnsearch function?

Answers (0)

Categories

Find more on Statistics and Machine Learning Toolbox in Help Center and File Exchange

Products


Release

R2018b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!