Python 使用scipy.optimize.curve\u拟合执行加权线性拟合
我想执行加权线性拟合,以提取方程Python 使用scipy.optimize.curve\u拟合执行加权线性拟合,python,scipy,linear-regression,curve-fitting,least-squares,Python,Scipy,Linear Regression,Curve Fitting,Least Squares,我想执行加权线性拟合,以提取方程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\u fit,但我不知道当每个y数据点都有一个与之相关
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\u 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)