我正在研究 LSTM。

输出是分类的。

其格式为 [[t11,t12,t13],[t21,t22,t23]

我能够为一维数组做到这一点,但我发现很难为二维数组做到这一点。

from keras.utils import to_categorical
print(to_categorical([[9,10,11],[10,11,12]]))

输出
[[ 0.  0.  0.  0.  0.  0.  0.  0.  0.  1.  0.  0.  0.]
[ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  1.  0.  0.]
[ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  1.  0.]
[ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  1.  0.  0.]
[ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  1.  0.]
[ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  1.]]

有两个不同的输入,每个输入都有 3 个时间步长,但在输出中它们全部组合在一起。

我需要它,
[[[ 0.  0.  0.  0.  0.  0.  0.  0.  0.  1.  0.  0.  0.]
[ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  1.  0.  0.]
[ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  1.  0.]],

[[ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  1.  0.  0.]
[ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  1.  0.]
[ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  1.]]]

最佳答案

如果形状很奇怪,请尝试将其设为 1D,使用该函数,然后将其重新整形:

originalShape = myData.shape
totalFeatures = myData.max() + 1

categorical = myData.reshape((-1,))
categorical = to_categorical(categorical)
categorical = categorical.reshape(originalShape + (totalFeatures,))

关于machine-learning - 如何使用 to_categorical 将 [[4,7,10],[10,20,30]] 转换为一种热编码,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51271145/

10-12 21:59