Classify observations using support vector machine (SVM) classifier for one-class and binary classification
Statistics and Machine Learning Toolbox / Classification
The ClassificationSVM Predict block classifies observations using an SVM
classification object ClassificationSVM
and its object function
predict
for one-class and two-class
(binary) classification.
Import a trained SVM classification object into the block by specifying the name of a
workspace variable that contains a ClassificationSVM
object. The input
port X receives an observation (predictor data), and the output
port Label returns a predicted class label for the observation. You
can add an optional output port Score that returns predicted class
scores or posterior probabilities.
X
— Predictor dataPredictor data, specified as a column vector or row vector of one observation.
The variables in X must have the same
order as the predictor variables that trained the SVM model
specified by Select trained machine learning
model
.
If you set 'Standardize',true
in
fitcsvm
when
training the SVM model, then the ClassificationSVM
Predict block standardizes the values of
X using the means and standard
deviations in the Mu
and
Sigma
properties (respectively) of
the SVM model.
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
| Boolean
| fixed point
Label
— Predicted class labelPredicted class label, returned as a scalar.
For one-class learning, Label is the value representing the positive class.
For two-class learning, Label is the class yielding the largest score or the largest posterior probability.
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
| Boolean
| fixed point
| enumerated
Score
— Predicted class scores or posterior probabilitiesPredicted class scores or posterior probabilities, returned as a scalar for one-class learning or a 1-by-2 vector for two-class learning.
For one-class learning, Score is the classification score of the positive class. You cannot obtain posterior probabilities for one-class learning.
For two-class learning, Score is a 1-by-2 vector.
The first and second element of
Score correspond to the
classification scores of the negative class
(svmMdl.ClassNames(1)
) and the
positive class
(svmMdl.ClassNames(2)
),
respectively, where svmMdl
is the
SVM model specified by Select trained machine learning
model
. You can use the
ClassNames
property of
svmMdl
to check the negative
and positive class names.
If you fit the optimal
score-to-posterior-probability transformation
function using fitPosterior
or fitSVMPosterior
, then
Score contains class
posterior probabilities. Otherwise,
Score contains class
scores.
To enable this port, select the check box for Add output port for predicted class scores
on the
Main tab of the Block Parameters dialog
box.
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
| fixed point
Select trained machine learning model
— SVM classification modelsvmMdl
(default) | ClassificationSVM
object | CompactClassificationSVM
objectSpecify the name of a workspace variable that contains a ClassificationSVM
object or
CompactClassificationSVM
object.
When you train the SVM model by using fitcsvm
, the following
restrictions apply:
The predictor data cannot include categorical predictors
(logical
,
categorical
, char
,
string
, or cell
).
If you supply training data in a table, the predictors must
be numeric (double
or
single
). Also, you cannot use the
'CategoricalPredictors'
name-value
pair argument. To include categorical predictors in an SVM
model, preprocess the categorical predictors by using
dummyvar
before fitting the SVM model.
The value of the 'ScoreTransform'
name-value pair argument
cannot be 'invlogit'
or an anonymous
function. For a block that predicts posterior probabilities
given new observations, pass a trained SVM model to fitPosterior
or
fitSVMPosterior
.
The value of the 'KernelFunction'
name-value pair argument
must be 'gaussian'
,
'linear'
, or
'polynomial'
.
Block Parameter:
TrainedLearner |
Type: workspace variable |
Values:
ClassificationSVM object |
CompactClassificationSVM object |
Default:
svmMdl |
Add output port for predicted class scores
— Add second output port for predicted class scoresoff
(default) | on
Select the check box to include the second output port Score in the ClassificationSVM Predict block.
Block Parameter:
ShowOutputScore |
Type: character vector |
Values:
'off' | 'on' |
Default:
'off' |
Integer rounding mode
— Rounding mode for fixed-point operationsFloor
(default) | Ceiling
| Convergent
| Nearest
| Round
| Simplest
| Zero
Specify the rounding mode for fixed-point operations. For more information, see Rounding (Fixed-Point Designer).
Block parameters always round to the nearest representable value. To control the rounding of a block parameter, enter an expression using a MATLAB® rounding function into the mask field.
Block Parameter:
RndMeth |
Type: character vector |
Values:
'Ceiling' | 'Convergent' | 'Floor' | 'Nearest' | 'Round' | 'Simplest' |
'Zero' |
Default:
'Floor' |
Saturate on integer overflow
— Method of overflow actionoff
(default) | on
Specify whether overflows saturate or wrap.
Action | Rationale | Impact on Overflows | Example |
---|---|---|---|
Select this check box
( | Your model has possible overflow, and you want explicit saturation protection in the generated code. | Overflows saturate to either the minimum or maximum value that the data type can represent. | The maximum value that the |
Clear this check box
( | You want to optimize efficiency of your generated code. You want to avoid overspecifying how a block handles out-of-range signals. For more information, see Troubleshoot Signal Range Errors (Simulink). | Overflows wrap to the appropriate value that the data type can represent. | The maximum value that the |
Block Parameter:
SaturateOnIntegerOverflow |
Type: character vector |
Values:
'off' | 'on' |
Default:
'off' |
Lock output data type setting against changes by the fixed-point tools
— Prevent fixed-point tools from overriding data typeoff
(default) | on
Select this parameter to prevent the fixed-point tools from overriding the data type you specify for the block. For more information, see Use Lock Output Data Type Setting (Fixed-Point Designer).
Block Parameter:
LockScale |
Type: character vector |
Values:
'off' | 'on' |
Default:
'off' |
Label data type
— Data type of Label outputInherit: Inherit via back
propagation
(default) | Inherit: Inherit from 'Constant
value'
| double
| single
| int8
| uint8
| int16
| uint16
| int32
| uint32
| int64
| uint64
| boolean
| fixdt(1,16)
| fixdt(1,16,0)
| fixdt(1,16,2^0,0)
| Enum: <class name>
| <data type expression>
Specify the data type for the Label output. The
type can be inherited, specified directly, or expressed as a data type
object such as Simulink.NumericType
.
When you select an inherited option, the software behaves as follows:
Inherit:
Inherit via back propagation
— Simulink
automatically determines the Label data
type of the block during data type
propagation (see Data Type Propagation (Simulink)). In this case, the
block uses the data type of a downstream block or signal
object.
Inherit: Inherit from 'Constant
value'
— The ClassificationSVM
predict block uses a Constant
block under the mask. The data type of the
Constant block depends on the observed
class labels that trained the SVM model specified by
Select trained machine learning
model
.
If the observed class labels are numeric, then
the Constant block data type is
double
.
If the observed class labels are
logical
, then the
Constant block data type is
boolean
.
If the observed class labels are not numeric
or logical
, then the
Constant block uses an
enumerated
data type variable
enumLabels
. For example, if the
class labels are class 1
and class 2
, then the
corresponding Label values
are enumLabels.class_1
and
enumLabels.class_2
. The block
converts the class labels to valid MATLAB identifiers by using the matlab.lang.makeValidName
function.
For more information about data types, see Control Signal Data Types (Simulink).
Click the Show data type assistant
button to display the Data Type Assistant,
which helps you set the data type attributes. For more information, see Specify Data Types Using Data Type Assistant (Simulink).
Block Parameter:
LabelDataTypeStr |
Type: character vector |
Values: 'Inherit:
Inherit via back propagation' | Inherit:
Inherit from 'Constant value' |
'double' | 'single' |
'int8' | 'uint8' |
'int16' | 'uint16' |
'int32' | 'uint32' |
'int64' | 'uint64' |
boolean |
'fixdt(1,16)' |
'fixdt(1,16,0)' |
'fixdt(1,16,2^0,0)' | Enum:
<class name> | '<data type
expression>' |
Default: 'Inherit:
Inherit via back propagation' |
Label minimum
— Minimum value of Label output for range checking[]
(default) | scalarLower value of the Label output range that Simulink® checks.
Simulink uses the minimum value to perform:
Parameter range checking (see Specify Minimum and Maximum Values for Block Parameters (Simulink)) for some blocks.
Simulation range checking (see Specify Signal Ranges (Simulink) and Enable Simulation Range Checking (Simulink)).
Automatic scaling of fixed-point data types.
Optimization of the code that you generate from the model. This optimization can remove algorithmic code and affect the results of some simulation modes such as SIL or external mode. For more information, see Optimize using the specified minimum and maximum values (Embedded Coder).
Note
The Label minimum parameter does not saturate or clip the actual Label output signal. Use the Saturation (Simulink) block instead.
Block Parameter:
LabelOutMin |
Type: character vector |
Values: '[
]' | scalar |
Default: '[
]' |
Label maximum
— Maximum value of Label output for range checking[]
(default) | scalarUpper value of the Label output range that Simulink checks.
Simulink uses the maximum value to perform:
Parameter range checking (see Specify Minimum and Maximum Values for Block Parameters (Simulink)) for some blocks.
Simulation range checking (see Specify Signal Ranges (Simulink) and Enable Simulation Range Checking (Simulink)).
Automatic scaling of fixed-point data types.
Optimization of the code that you generate from the model. This optimization can remove algorithmic code and affect the results of some simulation modes such as SIL or external mode. For more information, see Optimize using the specified minimum and maximum values (Embedded Coder).
Note
The Label maximum parameter does not saturate or clip the actual Label output signal. Use the Saturation (Simulink) block instead.
Block Parameter:
LabelOutMax |
Type: character vector |
Values: '[
]' | scalar |
Default: '[
]' |
Score data type
— Data type of Score outputdouble
(default) | Inherit: auto
| single
| int8
| uint8
| int16
| uint16
| int32
| uint32
| int64
| uint64
| fixdt(1,16)
| fixdt(1,16,0)
| fixdt(1,16,2^0,0)
| <data type expression>
Specify the data type for the Score output. The
type can be inherited, specified directly, or expressed as a data type
object such as Simulink.NumericType
.
When you select Inherit: auto
, the block
uses a rule that inherits a data
type.
For more information about data types, see Control Signal Data Types (Simulink).
Click the Show data type assistant
button to display the Data Type Assistant,
which helps you set the data type attributes. For more information, see Specify Data Types Using Data Type Assistant (Simulink).
Block Parameter:
ScoreDataTypeStr |
Type: character vector |
Values: 'Inherit:
auto' | 'double' |
'single' | 'int8' |
'uint8' | 'int16' |
'uint16' | 'int32' |
'uint32' | 'int64' |
'uint64' |
'fixdt(1,16)' |
'fixdt(1,16,0)' |
'fixdt(1,16,2^0,0)' | '<data
type expression>' |
Default:
'double' |
Score minimum
— Minimum value of Score output for range checking[]
(default) | scalarLower value of the Score output range that Simulink checks.
Simulink uses the minimum value to perform:
Parameter range checking (see Specify Minimum and Maximum Values for Block Parameters (Simulink)) for some blocks.
Simulation range checking (see Specify Signal Ranges (Simulink) and Enable Simulation Range Checking (Simulink)).
Automatic scaling of fixed-point data types.
Optimization of the code that you generate from the model. This optimization can remove algorithmic code and affect the results of some simulation modes such as SIL or external mode. For more information, see Optimize using the specified minimum and maximum values (Embedded Coder).
Note
The Score minimum parameter does not saturate or clip the actual Score signal. Use the Saturation (Simulink) block instead.
Block Parameter:
ScoreOutMin |
Type: character vector |
Values: '[
]' | scalar |
Default: '[
]' |
Score maximum
— Maximum value of Score output for range checking[]
(default) | scalarUpper value of the Score output range that Simulink checks.
Simulink uses the maximum value to perform:
Parameter range checking (see Specify Minimum and Maximum Values for Block Parameters (Simulink)) for some blocks.
Simulation range checking (see Specify Signal Ranges (Simulink) and Enable Simulation Range Checking (Simulink)).
Automatic scaling of fixed-point data types.
Optimization of the code that you generate from the model. This optimization can remove algorithmic code and affect the results of some simulation modes such as SIL or external mode. For more information, see Optimize using the specified minimum and maximum values (Embedded Coder).
Note
The Score maximum parameter does not saturate or clip the actual Score signal. Use the Saturation (Simulink) block instead.
Block Parameter:
ScoreOutMax |
Type: character vector |
Values: '[
]' | scalar |
Default: '[
]' |
Raw score data type
— Untransformed score data typedouble
(default) | Inherit: auto
| single
| int8
| uint8
| int16
| uint16
| int32
| uint32
| int64
| uint64
| fixdt(1,16)
| fixdt(1,16,0)
| fixdt(1,16,2^0,0)
| <data type expression>
Specify the data type for the internal untransformed scores. The type
can be inherited, specified directly, or expressed as a data type object
such as Simulink.NumericType
.
When you select Inherit: auto
, the block
uses a rule that inherits a data
type.
For more information about data types, see Control Signal Data Types (Simulink).
Click the Show data type assistant
button to display the Data Type Assistant,
which helps you set the data type attributes. For more information, see Specify Data Types Using Data Type Assistant (Simulink).
Block Parameter:
RawScoreDataTypeStr |
Type: character vector |
Values: 'Inherit:
auto' | 'double' |
'single' | 'int8' |
'uint8' | 'int16' |
'uint16' | 'int32' |
'uint32' | 'int64' |
'uint64' |
'fixdt(1,16)' |
'fixdt(1,16,0)' |
'fixdt(1,16,2^0,0)' | '<data
type expression>' |
Default:
'double' |
Raw score minimum
— Minimum untransformed score for range checking[]
(default) | scalarLower value of the untransformed score range that Simulink checks.
Simulink uses the minimum value to perform:
Parameter range checking (see Specify Minimum and Maximum Values for Block Parameters (Simulink)) for some blocks.
Simulation range checking (see Specify Signal Ranges (Simulink) and Enable Simulation Range Checking (Simulink)).
Automatic scaling of fixed-point data types.
Optimization of the code that you generate from the model. This optimization can remove algorithmic code and affect the results of some simulation modes such as SIL or external mode. For more information, see Optimize using the specified minimum and maximum values (Embedded Coder).
Note
The Raw score minimum parameter does not saturate or clip the actual untransformed score signal.
Block Parameter:
RawScoreOutMin |
Type: character vector |
Values: '[
]' | scalar |
Default: '[
]' |
Raw score maximum
— Maximum untransformed score for range checking[]
(default) | scalarUpper value of the untransformed score range that Simulink checks.
Simulink uses the maximum value to perform:
Parameter range checking (see Specify Minimum and Maximum Values for Block Parameters (Simulink)) for some blocks.
Simulation range checking (see Specify Signal Ranges (Simulink) and Enable Simulation Range Checking (Simulink)).
Automatic scaling of fixed-point data types.
Optimization of the code that you generate from the model. This optimization can remove algorithmic code and affect the results of some simulation modes such as SIL or external mode. For more information, see Optimize using the specified minimum and maximum values (Embedded Coder).
Note
The Raw score maximum parameter does not saturate or clip the actual untransformed score signal.
Block Parameter:
RawScoreOutMax |
Type: character vector |
Values: '[
]' | scalar |
Default: '[
]' |
Kernel data type
— Kernel computation data typedouble
(default) | Inherit: Inherit via internal rule
| Inherit: Keep MSB
| Inherit: Match scaling
| single
| int8
| uint8
| int16
| uint16
| int32
| int64
| uint64
| uint32
| fixdt(1,16)
| fixdt(1,16,0)
| fixdt(1,16,2^0,0)
| <data type expression>
Specify the data type of a parameter for kernel computation.
The Kernel data type paramter specifies the data type of a
different parameter depending on the type of kernel function of the specified SVM model.
You specify the 'KernelFunction'
name-value pair argument when
training the SVM model.
'KernelFunction' value | Data Type |
---|---|
'gaussian' or 'rbf' | Kernel data type specifies the data type of the squared distance for the Gaussian kernel , where x is the predictor data for an observation and s is a support vector. |
'linear' | Kernel data type specifies the data type for the output of the linear kernel function , where x is the predictor data for an observation and s is a support vector. |
'polynomial' | Kernel data type specifies the data type for the output of the polynomial kernel function , where x is the predictor data for an observation, s is a support vector, and p is a polynomial kernel function order. |
The type can be inherited, specified directly, or expressed as a data type object such
as Simulink.NumericType
.
When you select an inherited option, the software behaves as follows:
Inherit:
Inherit via internal rule
— The block uses an internal
rule to determine its data type. The internal rule chooses a data type that
optimizes numerical accuracy, performance, and generated code size, while
taking into account the properties of the embedded target hardware. The
software cannot simultaneously optimize efficiency and numerical accuracy in
all situations.
Inherit:
Keep MSB
— Simulink chooses a data type that maintains the full range of the
operation, and then reduces the precision to a size appropriate for the
embedded target hardware. This rule never produces overflows.
Tip
For more efficient generated code, clear the Saturate on integer overflow parameter.
Inherit:
Match scaling
— Simulink chooses a data type whose scaling matches the scaling of the
input types, where the input refers to an input of a block under the mask.
If the full range of the type does not fit on the embedded target hardware,
the software reduces the range to yield a type that is appropriate for the
embedded target hardware. This rule can produce overflows.
The software cannot simultaneously optimize code efficiency and numerical accuracy in all situations. If these internal rules do not meet your specific needs for numerical accuracy or performance, use one of the following options:
Specify the data type explicitly.
Specify a default data type explicitly, such as
fixdt(1,32,16)
, and then use the
Fixed-Point Tool to propose data types for your model. For more
information, see fxptdlg
(Fixed-Point Designer).
Inherit: Inherit via back propagation
— (Not
recommended) The block uses the data type of a downstream block under the
mask.
Inherit: Same as first input
— (Not
recommended) This first input refers to the first input of a block under the
mask.
For more information about data types, see Control Signal Data Types (Simulink).
Click the Show data type assistant
button to display the Data Type Assistant,
which helps you set the data type attributes. For more information, see Specify Data Types Using Data Type Assistant (Simulink).
Block Parameter:
KernelDataTypeStr |
Type: character vector |
Values: 'Inherit: Inherit via
internal rule | 'Inherit: Keep MSB' |
'Inherit: Match scaling' | 'double' |
'single' | 'int8' |
'uint8' | 'int16' |
'uint16' | 'int32' |
'uint32' | 'uint64' |
'int64' | 'fixdt(1,16)' |
'fixdt(1,16,0)' | 'fixdt(1,16,2^0,0)'
| '<data type expression>' |
Default:
'double' |
Kernel minimum
— Minimum kernel computation value for range checking[]
(default) | scalarLower value of the kernel computation internal variable range that Simulink checks.
Simulink uses the minimum value to perform:
Parameter range checking (see Specify Minimum and Maximum Values for Block Parameters (Simulink)) for some blocks.
Simulation range checking (see Specify Signal Ranges (Simulink) and Enable Simulation Range Checking (Simulink)).
Automatic scaling of fixed-point data types.
Optimization of the code that you generate from the model. This optimization can remove algorithmic code and affect the results of some simulation modes such as SIL or external mode. For more information, see Optimize using the specified minimum and maximum values (Embedded Coder).
Note
The Kernel minimum parameter does not saturate or clip the actual kernel computation value signal.
Block Parameter:
KernelOutMin |
Type: character vector |
Values: '[ ]' |
scalar |
Default: '[ ]' |
Kernel maximum
— Maximum kernel computation value for range checking[]
(default) | scalarUpper value of the kernel computation internal variable range that Simulink checks.
Simulink uses the maximum value to perform:
Parameter range checking (see Specify Minimum and Maximum Values for Block Parameters (Simulink)) for some blocks.
Simulation range checking (see Specify Signal Ranges (Simulink) and Enable Simulation Range Checking (Simulink)).
Automatic scaling of fixed-point data types.
Optimization of the code that you generate from the model. This optimization can remove algorithmic code and affect the results of some simulation modes such as SIL or external mode. For more information, see Optimize using the specified minimum and maximum values (Embedded Coder).
Note
The Kernel maximum parameter does not saturate or clip the actual kernel computation value signal.
Block Parameter:
KernelOutMax |
Type: character vector |
Values: '[ ]' |
scalar |
Default: '[ ]' |
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 is the trained SVM classification function. is also the numerical predicted response for x, or the score for predicting x into the positive class.
where are the estimated SVM parameters, 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(xj,x) = xj′x (the linear kernel), then the score function reduces to
s is the kernel scale and β is the vector of fitted linear coefficients.
For more details, see Understanding Support Vector Machines.
The posterior probability is the probability that an observation belongs in a particular class, given the data.
For SVM, the posterior probability is a function of the score P(s) that observation j is in class k = {-1,1}.
For separable classes, the posterior probability is the step function
where:
sj is the score of observation j.
+1 and –1 denote the positive and negative classes, respectively.
π is the prior probability that an observation is in the positive class.
For inseparable classes, the posterior probability is the sigmoid function
where the parameters A and B are the slope and intercept parameters, respectively.
The prior probability of a class is the assumed relative frequency with which observations from that class occur in a population.
If you are using a linear SVM model and it has many support vectors, then
prediction (classifying observations) can be slow. To efficiently classify
observations based on a linear SVM model, remove the support vectors from the
ClassificationSVM
object by using
discardSupportVectors
.
You can use a MATLAB Function block with the predict
object function of the ClassificationSVM
object. For an example, see Predict Class Labels Using MATLAB Function Block.
When deciding whether to use the ClassificationSVM Predict block in the
Statistics and Machine Learning Toolbox™ library or a MATLAB Function block with the predict
function, consider
the following:
If you use the Statistics and Machine Learning Toolbox library block, you can use the Fixed-Point Tool (Fixed-Point Designer) to convert a floating-point model to fixed point.
Support for variable-size arrays must be enabled for a MATLAB Function block with the predict
function.
If you use a MATLAB Function block, you can use MATLAB functions for preprocessing or post-processing before or after predictions in the same MATLAB Function block.