统计:Python 中的组合

Statistics: combinations in Python(统计:Python 中的组合)

本文介绍了统计:Python 中的组合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在 Python 中计算组合 (nCr),但在 mathnumpystat 库中找不到执行此操作的函数.类似于以下类型的函数:

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 中的组合

基础教程推荐