Ignoring -Inf values in arrays using numpy/scipy in Python

前端 未结 5 541
说谎
说谎 2020-12-25 14:11

I have an NxM array in numpy that I would like to take the log of, and ignore entries that were negative prior to taking the log. When I take the log of negative entries, it

5条回答
  •  失恋的感觉
    2020-12-25 14:36

    The easiest way to do this is to use numpy.ma.masked_invalid():

    a = numpy.log(numpy.arange(15))
    a.sum()
    # -inf
    numpy.ma.masked_invalid(a).sum()
    # 25.19122118273868
    

提交回复
热议问题