【发布时间】:2019-01-24 05:31:51
【问题描述】:
我有一个这样的 numpy 数组:
[[0. 1. 1. ... 0. 0. 1.]
[0. 0. 0. ... 0. 0. 1.]
[0. 0. 1. ... 0. 0. 0.]
...
[0. 0. 0. ... 0. 0. 1.]
[0. 0. 0. ... 0. 0. 1.]
[0. 0. 0. ... 1. 0. 1.]]
我这样改造它以减少内存需求:
x_val = x_val.astype(np.int)
导致:
[[0 1 1 ... 0 0 1]
[0 0 0 ... 0 0 1]
[0 0 1 ... 0 0 0]
...
[0 0 0 ... 0 0 1]
[0 0 0 ... 0 0 1]
[0 0 0 ... 1 0 1]]
但是,当我这样做时:
x_val = to_categorical(x_val)
我明白了:
in to_categorical
categorical = np.zeros((n, num_classes), dtype=np.float32)
MemoryError
任何想法为什么?最终,numpy 数组包含二进制分类问题的标签。到目前为止,我已经将它用作float32,就像在 Keras ANN 中一样,它运行良好,并且我取得了相当不错的性能。那么真的有必要运行to_categorical吗?
【问题讨论】:
-
如果是二分类问题,标签应该是零和一;但是你的标签对我来说似乎很奇怪!例如,第一行,即
[0. 1. 1. ... 0. 0. 1.],在解释为标签时对应于什么?如果要执行二进制分类,标签的形状应为(n_samples,)。我猜你正在做多标签分类,即每个样本可能有多个标签。在这种情况下,标签是正确的,您根本不需要使用to_categorical。 -
你想用 to_categorical 实现什么?对我来说,这里看起来错位了。
-
@today 抱歉,我可能在这里遗漏了一些东西,但不是 0. = 0 => Class1 和 1. = 1 > Class2。后一类有待预测...
-
@Digital-Thinking 不确定我理解。它是否像在多类问题中那样放错地方了,我会使用 categorical_crossentropy 作为损失函数,而在二元分类问题中我使用 binary_crossentropy(就像我目前所做的那样)?因此 to_categorical 对她没有意义,因为这是二进制的?
-
to_categorical 如果您有 multicalss 标签,它确实有意义,这些标签存储在一个单一的值中。如果你有像 (0,1,2,3,n) 这样的类,它们会被单热编码,并且你会得到每个不同值的维度(每个值的 n 维数组)。
标签: python numpy machine-learning keras classification