【问题标题】:to_categorical returns 2 column matrixto_categorical 返回 2 列矩阵
【发布时间】:2017-11-28 16:14:35
【问题描述】:

我使用 keras 中提供的 to_categorical 函数将浮点类型的 numpy ndarray 转换为其二进制对应物。 Y 的尺寸为 2144x1,但函数返回的数组的尺寸为 2144x2。 如何从 to_categorical 获取 2144x1 数组? Y 仅由 0 和 1 组成(FLOAT TYPE) 函数调用:

y_binary = to_categorical(Y)

【问题讨论】:

  • 如果您已经有了 0 和 1,为什么不直接使用 Y.astype(bool) 将类型从浮点型转换为布尔型?

标签: python numpy keras python-3.5


【解决方案1】:

keras.utils.to_categorical(y, num_classes=None)

将类向量(整数)转换为二进制类矩阵。

但它看起来不支持浮点数(它对所有数字都做底)

to_categorical([0, 0.1, 0.2, 0.3, 1])
[[ 1.  0.]
 [ 1.  0.]
 [ 1.  0.]
 [ 1.  0.]
 [ 0.  1.]]


to_categorical([0, 1, 2, 3, 1])
[[ 1.  0.  0.  0.]
 [ 0.  1.  0.  0.]
 [ 0.  0.  1.  0.]
 [ 0.  0.  0.  1.]
 [ 0.  1.  0.  0.]]

一种解决方案是将其放大为正整数并转换为分类。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-09-08
    • 1970-01-01
    • 2014-09-18
    • 2012-11-03
    • 2021-05-11
    • 2022-01-23
    • 2018-02-17
    相关资源
    最近更新 更多