predict responses using regressionsvm predict block -凯发k8网页登录
this example shows how to train a support vector machine (svm) regression model using the app, and then use the block for response prediction in simulink®. the block accepts an observation (predictor data) and returns the predicted response for the observation using the trained svm regression model.
train regression model in regression learner app
train an svm regression model by using hyperparameter optimization in the regression learner app.
1. in the matlab® command window, load the carbig
data set, and create a matrix containing most of the predictor variables and a vector of the response variable.
load carbig
x = [acceleration,cylinders,displacement,horsepower,model_year,weight];
y = mpg;
2. delete rows of x and y where either array has missing values.
r = rmmissing([x y]); x = r(:,1:end-1); y = r(:,end);
3. open regression learner. on the apps tab, in the apps section, click the show more arrow to display the apps gallery. in the machine learning and deep learning group, click regression learner.
4. on the regression learner tab, in the file section, select new session and select from workspace.
5. in the new session from workspace dialog box, select the matrix x
from the data set variable list. under response, click the from workspace option button and select the vector y from the workspace. the default validation option is 5-fold cross-validation, to protect against overfitting. for this example, do not change the default settings.
6. to accept the default options and continue, click start session.
7. select an optimizable svm model to train. on the regression learner tab, in the models section, click the show more arrow to open the gallery. in the support vector machines group, click optimizable svm.
8. select the model hyperparameters to optimize. in the summary tab, you can select optimize check boxes for the hyperparameters that you want to optimize. by default, all the check boxes for the available hyperparameters are selected. for this example, clear the optimize check boxes for kernel function and standardize data. by default, the app disables the optimize check box for kernel scale whenever the kernel function has a fixed value other than gaussian
. select a gaussian
kernel function, and select the optimize check box for kernel scale.
9. in the train section, click train all and select train selected. the app displays a minimum mse plot as it runs the optimization process. at each iteration, the app tries a different combination of hyperparameter values and updates the plot with the minimum validation mean squared error (mse) observed up to that iteration, indicated in dark blue. when the app completes the optimization process, it selects the set of optimized hyperparameters, indicated by a red square. for more information, see .
the app lists the optimized hyperparameters in both the optimization results section to the right of the plot and the model hyperparameters section of the model summary tab. in general, the optimization results are not reproducible.
10. export the model to the matlab workspace. on the regression learner tab, in the export section, click export model and select export model, then click ok. the default name for the exported model is trainedmodel
.
alternatively, you can generate matlab code that trains a regression model with the same settings used to train the svm model in the app. on the regression learner tab, in the export section, click generate function. the app generates code from your session and displays the file in the matlab editor. the file defines a function that accepts predictor and response variables, trains a regression model, and performs cross-validation. change the function name to trainregressionsvmmodel
and save the function file. train an svm model by using the trainregressionsvmmodel
function.
trainedmodel = trainregressionsvmmodel(x,y);
11. extract the trained svm model from the trainedmodel
variable. trainedmodel
contains a regressionsvm
model object in the regressionsvm
field.
svmmdl = trainedmodel.regressionsvm;
because hyperparameter optimization can lead to an overfitted model, the recommended approach is to create a separate test set before importing your data into the regression learner app and see how the optimized model performs on your test set. for more details, see .
create simulink model
this example provides the simulink model slexcardataregressionsvmpredictexample.slx
, which includes the block. you can open the simulink model or create a new model as described in this section.
open the simulink model slexcardataregressionsvmpredictexample.slx
.
simmdlname = 'slexcardataregressionsvmpredictexample';
open_system(simmdlname)
if you open the simulink model, then the software runs the code in the preloadfcn
callback function before loading the simulink model. the preloadfcn
callback function of slexcardataregressionsvmpredictexample
includes code to check if your workspace contains the svmmdl
variable for the trained model. if the workspace does not contain the variable, preloadfcn
loads the sample data, trains the svm model, and creates an input signal for the simulink model. to view the callback function, in the setup section on the modeling tab, click model settings and select model properties. then, on the callbacks tab, select the preloadfcn
callback function in the model callbacks pane.
to create a new simulink model, open the blank model template and add the regressionsvm predict block. add the inport and outport blocks and connect them to the regressionsvm predict block.
double-click the regressionsvm predict block to open the block parameters dialog box. you can specify the name of a workspace variable that contains the trained svm model. the default variable name is svmmdl
. click the refresh button. the dialog box displays the options used to train the svm model svmmdl
under trained machine learning model.
the regressionsvm predict block expects an observation containing 6 predictor values. double-click the inport block, and set the port dimensions to 6 on the signal attributes tab.
create an input signal in the form of a structure array for the simulink model. the structure array must contain these fields:
time
— the points in time at which the observations enter the model. the orientation must correspond to the observations in the predictor data. so, in this example,time
must be a column vector.signals
— a 1-by-1 structure array describing the input data and containing the fieldsvalues
anddimensions
, wherevalues
is a matrix of predictor data, anddimensions
is the number of predictor variables.
create an appropriate structure array for the slexcardataregressionsvmpredictexample
model from the carsmall
data set.
load carsmall
testx = [acceleration,cylinders,displacement,horsepower,model_year,weight];
testx = rmmissing(testx);
carsmallinput.time = (0:size(testx,1)-1)';
carsmallinput.signals(1).values = testx;
carsmallinput.signals(1).dimensions = size(testx,2);
to import signal data from the workspace:
open the configuration parameters dialog box. on the modeling tab, click model settings.
in the data import/export pane, select the input check box and enter
carsmallinput
in the adjacent text box.in the solver pane, under simulation time, set stop time to
carsmallinput.time(end)
. under solver selection, set type tofixed-step
, and set solver todiscrete (no continuous states)
.
for more details, see load signal data for simulation (simulink).
simulate the model.
sim(simmdlname);
when the inport block detects an observation, it directs the observation into the regressionsvm predict block. you can use the simulation data inspector (simulink) to view the logged data of the outport block.