I have numpy array:
wk = np.array([1, 0, 3, -4])
how to apply a formula based on certain condition, like how to sum all positive value in wk. I've tried
if wk.all > 0:
np.sum(wk)
But it threw an error.
np.sum(wk[wk > 0])
is what you are looking for. wk > 0 creates a boolean array of the same size as wk that is true everywhere where wk > 0. wk[wk > 0] then evaluates to an array that only contains those elements of wk where the former array is true and finally np.sum sums up those values.
TypeError: '>' not supported between instances of 'builtin_function_or_method' and 'int'