NumPy is a Python library, mostly used for mathematical operations in Python programming language. An array plays an essential role in Numpy, which is a container for storing the sequence of the same type of data.
There are many ways to manipulate an array in Numpy, one of which is reshaping. In this tutorial, we will learn about the reshaping of an array in Numpy with many examples.
Reshape Method
The reshape() method is used for reshaping an array in Numpy. This method takes one argument as a tuple of new dimensions of the required array.
Example 1: Reshape an array with one dimension
import numpy as np array1 = np.array([1,2,3,4,5,6,7,8,9]) array2 = np.reshape(array1, (3,3)) print(array2)
Output:
[[1 2 3] [4 5 6] [7 8 9]]
The above example reshapes the one-dimensional array into a two-dimensional array with the shape of 3×3.
Example2: Reshape an array with more than one dimension
import numpy as np array1 = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]]) array2 = np.reshape(array1, (4, 2)) print(array2)
Output:
[[1 2] [3 4] [5 6] [7 8]]
In the above example, we reshape the array1 into 4 rows of 2 items each.
Example3: Reshape an array using -1
We can use -1 in the tupleas an argument for reshape() skip that dimension and automatically calculate it based on the total number of items in an array.
import numpy as np array1 = np.array([1, 2, 3, 4, 5, 6]) array2 = np.reshape(array1, (-1, 3)) print(array2)
Output:
[[1 2 3] [4 5 6]]
Because the original array has 6 elements so it calculating one dimension to be 2 rows and 3 columns.
Example4: Converting a two-dimensional array to one-dimensional array in Numpy
import numpy as np array1 = np.array([[1, 2, 3], [4, 5, 6]]) array2 = np.reshape(array1, (6, )) print(array2)
Output:
[1 2 3 4 5 6]
In the above example, we convert a 2D array to a 1D array in Numpy using the reshape() method.
Example5: Reshape() method with Fortran order
The default is a C-style array which is row-major, but we can achieve Fortran-style which is column-major using Fortran order.
import numpy as np array1 = np.array([[1, 2], [3, 4], [5, 6]]) array2 = np.reshape(array1, (2, 3), order='F') print(array2)
Output:
[[1 5 4] [3 2 6]]
The above example reshapes a two-dimensional array and stores it in Fortran-style. The output is 2X3 shaper array.
Example6: Flatten method
The flatten() method is used to convert an array of any shape into a one-dimensional array.
import numpy as np array1 = np.array([[1, 2, 3], [4, 5, 6]]) array2 = array1.flatten() print(array2)
Output:
[1 2 3 4 5 6]
In the above example, we used the flatten() method to convert the two-dimensional array into a one-dimensional array.
Example7: Ravel method
The ravel() method is also used to convert an array of any shape into a one-dimensional array.
import numpy as np array1 = np.array([[1, 2, 3], [4, 5, 6]]) array2 = array1.ravel() print(array2)
Output:
[1 2 3 4 5 6]
In the above example, we used the ravel() method to convert the two-dimensional array into a one-dimensional array, Which gives the same result as flatten() method with ravel() but ravel() is one-dimensional
Conclusion
The reshape() method is used to reshaping an array to any given size, and by using the flatten() and ravel() methods, we can always convert any shape of an array to a single dimension.