Home > database >  Pandas dot product returns "ValueError: matrices are not aligned" although there is no sha
Pandas dot product returns "ValueError: matrices are not aligned" although there is no sha

Time:07-04

I'm trying to multiply a square matrix by itself.

df2 = pd.DataFrame({'C1':[0, 0, 0.7],  
                   'C2':[0.5, 0, 0],
                    'C3':[0.7, 0.3, 0]
                   }
                 )

Trying to do dot product:

df2.dot(df2)

The outcome should be

0.49  0.00  0.15
0.21  0.00  0.00
0.00  0.35  0.49

However, I got the following error.

ValueError: matrices are not aligned

There should not be a value error since there is no issue with the dimensions. What is the problem?

PS: df2.multiply(df2) is NOT the solution since it's an element-wise multiplication.

PS2: I am looking for a method that will give the same result as the "mmul" function in Excel.

CodePudding user response:

According to enter image description here

you can use numpy array instead

out = df2.dot(df2.values)
print(out)

      0     1     2
0  0.49  0.00  0.15
1  0.21  0.00  0.00
2  0.00  0.35  0.49
  • Related