主要内容

使用分类树预测类标签预测块

此示例显示了如何使用该模型训练分类决策树模型分类学习者app, and then use the分类树Predictblock for label prediction in Simulink®. The block accepts an observation (predictor data) and returns the predicted class label and class score for the observation using the trained classification decision tree model.

分类学习者应用中的火车分类模型

火车a classification decision tree model by using hyperparameter optimization in the Classification Learner App.

1.在MATLAB®命令窗口中,加载ionospheredata set, which contains radar return qualities (Y)和预测数据(X) of 34 variables. Radar returns are either of good quality ('g') or bad quality ('b')。

加载ionosphere数据集。确定样本量。

loadionospheren = numel(Y)
n = 351

假设按顺序检测到雷达返回,并且您有前300个观察结果,但是您尚未收到最后51个。将数据划分为当前和将来的样本。

prsntx = x(1:300,:);prsnty = y(1:300);ftrx = x(301:end,:);ftry = y(301:end);

2. Open Classification Learner. On theApps标签,在Apps部分,单击Show morearrow to display the apps gallery. In the机器学习和深度学习group, click分类学习者

3.在分类学习者标签,在文件section, click新会话和selectFrom Workspace

4. In the New Session from Workspace dialog box, select the matrixprsntX来自Data Set Variable列表。在下面回复, 点击From workspaceoption button and select the vectorprsntY来自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. Select an optimizable tree model to train. On the分类学习者标签,在楷模部分,单击Show morearrow to open the gallery. In the决策树group, clickOptimizable Tree

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

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

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

Alternatively, you can generate MATLAB code that trains a classification model with the same settings used to train the model in the app. On the分类学习者标签,在出口section, 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 classification model, and performs cross-validation. Change the function name toTrain ClassificationTreemodel并保存功能文件。通过使用训练决策树分类模型Train ClassificationTreemodelfunction.

TrainedModel = Train ClassificationTreemodel(prsntx,prsnty);

9.提取的训练模式trainedModelvariable.trainedModelcontains a分类树模型对象分类树field.

treemdl = TrainedModel.ClassificationTree;

由于超参数优化可以导致一个过度拟合的模型,因此推荐的方法是在将数据导入到分类学习者应用程序中,并查看优化模型在测试集中的性能。有关更多详细信息,请参阅火车Classifier Using Hyperparameter Optimization in Classification Learner App

创建Simul金宝appink模型

This example provides the Simulink modelSlexospherecificationTreepreepRedictexample.slx, which includes the分类树Predict堵塞。您可以打开Simulink模型或如金宝app本节中所述创建新模型。

Open the Simulink modelSlexospherecificationTreepreepRedictexample.slx

SimMdlName ='slexIonosphereClassificationTreePredictExample';open_system(simmdlname)

ThePreLoadFcn回调功能slexIonosphereClassificationTreePredictExampleincludes code to load the sample data, train the 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 Model模板并添加分类树预测块。添加INPORT和OUTPORT块,然后将它们连接到分类块预测块。

双击分类树预测块将打开“块参数”对话框。您可以指定包含训练有素模型的工作区变量的名称。默认变量名称是Treemdl。Click the刷新button. The dialog box displays the options used to train the modelTreemdlunder火车ed Machine Learning Model。Select theAdd output port for predicted class scores复选框添加第二个输出端口score

分类树预测块期望块有34个预测变量值的观察值。双击INPORT块,并设置端口尺寸到34信号属性标签。

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

  • time- 观测值进入模型的时间点。在此示例中,持续时间包括从0到50的整数。方向必须对应于预测器数据中的观测值。因此,在这种情况下,time必须是列矢量。

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

Create an appropriate structure array for future radar returns.

radarretnurninput。time = (0:50)'; radarReturnInput.signals(1).values = ftrX; radarReturnInput.signals(1).dimensions = size(ftrX,2);

To import signal data from the workspace:

  • Open the Configuration Parameters dialog box. On theModelingtab, clickModel Settings

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

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

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

Simulate the model.

SIM(simmdlname);

当Inport块检测到一个观察结果时,它将观察值引导到分类树预测块中。您可以使用Simulation Data Inspector(Simulink)to view the logged data of the Outport blocks.

See Also

相关话题