This object wraps the predictions returned by a learner of class LearnerClassif, i.e. the predicted response and class probabilities.

If the response is not provided during construction, but class probabilities are, the response is calculated from the probabilities: the class label with the highest probability is chosen. In case of ties, a label is selected randomly.

## Note

If this object is constructed manually, make sure that the factor levels for truth have the same levels as the task, in the same order. In case of binary classification tasks, the positive class label must be the first level.

## Thresholding

If probabilities are stored, it is possible to change the threshold which determines the predicted class label. Usually, the label of the class with the highest predicted probability is selected. For binary classification problems, such an threshold defaults to 0.5. For cost-sensitive or imbalanced classification problems, manually adjusting the threshold can increase the predictive performance.

• For binary problems only a single threshold value can be set. If the probability exceeds the threshold, the positive class is predicted. If the probability equals the threshold, the label is selected randomly.

• For binary and multi-class problems, a named numeric vector of thresholds can be set. The length and names must correspond to the number of classes and class names, respectively. To determine the class label, the probabilities are divided by the threshold. This results in a ratio > 1 if the probability exceeds the threshold, and a ratio < 1 otherwise. Note that it is possible that either none or multiple ratios are greater than 1 at the same time. Anyway, the class label with maximum ratio is selected. In case of ties in the ratio, one of the tied class labels is selected randomly.

Note that there are the following edge cases for threshold equal to 0 which are handled specially:

1. With threshold 0 the resulting ratio gets Inf and thus gets always selected. If there are multiple ratios with value Inf, one is selected according to ties_method (randomly per default).

2. If additionally the predicted probability is also 0, the ratio 0/0 results in NaN values. These are simply replaced by 0 and thus will never get selected.

Other Prediction: PredictionRegr, Prediction

## Super class

mlr3::Prediction -> PredictionClassif

## Active bindings

response

(factor())

prob

(matrix())

confusion

(matrix())
Confusion matrix, as resulting from the comparison of truth and response. Truth is in columns, predicted response is in rows.

## Methods

### Public methods

Inherited methods

### Method new()

Creates a new instance of this R6 class.

#### Usage

PredictionClassif$new( task = NULL, row_ids = task$row_ids,

#### Arguments

threshold

(numeric()).

ties_method

(character(1))
One of "random", "first" or "last" (c.f. max.col()) to determine how to deal with tied probabilities.

Returns the object itself, but modified by reference. You need to explicitly $clone() the object beforehand if you want to keeps the object in its previous state. ## Examples task = tsk("iris") learner = lrn("classif.rpart", predict_type = "prob") learner$train(task)
p = learner$predict(task) p$predict_types
#>    row_id  truth response prob.setosa prob.versicolor prob.virginica
#> 1:      1 setosa   setosa           1               0              0
#> 2:      2 setosa   setosa           1               0              0
#> 3:      3 setosa   setosa           1               0              0
#> 4:      4 setosa   setosa           1               0              0
#> 5:      5 setosa   setosa           1               0              0
#> 6:      6 setosa   setosa           1               0              0
# confusion matrix
p$confusion #> truth #> response setosa versicolor virginica #> setosa 50 0 0 #> versicolor 0 49 5 #> virginica 0 1 45 # change threshold th = c(0.05, 0.9, 0.05) names(th) = task$class_names

# new predictions
p$set_threshold(th)$response
#>   [1] setosa    setosa    setosa    setosa    setosa    setosa    setosa
#>   [8] setosa    setosa    setosa    setosa    setosa    setosa    setosa
#>  [15] setosa    setosa    setosa    setosa    setosa    setosa    setosa
#>  [22] setosa    setosa    setosa    setosa    setosa    setosa    setosa
#>  [29] setosa    setosa    setosa    setosa    setosa    setosa    setosa
#>  [36] setosa    setosa    setosa    setosa    setosa    setosa    setosa
#>  [43] setosa    setosa    setosa    setosa    setosa    setosa    setosa
#>  [50] setosa    virginica virginica virginica virginica virginica virginica
#>  [57] virginica virginica virginica virginica virginica virginica virginica
#>  [64] virginica virginica virginica virginica virginica virginica virginica
#>  [71] virginica virginica virginica virginica virginica virginica virginica
#>  [78] virginica virginica virginica virginica virginica virginica virginica
#>  [85] virginica virginica virginica virginica virginica virginica virginica
#>  [92] virginica virginica virginica virginica virginica virginica virginica
#>  [99] virginica virginica virginica virginica virginica virginica virginica
#> [106] virginica virginica virginica virginica virginica virginica virginica
#> [113] virginica virginica virginica virginica virginica virginica virginica
#> [120] virginica virginica virginica virginica virginica virginica virginica
#> [127] virginica virginica virginica virginica virginica virginica virginica
#> [134] virginica virginica virginica virginica virginica virginica virginica
#> [141] virginica virginica virginica virginica virginica virginica virginica
#> [148] virginica virginica virginica
#> Levels: setosa versicolor virginicap\$score(measures = msr("classif.ce"))
#> classif.ce
#>  0.3333333