Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bug in Numpy ndarray min/max method

Tags:

python

numpy

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.

like image 224
MrBobJamesBob Avatar asked Jun 02 '20 12:06

MrBobJamesBob


People also ask

Is Ndarray same as NumPy array?

numpy. array is just a convenience function to create an ndarray ; it is not a class itself. You can also create an array using numpy. ndarray , but it is not the recommended way.

Is NP min faster than min?

min is slower than builtin min for small arrays, occasionally VERY slow #12350.

Which function is used to size Ndarray?

len() is the Python built-in function that returns the number of elements in a list or the number of characters in a string. For numpy. ndarray , len() returns the size of the first dimension.

How do you find the max of a NP array?

Given a numpy array, you can find the maximum value of all the elements in the array. To get the maximum value of a Numpy Array, you can use numpy function numpy. max() function.


2 Answers

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

like image 143
konserw Avatar answered Oct 20 '22 14:10

konserw


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:

  • always fill initial parameter
  • use np.max or np.amax instead
  • disable PyCharm warning for the given statement (with # 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()
like image 30
Nerxis Avatar answered Oct 20 '22 12:10

Nerxis