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.log(arr, where=bools) does not return arr[bools] for bools=False

See original GitHub issue

Hi,

I recently have been debugging a script that takes the log of a 3D image and found that the solution was to circumvent the where argument that you can give the numpy.log function.

This was the orignal code:

print('min',image.min())
print('max', image.max())
image = np.divide(image, image.max())
print('min',image.min())
print('max', image.max())
image = np.log(image, where=image>0)
print('min',image.min())
print('max', image.max())

With the following output:

    min 0.0
    max 9.1653565625
    min 0.0
    max 1.0
    min -9.86549829948
    max 1.0

But when you take the log of a positive number (excluding 0), the answer should always be negative!

The where argument, according to the numpy documentation should do the following:

Values of True indicate to calculate the ufunc at that position, values of False indicate to leave the value in the output alone.

Thus either, the log of some values are wrong or the where argument does not do what the documentation says it should do.

When I started the script with image = image + 0.00001, it did give the intended behaviour:

min 1e-05
max 9.1653665625
min 1.091063836e-06
max 1.0
min -13.7283573414
max 0.0

This would mean that the where argument does not leave the value alone, but changes it. Since in this case, the values that are changed by log are 0 and the log of 0 is not defined, it is unclear what log should change it to…

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
jaimefriocommented, Jun 13, 2017

Yes, it wasn’t obvious to me what where= does either until I read the documentation. I’m going to reopen your issue: we should at the very least explicitly say that if you provide where= but not out=, the result will have uninitialized memory in the positions marked False by where=.

0reactions
mattipcommented, Sep 6, 2018

Closing, please reopen if needed.

Read more comments on GitHub >

github_iconTop Results From Across the Web

numpy.log — NumPy v1.24 Manual
The natural logarithm log is the inverse of the exponential function, ... If not provided or None, a freshly-allocated array is returned.
Read more >
numpy: log with -inf not nans - python - Stack Overflow
There are two serious problems with this answer. 1. The function prints instead of returning. That's not good! 2. The function will not...
Read more >
How to Use NumPy log() in Python? - Spark by {Examples}
NumPy log() function in Python is used to compute the natural logarithm of x where x, such that all the elements of the...
Read more >
Numpy log explained - Sharp Sight
This tutorial will show you how to use the Numpy log function. It explains the syntax of np.log and provides clear, step-by-step examples....
Read more >
numpy.log() in Python - GeeksforGeeks
The numpy.log() is a mathematical function that helps user to calculate Natural logarithm of x where x belongs to all the input array ......
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