0

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)?

1
  • len(a.shape) is fine. a.ndim also. I doubt if it makes much difference. Commented Jan 16, 2021 at 21:47

1 Answer 1

1

You should use numpy.ndarray.ndim. So

a.ndim # gives 1

and

A.ndim # gives 2
Sign up to request clarification or add additional context in comments.

1 Comment

AttributeError: 'numpy.ndarray' object has no attribute 'dims'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.