size of NumPy array

abalter picture abalter · Jun 20, 2012 · Viewed 161.7k times · Source

Is there an equivalent to the MATLAB

 size()

command in Numpy?

In MATLAB,

>>> a = zeros(2,5)
 0 0 0 0 0
 0 0 0 0 0
>>> size(a)
 2 5

In Python,

>>> a = zeros((2,5))
>>> 
array([[ 0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.]])

>>> ?????

Answer

Sven Marnach picture Sven Marnach · Jun 20, 2012

This is called the "shape" in NumPy, and can be requested via the .shape attribute:

>>> a = zeros((2, 5))
>>> a.shape
(2, 5)

If you prefer a function, you could also use numpy.shape(a).