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?

3 Answers

There is:

from numpy import inf
x[x == -inf] = 0
6

Use isneginf

x[numpy.isneginf(x)] = 0

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])

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Robert Thorne

Robert Thorne

Automotive & Future Transportation Editor

Robert Thorne covers electric vehicle innovations, autonomous driving systems, global mobility trends, and automotive engineering developments.

Share this article
Twitter Facebook Pinterest