scipy.optimize.leastsq

555 views
Skip to first unread message

JoanneA

unread,
May 31, 2011, 4:31:59 AM5/31/11
to Python Ireland
Hi everyone,

I have a little problem of optimisation. Does anyone is doing
optimization and can give me some tips ?

I am using the package scipy to use the function
scipy.optimize.leastsq. In fact, I have translated a matlab code which
uses the function lsqnonlin to do the optimization. I can then use the
same dataset and compare the results from the matlab and python
codes.

There doesn't seems to have any bug in the python version and I got a
result but the fit to the data is not very good. Also, the model is
very close to the reference model but can vary some more.

I've tried to run only the function called by optimize.leastsq
(without optimization, to check if the function does what it is meant
to do) with the same input in the python and matlab version. They both
output the same model. So I am confident with my function.

My problem is that I don't know which (and how) parameters I should
use to the optimization. Maybe the function leastsq is not the correct
one to use anyway...

Here are some pieces of my codes :

# ------------------------------- main program
# Nopt : the number of parameters to sample the data ( Nopt=35)
# path : directory where are the data
# bgmodel : object that contain all the informations about the model
# input : object that contain all the informatinos about the data.

# The model can vary by 0.05 with the data

x0=array(Nopt*[0.0])
[x,cov_x,infodict,mesg,ier]=optimize.leastsq(inv_scipy_LR_fct,x0,args=path,bgmodel,input),full_output=1,maxfev=80,epsfcn=0.05**2)

# ------------------------------- inv_scipy_LR_fct.py
# the model are wave velocities and the data are phase velocities
# the function will vary the reference model a little bit, compute the
corresponding phase velocities and then compare them with the data
(phase velocities)
# it outputs the difference between the model and the data and also
some other weights parameters (len = 175)

Sorry, it is the first time I am asking for help on the web so I don't
know which pieces of code are usefull or not...

Thanks very much...
Joanne

Michael Thompson

unread,
May 31, 2011, 6:26:17 AM5/31/11
to python...@googlegroups.com
On 31 May 2011 09:31, JoanneA <marmot...@gmail.com> wrote:
> Hi everyone,
>
> I have a little problem of optimisation. Does anyone is doing
> optimization and can give me some tips ?
>
> I am using the package scipy to use the function
> scipy.optimize.leastsq. In fact, I have translated a matlab code which
> uses the function lsqnonlin to do the optimization. I can then use the
> same dataset and compare the results from the matlab and python
> codes.
>
> There doesn't seems to have any bug in the python version and I got a
> result but the fit to the data is not very good. Also, the model is
> very close to the reference model but can vary some more.
>
> I've tried to run only the function called by optimize.leastsq
> (without optimization, to check if the function does what it is meant
> to do) with the same input in the python and matlab version. They both
> output the same model. So I am confident with my function.
>
> My problem is that I don't know which (and how) parameters I should
> use to the optimization. Maybe the function leastsq is not the correct
> one to use anyway...

Your problem might be that Matlab's lsqnonlin is closer to something
like numpy.polyfit, i.e. a non linear fit of a polynomial.

http://docs.scipy.org/doc/numpy/reference/generated/numpy.polyfit.html

http://www.scipy.org/Cookbook/FittingData

Michael

JoanneA

unread,
May 31, 2011, 7:01:33 AM5/31/11
to Python Ireland
Thank you for your reply Michael. I didn't know about numpy.polyfit.
But neither my data nor my model are (or should be) polynomial. The
function used by leastsq is quite complicate, that's why I liked that
the optimization function take it as an argument...
Reply all
Reply to author
Forward
0 new messages