Skip to contents

Compute a confusion matrix for categorical or binary predictions

Usage

confusion_matrix(truth, predicted, levels = NULL)

Arguments

truth

factor or character vector of true classes.

predicted

factor or character vector of predicted classes.

levels

character vector of class levels (default: union of truth and predicted levels).

Value

A list with:

table

Confusion matrix (rows = truth, columns = predicted).

accuracy

Overall accuracy.

per_class

Data.frame with per-class precision, recall, F1.

Examples

confusion_matrix(
  truth = c("a", "a", "b", "b"),
  predicted = c("a", "b", "b", "b")
)
#> $table
#>      Predicted
#> Truth a b
#>     a 1 1
#>     b 0 2
#> 
#> $accuracy
#> [1] 0.75
#> 
#> $per_class
#>   class tp precision recall        f1
#> 1     a  1 1.0000000    0.5 0.6666667
#> 2     b  2 0.6666667    1.0 0.8000000
#>