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

Predict resubstitution response of k-nearest neighbor classifier


label = resubPredict(mdl)
[label,score] = resubPredict(mdl)
[label,score,cost] = resubPredict(mdl)


label = resubPredict(mdl) returns the labels mdl predicts for the data mdl.X. label is the predictions of mdl on the data that fitcknn used to create mdl.

[label,score] = resubPredict(mdl) returns the posterior class probabilities for the predictions.

[label,score,cost] = resubPredict(mdl) returns the misclassification costs.

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


Predicted class labels for the points in the training data X, a vector with length equal to the number of rows in the training data X. The label is the class with minimal expected cost (see Expected Cost).


Numeric matrix of size N-by-K, where N is the number of observations (rows) in the training data X, and K is the number of classes (in mdl.ClassNames). score(i,j) is the posterior probability that row i of X is of class j. See Posterior Probability.


Matrix of expected costs of size N-by-K, where N is the number of observations (rows) in the training data X, and K is the number of classes (in mdl.ClassNames). cost(i,j) is the cost of classifying row i of X as class j. See Expected Cost.


expand all

Examine the quality of a classifier by its resubstitution predictions.

Load the data.

load fisheriris
X = meas;
Y = species;

Construct a classifier for 5-nearest neighbors.

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

Generate the resubstitution predictions.

label = resubPredict(mdl);

Calculate the number of differences between the predictions label and the original data Y.

mydiff = not(strcmp(Y,label)); % mydiff(i) = 1 means they differ
sum(mydiff) % Number of differences
ans =


A values of 1 in mydiff indicates that the observed label differs from the corresponding predicted label. There are 5 misclassifications.


expand all


If you specified to standardize the predictor data, that is, mdl.Mu and mdl.Sigma are not empty ([]), then resubPredict standardizes the predictor data before predicting labels.

Was this topic helpful?