r - 从决策树中提取变量名称
问题描述
所以我用包在 R 中构建了一个决策树,tree
并在树上运行 summary() 函数给了我:
Classification tree:
tree(formula = High temperature ~ ., data = summer.train)
Variables actually used in tree construction:
[1] "Humidity" "Cloudy" "Airy" "Dry"
"Windy"
Number of terminal nodes: 12
Residual mean deviance: 0.3874 = 377.7 / 975
Misclassification error rate: 0.08909 = 89 / 999
我想根据上面的汇总函数获取树构造使用的变量,“airy”,“dry”等。有什么办法我可以这样做吗?
解决方案
所以它链接到:
确实,该解决方案对我有用,我使用著名的垃圾邮件数据集对其进行了测试:
library(kernlab)
library(tree)
data(spam)
spam_tree_def <- tree(type~.,data=spam)
summary(spam_tree_def)
总结结果:
Classification tree:
tree(formula = type ~ ., data = spam)
Variables actually used in tree construction:
[1] "charDollar" "remove" "charExclamation" "hp" "capitalLong" "our"
[7] "capitalAve" "free" "george" "edu"
Number of terminal nodes: 13
Residual mean deviance: 0.4879 = 2238 / 4588
Misclassification error rate: 0.08259 = 380 / 4601
提取所需内容的方法:
as.character(summary(spam_tree_def)$used)
[1] "charDollar" "remove" "charExclamation" "hp" "capitalLong" "our"
[7] "capitalAve" "free" "george" "edu"
推荐阅读
- ruby-on-rails - 是否可以将数据插入到一个控制器内的两个不同的表中
- perl - 如何在 GD::Graph::linespoints 折线图中添加垂直线?
- javascript - 在 REPL 中运行时,Joi 验证库返回的结果与在脚本中运行时不同
- mysql - 如何在 MySql 中计算 GPA
- xslt - 去掉 for each 循环中最后一个元素的逗号
- c# - 使用 SqlAdapter 发送数据表
- numpy - 删除绘图中的点,直到 2D KDE 无处不在
- javascript - 如何在 node.js 上拆分我的参数而不为默认搜索创建错误
- javascript - 如何用 MySQL 中的值填充 5 个文本框列表中的 2 个文本框,并将剩下的 3 个留空
- reactjs - NextJS:处理 Connect / OwnProps / GetInitialProps 的最佳方式