【问题标题】:Extract the "levels" attribute from a "ctree" object into a Function that creates a Json object将“ctree”对象中的“levels”属性提取到创建 Json 对象的函数中
【发布时间】:2015-11-19 16:39:59
【问题描述】:

感谢以下问题: Converting toJson R object into a format that fits d3.js tree layout

我有一段很棒的代码可以为 d3.js 创建一个 Json 文件(见下文)

我知道如何从ctree 中提取分割点,如下所示:

library(party)
irisct <- ctree(Species ~ .,data = iris)

我只是不知道如何在下面的代码中实现它:

   #convert to Json that fits to d3.js layout####
get_ctree_parts <- function(x, ...)
{
  UseMethod("get_ctree_parts")
}

get_ctree_parts.BinaryTree <- function(x, ...)
{
  get_ctree_parts(attr(x, "tree"))
}

get_ctree_parts.SplittingNode <- function(x, ...)
{
  with(
    x,
    list(
      name = toString(nodeID),
      criteria=attr(psplit$splitpoint, "levels"),
      children   = list(get_ctree_parts(x$left),get_ctree_parts(x$right))

    )
  )
}

get_ctree_parts.TerminalNode <- function(x, ...)
{
  with(
    x,
    list(
      name     = paste(nodeID,
                       "weights",sum(weights),
                       "prediction",toString(paste("",toString(round(prediction,3)),"",sep=" ")),
                      # "criteria split",paste((attr(toString(psplit$splitpoint,levels)))), 
                       sep = " ")

    )
  )
}

toJSON(get_ctree_parts(irisct)) 

输出:

{"name":["1"],"criteria":{},"children":[{"name":["2 weights 50 prediction  1, 0, 0 "]},{"name":["3"],"criteria":{},"children":[{"name":["4"],"criteria":{},"children":[{"name":["5 weights 46 prediction  0, 0.978, 0.022 "]},{"name":["6 weights 8 prediction  0, 0.5, 0.5 "]}]},{"name":["7 weights 46 prediction  0, 0.022, 0.978 "]}]}]} 

请注意,“标准”留空:{},而我希望它们填充所有级别。

对此的任何帮助都会很棒!

【问题讨论】:

  • 我认为paste((attributes(ct@tree$right$psplit$splitpoint)$levels)) 与此处无关

标签: json r


【解决方案1】:

我最终通过夹板节点得到了信息:

#convert to Json that fits to d3.js layout####
get_ctree_parts <- function(x, ...)
{
  UseMethod("get_ctree_parts")
}

get_ctree_parts.BinaryTree <- function(x, ...)
{
  get_ctree_parts(attr(x, "tree"))
}

get_ctree_parts.SplittingNode <- function(x, ...)
{
  with(
    x,
    list(
      name = toString(nodeID),
      variableNames=toString(x$psplit$variableName),
      criteria=toString(attributes(x$psplit$splitpoint)),
     # split   = paste(attributes(get_ctree_parts(x$left$psplit$splitpoint)),attributes(get_ctree_parts(x$right$psplit$splitpoint))),
      children   = list(get_ctree_parts(x$left),get_ctree_parts(x$right))

    )
  )
}

get_ctree_parts.TerminalNode <- function(x, ...)
{
  with(
    x,
    list(
      name     = paste(nodeID,
                       "weights",sum(weights),
                       "prediction",toString(paste("",toString(round(prediction,3)),"",sep=" ")),
                      # "criteria split",paste((attr(toString(psplit$splitpoint,levels)))), 
                       sep = " ")

    )
  )
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-21
    • 2023-02-09
    • 2021-11-08
    • 2021-08-25
    • 2011-06-10
    • 2021-10-02
    • 2013-07-16
    相关资源
    最近更新 更多