Decode probability vectors into class labels
decodes probability vectors in
A = onehotdecode(
B to the most probable class label from
the labels specified by
specifies the dimension along which the probability vectors are defined. The probability
vectors are decoded into class labels by matching the position of the highest value in the
vector with the class label in the corresponding position in
Each probability vector in
A is replaced with the value of
classes that corresponds to the highest value in the probability
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, using the
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
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.
onehotdecode to decode a set of probability
vectors into the most probable class for each observation.
Create a set of ten 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
2. Obtain the most probable classes as a vector of
result = onehotdecode(prob,classes,2,"string")
result = 10×1 string "Green" "Blue" "Red" "Green" "Red" "Blue" "Purple" "Green" "Yellow" "Blue"
B— Probability vectors
Probability vectors to decode, specified as a numeric array.
B must be between
1. If a probability vector in
NaN values, then that observation is decoded to the class that has
the largest probability that is not
NaN. If an observation contains
NaN values, then that observation is decoded to the first class
Classes, specified as a cell array of character vectors, a string vector, a numeric vector, or a two-dimensional char array.
featureDim— Dimension containing probability vectors
Dimension containing probability vectors, specified as a positive integer.
featureDim to specify the dimension in
B that contains the probability vectors. Each vector in
B along the specified dimension is replaced by the element of
classes in the same position as the highest value along the
The dimension of
B specified by
must have length equal to the number of classes specified by
typename— Data type of decoded labels
'categorical'(default) | character vector | string scalar
Data type of decoded labels, specified as a character vector or a string scalar.
Valid values of
'string' and numeric types such as
'int64'. If you specify a numeric type,
must be a numeric vector.
A— Decoded class labels
Decoded class labels, returned as a categorical array, a string array, or a numeric array.