[SciPy-user] Fitting with global parameters optimize.leastsq

Iain Day iain at day-online.org.uk.invalid
Tue Nov 7 14:30:45 CST 2006

> What's x, y, z and def model(...) is missing :). I thought more of a 
> copy-paste-and-run example (e.g. with rawdata = numpy.random.randn(100, 
> 7) where 100 == len(data_points)) that produces the error :-)

Sorry, my mistake. I was quite tired!

> Anyway:
> 1) The error comes from the shape of err in your residuals(). leastsq 
> expects an array of shape (<number>,), not (1, <number>) (i.e. 
> len(err.shape) = 1, not 2.
> You would have to do reshape(err, (ntraces*len(points),)).
> I get this error when I replace
> def residuals(x, t, y):
>      return y - model(t, x)
> by
> def residuals(x, t, y):
>      err =  y - model(t, x)
>      err = scipy.reshape(err, (1, len(err)))
>      print "### err.shape:", err.shape
>      return err

Okay, thanks for the pointer. If I reshape the array as 
(ntraces*len(points),) it works fine. Though this leads to my next question:

what is the difference between (length,) and (length,1)?

> 2) You're trying to fit all data sets (t == data_points, y == 
> raw_signals[:, i]) at once and I guess that won't work. You have to loop 
> over A (or C or raw_signals[:, i]) building one data set and one initial 
> guess (x0 = numpy.array([A[i], B, C[i]])) per loop.

That sounds similar to fitting each trace separately, and wouldn't give 
you the global B?

Thanks for your help, I think I have a better understanding so far. 
Well, its only been 10 days since I started with Python and SciPy!


More information about the SciPy-user mailing list