使用scipy.optimize curve_fit拟合指数曲线时遇协方差无法估计问题
Scipy curve_fit协方差无法估计问题排查
问题现象
使用scipy.optimize.curve_fit拟合实验数据时触发警告:
optimizeWarning: Covariance of the parameters could not be estimated warnings.warn('Covariance of the parameters could not be estimated',
输出结果显示拟合参数popt与初始猜测值完全一致,协方差矩阵pcov全部为无穷大:
Popt [ 1.00000000e+00 7.80761109e-04 -1.00000000e+02] Pcov [[inf inf inf] [inf inf inf] [inf inf inf]]
显然curve_fit未能完成有效拟合,无法计算参数协方差,得到的参数并非最优解。数据可视化见配图,对应原始数据存储在CSV文件中。
拟合代码
# using curve_fit from scipy.optimize import curve_fit import numpy as np # exponential curve def _1_func(x, a0,b0,beta): """ calculates the exponential curve shifted by bo and scaled by a0 beta is exponential """ y = a0 * np.exp( beta * x ) + b0 return y # the code to fit # initial guess for exp fitting params numpoints = spectrum_one.shape[0] x = F[1:numpoints] # zero element is not used y = np.absolute(spectrum_one[1:numpoints])/signal_size # making an initial guess a0 = 1 b0 = y.mean() beta = -100 p0 = [a0, b0, beta] popt, pcov = curve_fit(_1_func, x, y, p0=p0) perr = np.sqrt(np.diag(pcov)) # errors print('Popt') print(popt) print('Pcov') print(pcov)
疑问
- 为何会出现协方差无法估计的错误?
- 是否需要对数据进行缩放处理?
内容的提问来源于stack exchange,提问作者twistfire
相关产品推荐
相关产品推荐

