【问题标题】:How to do decision trees in R? [closed]如何在 R 中做决策树? [关闭]
【发布时间】:2015-10-18 22:19:05
【问题描述】:

我通常在 SPSS 中做决策树以从 DDBB 获取目标,我做了一些研究,发现有三个包:tree、party 和 rpart 可用于 R,但哪个更适合该任务?

谢谢!

【问题讨论】:

  • 我以前用过party,挺好用的。这是我的一个项目中使用示例的链接:github.com/fiedukow/SejMOWaKlasyfikacja/blob/master/…
  • 我已将此问题标记为已关闭。我认为这主要是基于意见,因为哪个更好可能取决于几个因素,甚至是个人对编程风格的品味。

标签: r rstudio rpart party


【解决方案1】:

我以前用过rpart,很方便。我已经通过拆分训练集和测试集来进行预测建模。这是代码。希望这会给你一些想法......

 library(rpart)
    library(rattle)
    library(rpart.plot)
    ### Build the training/validate/test...

data(iris)
nobs <- nrow(iris) 
train <- sample(nrow(iris), 0.7*nobs)
test <- setdiff(seq_len(nrow(iris)), train)
colnames(iris)


### The following variable selections have been noted.
input <- c("Sepal.Length","Sepal.Width","Petal.Length","Petal.Width")
numeric <- c("Sepal.Length","Sepal.Width","Petal.Length","Petal.Width")
categoric <- NULL
target  <-"Species"
risk    <- NULL
ident   <- NULL
ignore  <- NULL
weights <- NULL

#set.seed(500)
# Build the Decision Tree model.
rpart <- rpart(Species~.,
    data=iris[train, ],
    method="class",
    parms=list(split="information"),
      control=rpart.control(minsplit=12,
        usesurrogate=0, 
        maxsurrogate=0))

# Generate a textual view of the Decision Tree model.
print(rpart)
printcp(rpart)

# Decision Tree Plot...
prp(rpart)
dev.new()
fancyRpartPlot(rpart, main="Decision Tree Graph")

【讨论】:

    猜你喜欢
    • 2017-10-23
    • 1970-01-01
    • 2016-05-18
    • 2014-08-21
    • 2014-05-09
    • 2016-08-16
    • 1970-01-01
    • 2012-06-29
    • 2016-04-09
    相关资源
    最近更新 更多