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 labels (default: sorted union of labels observed in truth and predicted). If you supply this, include every label you want counted. Any truth or prediction label not listed is treated as missing; that pair is omitted from the confusion table and the denominator used to calculate accuracy.

Value

A list with:

table

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

accuracy

Overall accuracy.

per_class

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

Details

Pairs with a missing truth or prediction are omitted from the confusion table and accuracy denominator. If no complete pairs remain, accuracy is NaN (undefined). Per-class precision or recall is NA when its denominator is zero. F1 is NA when precision or recall is NA, or when both are zero.

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
#>