In [111]: ar1 = np.ones((1196,14,62,1))
In [112]: ar2 = np.zeros((1196))
在 py3 中,zip 类似于生成器。你已经用list 扩展它以获得一个数组:
In [113]: np.array(zip(ar1,ar2))
Out[113]: array(<zip object at 0x7f188a465a48>, dtype=object)
这是一个单元素数组,形状为 ()。
如果你展开 zip,并创建一个数组,结果是一个对象 dtype 数组:
In [119]: A = np.array(list(zip(ar1,ar2)))
/usr/local/bin/ipython3:1: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray
#!/usr/bin/python3
In [120]: A.shape
Out[120]: (1196, 2)
In [121]: A.dtype
Out[121]: dtype('O')
In [122]: A[0,0].shape
Out[122]: (14, 62, 1)
In [123]: A[0,1].shape
Out[123]: ()
一列是 (14,62,1) 数组,另一列是 ar2 值。
另一个答案建议扩展ar2 以匹配ar1 的形状,并在最后一个轴上连接。结果是ar1 大小的两倍。 broadcast_to 进行“虚拟”复制(不增加内存),但这不会在连接中延续。您将获得每个 ar2 值的 14*62 个副本。
但是 ar2 可以广播到 (1196,1,62,1) 并在轴 1 上连接(62x 复制),或 (1196,14,1,1) 和轴 2 连接 (14x)。要连接,数组必须在除一个轴之外的所有轴上匹配。
但是对于savez 和load,您不需要连接数组。您可以单独保存和加载它们。 savez 将它们放在单独的 npy 文件中。 savez 展示了如何加载每个数组。
你可以单独洗牌。
做一个洗牌索引:
In [124]: idx = np.arange(1196)
In [125]: np.random.shuffle(idx)
In [126]: idx[:10]
Out[126]: array([ 561, 980, 42, 98, 1055, 375, 13, 771, 832, 787])
并将其应用于每个数组:
In [127]: ar1[idx,:,:,:];
In [128]: ar2[idx];