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
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])
Hi all,
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.
Best regards,
Andrei
Reproducing code example:
Numpy/Python version information:
1.17.4 3.7.5 (default, Nov 7 2019, 10:50:52)
[GCC 8.3.0]
The text was updated successfully, but these errors were encountered: