67

I have an array:

x =  numpy.array([-inf, -inf, 37.49668579])

Is there a way to change the -inf values to just 0?

feedMe
  • 3,431
  • 2
  • 36
  • 61
user1821176
  • 1,141
  • 2
  • 18
  • 29

3 Answers3

105

There is:

from numpy import inf
x[x == -inf] = 0
pv.
  • 33,875
  • 8
  • 55
  • 49
24

Use isneginf http://docs.scipy.org/doc/numpy/reference/generated/numpy.isneginf.html#numpy.isneginf

x[numpy.isneginf(x)] = 0
YXD
  • 31,741
  • 15
  • 75
  • 115
24

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])
pr94
  • 1,263
  • 12
  • 24