I am trying to solve for m1,m2,m3,m4 in the set of equations such that:
y=(m1*x1)+(m2*x2)+(m3*x3)+(m4*x4)
Where:
x1=[x11,x12,x13...]
x2=[x21,x22,x23...]
x3=[x31,x32,x33...]
x4=[x41,x42,x43...]
y=[y1,y2,y3,y4,...]
I have been trying to do:
m=numpy.linalg.lstsq(A,y)[0]
Where:
A = [[x11,x21,x31,x41],[x12,x22,x32,x42],.....]
But my results are not matching a similar analysis done in Mat-lab.
Any suggestions or issues you might see would be very helpful!
Thanks.
It seems you are building your A
matrix incorrectly. Moreover, np.linalg.lstsq
seems to only accept a 2D
array for A
, which means that you can do the least-square for one variable at once. Try this:
nvar = 4
one = np.ones(x1.shape)
A = np.vstack((x1,one,x2,one,x3,one)).T.reshape(nvar,x1.shape[0],2)
for i,Ai in enumerate(A):
a = np.linalg.lstsq(Ai,y)[0]
R = np.sqrt( ((y - Ai.dot(a))**2).sum() )
print R
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With