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

Question:

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

Asked By: Accelerator

||

Answers:

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)
Answered By: Alon Alush
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.