Index of chosen Nearest Neighbor from Classification Learner kNN Function
5 views (last 30 days)
Show older comments
Michael König
on 20 Apr 2020
Commented: Michael König
on 20 Apr 2020
Hi,
I created a trained kNN Classification model using the Classification Learner App in order to classify new data sets and then exported it as a function and implemented it into my code.
Training of model:
[trainedClassifier, validationAccuracy] = trainClassifier(training_data);
Classification of new data:
new_data = trainedClassifier.predictFcn(new_data);
Is there a way to extract the chosen NNs for each newly classified data point in addition to the resulting classification from the "trainedClassifier" model?
Since I'm using the existing functions of the App I'm curious on wether such a response is already possible or can at least easily be implemented into the automatically generated classification trainer function code.
Best regards, Michael
0 Comments
Accepted Answer
Image Analyst
on 20 Apr 2020
I don't see that (the indexes) as being returned directly. However if you use knnsearch(), it will return the distances:
[Idx,D] = knnsearch(___) additionally returns the matrix D using any of the input arguments in the previous syntaxes. Dcontains the distances between each observation in Y that correspond to the closest observations in Mdl.X. By default, the function arranges the columns of D in ascending order by closeness, with respect to the distance metric.
Then you can use sqrt() to find the distances yourself and use ismembertol() to find the index of the training point that has that minimum distances. So I think it's possible, you just have to do it in a few lines of code rather than having the indexes returned directly from a function.
3 Comments
Image Analyst
on 20 Apr 2020
I think you could alway use sqrt() but the problem is the distances are probably normalized for the range of each variable so a distance of 1 from, say, temperature is not the same as a 1 from, say, weight or pressure. I think the index would be a nice thing to add. I don't think I can help anymore. You might call tech support and ask them and if they can't get it then ask for it to be a feature in a future version.
More Answers (0)
See Also
Categories
Find more on Nearest Neighbors in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!