scipy curve_fit 指数拟合失败 [英] scipy curve_fit fails on exponential fit

查看:129
本文介绍了scipy curve_fit 指数拟合失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试使用 curve_fit 进行指数拟合时,scipy 返回错误.难道我做错了什么?从 np.exp(-b * t) 中删除负号允许 curve_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)         

因错误而失败:

  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) 

推荐答案

xy 改为 numpy 数组

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.

这篇关于scipy curve_fit 指数拟合失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