# edge

Find classification edge for support vector machine (SVM) classifier

## Syntax

## Description

returns the classification edge
(`e`

= edge(`SVMModel`

,`Tbl`

,`ResponseVarName`

)`e`

) for the support vector machine (SVM) classifier
`SVMModel`

using the predictor data in table
`Tbl`

and the class labels in
`Tbl.ResponseVarName`

.

The classification edge (`e`

) is a scalar value that
represents the weighted mean of the classification
margins.

returns the classification edge
(`e`

= edge(`SVMModel`

,`Tbl`

,`Y`

)`e`

) for the SVM classifier `SVMModel`

using the predictor data in table `Tbl`

and the class labels
in `Y`

.

computes the classification edge for the observation weights supplied in
`e`

= edge(___,`'Weights'`

,`weights`

)`weights`

using any of the input arguments in the
previous syntaxes.

**Note**

If the predictor data `X`

or the predictor variables in
`Tbl`

contain any missing values, the
`edge`

function can return NaN. For more
details, see edge can return NaN for predictor data with missing values.

## Examples

### Estimate Test Sample Edge of SVM Classifiers

Load the `ionosphere`

data set.

load ionosphere rng(1); % For reproducibility

Train an SVM classifier. Specify a 15% holdout sample for testing, standardize the data, and specify that `'g'`

is the positive class.

CVSVMModel = fitcsvm(X,Y,'Holdout',0.15,'ClassNames',{'b','g'},... 'Standardize',true); CompactSVMModel = CVSVMModel.Trained{1}; % Extract trained, compact classifier testInds = test(CVSVMModel.Partition); % Extract the test indices XTest = X(testInds,:); YTest = Y(testInds,:);

`CVSVMModel`

is a `ClassificationPartitionedModel`

classifier. It contains the property `Trained`

, which is a 1-by-1 cell array holding a `CompactClassificationSVM`

classifier that the software trained using the training set.

Estimate the test sample edge.

e = edge(CompactSVMModel,XTest,YTest)

e = 5.0766

The margin average of the test sample is approximately 5.

### Estimate Test Sample Weighted Margin Mean of SVM Classifiers

Suppose that the observations in a data set are measured sequentially, and that the last 150 observations have better quality due to a technology upgrade. Incorporate this advancement by weighing the better quality observations more than the other observations.

Load the `ionosphere`

data set.

load ionosphere rng(1); % For reproducibility

Define a weight vector that weighs the better quality observations two times the other observations.

n = size(X,1); weights = [ones(n-150,1);2*ones(150,1)];

Train an SVM classifier. Specify the weighting scheme and a 15% holdout sample for testing. Also, standardize the data and specify that `'g'`

is the positive class.

CVSVMModel = fitcsvm(X,Y,'Weights',weights,'Holdout',0.15,... 'ClassNames',{'b','g'},'Standardize',true); CompactSVMModel = CVSVMModel.Trained{1}; testInds = test(CVSVMModel.Partition); % Extract the test indices XTest = X(testInds,:); YTest = Y(testInds,:); wTest = weights(testInds,:);

`CVSVMModel`

is a trained `ClassificationPartitionedModel`

classifier. It contains the property `Trained`

, which is a 1-by-1 cell array holding a `CompactClassificationSVM`

classifier that the software trained using the training set.

Estimate the test sample weighted edge using the weighting scheme.

`e = edge(CompactSVMModel,XTest,YTest,'Weights',wTest)`

e = 4.8340

The weighted average margin of the test sample is approximately 5.

### Select SVM Classifier Features by Comparing Test Sample Edges

Perform feature selection by comparing test sample edges from multiple models. Based solely on this comparison, the classifier with the highest edge is the best classifier.

Load the `ionosphere`

data set.

load ionosphere rng(1); % For reproducibility

Partition the data set into training and test sets. Specify a 15% holdout sample for testing.

Partition = cvpartition(Y,'Holdout',0.15); testInds = test(Partition); % Indices for the test set XTest = X(testInds,:); YTest = Y(testInds,:);

`Partition`

defines the data set partition.

Define these two data sets:

`fullX`

contains all predictors (except the removed column of 0s).`partX`

contains the last 20 predictors.

fullX = X; partX = X(:,end-20:end);

Train SVM classifiers for each predictor set. Specify the partition definition.

FullCVSVMModel = fitcsvm(fullX,Y,'CVPartition',Partition); PartCVSVMModel = fitcsvm(partX,Y,'CVPartition',Partition); FCSVMModel = FullCVSVMModel.Trained{1}; PCSVMModel = PartCVSVMModel.Trained{1};

`FullCVSVMModel`

and `PartCVSVMModel`

are `ClassificationPartitionedModel`

classifiers. They contain the property `Trained`

, which is a 1-by-1 cell array holding a `CompactClassificationSVM`

classifier that the software trained using the training set.

Estimate the test sample edge for each classifier.

fullEdge = edge(FCSVMModel,XTest,YTest)

