简体   繁体   中英

Understanding numpy's lstsq

I understand the idea of the sum of least square solutions. The parameters of the solution reflect the coefficients that minimize the squared error. But I am having trouble understanding the lstsq function available from numpy.linalg . For example, I tried the following:

m1 = np.asarray([[1,2],[3,4],[5,6],[7,8]])

m2 = np.asarray([[9,10],[11,12],[13,14],[15,16]])


solution = np.linalg.lstsq(m1, m2)[0]

The value of solution is:

array([[-7., -8.],
   [ 8.,  9.]])

What does this output mean? I cannot visualize/understand this result.

I'll go out on a limb. The method returns m and c for the equation y=mx+c . When you pass a 2d array for the b parameter, you get two fits - one for the first column and one for the second; as if you were asking for fits on two different data sets/vectors.

In [22]: sol
Out[22]: 
array([[-7., -8.],
       [ 8.,  9.]])

In [23]: sol[:,0], sol[:,1]
Out[23]: (array([-7.,  8.]), array([-8.,  9.]))

In [24]: np.linalg.lstsq(m1,m2[:,0])[0]
Out[24]: array([-7.,  8.])

In [25]: np.linalg.lstsq(m1,m2[:,1])[0]
Out[25]: array([-8.,  9.])


In [30]: np.linalg.lstsq(m1, np.array([9,11,13,15]))[0]
Out[30]: array([-7.,  8.])

In [31]: np.linalg.lstsq(m1, np.array([10,12,14,16]))[0]
Out[31]: array([-8.,  9.])

Your question is more or less a math one. np.linalg.lstsq(m1, m2) finds x such that m1(x) = m2 , similar to solving Ax = b .

Since m1 and m2 are both 4 by 2, for the left hand side multiplication to be compatible, x should be 2 by 2.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM