[SciPy-user] data fitting question
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 <firstname.lastname@example.org> wrote:
> I need to fit some data using the form:
> Ydata = a*vect1+a*vect2+a*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
> 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*cos(2*pi/p*x+p) + p*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?
More information about the SciPy-user