I came across a Python code which had something similar to what follows:
a = np.array([1,2,3,4,5,6,7])
a
array([1, 2, 3, 4, 5, 6, 7])
np.mean(a)
4.0
np.mean(a <=3)
0.42857142857142855
np.mean(a <=4)
0.5714285714285714
I don't understand what does the comparison operator signify ? Any pointers for numpy's mean() function implementation would be nice.
Thank you.
Well if you write a <= 3
, you construct an array with values:
array([ True, True, True, False, False, False, False], dtype=bool)
Since True
has value 1.0
(or 1
) and False
has value 0.0
(or 0
), it calculates the mean
over the list of booleans. So in other words it will here count the number of elements for which the value holds over the total number of elements.
mean
itself has no specific behavior: if you feed it a list of Foo
s, it will simply evaluate Foo1+Foo2+...Foon
and divide it over the length of the list, and:
>>> False+True
1
>>> True+True
2
Therefore the result of np.mean(a <=3)
is 3/7 (the first three elements are <= 3
over seven elements) and np.mean(a <=4)
4/7 here.
You probably want to calculate the mean of the little numbers.
Here is the way :
In [2]: a=arange(8)
In [3]: b= a<=3
In [4]: b # condition
Out[4]: array([ True, True, True, True, False, False, False, False], dtype=bool)
In [5]: a[b] #selection
Out[5]: array([0, 1, 2, 3])
In [6]: a[b].mean()
Out[6]: 1.5
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