kNN classifier

k-NN classifier
Updated 4 Jan 2019

View License

-k-NN classifier: classifying using k-nearest neighbors algorithm. The nearest neighbors
-search method is euclidean distance
[predicted_labels,nn_index,accuracy] = KNN_(3,training,training_labels,testing,testing_labels)
predicted_labels = KNN_(3,training,training_labels,testing)
- k: number of nearest neighbors
- data: (NxD) training data; N is the number of samples and D is the
dimensionality of each data point
- labels: training labels
- t_data: (MxD) testing data; M is the number of datapoints and D
is the dimensionality of each data point
- t_labels: testing labels (default = [])
- predicted_labels: the predicted labels based on the k-NN
- nn_index: the indices of the nearest training data point (Mx1).
- accuracy: if the testing labels are supported, the accuracy of
the classification is returned, otherwise it will be zero.

Cite As

Mahmoud Afifi (2024). kNN classifier (, MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2015a
Compatible with any release
Platform Compatibility
Windows macOS Linux
Find more on Spatial Search in Help Center and MATLAB Answers

Community Treasure Hunt

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

Start Hunting!


Version Published Release Notes



Bug fixed