This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

Nearest Neighbors

k nearest neighbors classification using Kd-tree search

To train a k-nearest neighbors model, use the Classification Learner app. For greater flexibility, train a k-nearest neighbors 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


fitcknnFit k-nearest neighbor classifier
ExhaustiveSearcherPrepare exhaustive nearest neighbor searcher
KDTreeSearcherGrow Kd-tree
creatensCreate object to use in k-nearest neighbor search
crossvalCross-validated k-nearest neighbor classifier
kfoldEdgeClassification edge for observations not used for training
kfoldLossClassification loss for observations not used for training
kfoldfunCross validate function
kfoldMarginClassification margins for observations not used for training
kfoldPredictPredict response for observations not used for training
lossLoss of k-nearest neighbor classifier
resubLossLoss of k-nearest neighbor classifier by resubstitution
compareHoldoutCompare accuracies of two models using new data
edgeEdge of k-nearest neighbor classifier
marginMargin of k-nearest neighbor classifier
resubEdgeEdge of k-nearest neighbor classifier by resubstitution
resubMarginMargin of k-nearest neighbor classifier by resubstitution
predictPredict labels using k-nearest neighbor classification model
resubPredictPredict resubstitution response of k-nearest neighbor classifier
pdistPairwise distance between pairs of observations
pdist2Pairwise distance between two sets of observations

Using Objects

ClassificationKNNk-nearest neighbor classification
ClassificationPartitionedModelCross-validated classification model
ExhaustiveSearcherExhaustive nearest neighbor searcher
KDTreeSearcherNearest neighbor search using Kd-tree


Train Nearest Neighbor Classifiers Using Classification Learner App

Create and compare nearest neighbor classifiers, and export trained models to make predictions for new data.

Visualize Decision Surfaces of Different Classifiers

This example shows how to visualize the decision surface for different classification algorithms.

Supervised Learning Workflow and Algorithms

Understand the steps for supervised learning and the characteristics of nonparametric classification and regression functions.

Classification Using Nearest Neighbors

Categorize data points based on their distance to points in a training data set, using a variety of distance metrics.

Was this topic helpful?