QBoard » Statistical modeling » Stats - Tech » Statistics: combinations in Python

Statistics: combinations in Python

  • 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)​

    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.

      December 22, 2020 4:40 PM IST
    0
  • # A Python program to print all
    # combinations of given length
    from itertools import combinations
     
    # Get all combinations of [1, 2, 3]
    # and length 2
    comb = combinations([1, 2, 3], 2)
     
    # Print the obtained combinations
    for i in list(comb):
        print (i)
      August 6, 2021 9:01 PM IST
    0
  • 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.
      December 28, 2020 11:53 AM IST
    0