'How to reshape a numpy array of shape (3600, 256) to a shape (256, 1, 60, 60)?

I have a numpy array of shape (3600, 256). Each column is one data sample and I have 256 samples in total. How can I reshape my array to (256, 1, 60, 60) where 256 is the number of data samples, and each data sample is re-organized as a one-channel image of size 60 by 60?



Solution 1:[1]

I do this in two steps:

  1. Reshape the array to have the correct dimensions, and

  2. Transpose the array to put the axes in the desired order.

You can do this in either order. It's easier to reason about transposition in a 2D array, so I would do that first.

If the array is a, the transposed array a.T has shape (256, 3600). You can then reshape this:

a.T.reshape(256, 1, 60, 60)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dietrich Epp