【问题标题】:How do I plot the Variable Importance of my trained rpart decision tree model?如何绘制经过训练的 rpart 决策树模型的变量重要性?
【发布时间】:2019-10-11 18:26:08
【问题描述】:

我使用 rpart 训练了一个模型,我想生成一个图,显示它用于决策树的变量的变量重要性,但我不知道如何。

我能够提取变量重要性。我试过 ggplot 但没有显示任何信息。我尝试在其上使用 plot() 函数,但它只给了我一个平面图。我还尝试了 plot.default,它稍微好一点,但现在仍然是我想要的。

这是rpart模型训练:

argIDCART = rpart(Argument ~ ., 
                  data = trainSparse, 
                  method = "class")

将变量重要性放入数据框中。

argPlot <- as.data.frame(argIDCART$variable.importance)

这是打印内容的一部分:

       argIDCART$variable.importance
noth                             23.339346
humanitarian                     16.584430
council                          13.140252
law                              11.347241
presid                           11.231916
treati                            9.945111
support                           8.670958

我想绘制一个图表,显示变量/特征名称及其数值重要性。我只是无法让它这样做。它似乎只有一列。我尝试使用单独的功能将它们分开,但也不能这样做。

ggplot(argPlot, aes(x = "variable importance", y = "feature"))

只打印空白。

其他情节看起来很糟糕。

plot.default(argPlot)

看起来它绘制了点,但没有输入变量名。

【问题讨论】:

    标签: r ggplot2 plot rpart


    【解决方案1】:

    由于没有可用的可重现示例,我使用 ggplot2 包和其他用于数据操作的包基于自己的 R 数据集安装了我的响应。

    library(rpart)
    library(tidyverse)
    fit <- rpart(Kyphosis ~ Age + Number + Start, data = kyphosis)
    df <- data.frame(imp = fit$variable.importance)
    df2 <- df %>% 
      tibble::rownames_to_column() %>% 
      dplyr::rename("variable" = rowname) %>% 
      dplyr::arrange(imp) %>%
      dplyr::mutate(variable = forcats::fct_inorder(variable))
    ggplot2::ggplot(df2) +
      geom_col(aes(x = variable, y = imp),
               col = "black", show.legend = F) +
      coord_flip() +
      scale_fill_grey() +
      theme_bw()
    

    ggplot2::ggplot(df2) +
      geom_segment(aes(x = variable, y = 0, xend = variable, yend = imp), 
                   size = 1.5, alpha = 0.7) +
      geom_point(aes(x = variable, y = imp, col = variable), 
                 size = 4, show.legend = F) +
      coord_flip() +
      theme_bw()
    

    【讨论】:

    【解决方案2】:

    如果您想查看变量名称,最好将它们用作 x 轴上的标签。

    plot(argIDCART$variable.importance, xlab="variable", 
        ylab="Importance", xaxt = "n", pch=20)
    axis(1, at=1:7, labels=row.names(argIDCART))
    

    (您可能需要调整窗口大小才能正确查看标签。)

    如果您有很多变量,您可能需要轮换变量名称,以免它们重叠。

    par(mar=c(7,4,3,2))
    plot(argIDCART$variable.importance, xlab="variable", 
        ylab="Importance", xaxt = "n", pch=20)
    axis(1, at=1:7, labels=row.names(argIDCART), las=2)
    

    数据

    argIDCART = read.table(text="variable.importance
    noth                             23.339346
    humanitarian                     16.584430
    council                          13.140252
    law                              11.347241
    presid                           11.231916
    treati                            9.945111
    support                           8.670958", 
    header=TRUE)
    

    【讨论】:

    • 这真的很棒而且效果很好!感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-02-15
    • 2013-02-09
    • 1970-01-01
    • 2023-04-04
    • 2023-03-15
    • 1970-01-01
    • 2012-06-02
    相关资源
    最近更新 更多