[SciPy-user] Python - Matlab least squares difference

A. Rachel Grinberg agree74 at hotmail.com
Sat Jun 17 01:21:28 CDT 2006


Thanks for your response.

The solution to the least square problem minimizes the l2 norm of the 
residuals. In my example the residual of Matlab's solution is 
||A*(0,0,1)'-b|| = ||0|| = 0, whereas python's solution yields a number that 
is very close to zero.


A. Rachel Grinberg wrote:
>I noticed a difference between the linear least square solutions in Python 
>and Matlab. I understand that if the system is underdetermined the solution 
>is not going to be unique, nevertheless I would like figure out the 
>algorithm Matlab is using, since it seems "better" to me. For example, 
>let's say I have a Matrix
>       2 1 0
>A = 1 1 1 and b = (0,1)'
>While Matlab yields (0,0,1)' as the solution to A\b, scipy's result for 
>linalg.linear_least_squares(A,b) is
>        [ 0.33333333],
>        [ 0.83333333]])
>Any ideas, how Matlab's A\b is implemented?

Not sure, but it's wrong (more or less). In underdetermined linear least 
problems, it is conventional to choose the solution that has minimum 

>>>A = array([[2., 1, 0], [1, 1, 1]])
array([[ 2.,  1.,  0.],
       [ 1.,  1.,  1.]])
>>>b = array([0., 1.])
>>>linalg.lstsq(A, b)
(array([-0.16666667,  0.33333333,  0.83333333]), array([], dtype=float64), 
array([ 2.6762432 ,  0.91527173]))
>>>x = _[0]
>>>dot(x, x)
>>>dot([0., 0, 1], [0., 0, 1])

Implementations that do something else have some 'splainin' to do. Of 
A\b may not quite be "do linear least squares" in Matlab but something else. 
don't know. FWIW, Octave gives me the same answer as numpy/scipy.

Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it 
an underlying truth."
  -- Umberto Eco

Express yourself instantly with MSN Messenger! Download today - it's FREE! 

More information about the SciPy-user mailing list