change the values of an array from nan to zero

后端 未结 2 1958
野的像风
野的像风 2020-12-21 01:37

I have an array A on python that has some nan values created by numpy.nan. I want to set all the nan values to zero using A[A==numpy.nan] = 0. It doesn\'t chang

2条回答
  •  天命终不由人
    2020-12-21 02:10

    You want np.isnan:

    A[np.isnan(A)] = 0
    

    The problem with your code is that (according to IEEE), nan doesn't equal anything -- even itself.

    As a side note, there's also

    • np.isinf -> (+/- infinity)
    • np.isfinite -> (not infinity or NaN)
    • np.isposinf -> (+ infinity)
    • np.isneginf -> (- infinity)

提交回复
热议问题