You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Wrong gradient values for unsigned int data #15207
The "numpy.gradient()" function returns wrong values for unsigned integer types. While the outputs are of np.float64 type, a part of the calculations seem to be done following the rules of the input data type (unsigned int). In our case, a negative gradient of -1 is wrapped around to a value of 255, -2 becomes 254, and so forth. The resulting gradient values can be non-integer because of some averaging taking place during the calculations.
I think the input data should first be cast to np.float64 and only then calculate the gradient.
It is useless to get correct digits after the comma, yet get huge errors before the comma.
Below you can find the code that highlights the erroneous behavior.
In [62]: y = np.array([2.0, 3.0])
In [63]: x = np.array([-10, 127], dtype=np.int8)
In [64]: np.gradient(y, x) # Incorrect.
Out[64]: array([-0.00840336, -0.00840336])
In [65]: np.gradient(y, x.astype(float)) # This gives the expected result.
Out[65]: array([0.00729927, 0.00729927])
Activity
WarrenWeckesser commentedon Dec 31, 2019
@arares, thanks for reporting the issue.
The problem still exists in the development branch:
WarrenWeckesser commentedon Dec 31, 2019
Another broken case:
BUG: lib: Fix handling of integer arrays by gradient.