Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reshape 4D numpy array into 3D

I have a numpy array with the following dimensions - (256, 128, 4, 200) - basically the first two can form an image, the third is channels and the fourth is frames ("time instances"). How can I reshape the array so the frames are "stacked" one after the other, in other words the array would have a shape of (256, 128*200, 4)? It is important that the concatenating is frame-wise, so the order of the values in a frame is preserved.

Essentially, what is needed is to optimize:

data_new = data[:, :, :, 0]
for i in range(1, data.shape[3]):
    data_new = np.concatenate((data_new, data[:, :, :, i]), axis=1)
like image 575
Alexander Angelov Avatar asked Oct 05 '17 15:10

Alexander Angelov


People also ask

How do I change the shape of a NumPy array?

To convert the shape of a NumPy array ndarray , use the reshape() method of ndarray or the numpy. reshape() function.

Why do we do reshape (- 1 1?

reshape(-1, 1) if your data has a single feature or array. reshape(1, -1) if it contains a single sample. We could change our Series into a NumPy array and then reshape it to have two dimensions.


1 Answers

Permute axes with np.transpose and reshape -

m,n = data.shape[::2]
data_new = data.transpose(0,3,1,2).reshape(m,-1,n)

Or roll-axis and reshape -

data_new = np.rollaxis(data,3,1).reshape(m,-1,n)

Runtime test -

In [40]: data = np.random.randint(0,9,(256,128,4,200))

In [46]: %%timeit
    ...: data_new = data[:, :, :, 0]
    ...: for i in range(1, data.shape[3]):
    ...:     data_new = np.concatenate((data_new, data[:, :, :, i]), axis=1)
    ...: 
1 loop, best of 3: 3.56 s per loop

In [49]: m,n = data.shape[::2]

In [50]: %timeit data.transpose(0,3,1,2).reshape(m,-1,n)
10 loops, best of 3: 47.1 ms per loop

In [51]: %timeit np.rollaxis(data,3,1).reshape(m,-1,n)
10 loops, best of 3: 46.8 ms per loop

Thus, 76x+ speedup is the vectorization profit.

like image 141
Divakar Avatar answered Sep 22 '22 08:09

Divakar