I have a numpy array that I want to find the maximum element, so I call:
x = foo.max()
The problem is that foosometimes it is an empty array, and the function max(understandably) throws:
ValueError: zero-size array to reduction operation maximum which has no identity
This leads to my question:
Is there a way to specify an identifier value for a shrink operation? (Ideally, one that will work for arbitrary numpy ufuncs with methods reduce.) Or I'm stuck:
if foo.size > 0:
x = foo.max()
else:
x = 0 # or whatever identity value I choose
source
share