Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

numpy multivarient regression with linalg.lstsq

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.

like image 542
Rowan_Gaffney Avatar asked Nov 13 '22 03:11

Rowan_Gaffney


1 Answers

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
like image 153
Saullo G. P. Castro Avatar answered Nov 14 '22 21:11

Saullo G. P. Castro