Search code examples
pythonmathleast-squares

bad operand type when using np.linalg.lstsq in python


I am trying to use np.linalg.lstsq to solve a problem,

I started with simple arrays to solve the equation:

CX = D

where

C = [[66.31835487 67.46962851 58.22702243] [67.46962851 68.65912117 59.24895075] [58.22702243 59.24895075 51.56007083]]

D [0.01144368 0.01164468 0.01004645] `

I used:

x = - np.linalg.lstsq(C, D, rcond=None)`

But I received the error:

`---> 28             x = - np.linalg.lstsq(C, D, rcond=None) 

TypeError: bad operand type for unary -: 'tuple'`

I solved the same problem using np.linalg.solve and it was fine so i am not sure why the leastsquare did no solve it


Solution

  • try this

    import numpy as np
    
    C = np.array([[66.31835487, 67.46962851, 58.22702243], 
                  [67.46962851, 68.65912117, 59.24895075], 
                  [58.22702243, 59.24895075, 51.56007083]])
    D = np.array([0.01144368, 0.01164468, 0.01004645])
    
    x = -np.linalg.lstsq(C, D, rcond=None)[0]
    
    print(x)