重塑数组时参数 (..., -1) 是什么意思?

What does the parameter (..., -1) mean when reshaping an array?

我遇到了以下语句:

train_set_x.reshape(train_set_x.shape[0], -1).T

train_set_xshape是:(209, 64, 64, 3)

因此我认为shape[0]209,而T是转置?

但是我无法理解上面的reshape声明?什么是 -1

非常感谢任何对此的澄清。

谢谢。

-1 将采用剩余维度并将它们展平为 1 维。因此,对于形状为 (209, 64, 64, 3) 的数组,调用:

arr.reshape(209, -1)

将产生形状为 (209, 12288) 或 (209, 64 * 64 * 3)

的矩阵
 >>> a = np.zeros([209, 64, 64, 3])
 >>> a.reshape(209, -1).shape
 (209, 12288)

如果您的代码用于 64 x 64 RGB 图像,您最终会把每张图像重新整形为一个长向量。

另外,请注意,数组将被重塑为的新形状中只能只有一个-1