Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

shape vs len for numpy array

Tags:

python

numpy

Is there a difference (in performance for example) when comparing shape and len? Consider the following example:

In [1]: import numpy as np

In [2]: a = np.array([1,2,3,4])

In [3]: a.shape
Out[3]: (4,)

In [4]: len(a)
Out[4]: 4

Quick runtime comparison suggests that there's no difference:

In [17]: a = np.random.randint(0,10000, size=1000000)

In [18]: %time a.shape
CPU times: user 6 µs, sys: 2 µs, total: 8 µs
Wall time: 13.1 µs
Out[18]: (1000000,)

In [19]: %time len(a)
CPU times: user 5 µs, sys: 1 µs, total: 6 µs
Wall time: 9.06 µs
Out[19]: 1000000

So, what is the difference and which one is more pythonic? (I guess using shape).

like image 362
Dror Avatar asked May 24 '16 13:05

Dror


1 Answers

I wouldn't worry about performance here - any differences should only be very marginal.

I'd say the more pythonic alternative is probably the one which matches your needs more closely:

a.shape may contain more information than len(a) since it contains the size along all axes whereas len only returns the size along the first axis:

>>> a = np.array([[1,2,3,4], [1,2,3,4]])
>>> len(a)
2
>>> a.shape
(2L, 4L)

If you actually happen to work with one-dimensional arrays only, than I'd personally favour using len(a) in case you explicitly need the array's size.

like image 98
sebastian Avatar answered Sep 18 '22 18:09

sebastian