3

我正在尝试使用该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)

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

4

2 回答 2

2

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

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)
于 2015-12-20T20:30:53.303 回答
1

尝试这个:

pred <- predict(mod$learner.model, task = task, subset = test) 
于 2015-12-20T08:31:55.210 回答