label = predict(mdl,TBLnew)
label = predict(mdl,Xnew)
[label,score] = predict(___)
[label,score,cost] = predict(___)
mdl— Classifier modelclassifier model object
k-nearest neighbor classifier model, returned as a classifier model object.
Note that using the
'CVPartition' options results in a model of
You cannot use a partitioned tree for prediction, so this kind of
tree does not have a
Prediction data, specified as a table. Each row of
to one observation, and each column corresponds to one predictor variable.
contain all the predictors used to train the model
If you trained
mdl using sample data contained
table, then the input data for this method
must also be in a table.
Xnew— Prediction pointsmatrix
Points at which
mdl predicts classifications.
Each row of
Xnew is one point. The number of columns
Xnew must equal the number of predictors in
If you specified to standardize the predictor data, that is,
not empty (
label— Predicted class labelscategorical array | character array | logical vector | vector of numeric values | cell array of strings
Predicted class labels for the points in
returned as a vector with length equal to the number of rows of
The label is the class with minimal expected cost. See Predicted Class Label.
score— Predicted class scores or posterior probabilitiesnumeric column vector | numeric matrix
Predicted class scores or posterior probabilities, returned
as a numeric matrix of size
the number of observations (rows) in
K is the number of classes (in
the posterior probability that row
j. See Posterior Probability.
predict classifies so as to minimize the expected
is the predicted classification.
K is the number of classes.
is the posterior probability of class k for observation x.
is the cost of classifying an observation as y when its true class is k.
For a vector (single query point)
K be the number of nearest neighbors
used in prediction,
nbd(mdl,Xnew) be the
Y(nbd) be the classifications of
the points in
W(nbd) be the weights of the points
prior be the priors of the classes
If there is a vector of prior probabilities, then the observation
W are normalized by class to sum to the
priors. This might involve a calculation for the point
because weights can depend on the distance from
the points in
The posterior probability p(j|
mdl.Y(i) = j, and
There are two costs associated with KNN classification: the true misclassification cost per class, and the expected misclassification cost per observation.
You can set the true misclassification cost per class in the
pair when you run
the cost of classifying an observation into class
its true class is
i. By default,
i=j. In other
words, the cost is
0 for correct classification,
1 for incorrect classification.
There are two costs associated with KNN classification: the
true misclassification cost per class, and the expected misclassification
cost per observation. The third output of
the expected misclassification cost per observation.
Suppose you have
Nobs observations that you
want to classify with a trained classifier
Suppose you have
K classes. You place the observations
into a matrix
Xnew with one observation per row.
[label,score,cost] = predict(mdl,Xnew)
returns, among other outputs, a
K. Each row
cost matrix contains the expected (average)
cost of classifying the observation into each of the
Construct a k-nearest neighbor classifier for Fisher's iris data, where k = 5. Evaluate some model predictions on new data.
Load the data.
load fisheriris X = meas; Y = species;
Construct a classifier for 5-nearest neighbors. It is good practice to standardize non-categorical predictor data.
mdl = fitcknn(X,Y,'NumNeighbors',5,'Standardize',1);
Predict the classifications for flowers with minimum, mean, and maximum characteristics.
Xnew = [min(X);mean(X);max(X)]; [label,score,cost] = predict(mdl,Xnew)
label = 'versicolor' 'versicolor' 'virginica' score = 0.4000 0.6000 0 0 1.0000 0 0 0 1.0000 cost = 0.6000 0.4000 1.0000 1.0000 0 1.0000 1.0000 1.0000 0
The classifications have binary values for the score and cost matrices, meaning all five nearest neighbors of each of the three points have identical classifications.