R:从决策树中提取规则

2024-01-04

我正在使用 R 编程语言。最近,我读到了一种名为“强化学习树”(RLT)的新决策树算法,据说它有可能将“更好”的决策树适合数据集。该库的文档可在此处找到:https://cran.r-project.org/web/packages/RLT/RLT.pdf https://cran.r-project.org/web/packages/RLT/RLT.pdf

我尝试使用这个库在(著名的)鸢尾花数据集上运行分类决策树:

library(RLT)
data(iris)
fit = RLT(iris[,c(1,2,3,4)], iris$Species, model = "classification", ntrees = 1)

问题:从这里,是否可以从这个决策树中提取“规则”?

例如,如果您使用 CART 决策树模型:

library(rpart)
library(rpart.plot)

fit <-rpart( Species ~. , data = iris)
rpart.plot(fit)
 rpart.rules(fit)

    Species  seto vers virg                                               
     setosa [1.00  .00  .00] when Petal.Length <  2.5                     
 versicolor [ .00  .91  .09] when Petal.Length >= 2.5 & Petal.Width <  1.8
  virginica [ .00  .02  .98] when Petal.Length >= 2.5 & Petal.Width >= 1.8

使用 RLT 库可以做到这一点吗?我一直在阅读这个库的文档,似乎找不到提取决策规则的直接方法。我知道这个库通常是用作随机森林(没有决策规则)的替代品 - 但我正在阅读该算法的原始论文,其中他们指定 RLT 算法适合单个决策树(通过RLT 算法),然后将它们聚合在一起,就像在随机森林中一样。因此,在某种程度上,RLT 算法能够拟合单个决策树——理论上该决策树应该具有“决策规则”。

有谁知道如何提取这些规则?

Thanks!

参考:

  • https://www.researchgate.net/publication/277625959_Reinforcement_Learning_Trees https://www.researchgate.net/publication/277625959_Reinforcement_Learning_Trees

规则存储在fit$FittedTrees[[1]]采用相对难以解释的表格格式。

我为您构建了一个相当长的函数,它将提取规则作为数据框,并根据要求另外将树绘制为 ggplot。

RLT_tree <- function(RLT_obj, plot = TRUE)
{
  
  tree <- as.data.frame(t(RLT_obj$FittedTrees[[1]]))
  tree <- tree[c(2, 3, 5, 6, 8, 9, grep("Class\\d", names(tree)))]
  class_cols <- grep("Class\\d", names(tree))
  names(tree)[class_cols] <-
    RLT_obj$ylevels[1 + as.numeric(sub("Class(\\d+)", "\\1",
                                   names(tree)[class_cols]))]
  tree$variable <- RLT_obj$variablenames[tree$SplitVar1]
  tree$variable[is.na(tree$variable)] <- "(Leaf node)"
  tree$rule <- tree$variable
  tree$depth <- numeric(nrow(tree))
  tree$rightness <- numeric(nrow(tree))
  tree$group <- character(nrow(tree))
  
  walk_tree <- function(node, depth, rightness, node_label = "Start", group = "S")
  {
    new_row <- tree[which(tree$Node == node),]
    new_row$depth <- depth
    new_row$rightness <- rightness
    left_label <- paste(new_row$variable, new_row$SplitValue, sep = " < ")
    right_label <- paste(new_row$variable, new_row$SplitValue, sep = " > ")
    new_row$variable <- paste(node_label, "\nn = ", new_row$NumObs)
    new_row$rule <- node_label
    if(is.nan(new_row$SplitValue)) {
      n_objs <- round(new_row[,class_cols] * new_row$NumObs)
      classify <- paste((names(tree)[class_cols])[n_objs != 0], 
                        n_objs[n_objs != 0],
                        collapse = "\n")
      new_row$variable <- paste(new_row$variable, classify, sep = "\n")
    }
    new_row$group <- group
    tree[which(tree$Node == node),] <<- new_row
    if(!is.nan(new_row$SplitValue)){
      walk_tree(new_row$NextLeft, depth + 1, rightness - 2/(depth/2 + 1), 
                left_label, paste(group, "L"))
      walk_tree(new_row$NextRight, depth + 1, rightness + 2/(depth/2 + 1), 
                right_label, paste(group, "R"))
    }
  }
  
  walk_tree(0, 0, 0)
  tree$depth <- max(tree$depth) - tree$depth
  tree$type <- is.nan(tree$NextLeft)
  tree$group <- substr(tree$group, 1, nchar(tree$group) - 1)

  if(plot)
  {
  print(ggplot(tree, aes(rightness, depth)) + 
    geom_segment(aes(x = rightness, xend = rightness, 
                     y = depth, yend = depth - 1, alpha = type)) + 
    geom_line(aes(group = group)) +
    geom_label(aes(label = variable, fill = type), size = 4) + 
    theme_void() + 
    scale_x_continuous(expand = c(0, 1)) + 
    suppressWarnings(scale_alpha_discrete(range = c(1, 0)))  +
    theme(legend.position = "none"))
  }
  tree$isLeaf <- is.nan(tree$NextLeft)
  tree[c(match(c("Node", "rule", "depth", "isLeaf"), names(tree)), class_cols)]
}

