Shape of an Array and Reshape() function
Shape of an Numpy array
In Numpy array, shape is an attribute that is used to check the dimension of an array. shape attribute that returns the number of elements in each dimension of an array in numeric as a result.
syntax: array_name.shape
Example
array_1 = np.array([1,2,3,4,5,6])
array_2 = np.array([[0,9,8,7,6], [5,4,3,2,1]])
print("1D array :", array_1.shape)
print("2D array :", array_2.shape)
Output
1D array : (6,)
2D array : (2, 5)
Reshape() function
It is used to change the number of elements in each dimensional of an array.
syntax: array_name.reshape(attributes)
Example
print("Reshape :", array_1.reshape(3,2))
Output
Reshape : [[1 2]
[3 4]
[5 6]]
In the above example, we can reshape the 1D array into 2D array that contains 3 rows with 2 elements each.
Reshape 1D and 2D arrays
Example
print("Reshape 1D array :", array_1.reshape(3,2))
print("Reshape 2D array :", array_2.reshape(5,2))
Output
Reshape 1D array : [[1 2]
[3 4]
[5 6]]
Reshape 2D array : [[0 9]
[8 7]
[6 5]
[4 3]
[2 1]]
Here, we change the number of elements in each dimensions.
Reshape 1D array into 2D array
By using reshape() functions, we can also change the array from one dimension to another.
Example
print("Reshape 1D array into 2D array :", array_1.reshape(2,3))
print("Reshape 1D array into 3D array :", array_1.reshape(3,1,2))
Output
Reshape 1D array into 2D array : [[1 2 3]
[4 5 6]]
Reshape 1D array into 3D array : [[[1 2]]
[[3 4]]
[[5 6]]]
Here, we can reshape the 1D array into 2D and 3D arrays.
Reshape higher dimension array into single dimension array
Example
print("Reshape 2D array into 1D array :", array_2.reshape(10))
Output
Reshape 2D array into 1D array : [0 9 8 7 6 5 4 3 2 1]
Another Way
In the above example, the total number of elements to be used for reshape into single dimension array. When the complexity increases, we cannot count the number of elements each and every time. It is very complex and tedious process.
Example
print("Reshape array from multi dimension into single dimension :", array_1.reshape(-1))
Output
Reshape 1D array : [1 2 3 4 5]
Here, we can use -1 for reshape the multi dimension array into the single dimension array. when total number of elements is unknown, then -1 can be used. It automatically calculates the total number of elements in a dimension.
Unknown Dimension
Unknown dimension are used when we don't know the dimensions in an array.
Example
print("Reshape 1D array into 2D array with unknown :", array_1.reshape(2,-1))
print("Reshape 1D array into 3D array with unknown :", array_1.reshape(2,1,-1))
Output
Reshape 1D array into 2D array with unknown : [[1 2 3]
[4 5 6]]
Reshape 1D array into 3D array with unknown : [[[1 2 3]]
[[4 5 6]]]
In the above example, 1D array was reshaped into 2D and 3D arrays using the unknown dimension as -1.
Condition: We can use unknown dimension for only one dimension in an array. If -1 is used more than once, then the interpreter throws an error.
Challenge - Try to reshape the 3D or more dimension arrays.
Follow on LinkedIn - Trillionaire TK
Follow on Instagram - Trillionaire TK
Visit our website - Information Technology
Learn, Code, Practice!!!