【问题标题】:Random forest output interpretation随机森林输出解释
【发布时间】:2013-02-06 10:39:25
【问题描述】:

我为我的数据运行了一个随机森林,并以矩阵的形式获得了输出。 它适用于分类的规则是什么?

附:我想要一份客户档案作为输出, 例如纽约人,从事科技行业等工作。

如何解释随机森林的结果?

【问题讨论】:

  • 您可以添加 csv 输入的摘录吗?你可能需要做特征缩减

标签: r profiling output random-forest


【解决方案1】:

除了上面的好答案之外,我发现另一个有趣的工具旨在探索随机森林的一般输出:函数explain_forestrandomForestExplainer。详情请见here

示例代码:

library(randomForest)
data(Boston, package = "MASS")
Boston$chas <- as.logical(Boston$chas)
set.seed(123)
rf <- randomForest(medv ~ ., data = Boston, localImp = TRUE)

请注意:localImp 必须设置为TRUE,否则explain_forest 将退出并出现错误

library(randomForestExplainer)
setwd(my/destination/path)
explain_forest(rf, interactions = TRUE, data = Boston)

这将在您的my/destination/path 中生成一个名为Your_forest_explained.html.html 文件,您可以在网络浏览器中轻松打开该文件。

在本报告中,您将找到有关树木和森林结构的有用信息以及有关变量的一些有用统计数据。

作为示例,请参见下面的生长森林树木之间的最小深度分布图

或多向重要性图之一

报告解读可以参考this

【讨论】:

    【解决方案2】:

    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"
    

    衡量规则。 len是条件中变量值对的数量,freq是满足条件的数据的百分比,pred是规则的结果,即condition=>pred,@987654328 @ 是规则的错误率。

    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"
    

    修剪每条规则:

    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"
    

    选择一个紧凑的规则集:

    (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"
    

    构建一个有序的规则列表作为分类器:

    (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" 
    

    使规则更具可读性:

    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"
    

    提取频繁变量交互(注意规则没有修剪或选择):

    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"
    

    还可以使用函数 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}
    

    【讨论】:

      【解决方案3】:

      查看每棵树应用的规则

      假设您使用 randomForest 包,这就是您访问森林中适合的树木的方式。

      library(randomForest)
      data(iris)
      rf <- randomForest(Species ~ ., iris)
      getTree(rf, 1)
      

      这显示了 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
      ...
      

      您从描述根拆分的第一行开始阅读。根分裂基于变量 3,如果Petal.Length &lt;= 2.50 继续到左子节点(第 2 行),如果Petal.Length &gt; 2.50 继续到右子节点(第 3 行)。如果一行的状态是-1,因为它在第2行,这意味着我们已经到了一个叶子并且将进行预测,在这种情况下类1iesetosa .

      实际上都是在手册中写的,所以请查看?randomForest?getTree 了解更多详细信息。

      查看整个森林的不同重要性

      看看?importance?varImpPlot。这会为您在整个森林中聚合的每个变量提供一个分数。

      > importance(rf)
                   MeanDecreaseGini
      Sepal.Length         10.03537
      Sepal.Width           2.31812
      Petal.Length         43.82057
      Petal.Width          43.10046
      

      【讨论】:

      • 我理解 getTree 的输出,但如何在 Tree 结构中可视化它实际上是个疑问。由于我有分类变量,分割点要转换成二进制,然后手动形成一棵树(有点繁琐)
      • 通过谷歌搜索 "plot randomforest tree" 我发现了这个相当广泛的答案:How to actually plot a sample tree from randomForest::getTree()? 不幸的是,除非您切换到随机森林的 cforest 实现,否则似乎没有现成的功能可用(在party 包)。此外,如果您想知道如何绘制一棵树,您应该将其写在您的原始问题中。目前还不是很具体。
      • 我不想实际绘制一棵树,而是找出考虑最佳数据点的变量组合是什么(好的受访者)
      • 很抱歉,但我不知道你在这里的目的是什么。什么是“最佳数据点”?从你的其他问题来看,我认为你应该阅读faq on what to ask on stackoverflow and how to ask,你甚至会得到一个徽章:) 基本上你的问题应该很清楚,不要太宽泛,最好包括一个例子(你会的结果模型喜欢获取或者一段代码不起作用)。
      • 我们怎么能说 line1 Petal.Length &lt;= 2.50 它可能是 Petal.Length &gt; 2.50。我们如何提出&gt;&lt; 的条件?
      猜你喜欢
      • 1970-01-01
      • 2021-09-03
      • 2019-02-26
      • 2015-09-29
      • 2013-04-26
      • 2020-03-30
      • 2012-12-21
      • 2020-11-10
      • 2018-06-11
      相关资源
      最近更新 更多