Documentation

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.

edge

Class: ClassificationKNN

Edge of k-nearest neighbor classifier

Syntax

  • E = edge(mdl,tbl,ResponseVarName)
  • E = edge(mdl,tbl,Y)
  • E = edge(mdl,X,Y)
  • E = edge(___,Name,Value)

Description

E = edge(mdl,tbl,ResponseVarName) returns the classification edge for mdl with data tbl and classification tbl.ResponseVarName.

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

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

E = edge(___,Name,Value) computes the edge with additional options specified by one or more Name,Value pair arguments, using any of the previous syntaxes.

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.

Sample data used to train the model, specified as a table. Each row of tbl corresponds to one observation, and each column corresponds to one predictor variable. Optionally, tbl can contain one additional column for the response variable. Multi-column variables and cell arrays other than cell arrays of character vectors are not allowed.

If tbl contains the response variable used to train mdl, then you do not need to specify ResponseVarName or Y.

If you trained mdl using sample data contained in a table, then the input data for this method must also be in a table.

Data Types: table

Response variable name, specified as the name of a variable in tbl. If tbl contains the response variable used to train mdl, then you do not need to specify ResponseVarName.

If you specify ResponseVarName, then you must do so as a character vector. For example, if the response variable is stored as tbl.response, then specify it as 'response'. Otherwise, the software treats all columns of tbl, including tbl.response, as predictors.

The response variable must be a categorical or character array, logical or numeric vector, or cell array of character vectors. If the response variable is a character array, then each element must correspond to one row of the array.

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

A categorical array, cell array of character vectors, 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.

expand all

Observation weights, specified as the comma-separated pair consisting of 'Weights' and a numeric vector or the name of a variable in TBL.

If you specify Weights as a numeric vector, then the size of Weights must be equal to the number of rows in X or tbl.

If you specify Weights as the name of a variable in tbl, you must do so as a character vector. For example, if the weights are stored as tbl.w, then specify it as 'w'. Otherwise, the software treats all columns of tbl, including tbl.w, as predictors.

If you specify Weights, edge computes weighted classification edge. The software weights the observations in each row of X or tbl with the corresponding weight in Weights.

Output Arguments

E

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

Definitions

Edge

The edge is the mean value of the classification margin.

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.

Score

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.

Examples

expand all

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 =

     1

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?