简体   繁体   中英

scipy curve_fit fails on exponential fit

When I try to do an exponential fit using curve_fit, scipy returns an error. Am I doing something wrong? Removing the negative sign from np.exp(-b * t) allows curve_fit to work, but the values it returns are way off.

#!/usr/bin/python                                                                   

import numpy as np                                                                  
import scipy as sp  
from scipy.optimize import curve_fit                                                                                                                                                                                                       
import scipy.optimize as opt                                                        
import matplotlib.pyplot as plt                                                     

x = [40,45,50,55,60]                                                                
y = [0.99358851674641158, 0.79779904306220106, 0.60200956937799055, 0.49521531100478472, 0.38842105263157894]

def model_func(t, a, b, c):                                                         
    return a * np.exp(-b * t) + c                                                   

opt_parms, parm_cov = sp.optimize.curve_fit(model_func, x, y, maxfev=1000)          
a,b,c = opt_parms                                                                   
print a,b,c                                                                         
print x                                                                             
print y                                                                             
print model_func(x, a,b,c)         

Fails with error:

  Traceback (most recent call last):
      File "asdf.py", line 18, in <module>
        opt_parms, parm_cov = sp.optimize.curve_fit(model_func, x, y, maxfev=1000)
      File "/usr/lib/python2.7/dist-packages/scipy/optimize/minpack.py", line 426, in curve_fit
        res = leastsq(func, p0, args=args, full_output=1, **kw)
      File "/usr/lib/python2.7/dist-packages/scipy/optimize/minpack.py", line 276, in leastsq
        m = _check_func('leastsq', 'func', func, x0, args, n)[0]
      File "/usr/lib/python2.7/dist-packages/scipy/optimize/minpack.py", line 13, in _check_func
        res = atleast_1d(thefunc(*((x0[:numinputs],) + args)))
      File "/usr/lib/python2.7/dist-packages/scipy/optimize/minpack.py", line 346, in _general_function
        return function(xdata, *params) - ydata
    ValueError: operands could not be broadcast together with shapes (0) (5) 

change x and y to numpy arrays

x = np.array([40,45,50,55,60])
y = np.array([0.99358851674641158, 0.79779904306220106, 0.60200956937799055, 0.49521531100478472, 0.38842105263157894])

then I think you are good, because the function requires vectorized computation, whereas lists are not adequate.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM