This code creates a 10-element array.
In [14]: s = np.array(10)
In [15]: s
Out[15]: array(10)
In [16]: s.size
Out[16]: 1
In [17]: s.shape
Out[17]: ()
If one calls np.array()
on arbitrary object that is not iterable, numpy silently creates an empty array with no dimensions.
However, its size is 1.
Docs of numpy size tell us that x.size is equivalent to calling np.prod(x.shape)
. And docs for np.prod state that calling np.prod on empty sequence gives us 1. Probably it is so due to the fact that 1 is a neutral element for multiplication, meaning the following.
Say you have an array [4, 2, 3]
. Its elements product is 24
. Now you split it in two arrays: [4]
and [2, 3]
. You have a nice property: np.prod([4, 2, 3]) == np.prod([4]) * np.prod([2, 3])
. But if one of the arrays is empty, you want this property still hold: np.prod([4, 2, 3]) == np.prod([]) * np.prod([4, 2, 3])
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With