Back to questions
JuniorTheoryCommon
What does this program print?
import numpy as np
a = np.array([4, -1, 7, 0, -3])
print(a[a > 0].sum(), (a > 0).sum())11 2
- Expecting the mask's sum to add the values:
a > 0holdsTrueandFalse, and its sum counts theTrueentries, so it is2. - Expecting
a[a > 0]to return positions: a mask keeps the elements themselves,4and7, so their sum is11.
- How would you get the mean of the negative values of
ain one line? - What does
a[a > 0]return when no element is positive, and what is its sum?