如何在R中计算树中的节点数
我开始用R学习ML。我用RStudio做以下操作:如何在R中计算树中的节点数,r,tree,R,Tree,我开始用R学习ML。我用RStudio做以下操作: iris=datasets::iris tree(Species~., iris) 我得到: 1) root 150 329.600 setosa ( 0.33333 0.33333 0.33333 ) 2) Petal.Length < 2.45 50 0.000 setosa ( 1.00000 0.00000 0.00000 ) * 3) Petal.Length > 2.45 100 138.600
iris=datasets::iris
tree(Species~., iris)
我得到:
1) root 150 329.600 setosa ( 0.33333 0.33333 0.33333 )
2) Petal.Length < 2.45 50 0.000 setosa ( 1.00000 0.00000 0.00000 ) *
3) Petal.Length > 2.45 100 138.600 versicolor ( 0.00000 0.50000 0.50000 )
6) Petal.Width < 1.75 54 33.320 versicolor ( 0.00000 0.90741 0.09259 )
12) Petal.Length < 4.95 48 9.721 versicolor ( 0.00000 0.97917 0.02083 )
24) Sepal.Length < 5.15 5 5.004 versicolor ( 0.00000 0.80000 0.20000 ) *
25) Sepal.Length > 5.15 43 0.000 versicolor ( 0.00000 1.00000 0.00000 ) *
13) Petal.Length > 4.95 6 7.638 virginica ( 0.00000 0.33333 0.66667 ) *
7) Petal.Width > 1.75 46 9.635 virginica ( 0.00000 0.02174 0.97826 )
14) Petal.Length < 4.95 6 5.407 virginica ( 0.00000 0.16667 0.83333 ) *
15) Petal.Length > 4.95 40 0.000 virginica ( 0.00000 0.00000 1.00000 ) *
1)根150 329.600刚毛(0.33333 0.33333 0.33333)
2) 花瓣长度<2.45 50 0.000刚毛(1.00000 0.00000)*
3) 花瓣长度>2.45 100 138.600花色(0.00000 0.50000 0.50000)
6) 花瓣宽度<1.75 54 33.320花色(0.00000 0.90741 0.09259)
12) 花瓣长度<4.95 48 9.721花色(0.000000.97917 0.02083)
24)萼片长度<5.15 5.004花色(0.000000.80000 0.20000)*
25)萼片长度>5.15 43 0.000花色(0.000001.000000.00000)*
13) 花瓣长度>4.95 6 7.638维吉尼亚(0.00000 0.33333 0.66667)*
7) 花瓣宽度>1.75 46 9.635维吉尼亚(0.000000.02174 0.97826)
14) 花瓣长度<4.9565.407维吉尼亚(0.000000.16667 0.83333)*
15) 花瓣长度>4.95 40 0.000维吉尼亚(0.000000.000001.00000)*
我的问题是:
树
和数据集
提前谢谢。关于第二个问题,您可以:
library(tree)
# build your tree
m = tree(Species~., iris)
# count how many times you have <leaf> in the models dataframe
sum(m$frame$var == "<leaf>")
# [1] 6
# count terminal nodes
length(unique(m$where))
# [1] 6
# count nodes
nrow(m$frame)
# [1] 11
库(树)
#建树
m=树木(物种~,鸢尾属)
#计算您在模型数据框中的次数
总和(m$frame$var==“”)
# [1] 6
#计数终端节点
长度(唯一(m$where))
# [1] 6
#计数节点
nrow(百万美元帧)
# [1] 11
关于第二个问题,您可以:
library(tree)
# build your tree
m = tree(Species~., iris)
# count how many times you have <leaf> in the models dataframe
sum(m$frame$var == "<leaf>")
# [1] 6
# count terminal nodes
length(unique(m$where))
# [1] 6
# count nodes
nrow(m$frame)
# [1] 11
库(树)
#建树
m=树木(物种~,鸢尾属)
#计算您在模型数据框中的次数
总和(m$frame$var==“”)
# [1] 6
#计数终端节点
长度(唯一(m$where))
# [1] 6
#计数节点
nrow(百万美元帧)
# [1] 11
对于深度(+1,如果不是0,则取其max()
),对于节点数取其length()
以上是软件包内部的功能
对于深度(+1,如果不是0,则取其max()
),对于节点数取其length()
以上是软件包内部的功能您正在使用哪些库?你是说终端节点吗?对不起,我没有指定包。我使用树和数据集。你使用哪些库?你是说终端节点吗?对不起,我没有指定包。我使用树和数据集。我需要节点的总数,不仅仅是终端节点。我还需要节点的总数,不仅仅是终端节点。我还需要节点的总数。