check if numpy array is multidimensional or not

PythonNumpy

Python Problem Overview


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?

Python Solutions


Solution 1 - Python

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

Solution 2 - Python

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionsamView Question on Stackoverflow
Solution 1 - PythonAshwini ChaudharyView Answer on Stackoverflow
Solution 2 - PythonBaragornView Answer on Stackoverflow