Reshaping NumPy Array | Numpy Array Reshape Examples

In python, reshaping numpy array can be very critical while creating a matrix or tensor from vectors. In order to reshape numpy array of one dimension to n dimensions one can use np.reshape() method. Let’s check out some simple examples.

It is very important to reshape you numpy array, especially you are training with some deep learning network. Deep Learning models like CNN or LSTM in keras or any other python deep learning library some time requires reshaping of original numpy array.

Checking Shape of Existing numpy array

You can check shape of an existing array using shape attribute of numpy array.

If we check the shape of reshaped numpy array, we’ll find tuple (2, 5) which is a new shape of numpy array. Here first element of tuple is number of rows and second is number of columns.

Python numpy reshape() Method
Python numpy reshape() Method

Reshaping numpy array (vector to matrix)

Reshaping numpy array (matrix to tensor)

How reshaping works with numpy arrays?

Numpy reshape() method Visulizations
Numpy reshape() method Visulizations

Hope you like this short and sweet information on reshaping numpy array example. Stay tuned for more examples.

Leave a Reply

Your email address will not be published. Required fields are marked *