fitcknn
Fit a k-nearest neighbor classifier.
📝Syntax
mdl = fitcknn(X, Y)
mdl = fitcknn(X, Y, Name, Value)
label = predict(mdl, Xnew)
[label, score, cost] = predict(mdl, Xnew)
📄Description

fitcknn creates a ClassificationKNN object from numeric predictors X and class labels Y.

Name-value arguments include NumNeighbors, Distance, DistanceWeight, Standardize, P, Scale, and ClassNames. Prediction uses native nearest-neighbor search and returns class scores normalized across classes.

💡Examples
X = [0 0; 0 1; 1 0; 5 5; 5 6; 6 5];
Y = [1; 1; 1; 2; 2; 2];
mdl = fitcknn(X, Y, 'NumNeighbors', 3);
[label, score] = predict(mdl, [0.2 0.1; 5.2 5.1])
🔗See Also
knnsearchfitgmdistgrp2idx
🕔Version History
Version Description
2.0.0 initial version
Edit this page on GitHub