Reputation: 143
I'm assuming I'm doing something wrong here, but I'm working on a project in Pycharm, which notified me when using the ndarray.max()
function that initial
was undefined (parameter 'initial' unfilled
). Looking at the documentation, it does show that there is no default value for initial
argument.
When ctrl-clicking the ndarray.max()
function in Pycharm, opens the following function:
def max(self, axis=None, out=None, keepdims=False, initial, *args, **kwargs): # real signature unknown; NOTE: unreliably restored from __doc__
"""
a.max(axis=None, out=None, keepdims=False, initial=<no value>, where=True)
Return the maximum along a given axis.
Refer to `numpy.amax` for full documentation.
See Also
--------
numpy.amax : equivalent function
"""
pass
Which appears to not even do anything. Either way, the code works, only an IDE error is given.
Am I using the wrong function? I know there's amax
and max
, as well as the package level numpy.max
, but the above seems to be unwanted behaviour.
If this is a bug, I wouldn't know how to report it / start an issue or whatever, haha.
Upvotes: 12
Views: 3415
Reputation: 3917
First of all, as others already mentioned, this part is autogenerated. If you want to understand this better you can try to read through NumPy documentation (e.g. How to extend NumPy or FTPY User Guide) :) And there are a lot of other resources on this topic.
I see two questions in your post:
1. Why does the code works when PyCharm shows an error?
Others already pointed out that the implementation of max
function is empty because the code is taken from other place. Anyway, when you check the function signature, it's not valid one (non-default parameter follows default one). This results in PyCharm warning that you should fill initial
parameter because it's unfilled. In fact this is only warning because as you wrote everything is working fine because actual implementation is working (I do not have it in front of me but it should follow what you can see in np.max
).
To get rid of this warning you have three options:
initial
parameternp.max
or np.amax
instead# noinspection PyArgumentList
)If you want to do something with it, it would be better to post this directly to NumPy issue tracker or PyCharm issue tracker).
2. Should I fill initial
parameter?
It's ok to leave this parameter unfilled. You will likely never need to use it (at least I haven't) and a.max()
(where a
is your array) is common way how to calculate maximum and much simpler than a.max(initial=??)
.
But you should note that in some cases this will not work without it. Especially simple example like this will raise ValueError:
import numpy as np
np.empty(0).max() # or np.array([]).max()
But there might be more hidden cases like this (raising the same error because you have an empty slice):
np.array([1, 2, 3])[3:].max()
Upvotes: 2
Reputation: 231335
In an Ipython
session, numpy 1.18, np.max??
(like the charm
click?) shows:
def amax(a, axis=None, out=None, keepdims=np._NoValue,
initial=np._NoValue, where=np._NoValue):
.... docs
return _wrapreduction(a, np.maximum, 'max', axis, None, out,
keepdims=keepdims, initial=initial, where=where)
File: /usr/local/lib/python3.6/dist-packages/numpy/core/fromnumeric.py
So initial
is given this unique numpy
constant ._NoValue
. The actual action is equivalent to
np.maximum.reduce(a, ....)
initial
was added in version 1.15. np.maximum.reduce??
docs gives more information on initial
(which is generally applicable to binary ufunc
).
Upvotes: 0
Reputation: 554
it appears empty because it's not implemented in python, probably C/C++, as you can figure out from # real signature unknown; NOTE: unreliably restored from __doc__
- it's just a hint for you what parameter this function has. It's not even valid python ;)
Basing on documentation of amax:
initial scalar, optional
The minimum value of an output element. Must be present to allow computation on empty slice. See reduce for details.
You'd better pass something to initial
Upvotes: 2