Home > database >  Sum all possible combinations of an array with python
Sum all possible combinations of an array with python

Time:11-14

Have a good day. I have tried to find a real solution for the sum of all possible combinations of an array but have not found any so far using python.

Commonly people ask about the following combinatorics:

Given: a, b, c, d and e as records:

a b
a c
a d
a e
b c
b d
b e
c d
c e
d e

The real combination would be:

a b
a c
a d
a e
a b c
a b d
a b e
a c d
a c e
a d e
a b c d
a b c e
a c d e
a b c d e
b c
b d
b e
b c d
b c e
b d e
c d
c e
c d e
d e

So, the result I need to obtain is the following, but so far I have not found how to iterate it to cover any size of the array:

| COMBINATION |      SUM     |
|-------------|--------------|
|a b          |           XXX|
|a c          |           XXX|
|a d          |           XXX|
|a e          |           XXX|
|a b c        |           XXX|
|a b d        |           XXX|
|a b e        |           XXX|
|a c d        |           XXX|
|a c e        |           XXX|
|a d e        |           XXX|
|a b c d      |           XXX|
|a b c e      |           XXX|
|a c d e      |           XXX|
|a b c d e    |           XXX|
|b c          |           XXX|
|b d          |           XXX|
|b e          |           XXX|
|b c d        |           XXX|
|b c e        |           XXX|
|b d e        |           XXX|
|c d          |           XXX|
|c e          |           XXX|
|c d e        |           XXX|
|d e          |           XXX|

Note that the combination size is variable and must cover the total number of records in the array. Anyone have any ideas, possibly with the mathematical algorithm that is actually being applied here?

CodePudding user response:

# Look at all subsets from size 2 up to the whole thing
for size in range(2, len(records)   1):
   # Iterate through all subsets of size "size"
   for subset in itertools.combinations(records, size):
       # subset will be a subset of the records. Feel free
       # to grab their sum and print them out however you want

Edited: Accidentally mistyped combinations as combination. Sorry.

CodePudding user response:

You can try the following:

import pandas as pd
import numpy as np
from itertools import product

a = np.array([2, 6, 7])
df = pd.DataFrame(product(*[[0, 1]] * len(a)), columns=a)
df['sum'] = (df * a).sum(axis=1)
print(df)

This gives:

       2  6  7  sums
    0  0  0  0     0
    1  0  0  1     7
    2  1  0  0     2
    3  1  0  1     9
    4  0  1  0     6
    5  0  1  1    13
    6  1  1  0     8
    7  1  1  1    15

The first three columns of the dataframe are labeled with elements of the array a. The value of 0 in a column indicates that the corresponding element of a is used in a sum, and 0 that it is not used. The last column gives sums of the elements of a selected in this way.

  • Related