我有一个代码并使用 curve_fit 将洛伦兹曲线和高斯曲线拟合到数据。 我需要得到每个输出参数的误差估计,所以打印了 popt 和 pcov 我知道 scipy 引用指南说明了如何使用 pcov 矩阵来查找错误,但这对我来说还不清楚,因为我是编程新手。 谢谢
最佳答案
阅读文档通常可以为此类问题提供答案。例如,curve_fit()
的文档位于 https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.curve_fit.html说:
Returns: popt : array Optimal values for the parameters so that the sum of the squared residuals of f(xdata, *popt) - ydata is minimized
pcov : 2d 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.
也就是说:使用 p_sigma = np.sqrt(np.diag(pcov))
请允许我建议,对于高斯和洛伦兹模型的曲线拟合,您可能会发现 lmfit ( https://lmfit.github.io/lmfit-py/) 很有帮助。它为这些和其他模型提供内置版本。除其他功能外,它还可以打印格式精美的报告,以适应这种包含不确定性的情况。
关于python - 我如何在 python 中使用 pcov 来获取每个参数的错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43561036/