Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用scipy.optimize.curve\u拟合执行加权线性拟合_Python_Scipy_Linear Regression_Curve Fitting_Least Squares - Fatal编程技术网

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)