[SciPy-User] Problems with optimize.leastsq and curve_fit

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

[SciPy-User] Problems with optimize.leastsq and curve_fit

Paul Hobson-2
I guess I don't quite understand how to use the functions properly since they're not returning meaningful results on what should be a pretty trivial linear fit. My initial thought is that they're being tripped up by my `x` variable not being uniformly spaced.

Consider the following (overly) simplified example:

## start
import numpy as np
import scipy.optimize as opt
x = np.array([  0. ,   0.5,   1. ,   2. ,   3. ,   3.5,   5. ,   5.5,   6. ,
                6.5,   7. ,   7.5,   8. ,   8.5,   9. ,   9.5,  10. ,  11. ,
               11.5,  12.5], dtype=np.float32)

y = np.array([  2.7,   8.6,   9.  ,  13.8,  16.8,  17.8,  23.24,  25.2,
               26.3,  27.1,  27.68,  30.7,    32.9,  32.9,  37.07,  39.5,
               40.5,  45.7,  46.76,  50.2], dtype=np.float32)

def lsq_line(params, x, y):
    return y - (params[0]*x + params[1])

def cf_line(x, m, b):
    return m*x + b

np.polyfit(x, y, 1)  # works great
# Out[6]: array([ 3.54965901,  5.09342384], dtype=float32)

opt.leastsq(lsq_line, [3,5], args=(x,y))  # bad
# Out[9]: (array([ 3.,  5.]), 4)

opt.curve_fit(cf_line, x, y, p0=[3,5]) # bad
# Out[10]: (array([ 3.,  5.]), inf)
## stop

Any thoughts or advice on how to better use these functions would be much appreciated. 

Thanks,
-p

_______________________________________________
SciPy-User mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/scipy-user
Reply | Threaded
Open this post in threaded view
|

Re: Problems with optimize.leastsq and curve_fit

Pauli Virtanen-3
19.03.2013 21:47, Paul Hobson kirjoitti:
> I guess I don't quite understand how to use the functions properly since
> they're not returning meaningful results on what should be a pretty
> trivial linear fit. My initial thought is that they're being tripped up
> by my `x` variable not being uniformly spaced.

The problem is the use of float32 rather than float64. This is a bug,
which was fixed in 0.12.0.

--
Pauli Virtanen

_______________________________________________
SciPy-User mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/scipy-user
Reply | Threaded
Open this post in threaded view
|

Re: Problems with optimize.leastsq and curve_fit

Paul Hobson-2
On Tue, Mar 19, 2013 at 12:56 PM, Pauli Virtanen <[hidden email]> wrote:
19.03.2013 21:47, Paul Hobson kirjoitti:
> I guess I don't quite understand how to use the functions properly since
> they're not returning meaningful results on what should be a pretty
> trivial linear fit. My initial thought is that they're being tripped up
> by my `x` variable not being uniformly spaced.

The problem is the use of float32 rather than float64. This is a bug,
which was fixed in 0.12.0.


Thanks for the info and the prompt reply. This is the info I need.
-pauil


_______________________________________________
SciPy-User mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/scipy-user