how to average only positive number in array numpy

Solutions on MaxInterview for how to average only positive number in array numpy by the best coders in the world

showing results for - "how to average only positive number in array numpy"
Nicole
03 Aug 2020
1a = [[1, 2, 3, -1, -2, -3, -4, -1, 1, 2, 1, 2, 3, 2, 5],
2     [1, 2, 3, -1, -2, -3, -4, -1, 1, 2, 1, 2, 3, 2, 5],
3     [1, 2, 3, -1, -2, -3, -4, -1, 1, 2, 1, 2, 3, 2, 5]]
4
5b = np.array(a)
6
7def avg(a):
8    return a[a > 0].mean()
9
10np.apply_along_axis(avg, 1, b)