[SciPy-dev] parameter control in scipy.optimize.leastsq
Maximilian Fabricius
mxhf@gmx....
Mon Oct 20 02:09:40 CDT 2008
On Sun, Oct 19, 2008 at 5:40 PM, Robin <robince@gmail.com> wrote:
> On Sun, Oct 19, 2008 at 4:28 PM, Maximilian Fabricius <mxhf@gmx.net> wrote:
>> 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!
>
> Just want to point out that if fit is a boolean array (ie fit =
> array([True,False,False,True]) etc. then you can index with it
> directly p0[fit] instead of p0[where(fit==True)] and p0[~fit] instead
> of p0[where(fit==False)] which is likely to be a bit quicker. Doesn't
> matter outside, but in the fit_function you probably want to save any
> time you can since it will be run many times.
>
> Robin
Robin,
thank you. Indeed, I should know that. Python sometime is
overwhelmingly convenient.
Maximilian
More information about the Scipy-dev
mailing list