I have a numpy array (an image) called a with this size:
[3,128,192]
now i want create a numpy array that contains n copies of a which will have this dimension:
[n,3,128,192]
exist a numpy function that can help me with this problem without using loop instructions?
Simply use np.stack
# say you need 10 copies of a 3D array `a`
In [267]: n = 10
In [266]: np.stack([a]*n)
Alternatively, you should use np.concatenate
if you're really concerned about the performance.
In [285]: np.concatenate([a[np.newaxis, :, :]]*n)
Example:
In [268]: a
Out[268]:
array([[[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[12, 13, 14, 15]],
[[16, 17, 18, 19],
[20, 21, 22, 23],
[24, 25, 26, 27],
[28, 29, 30, 31]],
[[32, 33, 34, 35],
[36, 37, 38, 39],
[40, 41, 42, 43],
[44, 45, 46, 47]]])
In [271]: a.shape
Out[271]: (3, 4, 4)
In [269]: n = 10
In [270]: np.stack([a]*n).shape
Out[270]: (10, 3, 4, 4)
In [285]: np.concatenate([a[np.newaxis, :, :]]*n).shape
Out[285]: (10, 3, 4, 4)
Performance:
# ~ 4x faster than using `np.stack`
In [292]: %timeit np.concatenate([a[np.newaxis, :, :]]*n)
100000 loops, best of 3: 10.7 µs per loop
In [293]: %timeit np.stack([a]*n)
10000 loops, best of 3: 41.1 µs per loop
Approach #1 : One way would be with np.repeat
to repeat along the first axis after extending the input array to have one more dimension, a singleton one with None/np.newaxis
-
np.repeat(a[None],n,axis=0)
Sample runs -
1) 2D case :
In [209]: a
Out[209]:
array([[7, 8, 0, 1],
[5, 0, 1, 0],
[4, 3, 0, 1]])
In [210]: np.repeat(a[None],2,axis=0)
Out[210]:
array([[[7, 8, 0, 1],
[5, 0, 1, 0],
[4, 3, 0, 1]],
[[7, 8, 0, 1],
[5, 0, 1, 0],
[4, 3, 0, 1]]])
2) 3D case :
In [214]: a
Out[214]:
array([[[7, 2, 4, 2],
[6, 7, 7, 6],
[6, 8, 2, 1]],
[[1, 5, 8, 5],
[8, 0, 6, 4],
[1, 2, 8, 8]]])
In [215]: np.repeat(a[None],2,axis=0)
Out[215]:
array([[[[7, 2, 4, 2],
[6, 7, 7, 6],
[6, 8, 2, 1]],
[[1, 5, 8, 5],
[8, 0, 6, 4],
[1, 2, 8, 8]]],
[[[7, 2, 4, 2],
[6, 7, 7, 6],
[6, 8, 2, 1]],
[[1, 5, 8, 5],
[8, 0, 6, 4],
[1, 2, 8, 8]]]])
Approach #2 : If you don't mind a read-only version of the input array, we could use np.broadcast_to
-
np.broadcast_to(a, (n,) + a.shape)
Approach #3 : If you don't mind a view into the input array, here's one with NumPy strides -
def strided_repeat_newaxis0(a, n):
s0,s1,s2 = a.strides
shp = (n,) + a.shape
return np.lib.index_tricks.as_strided(a, shape=shp, strides=(0,s0,s1,s2))
Runtime test
In [290]: a = np.random.randint(0,9,(3,128,192))
In [291]: %timeit np.repeat(a[None],100,axis=0)
100 loops, best of 3: 6.15 ms per loop
In [292]: %timeit strided_repeat_newaxis0(a, 100)
100000 loops, best of 3: 4.69 µs per loop
In [293]: %timeit np.broadcast_to(a, (n,) + a.shape)
100000 loops, best of 3: 3.03 µs per loop
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With