Skip to contents

Represent existing class decisions as a one-hot matrix with one column per class. The helper converts the decision a predictor has already made; it never chooses a class from probabilities.

Usage

venn_abers_one_hot(x)

Arguments

x

A class decision: the factor returned by multiclass predict(..., type = "class") or by venn_abers_decode()$class; the logical vector returned by binary predict(..., type = "class"); or a data frame with a factor .pred_class column, as returned by workflow, Grid, parsnip and frozen predict(..., type = "class"). Other columns of a data frame are ignored.

Value

A base matrix of type double with values 0 and 1, one row per element of x in the same order and no row names. Column names are the factor levels in their stored order, or "FALSE" and "TRUE" for a logical vector, so the binary event is the TRUE column. Every class keeps its column, also when no row predicts it. Zero rows give a 0 x K matrix and one row a 1 x K matrix; the result is never reduced to a vector. A missing decision gives a row of NA.

Details

The ones follow the existing tie rules: a binary probability of exactly 0.5 is the event, and a multiclass tie keeps the first stored level, also with exactly two classes. The Python reference facade breaks a binary tie towards the first sorted class instead.

Ordered factors only set the column order. Schema-encoded .pred_class columns keep their encoded classes as column names; pass venn_abers_decode(prediction, schema)$class to obtain original class names. Probability matrices or tables, numeric or character vectors, data frames without a factor .pred_class, and factors without levels are errors.

Examples

source(system.file("examples", "one-hot-classes.R", package = "vennabers"))
#>      probability FALSE TRUE
#> [1,]   0.4736842     1    0
#> [2,]   0.5000000     0    1
#> [3,]   0.5263158     0    1
#>      cat dog bird
#> [1,]   1   0    0
#> [2,]   0   1    0
#> [3,]   1   0    0
#>      cat dog bird
#> [1,]   0   0    1
#> [2,]  NA  NA   NA
#> [3,]   1   0    0