我试图用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
通过重新采样描述自动将数据划分为训练集和测试集(请参阅教程):
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)
试试这个:
pred <- predict(mod$learner.model, task = task, subset = test)