onehotdecode
Description
decodes each probability vector in A
= onehotdecode(B
,classes
,featureDim
)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.
Examples
One-Hot Encode and Decode Labels
Encode a set of labels into probability vectors, and then decode them back into labels.
Create a vector of categorical labels specifying color types.
colorsOriginal = ["red","blue","red","green","yellow","blue"]; colorsOriginal = categorical(colorsOriginal)
colorsOriginal = 1x6 categorical
red blue red green yellow blue
Determine the classes in the categorical vector.
classes = categories(colorsOriginal)
classes = 4x1 cell
{'blue' }
{'green' }
{'red' }
{'yellow'}
One-hot encode the labels into probability vectors by using the onehotencode
function. Encode the labels into the first dimension, so that each row corresponds to a class and each column corresponds to a probability vector.
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
Decode the probability vectors by using the onehotdecode
function.
colorsDecoded = onehotdecode(colorsEncoded,classes,1)
colorsDecoded = 1x6 categorical
red blue red green yellow blue
The decoded labels match the original labels.
One-Hot Decode Dummy Variables
Create dummy variables, and then decode them back into the original data.
Create a column vector of categorical data specifying color types.
colorsOriginal = ["red";"blue";"red";"green";"yellow";"blue"]; colorsOriginal = categorical(colorsOriginal)
colorsOriginal = 6x1 categorical
red
blue
red
green
yellow
blue
Determine the classes in the categorical vector.
classes = categories(colorsOriginal);
Create dummy variables for each color type by using the dummyvar
function.
dummyColors = dummyvar(colorsOriginal)
dummyColors = 6×4
0 0 1 0
1 0 0 0
0 0 1 0
0 1 0 0
0 0 0 1
1 0 0 0
Decode the dummy variables in the second dimension by using the onehotdecode
function.
colorsDecoded = onehotdecode(dummyColors,classes,2)
colorsDecoded = 6x1 categorical
red
blue
red
green
yellow
blue
The decoded variables match the original color types.
Decode Probability Vectors into Most Probable Classes
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
prob = 10×5
0.2938 0.0568 0.2365 0.2546 0.1582
0.3895 0.4174 0.0154 0.0137 0.1641
0.0427 0.3217 0.2854 0.0931 0.2573
0.2878 0.1529 0.2943 0.0145 0.2505
0.2640 0.3341 0.2834 0.0405 0.0780
0.0422 0.0614 0.3280 0.3564 0.2120
0.1078 0.1632 0.2876 0.2689 0.1725
0.1940 0.3249 0.1392 0.1125 0.2293
0.2356 0.1949 0.1613 0.2338 0.1745
0.3345 0.3326 0.0593 0.0119 0.2616
Define the set of five classes.
classes = ["Red","Yellow","Green","Blue","Purple"];
Decode the probabilities into the most probable classes by using the onehotdecode
function. The probability vectors are encoded into the second dimension (each column corresponds to a unique class), so specify the dimension containing encoded probabilities as 2
. Obtain the most probable classes as a string vector.
result = onehotdecode(prob,classes,2,"string")
result = 10x1 string
"Red"
"Yellow"
"Yellow"
"Green"
"Yellow"
"Blue"
"Green"
"Yellow"
"Red"
"Red"
One-Hot Decode Classification Scores
Decode predicted class scores into predicted labels.
Load the fisheriris
data set. Create X
as a numeric matrix that contains four measurements for 150 irises. Create S
as a vector of categorical labels that contains the corresponding iris species.
load fisheriris
X = meas;
S = categorical(species);
One-hot encode the labels into probability vectors by using the onehotencode
function. Encode the probability vectors into the second dimension.
Y = onehotencode(S,2);
Compute the fitted coefficients of a simple linear classifier.
B = X\Y
B = 4×3
0.0834 0.2117 -0.1481
0.2533 -0.3059 0.1412
-0.2270 0.1888 0.0181
-0.0635 -0.5749 0.5873
Predict the class scores from the fitted coefficients, and ensure that the scores are in the range [0,1].
scores = X*B; scores = min(1,max(0,scores));
Decode the predicted class scores into predicted labels by using the onehotdecode
function. Then, create a confusion chart to compare the true labels S
with the predicted labels label
.
label = onehotdecode(scores,categories(S),2); confusionchart(S,label)
Input Arguments
B
— Probability vectors
numeric array
Probability vectors to decode, specified as a numeric array.
Values in B
must be between 0
and
1
. If a probability vector in B
contains
NaN
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
— Classes
cell array | string vector | numeric vector | character array
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
featureDim
— Dimension containing probability vectors
positive integer
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 in
B
along the specified dimension with the element of
classes
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
.
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 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
A
— Decoded class labels
categorical array (default) | string array | numeric array
Decoded class labels, returned as a categorical array, a string array, or a numeric array.
Version History
Introduced in R2021b
See Also
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)