使用scipy.optimize.curve_fit执行加权线性拟合

问题描述 投票:0回答:1

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

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个回答
0
投票

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)
© www.soinside.com 2019 - 2024. All rights reserved.