[SciPy-dev] parameter control in scipy.optimize.leastsq
Benny Malengier
benny.malengier at gmail.com
Sun Oct 19 13:44:04 EDT 2008
To avoid array creation overhead, I use objects encapsulating everything, so
def fit_function(self, x, ... ):
# x is a subset of all the parameters, self.tofit is bool array with
those arg to fit
self.p[self.tofit ][:] = x[:]
self.p[~self.tofit][:] = self.all_arg [~self.tofit][:]
# calls the original residuals routine
return residuals(self.p, ... )
Then in your loop, you can change the self.tofit array to change what you
fit.
Obviously, self.all_arg default to something in the __init__() method and
self.p is created once there, and things can be changed.
If things are slow, the line
self.p[~self.tofit][:] = self.all_arg [~self.tofit][:]
can be removed and put into it's own method, only to be called if self.tofit
changes.
Benny
2008/10/19 Maximilian Fabricius <mxhf at gmx.net>
> On Sun, Oct 19, 2008 at 12:52 PM, Robin <robince at gmail.com> wrote:
> > On Sun, Oct 19, 2008 at 11:28 AM, Maximilian Fabricius <mxhf at gmx.net>
> wrote:
> >> Robin,
> >>
> >> thank you for the quick reply.
> >> Indeed, but this would still involve to swap parameters around between
> >> fixed and non-fixed and
> >> changing the lambda function manually rather then just switching the
> >> fissint on and off
> >> with a single variable.
> >>
> >> But no doubt, a wrapper can be done.
> >
> > I couldn't think of a way to squeeze it into a lamdba single
> > statement, but something like this would do:
> >
> > def fit_function(x, fit, y):
> > p0 = zeros(fit.size)
> > p0[fit] = x
> > p0[~fit] = y
> >
> > where fit is a boolean array, x is the ones to optimised and y are the
> > fixed values.
> > You have the extra overhead of the p0 array allocation for every
> > iteration of the function - but perhaps that is worth it for the
> > convenience.
> >
> > Robin
> > _______________________________________________
> > Scipy-dev mailing list
> > Scipy-dev at scipy.org
> > http://projects.scipy.org/mailman/listinfo/scipy-dev
> >
>
>
> Robin,
>
> great, nice idea! As this may be of use to others, my solution now is:
>
>
> def fit_function(p_fit, fit, p_fixed, ... ):
> # assemble p_fit and p_fixed into a single array
> # while restoring the correct order of the original p0 array.
> p = zeros(fit.size)
> p[ where(fit == True) ] = p_fit
> p[ where(fit == False) ] = p_fixed
> # calls the original residuals routine
> return residuals(p, ... )
>
> main():
> # best guesses
> p0 = zeros(10)
> p0[0] = vrot_d
> p0[1] = vsig_d
> ....
>
> # choose which parameters to fit
> fit = zeros(len(p0))
> fit[0:7] = True
> fit[8] = False
> fit[9] = False
>
> # store parameters which should be fit in p_fit
> # all the others in p_fixed
> p_fit = p0[ where(fit == True) ]
> p_fixed = p0[ where(fit == False) ]
> plsq = leastsq(fit_function, p_fit, args=(fit, p_fixed, ...) )
>
> Thanks!
>
> Cheers,
>
> Maximilian
> _______________________________________________
> Scipy-dev mailing list
> Scipy-dev at scipy.org
> http://projects.scipy.org/mailman/listinfo/scipy-dev
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/scipy-dev/attachments/20081019/0dcb9872/attachment.html>
More information about the SciPy-Dev
mailing list