【发布时间】:2021-04-26 21:49:56
【问题描述】:
我一直在使用Rstudio 中的caret 包中的gbm 来查找发生故障的概率。
我使用 Youden 的 J 来找到最佳分类的阈值,即 0.63。我现在如何使用这个阈值?我认为最好的方法是以某种方式将阈值合并到caret 中的gbm 模型中以获得更准确的预测,然后再次在训练数据上重新运行模型?目前它默认为 0.5,我找不到更新阈值的明显方法。
或者,阈值是否仅用于将测试数据预测分成正确的类别?这似乎更直接,但是假设应该根据新阈值更新概率,那么我如何反映 ROC_AUC 图中的变化?
我们将不胜感激地接受任何帮助。谢谢
编辑:我正在处理的完整代码如下:
library(datasets)
library(caret)
library(MLeval)
library(dplyr)
data(iris)
data <- as.data.frame(iris)
# create class
data$class <- ifelse(data$Species == "setosa", "yes", "no")
# split into train and test
train <- data %>% sample_frac(.70)
test <- data %>% sample_frac(.30)
# Set up control function for training
ctrl <- trainControl(method = "cv",
number = 5,
returnResamp = 'none',
summaryFunction = twoClassSummary,
classProbs = T,
savePredictions = T,
verboseIter = F)
# Set up trainng grid - this is based on a hyper-parameter tune that was recently done
gbmGrid <- expand.grid(interaction.depth = 10,
n.trees = 20000,
shrinkage = 0.01,
n.minobsinnode = 4)
# Build a standard classifier using a gradient boosted machine
set.seed(5627)
gbm_iris <- train(class ~ .,
data = train,
method = "gbm",
metric = "ROC",
tuneGrid = gbmGrid,
verbose = FALSE,
trControl = ctrl)
# Calcuate best thresholds
caret::thresholder(gbm_iris, threshold = seq(.01,0.99, by = 0.01), final = TRUE, statistics = "all")
pred <- predict(gbm_iris, newdata = test, type = "prob")
roc <- evalm(data.frame(pred, test$class))
【问题讨论】:
-
您是如何找到最佳阈值的?
-
我通过使用
caretsthresholder函数找到了我的最佳阈值如下:thres <- caret::thresholder(gbm, threshold = seq(.01,0.99, by = 0.01), final = TRUE, statistics = "all")。由此我使用了 Youden 的 J,它给了我 0.63 的值,这给了我最好的 FPR,但也降低了 TPR。 -
请参阅stackoverflow.com/questions/65814703/…。如果有不清楚的地方,请重写问题以包含可重现的示例,我会尽力回答。
-
感谢误用,我已更新帖子以包含可重现的代码。您提供的链接真的很有帮助,据我所知,插入符号不支持从 0.5 更改模型阈值。我可以在 R 中使用哪个包来帮助更改
gbm模型的阈值?改变模型中的阈值是最好的方法吗?谢谢。 -
您的代码的问题是
All_train.rds在 SO 上我们无法访问。您能否发布一个带有内置数据集的可重现示例。您可以通过预测概率并手动设置阈值来更改插入符号中的预测阈值。