29 Mar Reshape a Numpy array
In Python Numpy, the number of elements in each dimension of an array is called the shape. To reshape the number of dimensions, use the numpy.reshape() method in Python.
Why reshape?
Reshape allows you to edit the number of elements in a dimension. Additionally, with reshape(), you can add or remove dimensions.
Let us see 5 examples to reshape arrays in NumPy:
- Reshape dimensions from a 1D to a 2D array
- Convert 3D Array to 1D array (Flattening the array)
- Convert 2D to 1D array (Flattening the array)
- Convert a 1D array to a 2D array in NumPy
- Convert a 1D array to a 3D array in NumPy
1. Reshape dimensions from a 1D to a 2D array
Let us see an example and create a 1D array. We will reshape this 1D array to 2D array using the reshape():
# Reshape dimensions from 1D to 2D array in NumPy
import numpy as np
# Create a 1d array
n = np.array([10, 20, 30, 40, 50, 60])
print("1D array = ",n)
# Reshape 1d to 3x2 matrix
res = n.reshape(3, 2)
print("2D array (3x2) = \n",res)
# Reshape 1d to 2x3 matrix
res = n.reshape(2, 3)
print("2D array (2x3) = \n",res)
Output
1D array = [10 20 30 40 50 60] 2D array (3x2) = [[10 20] [30 40] [50 60]] 2D array (2x3) = [[10 20 30] [40 50 60]]
2. Convert 3D Array to 1D array (Flattening the array)
Let us see an example and create a 3D array. We will reshape this 3D array into a 1D array using reshape(). We will use-1, which is a placeholder in NumPy’s reshape. It means: “Infer this dimension based on the total number of elements and the other dimensions provided.”
# Convert 3D to 1D array (Flattening the array)
import numpy as np
# Create a 3D array
n = np.array([
[[10, 20, 30, 40, 50],
[40, 60, 76, 88, 99]],
[[25, 20, 30, 40, 50],
[50, 60, 76, 88, 99]],
[[35, 20, 30, 40, 50],
[50, 60, 76, 88, 99]],
[[60, 20, 30, 40, 50],
[90, 60, 76, 88, 99]]
])
print("3D array = \n",n)
# Reshape the 3d array to 1d
# Automatic dimension inference
res = n.reshape(-1)
print("\n1D array = \n",res)
Output
3D array = [[[10 20 30 40 50] [40 60 76 88 99]] [[25 20 30 40 50] [50 60 76 88 99]] [[35 20 30 40 50] [50 60 76 88 99]] [[60 20 30 40 50] [90 60 76 88 99]]] 1D array = [10 20 30 40 50 40 60 76 88 99 25 20 30 40 50 50 60 76 88 99 35 20 30 40 50 50 60 76 88 99 60 20 30 40 50 90 60 76 88 99]
3. Convert 2D to 1D array (Flattening the array)
# Convert 2D to 1D array (Flattening the array)
import numpy as np
n = np.array([[10, 20, 30],
[40, 50, 60]])
# Flatten to 1D array
print(n.reshape(-1))
Output
[10 20 30 40 50 60]
4. Convert a 1D array to a 2D array in NumPy
# Convert 1D to 2D array in NumPy import numpy as np n = np.array([10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120]) # Reshape with 2 rows print(n.reshape(2, -1)) # Reshape with 3 rows print(n.reshape(3, -1)) # Reshape with 2 columns print(n.reshape(-1, 2)) # Reshape with 3 columns print(n.reshape(-1, 3))
Output
[[ 10 20 30 40 50 60] [ 70 80 90 100 110 120]] [[ 10 20 30 40] [ 50 60 70 80] [ 90 100 110 120]] [[ 10 20] [ 30 40] [ 50 60] [ 70 80] [ 90 100] [110 120]] [[ 10 20 30] [ 40 50 60] [ 70 80 90] [100 110 120]]
5. Convert a 1D array to a 3D array in NumPy
# Convert 1D to 3D array in NumPy import numpy as np n = np.array([10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120]) # 2 matrices and 2 rows print(n.reshape(2, 2, -1)) # 2 matrices and 3 rows print(n.reshape(2, 3, -1))
Output
[[[ 10 20 30] [ 40 50 60]] [[ 70 80 90] [100 110 120]]] [[[ 10 20] [ 30 40] [ 50 60]] [[ 70 80] [ 90 100] [110 120]]]
If you liked the tutorial, spread the word and share the link and our website, Studyopedia, with others.
For Videos, Join Our YouTube Channel: Join Now
Read More:
No Comments