使用 scipy.optimize.curve_fit 执行加权线性拟合
Performing a weighted linear fit with scipy.optimize.curve_fit
我想执行加权线性拟合以提取方程 y = mx+c
中的参数 m
和 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 数据点都有与之关联的错误时使用它。
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)
我想执行加权线性拟合以提取方程 y = mx+c
中的参数 m
和 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 数据点都有与之关联的错误时使用它。
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)