Set threshold of prediction object for classification or multilabel classification.
Creates corresponding discrete class response for the newly set threshold.
For binary classification: The positive class is predicted if the probability value exceeds the threshold.
For multiclass: Probabilities are divided by corresponding thresholds and the class with maximum resulting value is selected.
The result of both are equivalent if in the multi-threshold case the values are greater than 0 and sum to 1.
For multilabel classification: A label is predicted (with entry TRUE
) if a probability matrix entry
exceeds the threshold of the corresponding label.
Arguments
- pred
(Prediction)
Prediction object.- threshold
(numeric)
Threshold to produce class labels. Has to be a named vector, where names correspond to class labels. Only for binary classification it can be a single numerical threshold for the positive class.
Value
(Prediction) with changed threshold and corresponding response.
Examples
# create task and train learner (LDA)
task = makeClassifTask(data = iris, target = "Species")
lrn = makeLearner("classif.lda", predict.type = "prob")
mod = train(lrn, task)
#> Error in x[0, , drop = FALSE]: incorrect number of dimensions
# predict probabilities and compute performance
pred = predict(mod, newdata = iris)
#> Error in predict(mod, newdata = iris): object 'mod' not found
performance(pred, measures = mmce)
#> Error in performance(pred, measures = mmce): object 'pred' not found
head(as.data.frame(pred))
#> Error in as.data.frame(pred): object 'pred' not found
# adjust threshold and predict probabilities again
threshold = c(setosa = 0.4, versicolor = 0.3, virginica = 0.3)
pred = setThreshold(pred, threshold = threshold)
#> Error in checkClass(x, classes, ordered, null.ok): object 'pred' not found
performance(pred, measures = mmce)
#> Error in performance(pred, measures = mmce): object 'pred' not found
head(as.data.frame(pred))
#> Error in as.data.frame(pred): object 'pred' not found