pythonarray去除inf,在Python中使用numpy / scipy忽略数组中的-Inf值

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值