Predict responses using support vector machine regression model

`Mdl`

— SVM regression model`RegressionSVM`

object | `CompactRegressionSVM`

objectSVM regression model, specified as a `RegressionSVM`

model or a `CompactRegressionSVM`

model,
returned by `fitrsvm`

or `compact`

,
respectively.

`X`

— Predictor data used to generate responsesnumeric matrix | table

Predictor data used to generate responses, specified as a numeric matrix or table.

Each row of `X`

corresponds to one observation,
and each column corresponds to one variable.

For a numeric matrix:

The variables making up the columns of

`X`

must have the same order as the predictor variables that trained`Mdl`

.If you trained

`Mdl`

using a table (for example,`Tbl`

), then`X`

can be a numeric matrix if`Tbl`

contains all numeric predictor variables. To treat numeric predictors in`Tbl`

as categorical during training, identify categorical predictors using the`CategoricalPredictors`

name-value pair argument of`fitrsvm`

. If`Tbl`

contains heterogeneous predictor variables (for example, numeric and categorical data types) and`X`

is a numeric matrix, then`predict`

throws an error.

For a table:

`predict`

does not support multi-column variables and cell arrays other than cell arrays of character vectors.If you trained

`Mdl`

using a table (for example,`Tbl`

), then all predictor variables in`X`

must have the same variable names and data types as those that trained`Mdl`

(stored in`Mdl.PredictorNames`

). However, the column order of`X`

does not need to correspond to the column order of`Tbl`

.`Tbl`

and`X`

can contain additional variables (response variables, observation weights, etc.), but`predict`

ignores them.If you trained

`Mdl`

using a numeric matrix, then the predictor names in`Mdl.PredictorNames`

and corresponding predictor variable names in`X`

must be the same. To specify predictor names during training, see the`PredictorNames`

name-value pair argument of`fitrsvm`

. All predictor variables in`X`

must be numeric vectors.`X`

can contain additional variables (response variables, observation weights, etc.), but`predict`

ignores them.

If you set `'Standardize',true`

in `fitrsvm`

to
train `Mdl`

, then the software standardizes the columns
of `X`

using the corresponding means in `Mdl.Mu`

and
standard deviations in `Mdl.Sigma`

.

**Data Types: **`table`

| `double`

| `single`

`yfit`

— Predicted responsesvector

Predicted responses, returned as a vector of length *n*, where
*n* is the number of observations in the training
data.

For details about how to predict responses, see Equation 1 and Equation 2 in Understanding Support Vector Machine Regression.

Load the `carsmall`

data set. Consider a model that predicts a car's fuel efficiency given its horsepower and weight. Determine the sample size.

```
load carsmall
tbl = table(Horsepower,Weight,MPG);
N = size(tbl,1);
```

Partition the data into training and test sets. Hold out 10% of the data for testing.

rng(10); % For reproducibility cvp = cvpartition(N,'Holdout',0.1); idxTrn = training(cvp); % Training set indices idxTest = test(cvp); % Test set indices

Train a linear SVM regression model. Standardize the data.

Mdl = fitrsvm(tbl(idxTrn,:),'MPG','Standardize',true);

`Mdl`

is a `RegressionSVM`

model.

Predict responses for the test set.

YFit = predict(Mdl,tbl(idxTest,:));

Create a table containing the observed response values and the predicted response values side by side.

table(tbl.MPG(idxTest),YFit,'VariableNames',... {'ObservedValue','PredictedValue'})

`ans=`*10×2 table*
ObservedValue PredictedValue
_____________ ______________
14 9.4833
27 28.938
10 7.765
28 27.155
22 21.054
29 31.484
24.5 30.306
18.5 19.12
32 28.225
28 26.632

If

`mdl`

is a cross-validated`RegressionPartitionedSVM`

model, use`kfoldPredict`

instead of`predict`

to predict new response values.

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

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

Generate C and C++ code using MATLAB® Coder™.

Usage notes and limitations:

You can generate C/C++ code for both

`predict`

and`update`

by using a coder configurer. Or, generate code only for`predict`

by using`saveLearnerForCoder`

,`loadLearnerForCoder`

, and`codegen`

.Code generation for

`predict`

and`update`

— Create a coder configurer by using`learnerCoderConfigurer`

and then generate code by using`generateCode`

. Then you can update model parameters in the generated code without having to regenerate the code.Code generation for

`predict`

— Save a trained model by using`saveLearnerForCoder`

. Define an entry-point function that loads the saved model by using`loadLearnerForCoder`

and calls the`predict`

function. Then use`codegen`

to generate code for the entry-point function.

You can generate fixed-point C/C++ code for

`predict`

. Compared to the general C/C++ code generation workflow, fixed-point code generation requires an additional step that defines the fixed-point data types of the variables required for prediction. Create a fixed-point data type structure by using the data type function generated by`generateLearnerDataTypeFcn`

, and use the structure as an input argument of`loadLearnerForCoder`

in an entry-point function. Generating fixed-point C/C++ code requires MATLAB^{®}Coder™ and Fixed-Point Designer™.

This table contains notes about the arguments of

`predict`

. Arguments not included in this table are fully supported.Argument Notes and Limitations `Mdl`

For the usage notes and limitations of the model object, see Code Generation of the

`CompactRegressionSVM`

object.`X`

For general code generation,

`X`

must be a single-precision or double-precision matrix. For fixed-point code generation,`X`

must be a fixed-point matrix.`X`

can be variable-size, but the number of columns in`X`

must be fixed. Rows and columns must correspond to observations and predictors, respectively.

For more information, see Introduction to Code Generation.

`CompactRegressionSVM`

| `RegressionSVM`

| `fitrsvm`

| `kfoldPredict`

A modified version of this example exists on your system. Do you want to open this version instead?

You clicked a link that corresponds to this MATLAB command:

Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.

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: .

Select web siteYou can also select a web site from the following list:

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

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

- 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)