[Numpy-discussion] Help with linear_least_squares

Christopher Barker Chris.Barker at noaa.gov
Wed Mar 28 14:08:22 EDT 2007



Brad Malone wrote:
> Hi, I'm running into an error with linear least squares that I'm not 
> sure what to do about.  For reference, I'm trying to fit a curve 
> corresponding to the example at the bottom of this wikipedia page: 
> http://en.wikipedia.org/wiki/Linear_least_squares
> 
> My code looks like this:
> 
>     from numpy import *
>     import LinearAlgebra
>     A=array([0,1,2,1,4,1,-1,1])
>     A.shape=(4,2)
>     b=array([3,3,4,2])
>     b.shape= (4,1)
>     sol=LinearAlgebra.linear_least_squares(A,b)

Your using the old Numeric LinearAlgebra module with the new numpy;s 
arrays. Try this:

 >>> from numpy import linalg
 >>> sol=linalg.lstsq(A,b)
 >>> sol
(array([[ 0.33898305],
        [ 2.57627119]]), array([ 0.30508475]), 2, array([ 4.72879916, 
1.62433326]))

-Chris

-- 
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

Chris.Barker at noaa.gov



More information about the NumPy-Discussion mailing list