【问题标题】:Store terminal node split rule of ctree into a dataframe in R将ctree的终端节点拆分规则存储到R中的数据帧中
【发布时间】:2015-04-22 16:57:55
【问题描述】:

我有一个数据集 (Node1),其中包含 6 个品牌,并且在应用 ctree() 时,它被分成 2 个终端节点。 Node2包含4个品牌 Node3 包含 2 个品牌。 我想提取每个终端节点的这些品牌并将它们存储在两个不同的数据框中。请建议如何做同样的事情。

这是我编写的代码:

    library("party")
    library(gridExtra)
    fileName <- "C\\"
    data <- read.csv(paste(fileName, ".csv", sep=""), header=TRUE)
    pdd <- subset(data, select=c(col1,col2))

    pdd_ctree <- ctree(col1~col2, data=pdd, controls =  ctree_control(minsplit=30))

    print(pdd_ctree@tree$psplit$splitpoint[1:6])
    print(pdd_ctree@tree$psplit$splitpoint)

我得到的结果是:

    print(pdd_ctree@tree$psplit$splitpoint[1:6])
    [1] 1 0 1 1 0 1

    print(pdd_ctree@tree$psplit$splitpoint)
    [1] 1 0 1 1 0 1
    attr(,"levels")
    [1] "Brand01"            "Brand02"                "Brand03"          "Brand04"             
    [5] "Brand05" "Brand06"  

我的要求:

有 2 个数据框 left.df 和 right.df

left.df 将包含 [Brand01,Brand03,Brand04,Brand06]

right.df 将包含 [Brand02,Brand05]

【问题讨论】:

    标签: r decision-tree party


    【解决方案1】:

    我想你只是想要:

    lvls <- levels(pdd_ctree@tree$psplit$splitpoint)
    left.df = lvls[pdd_ctree@tree$psplit$splitpoint == 1]
    right.df = lvls[pdd_ctree@tree$psplit$splitpoint == 0]
    

    【讨论】:

    • 谢谢!!你让我开心:)
    猜你喜欢
    • 2021-07-17
    • 2015-07-12
    • 2016-02-09
    • 2015-08-19
    • 2015-09-09
    • 2014-02-21
    • 2017-06-17
    • 2012-12-07
    • 2023-03-31
    相关资源
    最近更新 更多