Index of chosen Nearest Neighbor from Classification Learner kNN Function

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

 Accepted Answer

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

Thanks, I know that function already but was hoping it would have been already implemented into the generated code of the App. Since I'm creating a self-optimized k-NN Classifier with the generated trainClassifier function the used distance metric, weight and count of chosen NN is not always the same and can change depending on the semi-randomly optimizing Classifier function or with changed training data.
Do you by chance know if there's a way to get the information on the used distance metric etc. from the created trained classifier model?
If not I'll have to standardize onto one set of parameters without the optimization process I guess...
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.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!