0

I have one dataframe/matrix which is MxJ and looks like this:

**dataframe: R**

index   0 1 2 3 ... J #columns, go from 0 to J

0       3 0 5 6 ... #values - first row
1       .........
....................
M

and one more matrix/dataframe which is JxJ and its made from 0 and 1, looks as this:

**dataframe: K / its symetric**

index   0 1 2 3 ... J #columns, go from 0 to J

0       1 0 1 0 ...
1       0 1 1 1 ...
2       0 1 1 1 ...
       .........
....................
J

I need for each row to calculate

sum[0 to J](sum[0 to J](K[i,j]*r[i]*r[j])

Basically for the first row the concrte example is: [(3*3)*1]+[(3*0)*0]+[(3*5)*1]+[(3*6)*0]+[(0*0)*1]+[(0*5)*1]+[(0*6)*1]+[(5*5)*1]+[(5*6)*1]. So the final result should be a 1xM.

I know how to do it with a 3 for loops, pseudo code for it:

sum = []
#J - number of columns
#M - number of rows
for m in range(o,M)
  for i in range(0, J):
    for j in range (i,J):
       sum[m] = sum[m] + (a[i] * a[j]) * wi[i,j]

But for loops isn't an option, because my data is huge. Can someone explain me how to do that cartesian product with rows (that part i dont know how to do it) in more pandas way.

Anajlim
  • 71
  • 6

1 Answers1

0

You can convert the rows to lists and use the following code:

    import itertools
def get_cartesian_product(input_lists):
    lst_out = []
    for element in itertools.product(*input_lists):
            lst_out.append(element)
    return lst_out
paparissam
  • 57
  • 9