develarist
develarist

Reputation: 1365

Detect number of dimensions of numpy array (not shape)

Getting the shape of two different numpy arrays returns tuples

a.shape
Out[131]: (3,)

A.shape
Out[132]: (3, 3)

Based on the tuples, one is a one-dimensional array (number of dimensions = 1), the other is 2d. How can I detect number of dimensions similar to how type(A) will tell me one of them is a numpy.ndarray? should I just use len(a.shape)?

Upvotes: 0

Views: 790

Answers (1)

black
black

Reputation: 857

You should use numpy.ndarray.ndim. So

a.ndim # gives 1

and

A.ndim # gives 2

Upvotes: 1

Related Questions