Python Scipy最小二乘法警告:“;无法估计参数的协方差”;

Python Scipy最小二乘法警告:“;无法估计参数的协方差”;,python,scipy,least-squares,scipy-optimize,Python,Scipy,Least Squares,Scipy Optimize,使用scipy.optimize.curve_fit()获取参数的协方差似乎是我遇到的一个常见问题,但常见的解决方案对我帮助不大。(没有给出好的初始猜测,没有足够的数据点,没有输入numpy数组,没有使用float64。)这再现了我的错误: x = np.array([103.15, 113.15, 269.3, 273.15, 273.15, 283.15, 290.0, 293.15, 293.15, 296.15, 296.15, 303.15, 303.15, 303.15, 310.0

使用scipy.optimize.curve_fit()获取参数的协方差似乎是我遇到的一个常见问题,但常见的解决方案对我帮助不大。(没有给出好的初始猜测,没有足够的数据点,没有输入numpy数组,没有使用float64。)这再现了我的错误:

x = np.array([103.15, 113.15, 269.3, 273.15, 273.15, 283.15, 290.0, 293.15, 293.15, 296.15, 296.15, 303.15, 303.15, 303.15, 310.0, 313.15, 313.15, 318.15, 323.15, 323.15, 330.0, 333.15, 348.16, 350.0, 353.15])
y = np.array([-0.2468283, -0.19681169, -0.01856526, -0.01707074, -0.01698771, -0.01333444,  -0.01072733, -0.00986383, -0.00974759, -0.00830289, -0.00896712, -0.00699103, -0.00667552, -0.0063102, -0.00440053, -0.00368648, -0.0034374, -0.00199269, -0.00092992, -0.00066423, 0.00106277, 0.00166058, 0.00464962, 0.0060445,0.00650947])

((u0, u1, r0, r1), pcov) = scipy.optimize.curve_fit(f, x, y, p0=np.array([1.0, 0.01, 0.3, 0.5]))
    
print(u0, u1, r0, r1)
print(pcov)
我的职能是:

def f(x, u0, u1, r0, r1):
    k = 8.617333262145E-5

    term1 = (np.exp(-u0/(k*x)) - np.exp(u1/(k*x))) * r0**3
    term2 = (np.exp(u1/(k*x)) - 1) * r1**3
    
return -(2 * np.pi / 3) * (term1 + term2)
输出是

OptimizeWarning: Covariance of the parameters could not be estimated
  warnings.warn('Covariance of the parameters could not be estimated',
1.0 0.010062693926842859 0.3093891634616651 0.4627801922742623
[[inf inf inf inf]
 [inf inf inf inf]
 [inf inf inf inf]
 [inf inf inf inf]]
以最佳拟合绘制数据点显示了极好的匹配。我真的不知道如何利用协方差来处理这个问题。有什么想法吗?

根据

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

我试着在你的数据上使用
method='trf'
,似乎奏效了。我不知道为什么雅可比矩阵在这种情况下是不可逆的