onehotdecode - Decode probability vectors into class labels - MATLAB (original) (raw)
Decode probability vectors into class labels
Since R2020b
Syntax
Description
[A](#mw%5F3029da7b-0865-4c80-bb6b-0796c75c1924) = onehotdecode([B](#mw%5F99008232-0873-4fab-8d2f-88c0d91a95a0),[classes](#mw%5F9aec1d5e-4db6-48d4-9cf6-e4ad450157c0),[featureDim](#mw%5F65dc0b1d-0564-4083-8f99-5c56b81906ab))
decodes each probability vector in B
to the most probable class label from the labels specified by classes
. featureDim
specifies the dimension along which the probability vectors are defined. The function decodes the probability vectors into class labels by matching the position of the highest value in the vector with the class label in the corresponding position in classes
. Each probability vector in A
is replaced with the value of classes
that corresponds to the highest value in the probability vector.
[A](#mw%5F3029da7b-0865-4c80-bb6b-0796c75c1924) = onehotdecode([B](#mw%5F99008232-0873-4fab-8d2f-88c0d91a95a0),[classes](#mw%5F9aec1d5e-4db6-48d4-9cf6-e4ad450157c0),[featureDim](#mw%5F65dc0b1d-0564-4083-8f99-5c56b81906ab),[typename](#mw%5Fec2f55e5-2bdd-43c0-937e-dcba0b738623))
decodes each probability vector in B
to the most probable class label and returns the result with data type typename
. Use this syntax to obtain decoded class labels with a specific data type.
Examples
Use the onehotencode and onehotdecode
functions to encode a set of labels into probability vectors and decode them back into labels.
Create a vector of categorical labels.
colorsOriginal = ["red" "blue" "red" "green" "yellow" "blue"]; colorsOriginal = categorical(colorsOriginal)
colorsOriginal = 1×6 categorical red blue red green yellow blue
Determine the classes in the categorical vector.
classes = categories(colorsOriginal);
One-hot encode the labels into probability vectors by using the onehotencode
function. Encode the probability vectors into the first dimension.
colorsEncoded = onehotencode(colorsOriginal,1)
colorsEncoded = 4×6
0 1 0 0 0 1
0 0 0 1 0 0
1 0 1 0 0 0
0 0 0 0 1 0
Use onehotdecode
to decode the probability vectors.
colorsDecoded = onehotdecode(colorsEncoded,classes,1)
colorsDecoded = 1×6 categorical red blue red green yellow blue
The decoded labels match the original labels.
Use onehotdecode to decode a set of probability vectors into the most probable class for each observation.
Create a set of 10 random probability vectors. The vectors express the probability that an observation belongs to one of five classes.
numObs = 10; numClasses = 5;
prob = rand(numObs,numClasses);
tot = sum(prob,2); prob = prob./tot;
Define the set of five classes.
classes = ["Red" "Yellow" "Green" "Blue" "Purple"];
Decode the probabilities into the most probable classes. The probability vectors are encoded into the second dimension, so specify the dimension containing encoded probabilities as 2
. Obtain the most probable classes as a vector of strings.
result = onehotdecode(prob,classes,2,"string")
result = 10×1 string "Red" "Yellow" "Yellow" "Green" "Yellow" "Blue" "Green" "Yellow" "Red" "Red"
Input Arguments
Probability vectors to decode, specified as a numeric array.
Values in B
must be between 0
and1
. If a probability vector in B
containsNaN
values, the function decodes that observation to the class with the largest probability that is not NaN
. If an observation contains only NaN
values, the function decodes that observation to the first class label in classes.
Data Types: single
| double
Classes, specified as a cell array of character vectors, a string vector, a numeric vector, or a two-dimensional character array.
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
| string
| cell
| char
Dimension containing probability vectors, specified as a positive integer.
Use featureDim
to specify the dimension in B that contains the probability vectors. The function replaces each vector inB
along the specified dimension with the element ofclasses in the same position as the highest value along the vector.
The dimension of B
specified by featureDim
must have length equal to the number of classes specified by classes
.
Data type of decoded labels, specified as a character vector or a string scalar.
Valid values of typename
are 'categorical'
,'string'
, and numeric types such as 'single'
and 'int64'
. If you specify a numeric type,classes
must be a numeric vector.
Example: 'double'
Data Types: char
| string
Output Arguments
Decoded class labels, returned as a categorical array, a string array, or a numeric array.
Version History
Introduced in R2020b