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 returns -Inf, so I will have a matrix with some -Inf values as a result. I then want to sum over the columns of this matrix, but ignoring the -Inf values -- how can I do this?
For example,
mylogarray = log(myarray)
# take sum, but ignore -Inf?
sum(mylogarray, 0)
I know there's nansum and I need the equivalent, something like infsum.
Thanks.
解决方案>>> 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