In this article, we’ll learn about Python Numpy reshape function. Everything from the parameters of reshape function to its proper implementation using code examples will be discussed.
Understanding Python Numpy reshape Function
As the name suggests, the reshape function is used to change the shape or we can say dimensions but not changing the data of the specified array. It means this function returns an array of the same data but a different shape. We can transform a 1D array into a multi-dimensional array by using this function.
This makes it efficient for datasets that are large, the reason is that we can modify the view of data with the help of this reshape Numpy function. This function does not create a new/copy of the array. It just modifies the view of the original array. Use copy method if you want to create a new array but make sure to do it after reshaping takes place so you can get your desired output.
Syntax of reshape() Function
numpy.reshape(arr, newshape, order='C')
- The first parameter(arr) specifies the array that is up to get reshaped.
- The second parameter(newshape) of reshape function can be a tuple and it’s used to specify the new shape or dimensions of an array.
- The thirst parameter(order) is optional. The role of this parameter is to define the reading order of elements during the process of reshaping it. We can pass only 2 values to the order parameter. It can either be ‘C‘ (C-style, row-major) or ‘F‘ (Fortran-style, column-major). The default value assigned to the order parameter is ‘C‘.
Python Numpy reshape Implementation (3 Code Examples)
Example 1: Convery 1D Array to 2D Array using reshape() Function
import numpy as np arr1D= np.arange(10) # 1D array created using arange reshapearr= arr1D.reshape((2,5)) # 2D array created from 1D using reshape function print(reshapearr)
[[0 1 2 3 4] [5 6 7 8 9]]
First, we’ve imported the library of Python Numpy. Then we created a simple 1D array by using the arange function of Numpy. After that, we used the reshape function on this 1D array and specified a tuple in which the first item represents the number of rows and the second item shows the number of columns.
As a result, we can see the 2D array having 2 rows and 5 columns in the output section, as specified.
We can also specify the reshape function like this:
If you want to make the code short then use this method. See below:
Just make sure to give it a possible shape. For instance, in the above example if we have specified the shape(2,4) then an error would be generated as this shape is not possible in this case.
Example 2: 2D array to 3D array
Let’s first create a 2D array. See below:
ar1D=np.arange(16) ar2D=np.reshape(ar1D,(2,8)) # Creation of 2d Array print(ar2D)
[[ 0 1 2 3 4 5 6 7] [ 8 9 10 11 12 13 14 15]]
Let’s now convert this 2D array into a 3D array using reshape function of Numpy. See below:
[[[ 0 1 2 3] [ 4 5 6 7]] [[ 8 9 10 11] [12 13 14 15]]]
So this is how we can create a 3D array from 2D with the help of reshape function.
Example 3: Change order of array using order Parameter(order)
We’ll see examples for both order ‘C‘ and order ‘F‘. See below.
1. Using Order C (C-style, row-major)
orderC=np.arange(8).reshape((2,4),order='C') print('C order:') print(orderC)
C order: [[0 1 2 3] [4 5 6 7]]
2. Using order F (Fortran-style, column-major)
orderF=np.arange(8).reshape((2,4),order='F') print('F order:') print(orderF)
F order: [[0 2 4 6] [1 3 5 7]]
We’ve used the same example for both order types. As a result, we can see different orders for both ‘F‘ and ‘C‘. In Fortran(‘F‘), the items are being read in column wise while in C-style(C), the items are read in row-wise format. By default, C is given to the order parameter.
In conclusion, hope the concept of the Python Numpy reshape function is now super clear to you. We’ve used multiple code examples to properly demonstrate the usage of reshape Numpy function.
Thank you for reading this article.