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 中将货币解析为数字 2022-01-01
- kivy 应用程序中的一个简单网页作为小部件 2022-01-01
- 在 Django Admin 中使用内联 OneToOneField 2022-01-01
- Python 中是否有任何支持将长字符串转储为块文字或折叠块的 yaml 库? 2022-01-01
- Kivy 使用 opencv.调整图像大小 2022-01-01
- 究竟什么是“容器"?在蟒蛇?(以及所有的 python 容器类型是什么?) 2022-01-01
- 对多索引数据帧的列进行排序 2022-01-01
- 比较两个文本文件以找出差异并将它们输出到新的文本文件 2022-01-01
- matplotlib 设置 yaxis 标签大小 2022-01-01
- Python,确定字符串是否应转换为 Int 或 Float 2022-01-01
