gpt4 book ai didi

javascript - 将 toJson R 对象转换为适合 d3.js 树布局的格式

转载 作者:行者123 更新时间:2023-11-30 12:11:18 24 4
gpt4 key购买 nike

这是后续问题:Converting ctree output into JSON Format (for D3 tree layout)

我有以下代码,使用 ctree 生成(来自 Party,请参阅下面生成它的代码),我的目的是创建一个递归函数来转换以下 Json 输出:

    {"nodeID":[1],"left":
{"nodeID":[2],"weights":[50],"prediction":[1,0,0]},
"right":
{"nodeID":[3],"left":{
"nodeID":[4],"left":
{"nodeID":[5],"weights":[46],"prediction":[0,0.9783,0.0217]},"right":
{"nodeID":[6],"weights":[8],"prediction":[0,0.5,0.5]}},"right":
{"nodeID":[7],"weights":[46],"prediction":[0,0.0217,0.9783]}}}

进入以下:

      { "name": "1", "children": [
{
"name": "3","children": [
{
"name": "4","children":[
{"name":"5 weights:[46] prediction:[0,0.9783,0.0217]"},
{"name":"6 weights:[8] prediction[0,0.5,0.5]}"
]
}
{{"nodeID":"7 weights:[46]prediction[0,0.0217,0.9783]"}
}
]
},
{
"name": "2 weights:[50] prediction[1,0,0]",
}
]
}

这让我可以创建以下精彩的 d3.js 输出:

ctree, converted into d3.js json

背景:

我有以下创建 Json 输出的代码,但没有退出我需要的代码:

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

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,
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
)
)
}

toJSON(get_ctree_parts(irisct))

我试图对其进行改造,但似乎我遇到了一些挑战,我无法解决:为了得到一个通用的解决方案,我需要一些递归函数,它创建一个嵌套的 Json 对象,只要有一个拆分( child ),而且,ctree 与 d3.js 导航约定有不同的导航约定,在 ctree 中导航使用“右”和“左”累积,其中 d3.js 需要“子级”导航

任何对此的帮助都会很棒,我已经坚持了一段时间 :)

最佳答案

嗨,我在看到帖子后才开始 R 编码,这是我到目前为止取得的成就..

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

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),
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),"prediaction",toString(paste("[",toString(prediction),"]",sep=" ")),sep = " ")

)
)
}

toJSON(get_ctree_parts(irisct))

Json 输出:

{"name":["1"],"children":[
{"name":["2 weights 50 prediaction [ 1, 0, 0 ]"]},
{"name":["3"],"children":[
{"name":["4"],"children":[
{"name":["5 weights 46 prediaction [ 0, 0.978260869565217, 0.0217391304347826 ]"]},
{"name":["6 weights 8 prediaction [ 0, 0.5, 0.5 ]"]}]
},
{"name":["7 weights 46 prediaction [ 0, 0.0217391304347826, 0.978260869565217 ]"]
}]
}]
}

如果您有任何问题,我会尽力而为。

关于javascript - 将 toJson R 对象转换为适合 d3.js 树布局的格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33730123/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com