ano*_*n01 76 python arrays numpy
我想将一个numpy 2D数组复制到第三维.例如,如果我有这样的(2D)numpy数组:
import numpy as np
arr = np.array([[1,2],[1,2]])
# arr.shape = (2, 2)
Run Code Online (Sandbox Code Playgroud)
将它转换为一个3D矩阵,在新的维度中包含N个这样的副本,如N = 3:
new_arr = np.array([[[1,2],[1,2]],[[1,2],[1,2]],[[1,2],[1,2]]])
# new_arr.shape = (3, 2, 2)
Run Code Online (Sandbox Code Playgroud)
ali*_*i_m 108
可能最干净的方法是使用np.repeat:
a = np.array([[1, 2], [1, 2]])
print(a.shape)
# (2, 2)
# indexing with np.newaxis inserts a new 3rd dimension, which we then repeat the
# array along, (you can achieve the same effect by indexing with None, see below)
b = np.repeat(a[:, :, np.newaxis], 3, axis=2)
print(b.shape)
# (2, 2, 3)
print(b[:, :, 0])
# [[1 2]
# [1 2]]
print(b[:, :, 1])
# [[1 2]
# [1 2]]
print(b[:, :, 2])
# [[1 2]
# [1 2]]
Run Code Online (Sandbox Code Playgroud)
话虽如此,您通常可以通过使用广播来完全避免重复阵列.例如,假设我想添加一个(3,)向量:
c = np.array([1, 2, 3])
Run Code Online (Sandbox Code Playgroud)
到a.我可以a在第三维中复制3次内容,然后c在第一维和第二维中复制两次内容,这样我的两个数组都可以(2, 2, 3)计算它们的总和.但是,这样做更简单快捷:
d = a[..., None] + c[None, None, :]
Run Code Online (Sandbox Code Playgroud)
这里,a[..., None]有形状(2, 2, 1)和c[None, None, :]形状(1, 1, 3)*.当我计算总和时,结果沿着大小为1的尺寸"广播"出来,给我一个形状的结果(2, 2, 3):
print(d.shape)
# (2, 2, 3)
print(d[..., 0]) # a + c[0]
# [[2 3]
# [2 3]]
print(d[..., 1]) # a + c[1]
# [[3 4]
# [3 4]]
print(d[..., 2]) # a + c[2]
# [[4 5]
# [4 5]]
Run Code Online (Sandbox Code Playgroud)
广播是一种非常强大的技术,因为它避免了在内存中创建输入数组的重复副本所涉及的额外开销.
*虽然为了清楚起见我将它们包括在内,但None索引c并不是必需的 - 你也可以这样做a[..., None] + c,即(2, 2, 1)对(3,)数组广播一个数组.这是因为如果其中一个阵列的尺寸小于另一个阵列,则只需要兼容两个阵列的尾随尺寸.举一个更复杂的例子:
a = np.ones((6, 1, 4, 3, 1)) # 6 x 1 x 4 x 3 x 1
b = np.ones((5, 1, 3, 2)) # 5 x 1 x 3 x 2
result = a + b # 6 x 5 x 4 x 3 x 2
Run Code Online (Sandbox Code Playgroud)
ray*_*ica 22
另一种方法是使用numpy.dstack.假设您想重复矩阵a num_repeats时间:
import numpy as np
b = np.dstack([a]*num_repeats)
Run Code Online (Sandbox Code Playgroud)
诀窍是将矩阵包装a到单个元素的列表中,然后使用*运算符复制此列表中的元素num_repeats次数.
例如,如果:
a = np.array([[1, 2], [1, 2]])
num_repeats = 5
Run Code Online (Sandbox Code Playgroud)
这[1 2; 1 2]在第三维中重复5次.验证(在IPython中):
In [110]: import numpy as np
In [111]: num_repeats = 5
In [112]: a = np.array([[1, 2], [1, 2]])
In [113]: b = np.dstack([a]*num_repeats)
In [114]: b[:,:,0]
Out[114]:
array([[1, 2],
[1, 2]])
In [115]: b[:,:,1]
Out[115]:
array([[1, 2],
[1, 2]])
In [116]: b[:,:,2]
Out[116]:
array([[1, 2],
[1, 2]])
In [117]: b[:,:,3]
Out[117]:
array([[1, 2],
[1, 2]])
In [118]: b[:,:,4]
Out[118]:
array([[1, 2],
[1, 2]])
In [119]: b.shape
Out[119]: (2, 2, 5)
Run Code Online (Sandbox Code Playgroud)
最后我们可以看到矩阵的形状,2 x 2在第三维中有5个切片.
n-dim数组扩展为n+1-dim在NumPy中1.10.0引入的,我们可以利用它numpy.broadcast_to来简单地生成输入数组的3D视图2D。好处将是没有额外的内存开销和几乎免费的运行时。这在数组很大且我们可以使用视图的情况下至关重要。同样,这将适用于一般n-dim情况。
我会用单词stack代替copy,因为读者可能会将它与创建内存副本的数组的副本混淆。
沿第一轴堆叠
如果我们要arr沿第一个轴堆叠输入,则np.broadcast_to创建3D视图的解决方案是-
np.broadcast_to(arr,(3,)+arr.shape) # N = 3 here
Run Code Online (Sandbox Code Playgroud)
沿第三个/最后一个轴堆叠
要arr沿第三轴堆叠输入,创建3D视图的解决方案是-
np.broadcast_to(arr[...,None],arr.shape+(3,))
Run Code Online (Sandbox Code Playgroud)
如果我们确实需要一个内存副本,则可以随时在此追加.copy()。因此,解决方案将是-
np.broadcast_to(arr,(3,)+arr.shape).copy()
np.broadcast_to(arr[...,None],arr.shape+(3,)).copy()
Run Code Online (Sandbox Code Playgroud)
这是两种情况下堆叠的工作方式,并显示了样品箱的形状信息-
# Create a sample input array of shape (4,5)
In [55]: arr = np.random.rand(4,5)
# Stack along first axis
In [56]: np.broadcast_to(arr,(3,)+arr.shape).shape
Out[56]: (3, 4, 5)
# Stack along third axis
In [57]: np.broadcast_to(arr[...,None],arr.shape+(3,)).shape
Out[57]: (4, 5, 3)
Run Code Online (Sandbox Code Playgroud)
相同的解决方案可以用来扩展n-dim输入以n+1-dim沿第一个轴和最后一个轴查看输出。让我们探讨一些更暗淡的情况-
3D输入盒:
In [58]: arr = np.random.rand(4,5,6)
# Stack along first axis
In [59]: np.broadcast_to(arr,(3,)+arr.shape).shape
Out[59]: (3, 4, 5, 6)
# Stack along last axis
In [60]: np.broadcast_to(arr[...,None],arr.shape+(3,)).shape
Out[60]: (4, 5, 6, 3)
Run Code Online (Sandbox Code Playgroud)
4D输入盒:
In [61]: arr = np.random.rand(4,5,6,7)
# Stack along first axis
In [62]: np.broadcast_to(arr,(3,)+arr.shape).shape
Out[62]: (3, 4, 5, 6, 7)
# Stack along last axis
In [63]: np.broadcast_to(arr[...,None],arr.shape+(3,)).shape
Out[63]: (4, 5, 6, 7, 3)
Run Code Online (Sandbox Code Playgroud)
等等。
让我们使用一个大样本示例2D,获取时间并验证输出是否为view。
# Sample input array
In [19]: arr = np.random.rand(1000,1000)
Run Code Online (Sandbox Code Playgroud)
让我们证明所提出的解决方案确实是一种观点。我们将使用沿第一轴的堆叠(沿第三轴的堆叠结果非常相似)-
In [22]: np.shares_memory(arr, np.broadcast_to(arr,(3,)+arr.shape))
Out[22]: True
Run Code Online (Sandbox Code Playgroud)
让我们获取表明它实际上是免费的时间-
In [20]: %timeit np.broadcast_to(arr,(3,)+arr.shape)
100000 loops, best of 3: 3.56 µs per loop
In [21]: %timeit np.broadcast_to(arr,(3000,)+arr.shape)
100000 loops, best of 3: 3.51 µs per loop
Run Code Online (Sandbox Code Playgroud)
N从视图上看,在时序上从增大3到3000不变,而在时序单位上两者都可以忽略不计。因此,在内存和性能上都是高效的!
这现在也可以使用np.tile 实现,如下所示:
import numpy as np
a = np.array([[1,2],[1,2]])
b = np.tile(a,(3, 1,1))
b.shape
(3,2,2)
b
array([[[1, 2],
[1, 2]],
[[1, 2],
[1, 2]],
[[1, 2],
[1, 2]]])
Run Code Online (Sandbox Code Playgroud)