调整mlr中的分类阈值



我正在使用mlr包训练Naive Bayes模型。

我想调整分类的阈值(并且阈值)。本教程提供了一个执行此操作的示例,同时还在嵌套CV设置中执行额外的超参数调整我实际上不想在找到最佳阈值的同时调整任何其他(超)参数

基于这里的讨论,我设置了一个makeTuneWrapper()对象,并将另一个参数(laplace)设置为固定值(1),然后在嵌套CV设置中运行resample()。

nbayes.lrn <- makeLearner("classif.naiveBayes", predict.type = "prob")
nbayes.lrn
nbayes.pst <- makeParamSet(makeDiscreteParam("laplace", value = 1))
nbayes.tcg <- makeTuneControlGrid(tune.threshold = TRUE)
# Inner 
rsmp.cv5.desc<-makeResampleDesc("CV", iters=5, stratify=TRUE)
nbayes.lrn<- makeTuneWrapper(nbayes.lrn, par.set=nbayes.pst, control=nbayes.tcg, resampling=rsmp.cv5.desc, measures=tpr) 
# Outer 
rsmp.cv10.desc<-makeResampleDesc("CV", iters=10, stratify=TRUE)
nbayes.res<-resample(nbayes.lrn, beispiel3.tsk, resampling= rsmp.cv10.desc, measures=list(tpr,ppv), extract=getTuneResult)
print(nbayes.res$extract)

为嵌套CV中的内部循环设置重新采样方案似乎是多余的。对tuneThreshold()的内部调用显然进行了更彻底的优化。但是,在没有重新采样方案的情况下调用makeTuneWrapper()会导致错误消息。

我有两个特定的问题

1.)有没有一种更简单的方法来调整阈值(并且只调整阈值)?

2.)给定上面使用的设置:我如何访问实际测试的阈值?

编辑:

这将是一个代码示例,用于根据@Lars Kotthoff的回答调整不同测量(准确度、灵敏度、精度)的阈值。

### Create fake data
y<-c(rep(0,500), rep(1,500))
x<-c(rep(0, 300), rep(1,200), rep(0,100), rep(1,400))
balanced.df<-data.frame(y=y, x=x)
balanced.df$y<-as.factor(balanced.df$y)
balanced.df$x<-as.factor(balanced.df$x)
balanced.tsk<-makeClassifTask(data=balanced.df, target="y",   positive="1")
summarizeColumns(balanced.tsk)
### TuneThreshold
logreg.lrn<-makeLearner("classif.logreg", predict.type="prob")
logreg.mod<-train(logreg.lrn, balanced.tsk)
logreg.preds<-predict(logreg.mod, balanced.tsk)
threshold_tpr<-tuneThreshold(logreg.preds, measure=list(tpr))
threshold_tpr
threshold_acc<-tuneThreshold(logreg.preds, measure=list(acc))
threshold_acc
threshold_ppv<-tuneThreshold(logreg.preds, measure=list(ppv))
threshold_ppv

您可以直接使用tuneThreshold():

require(mlr)
iris.model = train(makeLearner("classif.naiveBayes", predict.type = "prob"), iris.task)
iris.preds = predict(iris.model, iris.task)
res = tuneThreshold(iris.preds)

不幸的是,您无法访问在使用tuneThreshold()时测试的阈值。然而,您可以将阈值视为"正常"超参数,并使用mlr中的任何调整方法。这将允许您获得值和相应的性能。

最新更新