[Numpy-discussion] Help with linear_least_squares
Travis Oliphant
oliphant at ee.byu.edu
Wed Mar 28 14:04:45 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
>
You are mixing numpy arrays with a package from Numeric (which numpy
replaces) You should instead use the linear algebra routines from numpy.
Thus, eliminate
import LinearAlgebrea
Then, as the last line
sol = linalg.lstsq(A,b)
You also don't need to reshape the b array. It will work fine as a 1-d
array.
So, code is
from numpy import *
A = reshape([0,1,2,1,4,1,-1,1],(4,2))
b = array([3,3,4,2])
sol,resids,rank, s = linalg.lstsq(A,b)
sol is your desired solution vector
-Travis
More information about the NumPy-Discussion
mailing list