What specific I sights j can gain by using the “ndim” attribute?
I am currently working with a multidimensional array by using the NumPy. My doubt is what specific insights I can achieve if I use “ndim” attribute under NumPy.
In the context of Python programming language, using ndim we can find that the “ndim” attribute in NumPy allows you to gain or determine the wide range of dimensions or axes of the arrays of NumPy. It mainly returns an integer which indicates the dimensions of the array.
The ndim attribute is famous for providing a quick way to gain the dimensionality of NumPy array which can ease your task of manipulation of various arrays and even operation related to analysis.
Here is the example given to showcase the use of “ndim” attribute in NumPy:-
Import numpy as np
# Creating NumPy arrays of different dimensions
Array_1d = np.array([1, 2, 3]) # 1-dimensional array
Array_2d = np.array([[1, 2], [3, 4]]) # 2-dimensional array
Array_3d = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]]) # 3-dimensional array
# Using ndim to determine the dimensions of arrays
Print(“Dimensions of array_1d:”, array_1d.ndim)
Print(“Dimensions of array_2d:”, array_2d.ndim)
Print(“Dimensions of array_3d:”, array_3d.ndim)
Here is the output given of the above process:-
Dimensions of array_1d: 1
Dimensions of array_2d: 2
Dimensions of array_3d: 3