ndim() function in Numpy Python by R4R Team

- ndim is python Numpy method that find or return the dimension of the given Array.

Example-

array is [1 2]
dimension is 1

array is
[[1,2]
[3,4]]
dimension is 2

Syntax-

dim=array.ndim


program-

#import numpy
import numpy as np

l=[1,2,3,4]
arr=np.array(l)
print("Array isn",arr)
print("And dimension is",arr.ndim)

l=[[1,2,3],[3,2,1],[1,0,1]]
arr=np.array(l)
print("nArray isn",arr)
print("And dimension is",arr.ndim)

l=[[[1,2],[2,1]],[[3,4],[4,3]]]
arr=np.array(l)
print("nArray isn",arr)
print("And dimension is",arr.ndim)


output-

Array is
[1 2 3 4]
And dimension is 1

Array is
[[1 2 3]
[3 2 1]
[1 0 1]]
And dimension is 2

Array is
[[[1 2]
[2 1]]

[[3 4]
[4 3]]]
And dimension is 3

-In this program, we have different nested lists then we convert it into Numpy array.
-and ndim() method we pass that array and it return the actual dimension of the array.




Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!