[SciPy-user] data fitting question

Ryan Krauss ryanlists@gmail....
Tue Aug 28 11:01:50 CDT 2007

Sorry to waste your time, I think I found my solution about 15 seconds
after I sent my last email.  I am 95% certain that the solution to my
problem is linalg.lstsq:

def lstsq(a, b, cond=None, overwrite_a=0, overwrite_b=0):
    """ lstsq(a, b, cond=None, overwrite_a=0, overwrite_b=0) -> x,resids,rank,s

    Return least-squares solution of a * x = b.


      a -- An M x N matrix.
      b -- An M x nrhs matrix or M vector.

On 8/28/07, Ryan Krauss <ryanlists@gmail.com> wrote:
> I need to fit some data using the form:
> Ydata = a[0]*vect1+a[1]*vect2+a[2]*vect3+.....
> where Ydata might be the experimental data and vect1, vect2, and vect3
> are known and constant (i.e. they aren't changing during the
> optimization).  a is a vector of the unknown coefficients I am trying
> to find.  The length of a and the number of constant vectors vectN
> might change.  Is this a form that is already implemented using some
> existing optimization or least squares function, or do I just need to
> do something with fmin (for example) or optimize.leastsq?
> I guess it could be reformulated as
> Y=Ax
> where A would be a matrix with vect1, vect2, vect3, ... as its columns
> and x would be a column vector of the unknown a's.    I think this is
> a very standard form and it is a linear set of equations.  So, I think
> there is some simple way to do this, but it is eluding me at the
> moment.  I don't think I can just use linalg.solve because A wouldn't
> be square.  The matrix A might be 100x5 for example and Y would be
> 100x1 and x would be 5x1, so that I am trying to find a least squares
> solution of the 5 unknowns for the 100 equations.
> I think the more complicated way would be to do something like this:
> fitfunc = lambda p, x: p[0]*cos(2*pi/p[1]*x+p[2]) + p[3]*x  # Target function
> errfunc = lambda p, x, y: fitfunc(p,x) -y          # Distance to the
> target function
> p0 = [-15., 0.8, 0., -1.]                        # Initial guess for
> the parameters
> p1,success = optimize.leastsq(errfunc, p0[:], args = (Tx, tX))
> from http://scipy.org/Cookbook/FittingData.  This could work and I
> would just redefine fitfunc if the number of terms in my fit
> increased.  But I don't think this is necessary because my system is
> linear in the coefficients.
> What is the easiest/best/cleanest way?
> Thanks,
> Ryan

More information about the SciPy-user mailing list