np.clip fails to clip to upper bound
See original GitHub issueWhen setting a_max
to 1 - 1e-15
, the value is clipped to 1.0
and not 0.999999999999999
.
I’m not sure if this is a floating point representation issue or a NumPy bug, but simply printing the a_max
argument does not yield the same result as the clipped value.
a_max = 1 - 1e-15
print(a_max)
>>> 0.999999999999999
print(np.clip([0, 1, 2], a_min=1e-15, a_max=a_max))
>>> [ 1.00000000e-15 1.00000000e+00 1.00000000e+00]
print(min(a_max, 1))
>>> 0.999999999999999
I stumbled upon this as sklearn’s log_loss
metric function clips the probabilities between [eps, 1-eps], with the default epsilon set to 1e-15.
I’m using Python 3.6 with NumPy 1.13.1+mkl on Windows 10 64bit.
Issue Analytics
- State:
- Created 5 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
Numpy Clip | How to Use np.clip() Function in Python
The numpy clip() is an essential function available in the numpy module ... numpy clip function is to limit the upper value and...
Read more >numpy.clip — NumPy v1.24 Manual
Clip (limit) the values in an array. Given an interval, values outside the interval are clipped to the interval edges. For example, if...
Read more >NumPy clip(): Limit Array Values with Min and Max - Datagy
The NumPy clip() function will clip, or limit, the values in an array. This allows you to set a lower and/or upper limit...
Read more >How to Use NumPy clip() in Python - Spark by {Examples}
NumPy clip () function in Python is used to clip(limit) the elements in an array. In the clip() function, pass the interval(combination of ......
Read more >Pythonic way to replace list values with upper and lower ...
You can use numpy.clip : In [1]: import numpy as np In [2]: arr = np.array([0, 1, 2, 3, 4, 5, 6, 7,...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@GalAvineri as noted in the previous comment, the datatype you are using is
np.float32
, so the input the function is implicitly cast to:which is the same as:
So this should be expected, and while I cannot fully rule out it may change at some point, you should expect such things. There are some issues with deciding what the correct data type is here, but as long as the results datatype
b_clipped.dtype
is a float32, this is expected.I see. So using higher precision should solve the issue than 😃 Thank you!