Home > Back-end >  Matrix are not aligned for dot product
Matrix are not aligned for dot product

Time:03-18

Here is my df matrix:

    0   Rooms   Area    Price
0   0   0.4 0.32    0.307692
1   0   0.4 0.40    0.461538
2   0   0.6 0.48    0.615385
3   0   0.6 0.56    0.646154
4   0   0.6 0.60    0.692308
5   0   0.8 0.72    0.769231
6   0   0.8 0.80    0.846154
7   0   1.0 1.00    1.000000

Here is my B matrix:

weights
0   88
1   87
2   44
3   46

When I write df.dot(B) it says matrix are not aligned. But here df is 8*4 matrix and B is 4*1 So shouldn't it generate a `8*1 matrix as a dot product?

errors: ValueError: matrices are not aligned

CodePudding user response:

You can just do mul

out = df.mul(B['weights'].values,axis=1)
Out[207]: 
   0  Rooms   Area      Price
0  0   34.8  14.08  14.153832
1  0   34.8  17.60  21.230748
2  0   52.2  21.12  28.307710
3  0   52.2  24.64  29.723084
4  0   52.2  26.40  31.846168
5  0   69.6  31.68  35.384626
6  0   69.6  35.20  38.923084
7  0   87.0  44.00  46.000000

CodePudding user response:

"the column names of DataFrame and the index of other must contain the same values,"

https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.dot.html

Try renaming the index of B to match the column names of A.

  • Related