主要内容

使用RecressionsVM预测响应预测块

This example shows how to train a support vector machine (SVM) regression model using the回归学习者app, and then use theRecressionsVM预测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.

火车回归Model in Regression Learner App

火车an SVM regression model by using hyperparameter optimization in the Regression Learner App.

1.在MATLAB®命令窗口中,加载carbigdata set, and create a matrix containing most of the predictor variables and a vector of the response variable.

loadcarbigX = [Acceleration,Cylinders,Displacement,Horsepower,Model_Year,Weight]; Y = MPG;

2. Open Regression Learner. On theApps标签,在Apps部分,单击Show more箭头显示应用程序画廊。在里面机器学习和深度学习group, click回归学习者

3.在回归学习者标签,在文件section, select新会话和selectFrom Workspace

4. In the New Session from Workspace dialog box, select the matrixX来自数据集变量列表。在下面回复, 点击From workspaceoption 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.

5. To accept the default options and continue, click开始会话

6.选择可训练的可优化SVM模型。在回归学习者标签,在楷模部分,单击Show more箭头打开画廊。在里面Support Vector Machinesgroup, click优化的SVM

7. In the火车部分,click火车都和select选择火车。该应用显示最小MSE图在运行优化过程时。在每次迭代中,该应用程序都会尝试使用高参数值的不同组合,并通过观察到该迭代的最小验证平方误差(MSE)更新图,以深蓝色指示。当应用完成优化过程时,它选择了由红色正方形指示的优化超参数集。有关更多信息,请参阅最小MSE图

The app lists the optimized hyperparameters in both the优化结果情节右侧的部分Model Hyperparameters模型部分概括标签。通常,优化结果不可再现。

8. Export the model to the MATLAB workspace. On the回归学习者标签,在出口部分,click出口Model和select出口Model, then click好的。The default name for the exported model istrainedModel

另外,您可以生成MATLAB代码,该代码训练与用于训练应用程序中SVM模型的相同设置的回归模型。在回归学习者标签,在出口部分,clickGenerate 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 totrainRegressionSVMModel和save the function file. Train an SVM model by using thetrainRegressionSVMModelfunction.

trainedModel = trainRegressionSVMModel(X,Y);

9.从trainedModelvariable.trainedModel包含一个回归vm模型对象回归vmfield.

svmmdl = trainedmodel.regressionsvm;

由于超参数优化可以导致一个过度拟合的模型,因此推荐的方法是在将数据导入回归学习者应用程序之前创建一个单独的测试集,并查看优化模型在测试集上的性能。有关更多详细信息,请参阅在回归学习者应用中使用超参数优化的火车回归模型

创建Simul金宝appink模型

This example provides the Simulink modelslexcardatareGressionsvmpredictexample.slx, which includes theRecressionsVM预测堵塞。您可以打开Simulink模型或如金宝app本节中所述创建新模型。

打开Simulin金宝appk模型slexcardatareGressionsvmpredictexample.slx

SimMdlName =“ slexcardatareGressionsvmpredictexampame”;open_system(simmdlname)

ThePreLoadFcn回调功能slexcardatareGressionsvmpredictExampleincludes code to load the sample data, train the SVM model, and create an input signal for the Simulink model. If you open the Simulink model, then the software runs the code inPreLoadFcnbefore loading the Simulink model. To view the callback function, in theSetupsection on theModelingtab, clickModel Settings和selectModel Properties。然后,在回调选项卡,选择PreLoadFcn回调功能模型回调窗格。

To create a new Simulink model, open theBlank Modeltemplate and add the RegressionSVM Predict block. Add the Inport and Outport blocks and connect them to the RegressionSVM Predict block.

双击ReverSionsVM预测块将打开“块参数”对话框。您可以指定包含训练有素的SVM模型的工作区变量的名称。默认变量名称是svmMdl。Click the刷新button. The dialog box displays the options used to train the SVM modelsvmMdlunder火车ed Machine Learning Model

The RegressionSVM Predict block expects an observation containing 6 predictor values. Double-click the Inport block, and set the端口尺寸to 6 on the信号属性标签。

以Simulink模型的结构数组的形式创建一个输入信号。金宝app结构数组必须包含以下字段:

  • 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必须是列矢量。

  • signals— A 1-by-1 structure array describing the input data and containing the fields方面, 在哪里is a matrix of predictor data, and方面是预测变量的数量。

创建适当的结构数组slexcardatareGressionsvmpredictExamplemodel from the汽车舞data set.

load汽车舞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 theModelingtab, clickModel Settings

  • 在里面数据导入/导出pane, select the输入check box and enterCARSMALLINPUT在相邻的文本框中。

  • 在里面求解器pane, under模拟时间, set停止时间toCARSMALLINPUT。time(end)。在下面求解器选择, set类型toFixed-step, and set求解器to离散(没有连续状态)

有关更多详细信息,请参阅负载信号数据进行仿真(Simulink)

Simulate the model.

SIM(simmdlname);

当Inport块检测到一个观察结果时,它将观察值引导到RecressionsVM预测块中。您可以使用Simulation Data Inspector(Simulink)查看出口块的记录数据。

See Also

相关话题