【问题标题】:how to build a tree in r?如何在 r 中构建一棵树?
【发布时间】:2021-02-17 05:35:45
【问题描述】:

我正在处理学生表现数据集,我收到此消息错误

trafo 中的错误(数据 = 数据,numeric_trafo = numeric_trafo,factor_trafo = factor_trafo,: 不支持数据类“字符” 另外:警告信息: 在 storage.mode(RET@predict_trafo)

从这段代码,我不知道为什么?

set.seed(1)
ind <- sample(2,nrow(d2),replace = TRUE ,prob = c(0.7,0.3))
trainData <- d2[ind==1,]
testData <- d2[ind==2,]
library(party)
myFormula <- higher~G1+G2+G3
d2_ctree <- ctree(myFormula, data=trainData)
table(predict(d2_ctree),trainData$higher)

【问题讨论】:

  • 很遗憾您没有提供 d2 的数据。从错误中应该清楚,当它应该是因子(最可能)或数字(整数/双精度)时,您有一些变量作为字符......许多算法/函数不适用于字符,但它们在转换时工作这些列的因素...希望这会有所帮助
  • 您没有提供 d2 object 。但是,我认为这个例子可以帮助你:rpubs.com/njvijay/14899

标签: r classification ctree


【解决方案1】:

虹膜数据示例(因为您没有提供d2 数据):

library(party)
set.seed(1234) #To get reproducible result
ind <- sample(2,nrow(iris), replace=TRUE, prob=c(0.7,0.3))
trainData <- iris[ind==1,]
testData <- iris[ind==2,]
myFormula <- Species ~ Sepal.Length + Sepal.Width + Petal.Length + Petal.Width
d2_ctree <- ctree(myFormula, data=trainData)
table(predict(d2_ctree),trainData$Species)
plot(d2_ctree)

来源:source

【讨论】:

    【解决方案2】:

    您应该转换所有 nonnumerical列到数字。

    d2$higher = factor(d2$higher,levels = c("Label1","Label2","Label3"), 
                        labels = c(1, 2, 3))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-04-29
      • 2014-11-01
      • 2023-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-19
      相关资源
      最近更新 更多