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 Learner | Train models to classify data using supervised machine learning |

`fitcknn` |
Fit k-nearest neighbor classifier |

`predict` |
Predict labels using k-nearest neighbor classification model |

`templateKNN` |
k-nearest neighbor classifier template |

`ExhaustiveSearcher` |
Prepare exhaustive nearest neighbors searcher |

`KDTreeSearcher` |
Grow Kd-tree |

`createns` |
Create object to use in k-nearest neighbors search |

`ClassificationKNN` |
k-nearest neighbor classification |

`ClassificationPartitionedModel` |
Cross-validated classification model |

`ExhaustiveSearcher` |
Exhaustive nearest neighbors searcher |

`KDTreeSearcher` |
Nearest neighbor search using Kd-tree |

**Train Nearest Neighbor Classifiers Using Classification Learner
App**

Learn how to train nearest neighbor classifiers.

While there are many Statistics and Machine Learning Toolbox™ algorithms for supervised learning, most use the same basic workflow for obtaining a predictor model.

**k-Nearest Neighbor Search and Radius Search**

Given a set *X* of *n* points
and a distance function, *k*-nearest neighbor (*k*NN)
search lets you find the *k* closest points in *X* to
a query point or set of points `Y`

. For a positive
real value *r*, radius search finds all points in *X* that
are within a distance *r* of each point in `Y`

.

Construct a *k*-nearest neighbor
classifier for the Fisher iris data.

**Examine Quality of KNN Classifier**

Examine the quality of a *k*-nearest
neighbor classifier using resubstitution and cross-validation.

**Predict Classification Using KNN Classifier**

Predict classification for a *k*-nearest
neighbor classifier.

Modify a *k*-nearest neighbor classifier.

**Characteristics of Classification Algorithms**

Classification algorithms vary in speed, memory usage, interpretability, and flexibility.

Categorizing query points based on their distance to points in a training data set can be a simple yet effective way of classifying new points.

**k-Nearest Neighbor Search and Radius Search**

Given a set *X* of *n* points
and a distance function, *k*-nearest neighbor (*k*NN)
search lets you find the *k* closest points in *X* to
a query point or set of points `Y`

. For a positive
real value *r*, radius search finds all points in *X* that
are within a distance *r* of each point in `Y`

.

**K-Nearest Neighbor Classification for
Supervised Learning**

The `ClassificationKNN`

classification
model lets you:

Was this topic helpful?