gpt4 book ai didi

r - Caret 包中的用户定义指标

转载 作者:行者123 更新时间:2023-12-03 23:16:36 32 4
gpt4 key购买 nike

我希望使用带有不是默认选项之一的指标的插入符号包。对于下面的示例,我使用了 Metrics 包。我已经阅读了 StackOverflow 上的所有相关问题以及 caret 网站上的指南,但仍然收到错误消息。

在下面的示例中,我希望使用平均绝对误差。

创建一个函数:

maefunction<-function(data, lev=NULL, model=NULL){
require(Metrics)
MAE<-mae(data[, "obs"], data[, "pred"])
out<-c(MAE)
out
}

现在我将函数插入 trainControl
library(caret)
GBM<-train(train$result~., data=train, method="gbm", trControl=trainControl(summaryFunction=maefunction), metric=MAE)

我收到以下消息
Error in list_to_dataframe(res, attr(.data, "split_labels"), .id, id_as_factor) : 
Results must be all atomic, or all data frames
In addition: Warning messages:
1: In if (metric %in% c("Accuracy", "Kappa")) stop(paste("Metric", :
the condition has length > 1 and only the first element will be used
2: In if (metric == "ROC" & !ctrl$classProbs) stop("train()'s use of ROC codes requires class probabilities. See the classProbs option of trainControl()") :
the condition has length > 1 and only the first element will be used
3: In if (!(metric %in% perfNames)) { :
the condition has length > 1 and only the first element will be used
4: In train.default(x, y, weights = w, ...) :
The metric "4" was not in the result set. will be used instead.The metric "0.5" was not in the result set. will be used instead.

最佳答案

我认为您必须使用命名向量(请参见下面的示例)。我没有在文档中明确说明这一点,因此我将更新该部分。

最大限度

library(mlbench)
data(BostonHousing)

maeSummary <- function (data,
lev = NULL,
model = NULL) {
out <- mae(data$obs, data$pred)
names(out) <- "MAE"
out
}

mControl <- trainControl(summaryFunction = maeSummary)
marsGrid <- expand.grid(degree = 1, nprune = (1:10) * 2)

set.seed(1)
earthFit <- train(medv ~ .,
data = BostonHousing,
"earth",
tuneGrid = marsGrid,
metric = "MAE",
maximize = FALSE,
trControl = mControl)

关于r - Caret 包中的用户定义指标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22434850/

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