简体   繁体   English

如何在Matlab上设置后期修剪的决策树的最大深度?

[英]How to set maximum depth of decision tree for post prunning on matlab?

How do I build a tree which has a depth that i want? 如何构建具有所需深度的树? For example i want to create a decision tree which has a only 3 depth. 例如,我想创建一个只有3个深度的决策树。

load ionosphere 
treeModel = fitctree(X,Y) 
view(treeModel) 
view(treeModel,'mode','graph')

This code create 7 depth tree. 这段代码创建了7个深度树。 I use same data set but i want to create tree which has 3 or 2 depth. 我使用相同的数据集,但我想创建深度为3或2的树。 How can I do on matlab? 我该如何在matlab上进行操作?

You can control the maximum depth using the MaxDepth name-value pair argument. 您可以使用MaxDepth名称/值对参数控制最大深度。

Read the documentation for more details. 阅读文档以获取更多详细信息。

treeModel = fitctree(X,Y,'MaxDepth',3);

Try to be as flexible as possible when building up Matlab environments. 建立Matlab环境时,请尝试尽可能地灵活。 Also, as per official documentation, note that the MaxDepth option only applies when using fitctree on tall arrays. 另外,根据官方文档,请注意, MaxDepth选项仅在对高阵列使用fitctree时适用。

load ionosphere 
treeModel = CreateTreeModel(X,Y,3);
view(treeModel) 
view(treeModel,'mode','graph')

function tm = CreateTreeModel(x,y,depth)
    if (nargin < 3)
        tm = fitctree(x,y);
        return;
    end

    if (depth < 1)
        depth = 1;
    end

    tm = fitctree(x,y,'MaxDepth',depth);
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM