Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ignoring negative values when using np.log(array)

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.

like image 960
GCien Avatar asked Dec 11 '22 04:12

GCien


2 Answers

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.])
like image 62
Warren Weckesser Avatar answered Dec 31 '22 20:12

Warren Weckesser


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))
like image 40
Saullo G. P. Castro Avatar answered Dec 31 '22 21:12

Saullo G. P. Castro