scipy中的scipy.optimize.curve_fit

scipy中的scipy.optimize.curve_fit

scipy.optimize.``curve_fit(f, xdata, ydata, p0=None, sigma=None, absolute_sigma=False, check_finite=True, bounds=(- inf, inf), method=None, jac=None, kwargs)

使用非线性最小二乘法将函数f拟合到数据

参数

f:callable

The model function, f(x, …). It must take the independent variable as the first argument and the parameters to fit as separate remaining arguments.

模型函数f(x,…)。它必须将自变量作为第一个参数,将要拟合的参数作为单独的剩余参数。

xdata:array_like or object

The independent variable where the data is measured. Should usually be an M-length sequence or an (k,M)-shaped array for functions with k predictors, but can actually be any object.

测量数据的自变量。对于具有k个预测器的函数,通常应该是M长序列或(k,M)形数组,但实际上可以是任何对象。

ydata:array_like

The dependent data, a length M array - nominally f(xdata, ...).

因变量数据,一个长度为M的数组-名义上是’f(xdata,…)`。

p0:array_like, optional

Initial guess for the parameters (length N). If None, then the initial values will all be 1 (if the number of parameters for the function can be determined using introspection, otherwise a ValueError is raised).

参数的初始猜测(长度N)。如果没有,则初始值将全部为1(如果可以使用自省确定函数的参数数量,否则将引发ValueError)。

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).

  • A 2-D sigma should contain the covariance matrix of errors in ydata. In this case, the optimized function is chisq = r.T @ inv(sigma) @ r.

    New in version 0.19.

None (default) is equivalent of 1-D sigma filled with ones.

确定ydata中的不确定度。如果我们将残差定义为r=ydata-f(扩展数据,*popt),那么sigma的解释取决于其维数:

-一维sigma应包含ydata中误差的标准偏差值。在这种情况下,优化的函数是chisq=sum((r/sigma)**2)。

-二维sigma应包含ydata中的误差协方差矩阵。在这种情况下,优化的函数是chisq=r.T@inv(sigma)@r。

版本0.19中新增
None(默认值)相当于1-Dsigma填充1。

absolute_sigma:bool, optional

If True, sigma is used in an absolute sense and the estimated parameter covariance pcov reflects these absolute values.

If False (default), only the relative magnitudes of the sigma values matter. The returned parameter covariance matrix pcov is based on scaling sigma by a constant factor. This constant is set by demanding that the reduced chisq for the optimal parameters popt when using the scaled sigma equals unity. In other words, sigma is scaled to match the sample variance of the residuals after the fit. Default is False. Mathematically, pcov(absolute_sigma=False) = pcov(absolute_sigma=True) * chisq(popt)/(M-N)

如果为真,则在绝对意义上使用西格玛,估计的参数协方差pcov反映这些绝对值。
如果为False(默认值),则只有西格玛值的相对大小才重要。返回的参数协方差矩阵pcov基于按常数因子缩放sigma。该常数是通过要求在使用标度sigma时,最佳参数popt的简化chisq等于单位来设置的。换句话说,西格玛被缩放以匹配拟合后残差的样本方差。默认值为False。从数学上讲,pcov(absolute_sigma=False) = pcov(absolute_sigma=True) * chisq(popt)/(M-N)

check_finite:bool, optional

If True, check that the input arrays do not contain nans of infs, and raise a ValueError if they do. Setting this parameter to False may silently produce nonsensical results if the input arrays do contain nans. Default is True.

如果为True,请检查输入数组是否不包含INF的NAN,如果包含,则引发ValueError。如果输入数组中确实包含NAN,则将此参数设置为False可能会产生无意义的结果。默认值为True。

bounds:2-tuple of array_like, optional

Lower and upper bounds on parameters. Defaults to no bounds. Each element of the tuple must be either an array with the length equal to the number of parameters, or a scalar (in which case the bound is taken to be the same for all parameters). Use np.inf with an appropriate sign to disable bounds on all or some parameters.

New in version 0.17.

参数的上下限。默认为无边界。元组的每个元素必须是长度等于参数数量的数组,或者是标量(在这种情况下,所有参数的边界都是相同的)。使用带有适当符号的np.inf来禁用所有或某些参数的边界。

版本0.17中的新功能。

method:{‘lm’, ‘trf’, ‘dogbox’}, optional

Method to use for optimization. See least_squares for more details. Default is ‘lm’ for unconstrained problems and ‘trf’ if bounds are provided. The method ‘lm’ won’t work when the number of observations is less than the number of variables, use ‘trf’ or ‘dogbox’ in this case.

New in version 0.17.

用于优化的方法。有关详细信息,请参见最小二乘法。对于无约束问题,默认值为“lm”,如果提供了边界,则默认值为“trf”。当观察数小于变量数时,“lm”方法不起作用,在这种情况下使用“trf”或“dogbox”。版本0.17中的新功能。

jac:callable, string or None, optional

Function with signature jac(x, ...) which computes the Jacobian matrix of the model function with respect to parameters as a dense array_like structure. It will be scaled according to provided sigma. If None (default), the Jacobian will be estimated numerically. String keywords for ‘trf’ and ‘dogbox’ methods can be used to select a finite difference scheme, see least_squares.

New in version 0.18.

它是一个密集的雅可比矩阵模型,用雅可比矩阵的结构参数来计算雅可比函数。它将根据提供的西格玛进行缩放。如果没有(默认设置),将以数字方式估计雅可比矩阵。“trf”和“dogbox”方法的字符串关键字可用于选择有限差分格式,请参见最小二乘法。
版本0.18中的新功能。

kwargs

Keyword arguments passed to leastsq for method='lm' or least_squares otherwise.

传递给leastsq的方法为’lm’或最小二乘法的关键字参数。

Return

popt:array

Optimal values for the parameters so that the sum of the squared residuals of f(xdata, *popt) - ydata is minimized.

使f(xdata,*popt)-ydata的平方残差之和最小化的参数的最佳值。

pcov:2-D array

The estimated covariance of popt. The diagonals provide the variance of the parameter estimate. To compute one standard deviation errors on the parameters use perr = np.sqrt(np.diag(pcov)).

How the sigma parameter affects the estimated covariance depends on absolute_sigma argument, as described above.

If the Jacobian matrix at the solution doesn’t have a full rank, then ‘lm’ method returns a matrix filled with np.inf, on the other hand ‘trf’ and ‘dogbox’ methods use Moore-Penrose pseudoinverse to compute the covariance matrix.

popt的估计协方差。对角线提供参数估计的方差。要计算参数的一个标准偏差误差,请使用perr=np.sqrt(np.diag(pcov))。

sigma参数如何影响估计协方差取决于绝对sigma参数,如上所述。

如果解的雅可比矩阵没有满秩,“lm”方法返回一个填充了np.inf的矩阵,另一方面,“trf”和“dogbox”方法使用Moore-Penrose伪逆计算协方差矩阵。

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值