[Numpy-discussion] hold parameters
josef.pktd@gmai...
josef.pktd@gmai...
Tue Aug 2 04:07:20 CDT 2011
On Mon, Aug 1, 2011 at 8:43 PM, Bevan Jenkins <bevan07@gmail.com> wrote:
> Hello,
>
> I have a function that I fitting to a curve via scipy.optimize.leastsq. The
> function has 4 parameters and this is all working fine.
>
> For a site, I have a number of curves (n=10 in the example below). I would
> like to some of the parameters to be the best fit across all curves (best fit
> for a site) while letting the other parameters vary for each curve. I have
> this working as well.
>
> The issue I have is like to be able to vary this for a run. That is do a run
> where parameter1 is best fit for entire site, whith the remaining three
> varying per curve. Then on the next run, have two parameters being held or
> fitted for all curves at one. Or be able to do a run where all 4 parameters
> are fit for each individual curve.
>
> Using my e.g. below, if I change the 'fix' dict, so that 'a','b', and 'c' are
> True, with 'd' False, then I will have to change the zip to
> for a,b,c in zip(a,b,c):
> solve(a,b,c,d)
>
> I would prefer to find a way to do this via code. I hope this example makes
> sense. The code below is all within my objective function that is being
> called by scipy.optimize.leastsq.
> import numpy as np
>
> def solve(a,b,c,d):
> print a,b,c,d
> #return x*a*b*c*d
>
>
>
> fix = {"a":True,"b":True,"c":False,"d":False}
>
> n=10
> params = np.array([0,1,2,3]*n)
> params = params.reshape(-1,4)
>
> if fix["a"] is True:
> a = params[0,0]
> else:
> a = params[:,0]
> if fix["b"] is True:
> b = params[0,1]
> else:
> b = params[:,1]
> if fix["c"] is True:
> c = params[0,2]
> else:
> c = params[:,2]
> if fix["d"] is True:
> d = params[0,3]
> else:
> d = params[:,3]
>
> res=[]
> for c,d in zip(c,d):
> res = solve(a,b,c,d)
> #res = solve(a,b,c,d)-self.orig
> #return np.hstack(res)**2
I'm not a fan of named individual parameters for function arguments
when the number of arguments varies, *args
What I'm using is a full parameter array with nan's
fixed = np.array([nan, nan, c, d]) #fix c,d
def func(args):
fixed[np.isnan(fixed)] = args
a,b,c,d = fixed
...
to set starting values allstartvals = np.array([a0, b0, c0, d0])
startvals = allstartvals[np.isnan(fixed)
optimize.leastsq(func, startvals, other_args)
or something like this.
I find it easier to keep track of the parameters, if I just have an
array or tuple.
for an alternative, Travis used a different way in the scipy.stats
implementation of partially fixed parameters for distributions fit
with named arguments. (I don't remember the details)
Josef
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
More information about the NumPy-Discussion
mailing list