python numpy array最大长度_python numpy中的长数组(大于2000万个元素)求和

I am new to python and numpy so please excuse me if this problem is so rudimentary!

I have an array of negative values (it is sorted):

>>>neg

[ -1.53507843e+02 -1.53200012e+02 -1.43161987e+02 ..., -6.37326136e-1 -3.97518490e-10 -3.73480691e-10]

>>>neg.shape

(12922508,)

I need to add this array to its duplicate (but with positive values) to find the standard deviation of the distribution averaged to zero. So I do the following:

>>>pos=-1*neg

>>>pos=pos[::-1] #Just to make it look symmetric for the display bellow!

>>>total=np.hstack((neg,pos))

>>>total

[-153.50784302 -153.20001221 -143.1619873 ..., 143.1619873 153.20001221 153.50784302]

>>>total.shape

(25845016,)

So far everything is very good, but the strange thing is that the sum of this new array is not zero:

>>>numpy.sum(total)

11610.6

The standard deviation is also not at all near what I was expecting but I guess the root of that problem is the same as this: Why doesn't the sum result in zero?

When I apply this method to a small array; for example [-5, -3, -2] the sum becomes zero. So I guess the problem lies in the length of the array (over 20million elements). Is there any way to deal with this problem?

If any one could help me on this I would be most grateful.

解决方案

As noted in the comments, you get float roundoff problems from summing up many millions of equal-signed numbers. One possible way around this could be to mix positive and negative numbers in the combined array, so that any intermediate results while summing up always stay roughly within the same order of magnitude:

neg = -100*numpy.random.rand(20e6)

pos = -neg

combined = numpy.zeros(len(neg)+len(pos))

combined[::2] = neg

combined[1::2] = pos

Now combined.sum() should be pretty close to zero.

Maybe this approach will also help to improve the precision in the computation of the standard deviation.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值