scikit-learn 无法为 table 创建一个热点

scikit-learn could not create one hot for a table

我有一个数据集,其中每一列都有共同的类别。

dataset = [['aaaa', 'bbbb'], ['bbbb', 'ffff'], ['aaaa', 'gggg']]
categories = ['aaaa', 'bbbb', 'ffff', 'gggg']

我希望输出必须是:

output = [[1, 1, 0, 0], [0, 1, 1, 0], [1, 0, 0, 1]]

代码:

one_hot_encoder = OneHotEncoder(categories=['aaaa', 'bbbb', 'ffff', 'gggg'])
return one_hot_encoder.fit_transform([['aaaa', 'bbbb'], ['bbbb', 'ffff'], ['aaaa', 'gggg']])

但是告诉我这个错误:

Shape mismatch: if categories is an array, it has to be of shape (n_features,).

您想要的输出不是单热编码; “one-hot”表示每行一个 1(“hot”)。您可能想改用 MultiLabelBinarizer

解释你的错误:sklearn 认为你的输入有两个特征(数组的两列),如果你向编码器提供 categories 它必须是列表的列表:对于每一列,类别列表。