gpt4 book ai didi

r - r中的决策树

转载 作者:行者123 更新时间:2023-11-30 09:37:15 25 4
gpt4 key购买 nike

我的数据集是:

x=data.frame(v1=c(97 ,  97 ,  85 ,  84 ,  90 ,  80 ,  81 ,  90 ,  80,    70,    90 ,   90,    90    ,95  ,  88 ,   99),
+ v2=c(99 , 91 , 91 ,83 , 99 , 95 , 74 , 88 , 82 , 80 , 96 , 87 , 92 , 96 , 88, 95),
+ v3=c( 89 ,93 , 87 , 80 , 96 , 96 , 75 , 90 , 78, 86 , 92 ,88 , 80, 88 , 98 ,98),
+ v4=c( 89 , 97 ,91 , 86 , 95 , 95 , 89 , 88 , 75, 82 , 99, 92 , 95, 92 , 90, 98),
+ v5=c( 99 ,90 , 93 ,91 , 90 , 90 , 77 , 92 , 85, 76 , 90, 96 , 90, 90 , 90, 92))
> x
v1 v2 v3 v4 v5
1 97 99 89 89 99
2 97 91 93 97 90
3 85 91 87 91 93
4 84 83 80 86 91
5 90 99 96 95 90
6 80 95 96 95 90
7 81 74 75 89 77
8 90 88 90 88 92
9 80 82 78 75 85
10 70 80 86 82 76
11 90 96 92 99 90
12 90 87 88 92 96
13 90 92 80 95 90
14 95 96 88 92 90
15 88 88 98 90 90
16 99 95 98 98 92

我使用rpart包来应用决策树,如下所示:

# Classification Tree with rpart
library(rpart)
fit <- rpart(v5 ~ v1+v2+v3+v4,
method="class", data=x)

printcp(fit) # display the results

Classification tree:
rpart(formula = v5 ~ v1 + v2 + v3 + v4, data = x, method = "class")

Variables actually used in tree construction:
character(0)

Root node error: 9/16 = 0.5625

n= 16

CP nsplit rel error xerror xstd
1 0.01 0 1 0 0


> summary(fit) # detailed summary of splits

Call:
rpart(formula = v5 ~ v1 + v2 + v3 + v4, data = x, method = "class")
n= 16

CP nsplit rel error xerror xstd
1 0.01 0 1 0 0

Node number 1: 16 observations
predicted class=90 expected loss=0.5625 P(node) =1
class counts: 1 1 1 7 1 2 1 1 1
probabilities: 0.062 0.062 0.062 0.438 0.062 0.125 0.062 0.062 0.062

绘图树

 # plot tree 
plot(fit, uniform=TRUE,
+ main="Classification Tree ")

Error in plot.rpart(fit, uniform = TRUE, main = "Classification Tree ") :
fit is not a tree, just a root

text(fit, use.n=TRUE, all=TRUE, cex=.8)

Error in text.rpart(fit, use.n = TRUE, all = TRUE, cex = 0.8) :
fit is not a tree, just a root

我应用rpart时出了什么问题?为什么它给我 TreeMap 错误?如何修复此错误错误:

fit 不是树,只是根

最佳答案

如果要构建分类树,则使用 method="class";如果要构建回归树,则使用 method="anova"。看起来您有连续响应,因此您应该构建回归树(即 method="anova")。

关于r - r中的决策树,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35271280/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com