Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

numpy mean with comparison operator in the parameter

Tags:

python

numpy

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.

like image 984
Trojosh Avatar asked Feb 09 '17 15:02

Trojosh


2 Answers

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 Foos, 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.

like image 90
Willem Van Onsem Avatar answered Nov 01 '22 10:11

Willem Van Onsem


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 
like image 33
B. M. Avatar answered Nov 01 '22 08:11

B. M.