'Replace -inf with zero value
I have an array:
x = numpy.array([-inf, -inf, 37.49668579])
Is there a way to change the -inf values to just 0?
Solution 1:[1]
Use isneginf http://docs.scipy.org/doc/numpy/reference/generated/numpy.isneginf.html#numpy.isneginf
x[numpy.isneginf(x)] = 0
Solution 2:[2]
Perhaps even easier and more flexible to use numpy.nan_to_num:
numpy.nan_to_num(x, neginf=0)
Out[1]: array([ 0. , 0. , 37.49668579])
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | YXD |
| Solution 2 |
