Statistics: combinations in Python(统计:Python 中的组合)
问题描述
我需要在 Python 中计算组合 (nCr),但在 math
、numpy
或 stat
库中找不到执行此操作的函数.类似于以下类型的函数:
I need to compute combinatorials (nCr) in Python but cannot find the function to do that in math
, numpy
or stat
libraries. Something like a function of the type:
comb = calculate_combinations(n, r)
我需要可能的组合数量,而不是实际组合,所以 itertools.combinations
对我不感兴趣.
I need the number of possible combinations, not the actual combinations, so itertools.combinations
does not interest me.
最后,我想避免使用阶乘,因为我要计算组合的数字可能会变得太大,而阶乘会变得非常可怕.
Finally, I want to avoid using factorials, as the numbers I'll be calculating the combinations for can get too big and the factorials are going to be monstrous.
这似乎是一个非常容易回答的问题,但是我被关于生成所有实际组合的问题淹没了,这不是我想要的.
This seems like a REALLY easy to answer question, however I am being drowned in questions about generating all the actual combinations, which is not what I want.
推荐答案
参见 scipy.special.comb(scipy 旧版本中的 scipy.misc.comb).当 exact
为 False 时,它使用 gammaln 函数无需花费太多时间即可获得良好的精度.在确切的情况下,它返回一个任意精度的整数,这可能需要很长时间来计算.
See scipy.special.comb (scipy.misc.comb in older versions of scipy). When exact
is False, it uses the gammaln function to obtain good precision without taking much time. In the exact case it returns an arbitrary-precision integer, which might take a long time to compute.
这篇关于统计:Python 中的组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:统计:Python 中的组合


基础教程推荐
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- 筛选NumPy数组 2022-01-01