How can I prevent NAN issues?

前端 未结 1 934
[愿得一人]
[愿得一人] 2021-01-28 14:35

I\'m getting Mean of empty slice runtime warnings. When I print out what my variables are (numpy arrays), several of them contain nan values. The Runti

相关标签:
1条回答
  • 2021-01-28 14:59

    I assume the warning comes up in

    np.mean(dataPoints[t2 == [x]], axis=0)
    

    If t2 == [x] is all False (no match between t2 and x, then dataPoints[...] will be an empty array, resulting in the mean warning.

    I think you need to be more careful with that test. Maybe even skip the mean if the masked array is empty.

    == tests with floating values are unpredictable. You need to use something like np.isclose or np.allclose to test equivalence with a tolerance.

    The second warning comes from later in the mean calc, presumably when trying to divide by 0, the number of elements.

    The full mean code can be found in numpy.core._methods.py.

    In sum, don't try to take the mean of an empty array.

    0 讨论(0)
提交回复
热议问题