knearest neighbor classification
A nearestneighbor classification object, where both distance
metric ("nearest") and number of neighbors can be altered.
The object classifies new observations using the predict
method.
The object contains the data used for training, so can compute resubstitution
predictions.
creates
a knearest neighbor classification model.mdl
=
fitcknn(X
,y
)
creates
a classifier with additional options specified by one or more mdl
= fitcknn(X
,y
,Name,Value
)Name,Value
pair
arguments. For details, see fitcknn
.

String specifying the method
Change  

Specification of which predictors are categorical.
 

List of elements in the training data Change  

Square matrix, where Change a  

String or function handle specifying the distance metric. The
allowable strings depend on the
For definitions, see Distance Metrics. The distance metrics of
Change If  

String or function handle specifying the distance weighting function.
Change  

Additional parameter for the distance metric.
For values of the distance metric other than those in the table, You can alter  

Logical value indicating whether Change  

Parameters used in training  

Numeric vector of predictor means with length If you did not standardize  

Positive integer specifying the number of nearest neighbors
in  

Number of observations used in training  

Cell array of names for the predictor variables, in the order
in which they appear in the training data  

Numeric vector of prior probabilities for each class. The order
of the elements of Add or change a  

String describing the response variable  

Numeric vector of predictor standard deviations with length If you did not standardize  

Numeric vector of nonnegative weights with the same number of
rows as  

Numeric matrix of unstandardized predictor values. Each column
of  

A numeric vector, vector of categorical variables, logical vector,
character array, or cell array of strings, with the same number of
rows as

compareHoldout  Compare accuracies of two models using new data 
crossval  Crossvalidated knearest neighbor classifier 
edge  Edge of knearest neighbor classifier 
loss  Loss of knearest neighbor classifier 
margin  Margin of knearest neighbor classifier 
predict  Predict knearest neighbor classification 
resubEdge  Edge of knearest neighbor classifier by resubstitution 
resubLoss  Loss of knearest neighbor classifier by resubstitution 
resubMargin  Margin of knearest neighbor classifier by resubstitution 
resubPredict  Predict resubstitution response of knearest neighbor classifier 
ClassificationKNN
predicts the classification
of a point Xnew
using a procedure equivalent to
this:
Find the NumNeighbors
points in
the training set X
that are nearest to Xnew
.
Find the NumNeighbors
response
values Y
to those nearest points.
Assign the classification label Ynew
that
has smallest expected misclassification cost among the values in Y
.
For details, see Posterior Probability and Expected Cost in the predict
documentation.
Value. To learn how value classes affect copy operations, see Copying Objects in the MATLAB^{®} documentation.
knnsearch
finds the knearest
neighbors of points. rangesearch
finds
all the points within a fixed distance. You can use these functions
for classification, as shown in Classify Query Data. If you want
to perform classification, ClassificationKNN
can
be more convenient, in that you can construct a classifier in one
step and classify in other steps. Also, ClassificationKNN
has
crossvalidation options.