image thumbnail

k-Nearest Neighbors (kNN) Classifier

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

1.5K Downloads

Updated 28 Oct 2020

From GitHub

View license on GitHub

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

Description
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 = mdl.fit(X,Y);
Ypred = mdl.predict(Xnew)

Ypred =

'setosa'
'versicolor'
'virginica'


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

accuracy =

0.6667

See examples in the script files.

Cite As

David Ferreira (2019). k-Nearest Neighbors (kNN) (https://www.mathworks.com/matlabcentral/fileexchange/67018-k-nearest-neighbors-knn), MathWorks. Retrieved January 23, 2019.

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!
To view or report issues in this GitHub add-on, visit the GitHub Repository.
To view or report issues in this GitHub add-on, visit the GitHub Repository.