fullEdge = 2.8319

partEdge = edge(PCSVMModel,XTest(:,end-20:end),YTest)

partEdge = 1.5542

The edge for the classifier trained on the complete data set is greater, suggesting that the classifier trained with all the predictors is better.

## Input Arguments

`SVMModel`

— SVM classification model

`ClassificationSVM`

model object | `CompactClassificationSVM`

model object

SVM classification model, specified as a `ClassificationSVM`

model object or `CompactClassificationSVM`

model object returned by `fitcsvm`

or `compact`

,
respectively.

`Tbl`

— Sample data

table

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
additional columns for the response variable and observation
weights. `Tbl`

must contain all of the
predictors used to train `SVMModel`

.
Multicolumn variables and cell arrays other than cell arrays of
character vectors are not allowed.

If `Tbl`

contains the response variable used to
train `SVMModel`

, then you do not need to
specify `ResponseVarName`

or
`Y`

.

If you trained `SVMModel`

using sample data
contained in a table, then the input data for
`edge`

must also be in a
table.

If you set `'Standardize',true`

in `fitcsvm`

when training `SVMModel`

, then the software
standardizes the columns of the predictor data using the
corresponding means in `SVMModel.Mu`

and the
standard deviations in `SVMModel.Sigma`

.

**Data Types: **`table`

`X`

— Predictor data

numeric matrix

Predictor data, specified as a numeric matrix.

Each row of `X`

corresponds to one observation (also known as an instance
or example), and each column corresponds to one variable (also known as a feature). The
variables in the columns of `X`

must be the same as the variables
that trained the `SVMModel`

classifier.

The length of `Y`

and the number of rows in `X`

must be
equal.

If you set `'Standardize',true`

in `fitcsvm`

to train `SVMModel`

, then the software
standardizes the columns of `X`

using the corresponding means in
`SVMModel.Mu`

and the standard deviations in
`SVMModel.Sigma`

.

**Data Types: **`double`

| `single`

`ResponseVarName`

— Response variable name

name of variable in `Tbl`

Response variable name, specified as the name of a variable in
`Tbl`

. If `Tbl`

contains the response variable
used to train `SVMModel`

, then you do not need to specify
`ResponseVarName`

.

If you specify `ResponseVarName`

, then you must do so as a character vector
or string scalar. For example, if the response variable is stored as
`Tbl.Response`

, then specify `ResponseVarName`

as
`'Response'`

. Otherwise, the software treats all columns of
`Tbl`

, including `Tbl.Response`

, as
predictors.

The response variable must be a categorical, character, or string 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.

**Data Types: **`char`

| `string`

`Y`

— Class labels

categorical array | character array | string array | logical vector | numeric vector | cell array of character vectors

Class labels, specified as a categorical, character, or string array, logical or numeric
vector, or cell array of character vectors. `Y`

must be the same as the data type of
`SVMModel.ClassNames`

. (The software treats string arrays as cell arrays of character
vectors.)

The length of `Y`

must equal the number of rows in `Tbl`

or the number of rows in `X`

.

`weights`

— Observation weights

`ones(size(X,1),1)`

(default) | numeric vector | name of variable in `Tbl`

Observation weights, specified as 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 or string
scalar. For example, if the weights are stored as `Tbl.W`

,
then specify `weights`

as `'W'`

.
Otherwise, the software treats all columns of `Tbl`

,
including `Tbl.W`

, as predictors.

If you supply weights, `edge`

computes the weighted classification
edge. The software weights the observations in each row of
`X`

or `Tbl`

with the
corresponding weight in `weights`

.

**Example: **`'Weights','W'`

**Data Types: **`single`

| `double`

| `char`

| `string`

## More About

### Classification Edge

The *edge* is the weighted
mean of the *classification margins*.

The weights are the prior class probabilities. If you supply weights, then the software normalizes them to sum to the prior probabilities in the respective classes. The software uses the renormalized weights to compute the weighted mean.

One way to choose among multiple classifiers, for example, to perform feature selection, is to choose the classifier that yields the highest edge.

### Classification Margin

The *classification margin* for binary classification
is, for each observation, the difference between the classification score for the true class
and the classification score for the false class.

The software defines the classification margin for binary classification as

$$m=2yf\left(x\right).$$

*x* is an observation. If the true label of
*x* is the positive class, then *y* is 1, and –1
otherwise. *f*(*x*) is the positive-class classification
score for the observation *x*. The classification margin is commonly
defined as *m* =
*y**f*(*x*).

If the margins are on the same scale, then they serve as a classification confidence measure. Among multiple classifiers, those that yield greater margins are better.

### Classification Score

The SVM *classification score* for
classifying observation *x* is the signed distance
from *x* to the decision boundary ranging from -∞
to +∞. A positive score for a class indicates that *x* is
predicted to be in that class. A negative score indicates otherwise.

