课程评价 (0)

请对课程作出评价:
0/300

学员评价

暂无精选评价
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,后面的参数是待优化参数
  • xdatax坐标
  • ydatay坐标
  • p0:初始迭代值
  • sigmay值的不确定性的度量
  • 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,)