gpt4 book ai didi

arrays - Matlab One Hot Encoding - 将带有分类的列转换为多个逻辑列

转载 作者:太空宇宙 更新时间:2023-11-03 20:23:20 25 4
gpt4 key购买 nike

语境

我有大量带有分类的列,所有列都有不同的、不可排序的选择。为了让我的分析更轻松,我想将它们中的每一个都带走并将其转换为具有逻辑的多个列。例如:

1   GENRE
2 Pop
3 Classical
4 Jazz

...会变成...

1   Pop Classical Jazz
2 1 0 0
3 0 1 0
4 0 0 1

问题

我试过使用 ind2vec 但这只适用于数字或逻辑。我也遇到过 this但我不确定它是否适用于分类。在这种情况下正确使用的功能是什么?

最佳答案

如果你想从 categorical 转换矢量到 logical array , 您可以使用 unique函数生成列索引,然后使用 any of the options from this related question 执行编码:

% Sample data:
data = categorical({'Pop'; 'Classical'; 'Jazz'; 'Pop'; 'Pop'; 'Jazz'});

% Get unique categories and create indices:
[genre, ~, index] = unique(data)

genre =

Classical
Jazz
Pop


index =

3
1
2
3
3
2

% Create logical matrix:
mat = logical(accumarray([(1:numel(index)).' index], 1))

mat =

6×3 logical array

0 0 1
1 0 0
0 1 0
0 0 1
0 0 1
0 1 0

关于arrays - Matlab One Hot Encoding - 将带有分类的列转换为多个逻辑列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47666477/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com