k-Nearest Neighbors (kNN) Classifier
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
Platform Compatibility
Windows macOS LinuxCategories
Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Discover Live Editor
Create scripts with code, output, and formatted text in a single executable document.
Versions that use the GitHub default branch cannot be downloaded
Version | Published | Release Notes | |
---|---|---|---|
2.2.1 | See release notes for this release on GitHub: https://github.com/ferreirad08/kNNeighborsClassifier/releases/tag/2.2.1 |
||
2.2.0 | See release notes for this release on GitHub: https://github.com/ferreirad08/k-Nearest-Neighbors-kNN-Algorithm/releases/tag/2.2.0 |
||
2.1.9 | See release notes for this release on GitHub: https://github.com/ferreirad08/k-Nearest-Neighbors-kNN-Algorithm/releases/tag/2.1.9 |
||
2.1.8 | Find function added |
|
|
2.1.7 | Description correction |
|
|
2.1.6 | Description correction |
|
|
2.1.5 | Description correction |
|
|
2.1.4 | Description correction |
|
|
2.1.3 | 3-D chart added and cell type label support. |
|
|
2.1.2 | The plotting 3-D function was added in the main algorithm. |
|
|
2.1.1 | More markers were added to the chart. |
|
|
2.1.0 | The plotting function was added in the main algorithm. |
|
|
2.0.9 | Function Pack |
|
|
2.0.8 | Title correction |
|
|
2.0.7 | Title correction |
|
|
2.0.6 | Added to github. |
|
|
2.0.5 | New tools have been added, bringing a more complete algorithm. |
||
2.0.4 | Simplification in k nearest instances and k nearest labels |
||
2.0.3 | Basic update |
||
2.0.2 | Update in maximum frequency |
||
2.0.1 | Simplification in frequency verification |
||
2.0.0 | Check the frequencies using 'histcounts' |
||
1.62 | Title correction |
||
1.61 | Correction of the k nearest labels. |
||
1.60 | Returns the k nearest labels. |
||
1.54 | Basic update |
||
1.53 | Updating the Euclidean distance between two points |
||
1.52 | Description correction |
||
1.51 | Description correction |
||
1.5 | Elements sorted by "sort" function and reduced processing time by approximately 1/3. |
||
1.1.0.0 | Update of the calculation of the Euclidean distance between two points |
||
1.0.0.0 | Description correction
|