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

前端 未结 5 553
说谎
说谎 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:46

    Use masked arrays:

    >>> a = numpy.array([2, 0, 1.5, -3])
    >>> b = numpy.ma.log(a)
    >>> b
    masked_array(data = [0.69314718056 -- 0.405465108108 --],
                 mask = [False  True False  True],
           fill_value = 1e+20)
    
    >>> b.sum()
    1.0986122886681096
    

提交回复
热议问题