Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH,DOC: np.max of a zero-sized array throws a ValueError #22497

Open
ev-br opened this issue Oct 29, 2022 · 6 comments
Open

ENH,DOC: np.max of a zero-sized array throws a ValueError #22497

ev-br opened this issue Oct 29, 2022 · 6 comments

Comments

@ev-br
Copy link
Contributor

ev-br commented Oct 29, 2022

Describe the issue:

np.max of a zero-sized array throws a ValueError

Reproduce the code example:

In [1]: import numpy as np

In [2]: a = np.zeros([])

In [3]: np.max(a)        # works
Out[3]: 0.0

In [4]: b = np.zeros((5, 0, 3))

In [5]: np.max(b)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Input In [5], in <cell line: 1>()
----> 1 np.max(b)

File <__array_function__ internals>:5, in amax(*args, **kwargs)

File ~/.virtualenvs/scipy-dev/lib/python3.8/site-packages/numpy/core/fromnumeric.py:2754, in amax(a, axis, out, keepdims, initial, where)
   2638 @array_function_dispatch(_amax_dispatcher)
   2639 def amax(a, axis=None, out=None, keepdims=np._NoValue, initial=np._NoValue,
   2640          where=np._NoValue):
   2641     """
   2642     Return the maximum of an array or maximum along an axis.
   2643 
   (...)
   2752     5
   2753     """
-> 2754     return _wrapreduction(a, np.maximum, 'max', axis, None, out,
   2755                           keepdims=keepdims, initial=initial, where=where)

File ~/.virtualenvs/scipy-dev/lib/python3.8/site-packages/numpy/core/fromnumeric.py:86, in _wrapreduction(obj, ufunc, method, axis, dtype, out, **kwargs)
     83         else:
     84             return reduction(axis=axis, out=out, **passkwargs)
---> 86 return ufunc.reduce(obj, axis, dtype, out, **passkwargs)

ValueError: zero-size array to reduction operation maximum which has no identity

Error message:

ValueError                                Traceback (most recent call last)
Input In [5], in <cell line: 1>()
----> 1 np.max(b)

File <__array_function__ internals>:5, in amax(*args, **kwargs)

File ~/.virtualenvs/scipy-dev/lib/python3.8/site-packages/numpy/core/fromnumeric.py:2754, in amax(a, axis, out, keepdims, initial, where)
   2638 @array_function_dispatch(_amax_dispatcher)
   2639 def amax(a, axis=None, out=None, keepdims=np._NoValue, initial=np._NoValue,
   2640          where=np._NoValue):
   2641     """
   2642     Return the maximum of an array or maximum along an axis.
   2643 
   (...)
   2752     5
   2753     """
-> 2754     return _wrapreduction(a, np.maximum, 'max', axis, None, out,
   2755                           keepdims=keepdims, initial=initial, where=where)

File ~/.virtualenvs/scipy-dev/lib/python3.8/site-packages/numpy/core/fromnumeric.py:86, in _wrapreduction(obj, ufunc, method, axis, dtype, out, **kwargs)
     83         else:
     84             return reduction(axis=axis, out=out, **passkwargs)
---> 86 return ufunc.reduce(obj, axis, dtype, out, **passkwargs)

ValueError: zero-size array to reduction operation maximum which has no identity

NumPy/Python version information:

In [8]: np.version
Out[8]: '1.21.1'

Context for the issue:

Seen as a part of scipy/scipy#17241 (comment). Can be worked around of course, am just reporting that it needs a workaround.

@ev-br ev-br added the 00 - Bug label Oct 29, 2022
@ev-br ev-br changed the title BUG: <Please write a comprehensive title after the 'BUG: ' prefix> np.max of a zero-sized array throws a ValueError Oct 29, 2022
@eric-wieser
Copy link
Member

This is by design; what would you expect instead?

@eric-wieser
Copy link
Member

I suppose you could make the argument that it should return -inf

@ev-br
Copy link
Contributor Author

ev-br commented Oct 29, 2022

On the face of it, -inf sounds reasonable, but I don't know enough to make a compelling argument.

From a user perspective, it would be best if these zero-sized arrays were either not allowed at all (raise on construction?) or behave consistently (for some definition of being consistent). The latter expectation is nicely expressed by Warren in scipy/scipy#17241 (comment).

Otherwise, they regularly need to be special-cased in user code. OTOH, this is all a minor annoyance anyway, and workarounds are not too difficult. Cf scipy/scipy#17305 for a typical example (this is just a recent one out of I don't remember how many; the gut feeling is that this corner case shows up "rarely but regularly", but I've no hard data).

@seberg
Copy link
Member

seberg commented Oct 29, 2022

This has been discussed a few times, I have no strong opinion either way. Also maybe a bit discussed here.

Just to be sure you are aware, you can also pass initial=-np.inf.

@ev-br
Copy link
Contributor Author

ev-br commented Oct 29, 2022

Oh thanks, was not aware of the initial=-np.inf! And now that I am aware, I see that it's documented to solve exactly this issue, "Must be present to allow computation on empty slice."

A minor gripe is that this sentence is not very discoverable unless already known. Not that I have a good actionable suggestion though.

Using initial=-np.inf fixes the original downstream problem that lead me to reporting this, so this issue can be closed unless it's useful for a more general discussion of zero-sized arrays (which I doubt, but in case of ambiguity etc).

@seberg
Copy link
Member

seberg commented Oct 29, 2022

Well, for a long time it might have been a bit trick to have -inf as an identity. That would change after gh-20970.
So it isn't a bad time for someone with a mission to ask for a change :).

@InessaPawson InessaPawson changed the title np.max of a zero-sized array throws a ValueError BUG: np.max of a zero-sized array throws a ValueError Oct 31, 2022
@seberg seberg changed the title BUG: np.max of a zero-sized array throws a ValueError ENH,DOC: np.max of a zero-sized array throws a ValueError Nov 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants