- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
现在是否可以确定 scipy.misc.comb
确实比 ad-hoc 实现更快?
根据旧答案,Statistics: combinations in Python , 这个自制函数在计算组合时比 scipy.misc.comb
快 nCr
:
def choose(n, k):
"""
A fast way to calculate binomial coefficients by Andrew Dalke (contrib).
"""
if 0 <= k <= n:
ntok = 1
ktok = 1
for t in xrange(1, min(k, n - k) + 1):
ntok *= n
ktok *= t
n -= 1
return ntok // ktok
else:
return 0
但是在我自己的机器上运行了一些测试之后,情况似乎不是这样,使用这个脚本:
from scipy.misc import comb
import random, time
def choose(n, k):
"""
A fast way to calculate binomial coefficients by Andrew Dalke (contrib).
"""
if 0 <= k <= n:
ntok = 1
ktok = 1
for t in xrange(1, min(k, n - k) + 1):
ntok *= n
ktok *= t
n -= 1
return ntok // ktok
else:
return 0
def timing(f):
def wrap(*args):
time1 = time.time()
ret = f(*args)
time2 = time.time()
print '%s function took %0.3f ms' % (f.__name__, (time2-time1)*1000.0)
return ret
return wrap
@timing
def test_func(combination_func, nk):
for n,k in nk:
combination_func(n, k)
nk = []
for _ in range(1000):
n = int(random.random() * 10000)
k = random.randint(0,n)
nk.append((n,k))
test_func(comb, nk)
test_func(choose, nk)
我得到以下输出:
$ python test.py
/usr/lib/python2.7/dist-packages/scipy/misc/common.py:295: RuntimeWarning: overflow encountered in exp
vals = exp(lgam(N+1) - lgam(N-k+1) - lgam(k+1))
999
test_func function took 32.869 ms
999
test_func function took 1859.125 ms
$ python test.py
/usr/lib/python2.7/dist-packages/scipy/misc/common.py:295: RuntimeWarning: overflow encountered in exp
vals = exp(lgam(N+1) - lgam(N-k+1) - lgam(k+1))
999
test_func function took 32.265 ms
999
test_func function took 1878.550 ms
时间分析测试是否表明新的 scipy.misc.comb
比 ad-hoc choose()
函数更快?我的测试脚本是否有任何错误导致计时不准确?
为什么现在 scipy.misc.comb
更快了?是因为一些 cython
/c
包装技巧?
@WarrenWeckesser 评论后:
在使用 scipy.misc.comb()
时使用默认的浮点近似,计算会因浮点溢出而中断。
(参见 http://docs.scipy.org/doc/scipy-0.16.0/reference/generated/scipy.misc.comb.html 获取文档)
当使用 exact=True
进行测试时,使用以下函数使用长整数而不是 float 进行计算,计算 1000 个组合时会慢很多:
@timing
def test_func(combination_func, nk):
for i, (n,k) in enumerate(nk):
combination_func(n, k, exact=True)
[出]:
$ python test.py
test_func function took 3312.211 ms
test_func function took 1764.523 ms
$ python test.py
test_func function took 3320.198 ms
test_func function took 1782.280 ms
最佳答案
引用scipy.misc.comb的源码,结果的更新例程为:
val = 1
for j in xrange(min(k, N-k)):
val = (val*(N-j))//(j+1)
return val
而您建议的更新程序是:
ntok = 1
ktok = 1
for t in xrange(1, min(k, n - k) + 1):
ntok *= n
ktok *= t
n -= 1
return ntok // ktok
我对 SciPy 实现速度较慢的原因的猜测是因为子例程在每次迭代中都涉及整数除法,而您的子例程只在 return 语句中调用一次除法。
关于python - `scipy.misc.comb` 比临时二项式计算快吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33468821/
使用 glm 时,binomial、binomial() 和 'binomial' 之间有什么区别。它们并不相同,如以下代码所示: > library(MASS) > bwdf = birthwt[-
我一直在用我的(非 r-savvy)大脑来让 R 产生二项式 glmer 模型的正确预测的百分比。我知道这不是统计上的 super 信息,但经常被报道;所以我也想举报。 数据: 因变量:Tipo,它有
我一直在寻找一种方法来使数据符合 beta 二项分布并估计 alpha 和 beta,类似于 VGAM 库中的 vglm 包的方式。我一直无法找到如何在 python 中执行此操作。有一个 scipy
如何在 Julia 中提取一般线性模型中指定的数据分布?例如,下面我安装了一个玩具示例 Poisson GLM。我想从模型中提取一个字符串“Poisson”。同样,如果使用数据分布指定模型 = Bin
我是一名优秀的程序员,十分优秀!