NumPy provides a feature to reshape your array from 1D to 2D or vice versa. So today we will learn the concepts of shape and reshape in Numpy array with detailed explanations and examples.
You can also check out our previous article π Data Types in NumPy with Simple Example
So letβs start learning both things one by one.
Shape attribute in Numpy Array
The shape
attribute is used to identify the shape of a Numpy array. it returns a tuple representing the dimensions or sizes of each axis in the array.
import numpy as np
my_arr = np.array([[1,2],[2,1]])
print(my_arr.shape)
Output:
(2, 2)
Here in the output (2, 2)
indicates that the given array has 2 rows and 2 columns.
A simple method to determine the shape of an array is by examining the input: Number of list passed in array function are considered as rows and Number of elements in single list are considered as columns.
Check Shape of Multi-Dimensions array
import numpy as np
my_arr = np.array([1,2,3,5,7,11,13,17], ndmin=4)
print(my_arr.shape)
Output:
(1, 1, 1, 8)
Here we create a 4D array with singleton dimensions in the first three axes (that represent rows) and a size of 8 along the fourth axis (represents columns).
Another Example:
import numpy as np
# Creating a 3D array
array_3d = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])
# Checking the shape
shape_3d = array_3d.shape
print(array_3d)
print() # for space
print("Shape of the 3D array:", shape_3d)
Output:
[[[ 1 2 3]
[ 4 5 6]]
[[ 7 8 9]
[10 11 12]]]
Shape of the 3D array: (2, 2, 3)
Reshape Method in NumPy Array
reshape()
method is used to change the shape of a numpy array without changing its data. It takes the desired new shape as an argument and returns a new array with the specified dimensions
You can use reshape()
method to transform an array into a different shape, like changing a 1D array into a 2D array or vice versa, as long as the total number of elements in the array remains the same.
Syntax: reshape(shape)
Convert 1D Array into 2D Array
import numpy as np
# Creating 1D array
a1 = np.array([1,2,3,4,5,6])
# Convert 1D --> 2D using reshape() method
a2 = a1.reshape(3,2)
# 1D
print('Before reshaping:')
print(a1)
print() # for space
#2D
print('After reshaping')
print(a2)
Output:
Before reshaping:
[1 2 3 4 5 6]
After reshaping
[[1 2]
[3 4]
[5 6]]
In this example, we use the reshape()
method to transform a 1D array into a 2D array with 3 rows and 2 columns. The operation succeeds seamlessly because the product of 3 and 2 equals 6, matching the number of elements in the original 1D array. However, attempting to reshape it into a 3Γ3 array would result in an error, as there are only 6 elements available, insufficient for creating a 3Γ3 array requiring 9 elements.
So always reshape an array according to the number of elements present in that array; otherwise, you will get an error.
Convert 1D Array into 3D Array
import numpy as np
# Creating 1D array
arr = np.array([1,2,3,4,5,6,7,8,9,10,11,12])
# Reshape 1D array to 3D array
arr_3d = arr.reshape(2,3,2)
print(arr_3d)
print(f"Dimensions of an array: {arr_3d.ndim}")
Output:
[[[ 1 2]
[ 3 4]
[ 5 6]]
[[ 7 8]
[ 9 10]
[11 12]]]
Dimensions of an array: 3
Convert 3D Array into 1D Array
To convert any multi-dimensional array back to a 1D array, simply pass -1 inside the reshape() method it will convert to a 1D array.
import numpy as np
# Creating 3D array
arr_3d = np.array([1,2,3,4,5,6,7], ndmin=3)
# Converte 3d array to 1d array
arr1 = arr_3d.reshape(-1)
print("Before reshaping")
print(arr_3d)
print() # for Space
print("After reshaping")
print(arr1)
Output:
Before reshaping
[[[1 2 3 4 5 6 7]]]
After reshaping
[1 2 3 4 5 6 7]