回归树的最佳决策 [英] Best decision for regression tree

查看:81
本文介绍了回归树的最佳决策的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假装我创建了一个回归树:

Pretend I've created a regression tree:

library(rpart)
library(rpart.plot)
data("mtcars")
fit <- rpart(mpg~., data = mtcars)
prp(fit)

现在,通过查看树,我可以了解哪些变量会使我达到mpg的最大值和最小值.

Now by looking at the tree I can understand what variables will lead me to the maximum of mpg and to minimum.

但是,如果我有一棵大树怎么办?如何找出哪些变量值可以使我获得最高结果?

But what if I have a big tree? How can I find out what variable values will lead me to the highest result?

推荐答案

如果您只想要最高或最低,则可以使用.您可以使用

If you just want the highest or lowest, this will work. You can get the value of mpg at every node using

fit$frame$yval
[1] 20.09062 16.64762 13.41429 18.26429 26.66364

每当一个节点分裂时,如果该节点的mpg = M,则两个分裂的节点将比M高一个,而比M低一个.因此,最大值和最小值都会出现在树的叶子上.所以最高的叶是

Every time a node splits, if that node has mpg = M, the two split nodes will have one higher than M and one lower than M. Therefore, both the maximum and minimum will occur at leaves of the tree. So the highest leaf is

max(fit$frame$yval)
[1] 26.66364

添加

对不起,我错过了问题的那一部分.

Addition

Sorry, I missed that part of the question.

通过使用 which.max ,我们可以确定哪个节点是最大的节点.

By using which.max we can determine which node is the one with the maximum.

which.max(fit$frame$yval)
[1] 5

现在我们知道它是节点5.从那里,我们可以使用 partykit 包中的 .list.rules.party 来获取相关规则.由于未从包中导出 .list.rules.party ,因此我们必须指定函数的来源.另外,该函数仅给出叶子的规则,因此我们必须使用 name 5而不是第5条规则获取节点的规则.

Now we know it is node 5. From there we can use .list.rules.party from the partykit package to get the relevant rule. Since .list.rules.party is not exported from the package, we have to specify where the function comes from. Also, the function only gives rules for the leaves, so we have to get the rules for the node with the name 5, not with the 5th rule.

PFit = as.party(fit)
Rules = partykit:::.list.rules.party(PFit)
Rules["5"]
        5 
"cyl < 5"

或者您可以使用

Rules[as.character(which.max(fit$frame$yval))]
        5 
"cyl < 5"

获取最小值的规则

Rules[as.character(which.min(fit$frame$yval))]
                       3 
"cyl >= 5 & hp >= 192.5" 

这篇关于回归树的最佳决策的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