将ctree输出转换为JSON格式(用于D3树布局) [英] Converting ctree output into JSON Format (for D3 tree layout)

查看:164
本文介绍了将ctree输出转换为JSON格式(用于D3树布局)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个需要运行 ctree 的项目,然后以交互模式绘制 - 像'D3.js'树布局,我的主要障碍是将 ctree 输出转换为 json 格式,以供以后通过javascript使用。

I'm working on a project that requires to run a ctree and then plot it in interactive mode - like the 'D3.js' tree layout, my main obstacle is to convert the ctree output into a json format, to later use by javascript.

以下是我需要的(例如来自iris数据):

Following is what i need (with example from the iris data):

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

     Conditional inference tree with 4 terminal nodes

Response:  Species 
Inputs:  Sepal.Length, Sepal.Width, Petal.Length, Petal.Width 
Number of observations:  150 

1) Petal.Length <= 1.9; criterion = 1, statistic = 140.264
  2)*  weights = 50 
1) Petal.Length > 1.9
  3) Petal.Width <= 1.7; criterion = 1, statistic = 67.894
    4) Petal.Length <= 4.8; criterion = 0.999, statistic = 13.865
      5)*  weights = 46 
    4) Petal.Length > 4.8
      6)*  weights = 8 
  3) Petal.Width > 1.7
    7)*  weights = 46 



现在我要转换 ctee 使用一些算法输出到下面的JSON格式(我手动),但这可能不是最好的方式来转换:

Now i want to convert the ctee output into the following JSON format using some algorithm (i did it manually), though, this is probably not the best way to convert it:

{"name" : "Petal.Length <= 1.9  criterion = 1","value": 60, "children" : [
            {"name" : "n=50" ,"value": 60},
            {"name" : "Petal.Length > 1.9 criterion = 1","value": 60, "children": [
                  {"name" : "n=46","value": 60 },
                  {"name" : "Petal.Length > 4.8","value": 60, "children" :[
            {"name" : "Petal.Width > 1.7" ,"value": 60},
            {"name" : "46" ,"value": 60}
    ]}] }
      ]}

这里有两个R和 D3的图片。 js 图示:


我已经尝试在ctree对象上使用 RJSONIO ,这没有什么帮助。

i already tried using RJSONIO on the ctree object, that didn't help much.

任何人都转换一个ctree对象/输出为JSON使用D3.js树布局?

Has anyone ever converted a ctree object/output into JSON for the use of D3.js tree layout? if not, does anyone have any idea of an algorithm that can convert one output to the other?

预先感谢任何帮助!

类似这样:

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(
      nodeID       = nodeID,
      variableName = psplit$variableName,
      splitPoint   = psplit$splitpoint,
      pValue       = 1 - round(criterion$maxcriterion, 3),
      statistic    = round(max(criterion$statistic), 3),
      left         = get_ctree_parts(x$left),
      right        = get_ctree_parts(x$right)
    )
  )
}

get_ctree_parts.TerminalNode <- function(x, ...)
{
  with(
    x,
    list(
      nodeID     = nodeID,
      weights    = sum(weights),
      prediction = prediction
    )
  )
}

useful_bits_of_irisct <- get_ctree_parts(irisct)
toJSON(useful_bits_of_irisct)

unclass 函数。例如:

unclass(irisct)
unclass(attr(irisct, "tree"))
unclass(attr(irisct, "tree")$psplit)

code> party ::: print.SplittingNode 和 party ::: print.TerminalNode 也非常有用。 ( party ::: print。和自动填充以查看可用的内容。)

The print methods in the package, party:::print.SplittingNode and party:::print.TerminalNode were also very useful. (Type party:::print. and autocomplete to see what is available.)

这篇关于将ctree输出转换为JSON格式(用于D3树布局)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