python的计算怎么那么慢_python – 为什么是statistics.mean()这么慢?

Python的statistics模块注重精度而非速度,导致statistics.mean()在处理浮点数时速度较慢。math.fsum可以提高精度但可能导致不必要的类型转换。相比之下,Numpy的mean函数虽然精度较低,但速度快,使用pairwise summation算法。对于速度敏感的场景,推荐使用Numpy。
摘要由CSDN通过智能技术生成

Python的统计模块不是为速度,而是为精度

The built-in sum can lose accuracy when dealing with floats of wildly

differing magnitude. Consequently, the above naive mean fails this

“torture test”

assert mean([1e30, 1, 3, -1e30]) == 1

returning 0 instead of 1, a purely computational error of 100%.

Using math.fsum inside mean will make it more accurate with float

data, but it also has the side-effect of converting any arguments to

float even when unnecessary. E.g. we should expect the mean of a list

of Fractions to be a Fraction, not a float.

相反,如果我们看一下在这个模块中的_sum()的实现,方法的docstring seem to confirm that的第一行:

def _sum(data, start=0):

"""_sum(data [, start]) -> (type, sum, count)

Return a high-precision sum of the given numeric data as a fraction,

together with the type to be converted to and the count of items.

[...] """

所以,sum的统计实现,而不是一个简单的单线程调用Python的内置sum()函数,本身需要大约20行,其中有一个嵌套for循环。

这是因为statistics._sum选择保证它可能遇到的所有类型的数字的最大精度(即使它们彼此差别很大),而不是简单地强调速度。

因此,看起来正常的是,内置的和证明了一百倍更快。它的成本是一个低得多的精度在你碰巧用异国情调的数字。

其他选项

如果你需要在你的算法的优先级的速度,你应该看看Numpy相反,其实现在C的算法。

NumPy的平均值不如通过长时间统计的精确,但它实现(自2013年)一个routine based on pairwise summation这比天真的sum / len(更多的信息在链接)。

然而…

import numpy as np

import statistics

np_mean = np.mean([1e30, 1, 3, -1e30])

statistics_mean = statistics.mean([1e30, 1, 3, -1e30])

print('NumPy mean: {}'.format(np_mean))

print('Statistics mean: {}'.format(statistics_mean))

> NumPy mean: 0.0

> Statistics mean: 1.0

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值