这允许:

df <- RLT_tree(fit, plot = TRUE)

and

df
#>    Node               rule depth isLeaf    setosa versicolor virginica
#> 1     0              Start     6  FALSE 0.3111111 0.34814815 0.3407407
#> 2     1  Sepal.Width < 3.2     5  FALSE 0.1573034 0.51685393 0.3258427
#> 3     2  Sepal.Width > 3.2     5  FALSE 0.6086957 0.02173913 0.3695652
#> 4     3 Sepal.Length < 5.4     4  FALSE 0.7000000 0.30000000 0.0000000
#> 5     4 Sepal.Length > 5.4     4   TRUE 0.0000000 0.57971014 0.4202899
#> 6     5 Petal.Length < 1.3     3   TRUE 1.0000000 0.00000000 0.0000000
#> 7     6 Petal.Length > 1.3     3  FALSE 0.6000000 0.40000000 0.0000000
#> 8     7 Petal.Length < 1.4     2   TRUE 1.0000000 0.00000000 0.0000000
#> 9     8 Petal.Length > 1.4     2  FALSE 0.5000000 0.50000000 0.0000000
#> 10    9 Petal.Length < 3.9     1  FALSE 0.7500000 0.25000000 0.0000000
#> 11   10 Petal.Length > 3.9     1   TRUE 0.0000000 1.00000000 0.0000000
#> 12   11 Sepal.Length < 4.9     0   TRUE 1.0000000 0.00000000 0.0000000
#> 13   12 Sepal.Length > 4.9     0   TRUE 0.0000000 1.00000000 0.0000000
#> 14   13  Petal.Width < 0.2     4   TRUE 1.0000000 0.00000000 0.0000000
#> 15   14  Petal.Width > 0.2     4  FALSE 0.3793103 0.03448276 0.5862069
#> 16   15 Sepal.Length < 5.7     3   TRUE 1.0000000 0.00000000 0.0000000
#> 17   16 Sepal.Length > 5.7     3  FALSE 0.0000000 0.05555556 0.9444444
#> 18   17  Sepal.Width < 3.3     2   TRUE 0.0000000 0.00000000 1.0000000
#> 19   18  Sepal.Width > 3.3     2  FALSE 0.0000000 0.08333333 0.9166667
#> 20   19 Petal.Length < 6.1     1  FALSE 0.0000000 0.11111111 0.8888889
#> 21   20 Petal.Length > 6.1     1   TRUE 0.0000000 0.00000000 1.0000000
#> 22   21 Sepal.Length < 6.3     0   TRUE 0.0000000 0.16666667 0.8333333
#> 23   22 Sepal.Length > 6.3     0   TRUE 0.0000000 0.00000000 1.0000000

为了在更一般的情况下展示这一点,我们还可以这样做:

fit2 = RLT(mtcars[,1:3], factor(rownames(mtcars)), model = "classification", ntrees = 1)

df <- RLT_tree(fit2)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

R:从决策树中提取规则 的相关文章

