onehot_with_decoder {listarrays} | R Documentation |
Convert vector to a onehot representation (binary class matrix)
Description
Convert vector to a onehot representation (binary class matrix)
Usage
onehot_with_decoder(y, order = NULL, named = TRUE)
onehot(y, order = NULL, named = TRUE)
decode_onehot(
Y,
classes = colnames(Y),
n_classes = ncol(Y) %||% length(classes)
)
onehot_decoder(Y, classes = colnames(Y), n_classes = length(classes))
Arguments
y |
character, factor, or numeric vector |
order |
|
named |
if the returned matrix should have column names |
Y |
a matrix, as returned by |
classes |
A character vector of class names in the order corresponding
to |
n_classes |
The total number of classes expected in |
Value
A binary class matrix
See Also
Examples
if(require(zeallot)) {
y <- letters[1:4]
c(Y, decode) %<-% onehot_with_decoder(y)
Y
decode(Y)
identical(y, decode(Y))
decode(Y[2,,drop = TRUE])
decode(Y[2,,drop = FALSE])
decode(Y[2:3,])
rm(Y, decode)
}
# more peicemeal functions
Y <- onehot(y)
decode_onehot(Y)
# if you need to decode a matrix that lost colnames,
# make your own decoder that remembers classes
my_decode <- onehot_decoder(Y)
colnames(Y) <- NULL
my_decode(Y)
decode_onehot(Y)
# factor and numeric vectors also accepted
onehot(factor(letters[1:4]))
onehot(4:8)