[Numpy-discussion] hold parameters
Bevan Jenkins
bevan07@gmail....
Mon Aug 1 19:43:28 CDT 2011
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
More information about the NumPy-Discussion
mailing list