8分钟
最小二乘法拟合数据2
scipy
提供了另一个函数来执行最小二乘法的曲线拟合:
scipy.optimize.curve_fit(f, xdata, ydata, p0=None, sigma=None, absolute_sigma=False,
check_finite=True, bounds=(-inf, inf), method=None, **kwargs)
f
:可调用函数,它的优化参数被直接传入。其第一个参数一定是xdata
,后面的参数是待优化参数xdata
:x
坐标ydata
:y
坐标p0
:初始迭代值sigma
:y
值的不确定性的度量absolute_sigma
: If False, sigma denotes relative weights of the data points. The returned covariance matrix pcov is based on estimated errors in the data, and is not affected by the overall magnitude of the values in sigma. Only the relative magnitudes of the sigma values matter.If True, sigma describes one standard deviation errors of the input data points. The estimated covariance in pcov is based on these values.check_finite
:如果为True
,则检测输入中是否有nan
或者inf
bounds
:指定变量的取值范围method
:指定求解算法。可以为'lm'/'trf'/'dogbox'
kwargs
:传递给leastsq/least_squares
的关键字参数。
返回值:
popt
:最优化参数pcov
:The estimated covariance of popt.
假设我们拟合的函数是 y=f(x;a,b,c),其中 a,b,c为参数。假设数据点的横坐标为 X,纵坐标为 Y,那么我们可以给出func
为:
def func(x,a,b,c):
return f(x;a,b,c)#x 为数组,形状为 (N,)
学员评价