Class: ClassificationKNN

Margin of k-nearest neighbor classifier


m = margin(mdl,X,Y)


m = margin(mdl,X,Y) returns the classification margins for the matrix of predictors X and class labels Y. For the definition, see Margin.

Input Arguments

collapse all

mdl — Classifier modelclassifier model object

k-nearest neighbor classifier model, returned as a classifier model object.

Note that using the 'CrossVal', 'KFold', 'Holdout', 'Leaveout', or 'CVPartition' options results in a model of class ClassificationPartitionedModel. You cannot use a partitioned tree for prediction, so this kind of tree does not have a predict method.

Otherwise, mdl is of class ClassificationKNN, and you can use the predict method to make predictions.

X — Matrix of predictor valuesmatrix

Matrix of predictor values. Each column of X represents one variable, and each row represents one observation.

Y — Categorical variablescategorical array | cell array of strings | character array | logical vector | numeric vector

A categorical array, cell array of strings, character array, logical vector, or a numeric vector with the same number of rows as X. Each row of Y represents the classification of the corresponding row of X.

Output Arguments


Numeric column vector of length size(X,1). Each entry in m represents the margin for the corresponding rows of X and (true class) Y, computed using mdl.



The classification margin is the difference between the classification score for the true class and maximal classification score for the false classes.


The score of a classification is the posterior probability of the classification. The posterior probability is the number of neighbors that have that classification, divided by the number of neighbors. For a more detailed definition that includes weights and prior probabilities, see Posterior Probability.


collapse all

Margin Calculation

Construct a k-nearest neighbor classifier for the Fisher iris data, where k = 5.

Load the data.

load fisheriris

Construct a classifier for 5-nearest neighbors.

mdl = fitcknn(meas,species,'NumNeighbors',5);

Examine the margin of the classifier for a mean observation classified 'versicolor'.

X = mean(meas);
Y = {'versicolor'};
m = margin(mdl,X,Y)
m =


The classifier has no doubt that 'versicolor' is the correct classification (all five nearest neighbors classify as 'versicolor').

Was this topic helpful?