change the values of an array from nan to zero

后端 未结 2 1945
野的像风
野的像风 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)
    0 讨论(0)
  • 2020-12-21 02:18

    You can use the numpy function to change all the nan value at matrix x to zero.

    numpy.nan_to_num(x)
    
    0 讨论(0)
提交回复
热议问题