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?

Asked By: user1821176



There is:

from numpy import inf
x[x == -inf] = 0
Answered By: pv.

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])
Answered By: pr94
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.