gpt4 book ai didi

r - 运行mlr包中的predict()函数时出错

转载 作者:行者123 更新时间:2023-12-01 19:31:48 26 4
gpt4 key购买 nike

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com