我正在尝试使用 mlr 包运行模型,但我在使用 predict() 函数时遇到了一些问题。它给了我以下错误消息:

Error in predict(mod, task = task, subset = test) :
Assertion on 'subset' failed: Must be of type 'integerish', not 'data.frame'

请在下面找到一个可重现的示例:
require(mlr)     # models
require(caTools) # sampling
require(Zelig)   # data

data("voteincome")
voteincome$vote <- as.factor(voteincome$vote)

set.seed(0)
sample <- sample.split(voteincome, SplitRatio = .75)
train <- subset(voteincome, sample == TRUE)
test <- subset(voteincome, sample == FALSE)

train <- na.omit(train)
test <- na.omit(test)

task <- makeClassifTask(data = train, target = "vote")
lrnr <- makeLearner("classif.randomForest")
mod <- train(lrnr, task)
pred <- predict(mod, task = task, subset = test)

然后出现错误。难道我做错了什么?谢谢!

最佳答案

mlr 期望将索引向量传递给 subset 参数。然后它会自动对数据帧进行子集化,因此您不必自己执行此操作。您还可以使用 mlr 使用重采样描述自动将训练集和测试集分区(请参阅 the tutorial ):

require(mlr)     # models
require(caTools) # sampling
require(Zelig)   # data

data("voteincome")
voteincome$vote <- as.factor(voteincome$vote)

set.seed(0)
task <- makeClassifTask(data = voteincome, target = "vote")
lrnr <- makeLearner("classif.randomForest")
rdesc <- makeResampleDesc("Holdout", split = 0.75)

res <- resample(learner = lrnr, task = task, resampling = rdesc)

# get predictions on test set
getPredictionResponse(res$pred)

# compute accuracy, also see https://mlr-org.github.io/mlr-tutorial/devel/html/performance/index.html
performance(res$pred, acc)

关于r - 在 mlr 包中运行 predict() 函数时出错,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34378914/

10-12 18:02