Not possible to one hot encode in a timetable column
조회 수: 5 (최근 30일)
이전 댓글 표시
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
답변 (1개)
Ayush Modi
2024년 6월 19일
편집: Ayush Modi
2024년 6월 19일
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:
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!