Not possible to one hot encode in a timetable column
2 views (last 30 days)
Show older comments
I am trying to do a simple one hot encoding in one of the column of a time table but it's becoming an impossible task.
I have tried to convert that column to a table and even to an array buty nothing works.
months = timetable2table(df_tmp(:, {'month'}), 'ConvertRowTimes', false);
nehotencode(months);
The error states: "First argument must contain a single label for each observation."
It shouldn't be that difficult but I do not manage to convert a single column to a one hot enconding.
Thanks in advance,
Rafa
0 Comments
Answers (1)
Ayush Modi
on 19 Jun 2024
Edited: Ayush Modi
on 19 Jun 2024
Hi Rafael,
I am assuming you are referring to onehotencode function in MATLAB.
The error is because onehotencode function expects a table of categorical data. Otherwise, a class for each variable needs to be provided to the function.
Here is the example code to demonstrate how to achieve the same with the categorical data:
% I am using sample data for the example.
dt = datetime(2021, 1, 1) + calmonths(0:11); % Monthly data for one year
months = {'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'};
df_table = timetable(dt', months', 'VariableNames', {'Months'});
disp(df_table);
% Converting the data into categorical data
categoricalData = categorical(df_table.Months)
% Converting to a table
tblA = table(categoricalData)
encodedData = onehotencode(tblA);
disp(encodedData);
Refer to the following MathWorks documentation for more information on:
See Also
Categories
Find more on Cell Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!