Class: ClassificationKNN

Edge of k-nearest neighbor classifier


E = edge(mdl,X,Y)
E = edge(mdl,X,Y,Name,Value)


E = edge(mdl,X,Y) returns the classification edge for mdl with data X and classification Y.

E = edge(mdl,X,Y,Name,Value) computes the edge with additional options specified by one or more Name,Value pair arguments.

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.

Name-Value Pair Arguments

Specify optional comma-separated pairs of Name,Value arguments. Name is the argument name and Value is the corresponding value. Name must appear inside single quotes (' '). You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.


Observation weights, a numeric vector of length size(X,1). If you supply weights, edge computes weighted classification edge.

Default: ones(size(X,1))

Output Arguments


Classification edge, a scalar that is the mean classification margin (see Margin).



The edge is the mean value of the classification margin.


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

Margin is a column vector with the same number of rows as X.


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

Edge Calculation

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

Load the data.

load fisheriris
X = meas;
Y = species;

Construct a classifier for five-nearest neighbors.

mdl = fitcknn(X,Y,'NumNeighbors',5);

Examine the edge of the classifier for minimum, mean, and maximum observations classified 'setosa', 'versicolor', and 'virginica' respectively.

NewX = [min(X);mean(X);max(X)];
Y = {'setosa';'versicolor';'virginica'};
E = edge(mdl,NewX,Y)
E =


The classifier has no doubt that the Y entries are correct classifications (all five nearest neighbors of each NewX point classify as the corresponding Y entry).

Was this topic helpful?