question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

np.clip fails to clip to upper bound

See original GitHub issue

When 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:closed
  • Created 5 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
sebergcommented, May 3, 2019

@GalAvineri as noted in the previous comment, the datatype you are using is np.float32, so the input the function is implicitly cast to:

b_clipped = np.clip(b , np.float32(eps), np.float32(1 - eps))

which is the same as:

b_clipped = np.clip(b , 0, 1)

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.

0reactions
GalAvinericommented, May 3, 2019

I see. So using higher precision should solve the issue than 😃 Thank you!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found