%% Embed Categorical Data
% Embed a mini-batch of categorical features.
% Create an array of categorical features containing 5 observations with values "Male" or "Female".

X = categorical(["Male" "Female" "Male" "Female" "Female"])';

% Initialize the embedding weights. Specify an embedding dimension of 10, and a vocabulary corresponding to the number of categories of the input data plus one.

embeddingDimension = 10;
vocabularySize = numel(categories(X));
weights = rand(embeddingDimension,vocabularySize+1);

% To embed the categorical data, first convert it to mini-batch of numeric indices.

X = double(X)

% For formatted dlarray input, the embed function expands into a singleton 'C' (channel) dimension with size 1. Create a formatted dlarray object containing the data. To specify that the rows correspond to observations, specify the format 'BC' (batch, channel).

dlX = dlarray(X,'BC')

% Embed the numeric indices using the embed function. The embed function expands into the 'C' dimension.

dlY = embed(dlX,weights)

% In this case, the output is an embeddingDimension-by-N matrix with format 'CB' (channel, batch), where N is the number of observations. Each column contains the embedding vectors.
% Copyright 2020 The MathWorks, Inc.