Not possible to one hot encode in a timetable column
3 ビュー (過去 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:
参考
カテゴリ
Help Center および File Exchange で Tables についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!