[英]Copy a 2D array to make it 3D
Suppose that I have a 2D Numpy array, A
. 假设我有一个2D Numpy数组,
A
。
I want to build a 3D array B
with depth of 100 such that for every i
such that 0 <= i < 100
, we have B[:,:,i] == A
. 我想构建一个深度为100的3D数组
B
,这样对于每个i
, 0 <= i < 100
,我们有B[:,:,i] == A
Is there any efficient way to do this in Python/Numpy? 有没有有效的方法在Python / Numpy中执行此操作?
Just make a zero 3D array of your desired shape, and add your A
to it 只需制作所需形状的零3D阵列,然后将
A
添加到其中
In [13]:
A = np.array([[1,2,3],[4,5,6]])
In [14]:
C = np.zeros(shape=(A.shape[0], A.shape[1], 100), dtype=A.dtype))
In [15]:
B = C+A[...,...,np.newaxis]
In [16]:
B[:,:,1]
Out[16]:
array([[ 1, 2, 3],
[ 4, 5, 6]])
In [17]:
B[:,:,2]
Out[17]:
array([[ 1, 2, 3],
[ 4, 5, 6]])
It is not going to be 100 copies of A
, (and I doubt if you can ever make it so), because B
has to be a contiguous memory block by itself. 它不会是
A
100份副本(我怀疑你能不能这样做),因为B
本身必须是一个连续的内存块。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.