Bac*_*lin 33
假设你使用这个randomForest包,这就是你如何访问森林里的树木.
library(randomForest)
data(iris)
rf <- randomForest(Species ~ ., iris)
getTree(rf, 1)
Run Code Online (Sandbox Code Playgroud)
这显示了500的树#1的输出:
left daughter right daughter split var split point status prediction
1 2 3 3 2.50 1 0
2 0 0 0 0.00 -1 1
3 4 5 4 1.65 1 0
4 6 7 4 1.35 1 0
5 8 9 3 4.85 1 0
6 0 0 0 0.00 -1 2
...
Run Code Online (Sandbox Code Playgroud)
您开始阅读描述根分割的第一行.根分裂基于变量3,即如果Petal.Length <= 2.50继续到左子节点(第2行)并且如果Petal.Length > 2.50继续到右子节点(第3行).如果一条线的状态-1如第2行那样,则意味着我们已经到达一个叶子并且将进行预测,在这种情况下是类1,即 setosa.
这些都是在手册中写的,所以请看一下?randomForest并?getTree了解更多细节.
看看?importance和?varImpPlot.这样,您可以在整个森林中汇总每个变量的单个分数.
> importance(rf)
MeanDecreaseGini
Sepal.Length 10.03537
Sepal.Width 2.31812
Petal.Length 43.82057
Petal.Width 43.10046
Run Code Online (Sandbox Code Playgroud)
小智 32
" inTrees "R包可能很有用.
这是一个例子.
从随机林中提取原始规则:
library(inTrees)
library(randomForest)
data(iris)
X <- iris[, 1:(ncol(iris) - 1)] # X: predictors
target <- iris[,"Species"] # target: class
rf <- randomForest(X, as.factor(target))
treeList <- RF2List(rf) # transform rf object to an inTrees' format
exec <- extractRules(treeList, X) # R-executable conditions
exec[1:2,]
# condition
# [1,] "X[,1]<=5.45 & X[,4]<=0.8"
# [2,] "X[,1]<=5.45 & X[,4]>0.8"
Run Code Online (Sandbox Code Playgroud)
衡量规则.len是条件中变量值对的数量,freq是满足条件的数据的百分比,pred是规则的结果,即condition=> pred,err是规则的错误率.
ruleMetric <- getRuleMetric(exec,X,target) # get rule metrics
ruleMetric[1:2,]
# len freq err condition pred
# [1,] "2" "0.3" "0" "X[,1]<=5.45 & X[,4]<=0.8" "setosa"
# [2,] "2" "0.047" "0.143" "X[,1]<=5.45 & X[,4]>0.8" "versicolor"
Run Code Online (Sandbox Code Playgroud)
修剪每条规则:
ruleMetric <- pruneRule(ruleMetric, X, target)
ruleMetric[1:2,]
# len freq err condition pred
# [1,] "1" "0.333" "0" "X[,4]<=0.8" "setosa"
# [2,] "2" "0.047" "0.143" "X[,1]<=5.45 & X[,4]>0.8" "versicolor"
Run Code Online (Sandbox Code Playgroud)
选择一个紧凑的规则集:
(ruleMetric <- selectRuleRRF(ruleMetric, X, target))
# len freq err condition pred impRRF
# [1,] "1" "0.333" "0" "X[,4]<=0.8" "setosa" "1"
# [2,] "3" "0.313" "0" "X[,3]<=4.95 & X[,3]>2.6 & X[,4]<=1.65" "versicolor" "0.806787615686919"
# [3,] "4" "0.333" "0.04" "X[,1]>4.95 & X[,3]<=5.35 & X[,4]>0.8 & X[,4]<=1.75" "versicolor" "0.0746284932951366"
# [4,] "2" "0.287" "0.023" "X[,1]<=5.9 & X[,2]>3.05" "setosa" "0.0355855756152103"
# [5,] "1" "0.307" "0.022" "X[,4]>1.75" "virginica" "0.0329176860493297"
# [6,] "4" "0.027" "0" "X[,1]>5.45 & X[,3]<=5.45 & X[,4]<=1.75 & X[,4]>1.55" "versicolor" "0.0234818254947883"
# [7,] "3" "0.007" "0" "X[,1]<=6.05 & X[,3]>5.05 & X[,4]<=1.7" "versicolor" "0.0132907201116241"
Run Code Online (Sandbox Code Playgroud)
构建有序规则列表作为分类器:
(learner <- buildLearner(ruleMetric, X, target))
# len freq err condition pred
# [1,] "1" "0.333333333333333" "0" "X[,4]<=0.8" "setosa"
# [2,] "3" "0.313333333333333" "0" "X[,3]<=4.95 & X[,3]>2.6 & X[,4]<=1.65" "versicolor"
# [3,] "4" "0.0133333333333333" "0" "X[,1]>5.45 & X[,3]<=5.45 & X[,4]<=1.75 & X[,4]>1.55" "versicolor"
# [4,] "1" "0.34" "0.0196078431372549" "X[,1]==X[,1]" "virginica"
Run Code Online (Sandbox Code Playgroud)
使规则更具可读性:
readableRules <- presentRules(ruleMetric, colnames(X))
readableRules[1:2, ]
# len freq err condition pred
# [1,] "1" "0.333" "0" "Petal.Width<=0.8" "setosa"
# [2,] "3" "0.313" "0" "Petal.Length<=4.95 & Petal.Length>2.6 & Petal.Width<=1.65" "versicolor"
Run Code Online (Sandbox Code Playgroud)
提取频繁的变量交互(注意规则未被修剪或选择):
rf <- randomForest(X, as.factor(target))
treeList <- RF2List(rf) # transform rf object to an inTrees' format
exec <- extractRules(treeList, X) # R-executable conditions
ruleMetric <- getRuleMetric(exec, X, target) # get rule metrics
freqPattern <- getFreqPattern(ruleMetric)
# interactions of at least two predictor variables
freqPattern[which(as.numeric(freqPattern[, "len"]) >= 2), ][1:4, ]
# len sup conf condition pred
# [1,] "2" "0.045" "0.587" "X[,3]>2.45 & X[,4]<=1.75" "versicolor"
# [2,] "2" "0.041" "0.63" "X[,3]>4.75 & X[,4]>0.8" "virginica"
# [3,] "2" "0.039" "0.604" "X[,4]<=1.75 & X[,4]>0.8" "versicolor"
# [4,] "2" "0.033" "0.675" "X[,4]<=1.65 & X[,4]>0.8" "versicolor"
Run Code Online (Sandbox Code Playgroud)
还可以使用函数presentRules以可读形式呈现这些频繁模式.
此外,可以在LaTex中格式化规则或频繁模式.
library(xtable)
print(xtable(freqPatternSelect), include.rownames=FALSE)
# \begin{table}[ht]
# \centering
# \begin{tabular}{lllll}
# \hline
# len & sup & conf & condition & pred \\
# \hline
# 2 & 0.045 & 0.587 & X[,3]$>$2.45 \& X[,4]$<$=1.75 & versicolor \\
# 2 & 0.041 & 0.63 & X[,3]$>$4.75 \& X[,4]$>$0.8 & virginica \\
# 2 & 0.039 & 0.604 & X[,4]$<$=1.75 \& X[,4]$>$0.8 & versicolor \\
# 2 & 0.033 & 0.675 & X[,4]$<$=1.65 \& X[,4]$>$0.8 & versicolor \\
# \hline
# \end{tabular}
# \end{table}
Run Code Online (Sandbox Code Playgroud)
除了上面的好答案之外,我还发现了另一个有趣的工具,旨在探索随机森林的一般输出:函数explain_forest包randomForestExplainer。请参阅此处了解更多详情。
示例代码:
library(randomForest)
data(Boston, package = "MASS")
Boston$chas <- as.logical(Boston$chas)
set.seed(123)
rf <- randomForest(medv ~ ., data = Boston, localImp = TRUE)
Run Code Online (Sandbox Code Playgroud)
请注意:localImp必须设置为TRUE,否则explain_forest将退出并出现错误
library(randomForestExplainer)
setwd(my/destination/path)
explain_forest(rf, interactions = TRUE, data = Boston)
Run Code Online (Sandbox Code Playgroud)
这将生成一个.html文件,命名Your_forest_explained.html,在你的my/destination/path,你可以在Web浏览器中轻松打开。
在此报告中,您将找到有关树木和森林结构的有用信息以及有关变量的一些有用统计信息。
例如,请参见下面的已生长森林的树木之间的最小深度分布图
或多向重要性图之一
您可以参考本报告的解读。
| 归档时间: |
|
| 查看次数: |
47120 次 |
| 最近记录: |