Replace -inf with zero value

PythonArraysNumpyInfinity

Python Problem Overview


I have an array:

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

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

Python Solutions


Solution 1 - Python

There is:

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

Solution 2 - Python

Solution 3 - Python

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser1821176View Question on Stackoverflow
Solution 1 - Pythonpv.View Answer on Stackoverflow
Solution 2 - PythonYXDView Answer on Stackoverflow
Solution 3 - Pythonpr94View Answer on Stackoverflow