Numpy反向keras to_categorical

时间:2017-11-19 18:47:36

标签: numpy keras

在keras中,我使用$('.products').each(function() { let $product = $(this); // Do something with $product }); 将二进制nx1向量y转换为nx2矩阵,其中如果y = 1则第一列为1,第二列为y = 0。如何使用numpy来反转此操作?

4 个答案:

答案 0 :(得分:5)

添加到MazeRunner09的答案。如果您使用keras中的to_categorical,您将拥有一个列表,并且可以对整个单热编码列表使用列表解析:

y_classes = [np.argmax(y, axis=None, out=None) for y in y_test]

答案 1 :(得分:4)

简单。

numpy.argmax(a, axis=None, out=None)

这将返回沿轴的最大值索引。

答案 2 :(得分:1)

这是如何在 tensorflow keras 中反转标签的完整示例:

label = [0,1,1,0]
label = tf.keras.utils.to_categorical(label)
print(label) #output: label = [[1,0],[0,1],[0,1],[1,0]]
label = tf.math.argmax(label, axis=1)
print(label) #output back to [0,1,1,0]

答案 3 :(得分:0)

无需执行列表理解。只是

numpy.argmax(a, axis=1)

将在所有行的每一行中找到argmax