Updated April 20, 2023
Introduction to NumPy reshape
The word reshape in python means to change the shape of an array where several elements in every dimension are the meaning of the shape of an array and we make use of NumPy in python to reshape the array meaning to change the number of elements in each dimension. We make use of NumPy reshape in python which is used to remove or add the dimensions to the existing array or to modify the count of elements in every dimension in the existing array and when we make use of NumPy reshape in python, the data is not affected because of reshaping the array and the reshaped array without any modifications in its data is returned by NumPy reshape.
Syntax:
numpy.reshape(arrayname, newshape, order='C')
where arrayname is the name of the array that is to be reshaped,
newshape is the intended shape of the given array by making use of NumPy reshape and
order is the index order using which the elements of the array can be read and placed into the reshaped array represented by new shape. The allowed values for order are C, F and A where C represents C like the order of index using which the elements are read and written, F represents Fortran like the order of index using which elements are read and written and A represents Fortran like the order of index if the memory of the given array represented by arrayname is Fortran contiguous or it represents C like the order of index if the memory of the given array represented by arrayname is C contiguous.
Working of NumPy reshape
- Whenever there is a need to change the shape of an array where several elements in every dimension are the meaning of the shape of an array, we make use of NumPy reshape in Python.
- It is used to remove or add the dimensions to the existing array or to modify the count of elements in every dimension in the existing array.
- When we make use of NumPy reshape in python, the data is not affected because of reshaping the array and the reshaped array without any modifications in its data is returned by NumPy reshape.
- The index order in which the elements of the array can be read and placed into the reshaped array can be specified in the reshape function.
- The allowed values for the index order in which the elements of the array can be read and placed into the reshaped array are C, F, and A.
- Where C represents C like an order of index using which the elements are read and written into the reshaped array.
- F represents Fortran like the order of index using which elements are read and written into the reshaped array.
- A represents Fortran like the order of index if the memory of the given array is Fortran contiguous or it represents C like order of index if the memory of the given array is C contiguous.
Examples
Below are the different examples of NumPy reshape:
Example #1
Code:
#importing the package numpy
import numpy as num
#defining a 3*2 array to store the elements in the array
orgarrayname = num.array([[1,2,3], [4,5,6]])
#using Numpy reshape function to modify the dimensions of the given array without changing the data in the given array
modarrayname = num.reshape(orgarrayname, (6, 1))
print("The modified array after using reshape function is:")
#printing the elements of the modified array
print(modarrayname)
Output:
In the above program, a package called NumPy is imported to enable us to make use of reshape function. Then a 3*2 array is defined to store the elements in the array. Then we are making use of reshape function to modify the dimensions of the given 3*2 array to 6*1 array without changing the elements of the given array, The reshaped or modified array is printed as the output on the screen.
Example #2
Code:
#importing the package numpy
import numpy as num
#defining a 2*2 array to store the elements in the array
orgarrayname = num.array([[1,2], [3,4]])
#using Numpy reshape function to modify the dimensions of the given array without changing the data in the given array
modarrayname = num.reshape(orgarrayname, (1, 4))
print("The modified array after using reshape function is:")
#printing the elements of the modified array
print(modarrayname)
Output:
In the above program, a package called NumPy is imported to enable us to make use of reshape function. Then a 2*2 array is defined to store the elements in the array. Then we are making use of reshape function to modify the dimensions of the given 2*2 array to 1*4 array without changing the elements of the given array, The reshaped or modified array is printed as the output on the screen.
Example #3
Code:
#importing the package numpy
import numpy as num
#defining a 3*2 array to store the elements in the array
orgarrayname = num.array([[1,2,3], [4,5,6]])
#using Numpy reshape function to modify the dimensions of the given array without changing the data in the given array
modarrayname = num.reshape(orgarrayname, (2, 3))
print("The modified array after using reshape function is:")
#printing the elements of the modified array
print(modarrayname)
Output:
In the above program, a package called NumPy is imported to enable us to make use of reshape function. Then a 3*2 array is defined to store the elements in the array. Then we are making use of reshape function to modify the dimensions of the given 3*2 array to 2*3 array without changing the elements of the given array, The reshaped or modified array is printed as the output on the screen. The output is shown in the snapshot above.
Conclusion
In this tutorial, we understand the concept of NumPy reshape in Python through definition, syntax, and working through programming examples and their outputs.
Recommended Articles
This is a guide to NumPy reshape. Here we discuss the definition, syntax and working of NumPy reshape in Python along with the Examples. You may also have a look at the following articles to learn more –