Ignoring negative values when using np.log(array)

帅比萌擦擦* 提交于 2021-01-21 09:18:23

问题


When taking the log of a specific column within a numpy array, i.e., logSFROIIdC = np.log(data_dC[:, 9]) the compiler returns the error:

-c:13: RuntimeWarning: divide by zero encountered in log.

Now, I know why this happens, i.e., log(-1) = Math Error.

However, I want to be able to call something or write some code which then skips any value in the array which would cause this error, then ignoring that row altogether. Allowing that data column to be usable again.

I have tried various methods and this is a last resort asking the community.


回答1:


You can control this behavior with np.seterr. Here's an example.

First, tell numpy to ignore invalid values:

In [4]: old = np.seterr(invalid='ignore')

Now log(-1) doesn't generate a warning:

In [5]: x = np.array([-1.,1])

In [6]: np.log(x)
Out[6]: array([ nan,   0.])

Restore the previous settings:

In [7]: np.seterr(**old)
Out[7]: {'divide': 'warn', 'invalid': 'ignore', 'over': 'warn', 'under': 'ignore'}

And now we get the warning:

In [8]: np.log(x)
/Users/warren/anaconda/bin/ipython:1: RuntimeWarning: invalid value encountered in log
  #!/Users/warren/anaconda/python.app/Contents/MacOS/python
Out[8]: array([ nan,   0.])

There is also a context manager, np.errstate. For example,

In [10]: with np.errstate(invalid='ignore'):
   ....:     y = np.log(x)
   ....:     

In [11]: y
Out[11]: array([ nan,   0.])



回答2:


You can also use a masked array and NumPy will automatically apply a mask for the invalid values after you perform the np.log() calculation:

a = np.array([1,2,3,0,4,-1,-2])
b = np.log(np.ma.array(a))

print(b.sum())
# 3.17805383035

Where np.ma.array(a) is creating a masked array with no masked elements. It works because NumPy automatically masks elements that are inf (or any invalid value) in calculations with masked arrays.

Alternatively, you could have created the mask yourself (which I recommend) like:

a = np.ma.array(a, mask=(a<=0))



回答3:


One hack is to limit the values from being negative in the first place. np.clip to the rescue.

positive_array = np.clip(array, some_small_positive_value, None) to avoid negative values in your array. Though I am not sure if bringing the values close to zero serve your purpose.



来源:https://stackoverflow.com/questions/24972493/ignoring-negative-values-when-using-np-logarray

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!