The positive class classification score $$f(x)$$ is the trained SVM classification function. $$f(x)$$ is also the numerical predicted response for *x*, or the
score for predicting *x* into the positive class.

$$f(x)={\displaystyle \sum _{j=1}^{n}{\alpha}_{j}}{y}_{j}G({x}_{j},x)+b,$$

where $$({\alpha}_{1},\mathrm{...},{\alpha}_{n},b)$$ are the estimated SVM parameters, $$G({x}_{j},x)$$ is the dot product in the predictor space between *x* and
the support vectors, and the sum includes the training set observations. The negative class
classification score for *x*, or the score for predicting
*x* into the negative class, is
–*f*(*x*).

If *G*(*x _{j}*,

*x*) =

*x*′

_{j}*x*(the linear kernel), then the score function reduces to

$$f\left(x\right)=\left(x/s\right)\prime \beta +b.$$

*s* is
the kernel scale and *β* is the vector of fitted
linear coefficients.

For more details, see Understanding Support Vector Machines.

## Algorithms

For binary classification, the software defines the margin for
observation *j*, *m _{j}*, as

$${m}_{j}=2{y}_{j}f({x}_{j}),$$

where *y _{j}* ∊ {-1,1}, and

*f*(

*x*) is the predicted score of observation

_{j}*j*for the positive class. However,

*m*=

_{j}*y*

_{j}*f*(

*x*) is commonly used to define the margin.

_{j}## References

[1] Christianini, N., and J. C. Shawe-Taylor. *An
Introduction to Support Vector Machines and Other Kernel-Based Learning
Methods*. Cambridge, UK: Cambridge University Press, 2000.

## Extended Capabilities

### Tall Arrays

Calculate with arrays that have more rows than fit in memory.

This function fully supports tall arrays. For more information, see Tall Arrays.

### GPU Arrays

Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing Toolbox™.

Usage notes and limitations:

The

`edge`

function does not support one-class classification models.

For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).

## Version History

**Introduced in R2014a**

### R2022a: `edge`

returns a different value for
a model with a nondefault cost matrix

If you specify a nondefault cost matrix when you train the input model object, the `edge`

function returns a different value compared to previous releases.

The `edge`

function uses the prior
probabilities stored in the `Prior`

property to normalize the observation
weights of the input data. The way the function uses the `Prior`

property
value has not changed. However, the property value stored in the input model object has changed
for a model with a nondefault cost matrix, so the function can return a different value.

For details about the property value change, see Cost property stores the user-specified cost matrix.

If you want the software to handle the cost matrix, prior
probabilities, and observation weights as in previous releases, adjust the prior probabilities
and observation weights for the nondefault cost matrix, as described in Adjust Prior Probabilities and Observation Weights for Misclassification Cost Matrix. Then, when you train a
classification model, specify the adjusted prior probabilities and observation weights by using
the `Prior`

and `Weights`

name-value arguments, respectively,
and use the default cost matrix.

### R2022a: `edge`

can return NaN for predictor data with missing values

The `edge`

function no longer omits an observation with a
NaN score when computing the weighted mean of the classification margins. Therefore,
`edge`

can now return NaN when the predictor data
`X`

or the predictor variables in `Tbl`

contain any missing values. In most cases, if the test set observations do not contain
missing predictors, the `edge`

function does not return
NaN.

This change improves the automatic selection of a classification model when you use
`fitcauto`

.
Before this change, the software might select a model (expected to best classify new
data) with few non-NaN predictors.

If `edge`

in your code returns NaN, you can update your code
to avoid this result. Remove or replace the missing values by using `rmmissing`

or `fillmissing`

, respectively.

The following table shows the classification models for which the
`edge`

object function might return NaN. For more details,
see the Compatibility Considerations for each `edge`

function.

Model Type | Full or Compact Model Object | `edge` Object Function |
---|---|---|

Discriminant analysis classification model | `ClassificationDiscriminant` , `CompactClassificationDiscriminant` | `edge` |

Ensemble of learners for classification | `ClassificationEnsemble` , `CompactClassificationEnsemble` | `edge` |

Gaussian kernel classification model | `ClassificationKernel` | `edge` |

k-nearest neighbor classification model | `ClassificationKNN` | `edge` |

Linear classification model | `ClassificationLinear` | `edge` |

Neural network classification model | `ClassificationNeuralNetwork` , `CompactClassificationNeuralNetwork` | `edge` |

Support vector machine (SVM) classification model | `edge` |

## See Also

`ClassificationSVM`

| `CompactClassificationSVM`

| `loss`

| `predict`

| `margin`

| `resubEdge`

| `kfoldEdge`

| `fitcsvm`

## MATLAB 命令

您点击的链接对应于以下 MATLAB 命令：

请在 MATLAB 命令行窗口中直接输入以执行命令。Web 浏览器不支持 MATLAB 命令。

# Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list:

## How to Get Best Site Performance

Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.

### Americas

- América Latina (Español)
- Canada (English)
- United States (English)

### Europe

- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)

- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)