python - Identity value for numpy ufunc reducers -
i have numpy array want find maximum element of, call:
x = foo.max()
the problem foo
empty array, , max
function (understandably) throws:
valueerror: zero-size array reduction operation maximum has no identity
this leads question:
is there way supply identity value reduction operation? (ideally, 1 work arbitrary numpy ufuncs reduce
methods.) or stuck doing:
if foo.size > 0: x = foo.max() else: x = 0 # or whatever identity value take
surely "pythonic" way go is:
def safe_max(ar,default=np.nan): try: homecoming np.max(ar) except valueerror: homecoming default x = safe_max(foo,0)
considering maximum of empty sequence not defined, seem logical approach.
python numpy
No comments:
Post a Comment