Main Content

Nearest Neighbors

k-nearest neighbor classification

To train a k-nearest neighbor model, use the Classification Learner app. For greater flexibility, train a k-nearest neighbor model using fitcknn in the command-line interface. After training, predict labels or estimate posterior probabilities by passing the model and predictor data to predict.


Classification LearnerTrain models to classify data using supervised machine learning


ClassificationKNN PredictClassify observations using nearest neighbor classification model (Since R2022b)


expand all

fitcknnFit k-nearest neighbor classifier
ExhaustiveSearcherCreate exhaustive nearest neighbor searcher
KDTreeSearcherCreate Kd-tree nearest neighbor searcher
hnswSearcherHierarchical Navigable Small Worlds (HNSW) approximate nearest neighbor search (Since R2024a)
creatensCreate nearest neighbor searcher object
limeLocal interpretable model-agnostic explanations (LIME) (Since R2020b)
partialDependenceCompute partial dependence (Since R2020b)
permutationImportancePredictor importance by permutation (Since R2024a)
plotPartialDependenceCreate partial dependence plot (PDP) and individual conditional expectation (ICE) plots
shapleyShapley values (Since R2021a)
crossvalCross-validate machine learning model
kfoldEdgeClassification edge for cross-validated classification model
kfoldLossClassification loss for cross-validated classification model
kfoldfunCross-validate function for classification
kfoldMarginClassification margins for cross-validated classification model
kfoldPredictClassify observations in cross-validated classification model
lossLoss of k-nearest neighbor classifier
resubLossResubstitution classification loss
compareHoldoutCompare accuracies of two classification models using new data
edgeEdge of k-nearest neighbor classifier
marginMargin of k-nearest neighbor classifier
resubEdgeResubstitution classification edge
resubMarginResubstitution classification margin
testckfoldCompare accuracies of two classification models by repeated cross-validation
predictPredict labels using k-nearest neighbor classification model
resubPredictClassify training data using trained classifier
gatherGather properties of Statistics and Machine Learning Toolbox object from GPU (Since R2020b)
pdistPairwise distance between pairs of observations
pdist2Pairwise distance between two sets of observations


expand all

ClassificationKNNk-nearest neighbor classification
ClassificationPartitionedModelCross-validated classification model


Related Information