Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

check if numpy array is multidimensional or not

Tags:

python

numpy

I want to check if a numpy array is multidimensional or not?

V = [[ -7.94627203e+01  -1.81562235e+02  -3.05418070e+02  -2.38451033e+02][  9.43740653e+01   1.69312771e+02   1.68545575e+01  -1.44450299e+02][  5.61599000e+00   8.76135909e+01   1.18959245e+02  -1.44049237e+02]]

How can I do that in numpy?

like image 429
sam Avatar asked Jan 23 '14 05:01

sam


People also ask

How can I tell if a NumPy array is 2D?

Look for array. shape: if it comes like (2,) means digit at first place but nothing after after comma,its 1D. Else if it comes like (2,10) means two digits with comma,its 2D.

How do I check if an array is an NP?

Check if the Numpy array is Nan in Python To check for Nan values in a Numpy array we can use the function Np. isNan(). The output array is true for the indices that are Nan in the original array and false for the rest.

What is the preferred method to check for an empty array in NumPy?

Method 1: numpy. any() to check if the NumPy array is empty in Python. numpy. any() method is used to test whether any array element along a given axis evaluates to True.

How do you find the dimension of a list?

To find the shape (or dimensions) of a nested list or tuple in Python, iterate over each element in the list or tuple and identify its length with the built-in len() function.


2 Answers

Use the .ndim property of the ndarray:

>>> a = np.array([[ -7.94627203e+01,  -1.81562235e+02,  -3.05418070e+02,  -2.38451033e+02],[  9.43740653e+01,   1.69312771e+02,   1.68545575e+01,  -1.44450299e+02],[  5.61599000e+00,   8.76135909e+01,   1.18959245e+02,  -1.44049237e+02]])
>>> a.ndim
2
like image 133
Ashwini Chaudhary Avatar answered Oct 17 '22 21:10

Ashwini Chaudhary


In some cases, you should also add np.squeeze() to make sure there are no "empty" dimensions

>>> a = np.array([[1,2,3]])
>>> a.ndim
2
>>> a = np.squeeze(a)
>>> a .ndim
1
like image 7
Baragorn Avatar answered Oct 17 '22 21:10

Baragorn