scipy.curve_fit 当 exp 的指数本身有一个指数(常量)时不适合

scipy.curve_fit not fitting when the exponent of an exp has an exponent itself (constant)

我正在尝试拟合这条曲线:

def logistic2_model(x, a, b, dtau, tau):
    return a/(1+b*np.exp(-np.power((x-dtau)/tau, 0.9)))

使用curve_fit

x = [54L, 55L, 56L, 57L, 58L, 59L, 60L, 61L, 62L, 63L, 64L, 65L, 66L, 67L, 68L, 69L, 70L, 71L, 72L, 73L, 74L, 75L, 76L, 77L, 78L, 79L, 80L, 81L, 82L, 83L, 84L, 85L, 86L, 87L, 88L, 89L, 90L, 91L, 92L, 93L, 94L, 95L, 96L, 97L, 98L, 99L]
y = [229.0, 322.0, 400.0, 650.0, 888.0, 1128.0, 1694.0, 2036.0, 2502.0, 3089.0, 3858.0, 4636.0, 5883.0, 7375.0, 9172.0, 10149.0, 12462.0, 15113.0, 17660.0, 21157.0, 24747.0, 27980.0, 31506.0, 35713.0, 41035.0, 47021.0, 53578.0, 59138.0, 63927.0, 69176.0, 74386.0, 80539.0, 86498.0, 92472.0, 97689.0, 101739.0, 105792.0, 110574.0, 115242.0, 119827.0, 124632.0, 128948.0, 132547.0, 135586.0, 139422.0, 143626.0]

p0 = [1.52646450e+05, 1.56215676e-01, 9.59401246e+01, 6.23161909e+00]
fit = curve_fit(logistic2_model, x, y, maxfev=100000, p0=p0)

只有我使用 1.0 才有效。如果我使用任何其他浮动,即使接近,它也只是达到 maxfev 而没有成功。对于 2.0,它原则上可以工作,但拟合的曲线没有任何意义。

p0 是指数等于 1.0 时获得的拟合:

我最初的目标是将该指数添加到参数中以适应,但如果这样还是行不通,那就没希望了。

有什么提示吗?

请添加完整代码,包括。密谋。

为了测试,请使用 1.0 的收敛解,并在图中添加点,例如 0.95 和 1.05。这应该让我们和您了解您的方程式对该参数的敏感程度。可能是方程式变得很疯狂。

对于您发布的解决方案 (0.9),返回的协方差矩阵仅为 np.inf

文档说明:

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.

您可以使用 method='trf'method='dogbox' 尝试任何其他方法 看看是否能解决问题。

但也可能是你的变量中至少有两个调整相同的东西而不是独立的。

我犯了一个天真的错误,没有检查函数的实际值。指数的底数是负数当然不能拟合。