【问题标题】:Performing a weighted linear fit with scipy.optimize.curve_fit使用 scipy.optimize.curve_fit 执行加权线性拟合
【发布时间】:2020-06-07 06:50:17
【问题描述】:

我想执行加权线性拟合以提取方程y = mx+c 中的参数mc。 我要进行拟合的数据是:

xdata = [661.657, 1173.228, 1332.492, 511.0, 1274.537]

ydata = [242.604, 430.086, 488.825, 186.598, 467.730]

yerr = [0.08, 0.323, 0.249, 0.166, 0.223]

我想使用scipy.optimize.curve_fit,但是当每个 y 数据点都有与之相关的错误时,我不知道如何使用它。

【问题讨论】:

    标签: python scipy linear-regression curve-fitting least-squares


    【解决方案1】:

    IIUC 那么您正在寻找的是 sigma 关键字参数。

    sigma: None or M-length sequence or MxM array, optional
    
    Determines the uncertainty in ydata. If we define residuals as r = ydata - f(xdata, *popt), 
    then the interpretation of sigma depends on its number of dimensions:
    A 1-d sigma should contain values of standard deviations of errors in ydata. 
    In this case, the optimized function is chisq = sum((r / sigma) ** 2).
    
    None (default) is equivalent of 1-d sigma filled with ones.
    

    那么代码会变成:

    def func(x, m, c):
        return m * x + c
    
    curve_fit(func, xdata, ydata, sigma=yerr)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-01-02
      • 1970-01-01
      • 2014-03-29
      • 1970-01-01
      • 2018-11-22
      • 1970-01-01
      • 2019-02-15
      • 2019-01-23
      相关资源
      最近更新 更多