【发布时间】:2019-07-06 00:27:19
【问题描述】:
我正在使用 mlr 包训练朴素贝叶斯模型。
我想调整分类的阈值(仅阈值)。 tutorial 提供了这样做的示例,同时还在嵌套的 CV 设置中进行了额外的超参数调整。 我实际上不想在找到最佳阈值时调整任何其他(超)参数。
基于here 的讨论,我设置了一个 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
【问题讨论】:
标签: machine-learning classification threshold mlr