随机推荐

  • 使用 dotCover 时测试结果不一致

    我有一些带有单元测试的代码 这些代码在调试版本中通过 但在发布版本中失败 这是正确的 但是 当使用 JetBrains dotCover 运行时 相同的测试可以在调试和发布模式下通过 为了提供一些背景知识 这里是有问题的测试代码 只是为了让
  • iPhone应用程序允许背景音乐继续播放

    当我启动 iPhone 游戏时 一旦有声音播放 背景音乐或正在播放的播客就会停止 我注意到其他游戏允许背景音频继续播放 这怎么可能 我需要重写应用程序委托中的方法吗 将此行放入您的application didFinishLaunching
  • 为什么我不能从模板函数调用模板类的模板方法[重复]

    这个问题在这里已经有答案了 可能的重复 令人困惑的模板错误 https stackoverflow com questions 3786360 confusing template error 我有一个带有模板方法的模板类 现在我有另一个函
  • RabbitMQ 连接被拒绝 127.0.0.1:5672

    我正在准备一个简单的 ASP NET Core MVC Web 应用程序 我已经在我的笔记本电脑上安装了 RabbitMQ 服务器 RabbitMQ 管理 UI 正在运行localhost 15672 Rabbitmq 集群名称如下 ema
  • Rails 3 渲染视图没有动作

    我定义了特定操作的路线并创建了一个链接 我也创建了相应的视图 但没有代码定义控制器操作方法 单击链接后仍然会呈现视图 也就是说 视图是在实际不存在动作的情况下呈现的 有什么解释吗 是的 即使不存在相应的操作 视图也会被渲染 它会像为其定义的
  • .gitignore 不会忽略“git status”上带有空格的文件名

    当我做一个git status I get modified COM config Config Edit Project Settings lnk 但在我的 gitignore 中我有 lnk 这里发生了什么 会不会是空格的问题 问题不在
  • Cordova 的 FileTransfer 写入错误(代码 1)

    我正在使用 Android 版 Cordova 4 2 0 我有一些麻烦要解决文件传输插件 https github com apache cordova plugin file transfer好好工作 我认为有一个书写错误 except
  • 遗传算法中的轮盘赌选择。需要先对人口进行排序吗?

    在遗传算法中 当使用轮盘赌选择方法选择交叉成员时 是否需要首先按适应度等级对群体进行排序 可能性似乎是 首先按适应度升序对人口进行排序 按适应度降序对人口进行排序 不要对人口进行排序 让轮盘赌球落到它可能落下的地方 我认为无论哪种方式排序都
  • Xamarin.Android Intellisense 无法在 Visual Studio 2010 中工作

    我正在 Visual Studio 2010 中测试 Xamarin Android 并注意到在处理 Android 布局 axml 文件时没有 Android Intellisense 但是 当我处理 cs 文件时 我确实获得了 Andr
  • 在 Ionic 3 中定义模型的正确方法

    在 Ionic 3 中使用 getter 和 setter 定义模型的正确方法是什么 我跟着这个 export class ItemModel private name string constructor private n string
  • 封装 Ruby 函数

    我希望能够完全透明地包装任何 Ruby 过程 包括我自己没有编写源代码的过程 并记录其执行时间 my proc 也就是说 我想创建一个调用的过程my proc保存 上下文 接收者 论点 块 并打印出调用时的执行时间 例如 my proc p
  • 为什么 C# 中基类规范的含义不能递归地依赖于自身?

    以下 C 代码无法编译 public class A public interface B public class C A C B Error given here The type name B does not exist in th
  • WebResource.axd 和 ScriptResource.axd 加载时间超过 1 分钟

    我有个问题 有时 WebResource 和 ScriptResource 需要很长时间才能加载 超过 1 分钟 我们在一个集群中有多个节点 请注意 如果您在出现此问题之前打开了浏览器会话 则 axd 文件加载速度相当快 但新会话加载这些文
  • Spring数据休息@ManyToOne字段不以json形式出现

    我正在使用 Spring Boot Spring Data JPA 和 Spring Data Rest 技术开发一个 Web 项目 我能够成功设置所有内容并能够获取简单 POJO 的 JSON 我自定义了两个类以具有 OneToMany
  • 内联函数中 __LINE__ 的行为

    我有一个将行号和文件名传递给错误处理程序的宏 define SYSTEM FAILURE error code comment System Failure error code comment LINE FILE 如何将 LINE 在内联
  • Spring Rest API 和身份验证的自定义错误对象

    我有一个 Spring Boot Rest API 项目 我正在考虑如何更改从 Spring Boot 返回的默认错误对象 UseCase token api无需认证即可调用 其他api则通过传递token来调用 swagger UI 需要
  • 为什么我不应该混合使用制表符和空格?

    我经常读到 我不应该在 Haskell 中混合使用制表符和空格 或者我根本不应该使用制表符 为什么 问题是双重的 首先 Haskell 对缩进敏感 例如以下代码无效 example a b where a Hello b World 两个绑
  • 像表格一样格式化 Java 输出

    我试图以类似表格的格式输出有关我的程序存储的学生的信息 因为 t 并不总是提供正确的间距 为了做到这一点 我遇到了这个问题 https stackoverflow com a 2745239 1692226并尝试启用类似的解决方案 但是 当
  • 反应图不可见

    遵循projectstorm react diagrams中的安装指南docs https projectstorm gitbooks io react diagrams docs Getting 20Started html 我遇到图表无
  • R:从决策树中提取规则

    我正在使用 R 编程语言 最近 我读到了一种名为 强化学习树 RLT 的新决策树算法 据说它有可能将 更好 的决策树适合数据集 该库的文档可在此处找到 https cran r project org web packages RLT RL