Main Content

onehotdecode

Decode probability vectors into class labels

    Description

    example

    A= onehotdecode(B,classes,featureDim)decodes each probability vector inBto the most probable class label from the labels specified byclasses.featureDim指定的尺寸以及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 inclasses. Each probability vector inAis replaced with the value ofclassesthat corresponds to the highest value in the probability vector.

    example

    A= onehotdecode(B,classes,featureDim,typename)decodes each probability vector inBto the most probable class label and returns the result with data typetypename. Use this syntax to obtain decoded class labels with a specific data type.

    Examples

    collapse all

    Use theonehotencodeandonehotdecodefunctions 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 =1x6 categoricalred 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 theonehotencode函数。编码probability vectors into the first dimension.

    colorsEncoded = onehotencode(colorsOriginal,1)
    colorsEncoded =4×60 1 0 0 0 1 0 0 0 1 0 0 1 0 1 0 0 0 0 0 0 0 1 0

    Useonehotdecodeto decode the probability vectors.

    colorsDecoded = onehotdecode(colorsEncoded,classes,1)
    colorsDecoded =1x6 categoricalred 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 as2. Obtain the most probable classes as a vector of strings.

    result = onehotdecode(prob,classes,2,"string")
    result =10x1 string"Red" "Yellow" "Yellow" "Green" "Yellow" "Blue" "Green" "Yellow" "Red" "Red"

    Input Arguments

    collapse all

    Probability vectors to decode, specified as a numeric array.

    Values inBmust be between0and1. If a probability vector inBcontainsNaNvalues, the function decodes that observation to the class with the largest probability that is notNaN. If an observation contains onlyNaNvalues, the function decodes that observation to the first class label inclasses.

    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.

    UsefeatureDimto specify the dimension inBthat contains the probability vectors. The function replaces each vector inBalong the specified dimension with the element ofclassesin the same position as the highest value along the vector.

    The dimension ofBspecified byfeatureDimmust have length equal to the number of classes specified byclasses.

    Data type of decoded labels, specified as a character vector or a string scalar.

    Valid values oftypenameare'categorical','string', and numeric types such as'single'and'int64'. If you specify a numeric type,classesmust be a numeric vector.

    Example:'double'

    Data Types:char|string

    Output Arguments

    collapse all

    Decoded class labels, returned as a categorical array, a string array, or a numeric array.

    Introduced in R2020b