use*_*176 36 python arrays numpy infinity
我有一个数组:
x = numpy.array([-inf, -inf, 37.49668579])
Run Code Online (Sandbox Code Playgroud)
有没有办法将-inf值更改为0?
pv.*_*pv. 59
有:
from numpy import inf
x[x == -inf] = 0
Run Code Online (Sandbox Code Playgroud)
YXD*_*YXD 14
使用isneginf http://docs.scipy.org/doc/numpy/reference/generated/numpy.isneginf.html#numpy.isneginf
x[numpy.isneginf(x)] = 0
Run Code Online (Sandbox Code Playgroud)
也许使用起来更简单、更灵活numpy.nan_to_num:
numpy.nan_to_num(x, neginf=0)
Out[1]: array([ 0. , 0. , 37.49668579])
Run Code Online (Sandbox Code Playgroud)