In this tutorial, we will cover the reshape()
function of the NumPy library.
The word “reshape” simply indicates changing the shape and that is what this function is used for
- The
reshape()
function in the NumPy library is mainly used to change the shape of the array without changing its original data. - Thus
reshape()
function helps in providing new shape to an array, which can be useful baed on your usecase. - In cases where you want to convert the array’s long shape into the wide shape of the array this function is used.
Syntax of reshape()
:
The syntax required to use this function is as follows:
numpy.reshape(a, newshape, order='C')
Parameters:
Let us take a look at the parameters of this function:
- a This parameter indicates the input array that is to be reshaped.
- newshape The newshape parameter should provide a shape which is compatible with the original shape. If the value of this parameter is an integer value, then the result will be a 1-D array of that length. If the shape dimension is -1, then the value is inferred from the length of the array and the remaining dimensions.
- order The index order parameter is very important in
reshape()
function. Basically it is used to read the elements of the source array and then place the elements into the reshaped array using this index order.
The index order ‘C‘ means to read/write the elements which are using a C-like index order(and where the last axis index is changing fast, back to the first axis index is changing slowest)
Further, the index order ‘F‘ indicates to read/write the elements which are using the Fortran-like index order(and where the last axis index is changing slowest and the first axis index is changing fastest)
Then the ‘C’ and ‘F’ order take no amount of the memory layout of the underlying array and they only refers to the order of indexing.
Last but not least index order ‘A’ simply means to read/write the elements in Fortran-like index order only when the array to be reshaped is contiguous in memory, otherwise you can use C-like order.
Returned Values:
The reshape()
function will return the reshaped array without changing the data of the original array.
Example 1:
Let us have a look at the basic example of this function and the code snippet for the same is as follows:
import numpy as np
a = np.arange(12)
print("The Original array : \n", a)
# shaping the array with 2 rows and 4 columns
a1= np.arange(12).reshape(2, 6)
print("\n The reshaped array with 2 rows and 6 columns : \n", a1)
# shaping the array with 4 rows and 2 columns
a2 = np.arange(12).reshape(6,2)
print("\n The reshaped array with 6 rows and 2 columns : \n", a2)
# Construction of a 3D array
a3 = np.arange(12).reshape(2, 3, 2)
print("\nAfter reshaping the original array to 3D : \n", a3)
Output:
The output of the above code will be:
Example 2:
In the code snippet given below we will cover how to do fortran-like index ordering using reshape()
function and check the output for the same:
import numpy as np
x = np.arange(12)
print("The array is :\n",x)
y = np.reshape(x, (4, 3), order='F')
print("Reshaping the original array using F-like index ordering")
print(y)
Output:
Example 3:
Now we will show you another code snippet where we will apply C-like index order to above example and output for the same:
import numpy as np
x = np.arange(12)
print("The array is :\n",x)
y = np.reshape(x, (4, 3), order='C')
print("Reshaping the original array using C-like index ordering")
print(y)
Output: