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.


Class: ClassificationKNN

Margin of k-nearest neighbor classifier by resubstitution


m = resubMargin(mdl)


m = resubMargin(mdl) returns the classification margins of the data used to train mdl. For the definition, see Margin.

Input Arguments

expand all

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.

Output Arguments


A numeric column vector of length size(mdl.X,1), where mdl.X is the training data for mdl. Each entry in m represents the margin for the corresponding row of mdl.X and (true class) mdl.Y.


expand all

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

Load the data.

load fisheriris
X = meas;
Y = species;

Construct a classifier for 5-nearest neighbors.

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

Examine some statistics of the resubstitution margin of the classifier.

m = resubMargin(mdl);
[max(m) min(m) mean(m)]
ans =

    1.0000   -0.6000    0.9253

The mean margin is over 0.9, indicating fairly high classification accuracy for resubstitution. For more reliable assessment of model accuracy, consider cross validation, such as kfoldLoss.


expand all

Was this topic helpful?