NumPy: calculate averages with NaNs removed

前端 未结 12 2531
慢半拍i
慢半拍i 2020-11-27 18:45

How can I calculate matrix mean values along a matrix, but to remove nan values from calculation? (For R people, think na.rm = TRUE).

Here

12条回答
  •  天涯浪人
    2020-11-27 19:14

    From numpy 1.8 (released 2013-10-30) onwards, nanmean does precisely what you need:

    >>> import numpy as np
    >>> np.nanmean(np.array([1.5, 3.5, np.nan]))
    2.5
    

提交回复
热议问题