File Exchange

image thumbnail

k-Nearest Neighbors (kNN) Classifier

version 2.2.1 (35.8 KB) by David Ferreira
Multi-class Classification with the kNN


Updated 28 Oct 2020

From GitHub

View Version History

View license on GitHub

1. kNNeighbors.predict(_)
2. kNNeighbors.find(_)

1. Returns the estimated labels of one or multiple test instances.
2. Returns the indices and the respective distances of the k nearest training instances.

Examples using Iris Data Set

load fisheriris

X = meas;
Y = species;
Xnew = [min(X);mean(X);max(X)];
k = 5;
metric = 'euclidean';

mdl = kNNeighbors(k,metric);
mdl =,Y);
Ypred = mdl.predict(Xnew)

Ypred =


Ynew = {'versicolor';'versicolor';'virginica'};
accuracy = accuracy_score(Ypred,Ynew)

accuracy =


See examples in the script files.

Cite As

David Ferreira (2019). k-Nearest Neighbors (kNN) (, MathWorks. Retrieved January 23, 2019.

Comments and Ratings (3)

gisoo fathi


khawar iqbal

Carla Coelho

MATLAB Release Compatibility
Created with R2016a
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

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

Start Hunting!