Predict Responses Using RegressionKernel Predict Block
This example shows how to use the RegressionKernel Predict block for response prediction in Simulink®. The block accepts an observation (predictor data) and returns the predicted response for the observation using the trained Gaussian kernel regression model. To complete this example, you can use the provided Simulink model, or create a new model.
Train Regression Model
Load the carbig
data set, which contains measurements of cars made in the 1970s and early 1980s. Create a matrix containing the predictor variables and a vector of the response variable.
load carbig
X = [Acceleration,Cylinders,Displacement,Horsepower,Model_Year,Weight];
Y = MPG;
Delete rows of X
and Y
where either array has NaN
values. Removing rows with NaN
values before passing data to fitrkernel
can speed up training and reduce memory usage.
R = rmmissing([X Y]); % Data with missing entries removed
X = R(:,1:6);
Y = R(:,end);
Train a kernel regression model.
rng(0,"twister") % For reproducibility kernelMdl = fitrkernel(X,Y,Standardize=true);
kernelMdl
is a RegressionKernel
model. You can use dot notation to access the properties of kernelMdl
. For example, you can enter kernelMdl.ModelParameters
to get more information about the trained model parameters.
Open Provided Simulink Model
This example provides the Simulink model slexRegressionKernelPredictExample.slx
, which includes the RegressionKernel Predict block. You can open the Simulink model or create a new model as described later in this section.
Open the Simulink model slexRegressionKernelPredictExample.slx
.
open_system("slexRegressionKernelPredictExample")
When you open the Simulink model, the software runs the code in the PreLoadFcn
callback function before loading the model. The PreLoadFcn
callback function of slexRegressionKernelPredictExample
includes code to check if your workspace contains the kernelMdl
variable for the trained model. If the workspace does not contain the variable, PreLoadFcn
loads the sample data, trains the kernel 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.
Create Simulink Model
To create a new Simulink model, open the Blank Model template and add the RegressionKernel Predict block from the Classification section of the Statistics and Machine Learning Toolbox™ library. Add the Inport and Outport blocks and connect them to the RegressionKernel Predict block.
Double-click the RegressionKernel Predict block to open the Block Parameters dialog box. Specify the Select trained machine learning model parameter as kernelMdl
, which is the name of a workspace variable that contains the trained kernel model. Click the Refresh button. The dialog box displays the options used to train the kernel model kernelMdl
under Trained Machine Learning Model.
The RegressionKernel 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.
At the command line, 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 slexRegressionKernelPredictExample
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)
. These settings enable the model to run the simulation for each query point incarsmallInput
. Click OK.
For more details, see Load Signal Data for Simulation (Simulink).
Save the model as slexRegressionKernelPredictExample.slx
in Simulink.
Simulate the model
sim("slexRegressionKernelPredictExample");
When the Inport block detects an observation, it directs the observation into the RegressionKernel Predict block. You can use the Simulation Data Inspector (Simulink) to view the logged data of the Outport block.
See Also
RegressionKernel
Predict | fitrkernel
| predict