python - 使用边界时 scipy.optimize.curvefit 失败

标签 python numpy scipy

我正在尝试使用scipy.optimize.curvefit将一组数据与函数相匹配(请参见下面的示例), 但是当我使用边界( documentation )时,拟合失败,我只是得到 初始猜测参数作为输出。 一旦我替换 -np.inf ad np.inf 作为第二个参数的边界 (函数中的dt),拟合有效。 我做错了什么?

import numpy as np
import matplotlib.pyplot as plt
import scipy.optimize as opt

#Generate data
crc=np.array([-1.4e-14, 7.3e-14, 1.9e-13, 3.9e-13, 6.e-13, 8.0e-13, 9.2e-13, 9.9e-13, 
              1.e-12, 1.e-12, 1.e-12, 1.0e-12, 1.1e-12, 1.1e-12, 1.1e-12, 1.0e-12, 1.1e-12])
time=np.array([0., 368., 648., 960., 1520.,1864., 2248., 2655., 3031., 
                3384., 3688., 4048., 4680., 5343., 6055.,  6928.,  8120.])

#Define the function for the fit
def testcurve(x, Dp, dt):
    k = -Dp*(x+dt)*2e11
    curve = 1e-12 * (1+2*(-np.exp(k) + np.exp(4*k) - np.exp(9*k) + np.exp(16*k)))
    curve[0]= 0
    return curve

#Set fit bounds 
dtmax=time[2]
param_bounds = ((-np.inf, -dtmax),(np.inf, dtmax))

#Perform fit
(par, par_cov) = opt.curve_fit(testcurve, time, crc, p0 = (5e-15, 0), bounds = param_bounds)

#Print and plot output
print(par)       
plt.plot(time, crc, 'o')
plt.plot(time, testcurve(time, par[0], par[1]), 'r-')
plt.show()

最佳答案

我今天在不同的拟合问题中遇到了相同的行为。经过一番网上搜索,我发现这个链接很有帮助:Why does scipy.optimize.curve_fit not fit to the data?

简短的回答是:在数值拟合中使用极小(或大)的数字并不稳健,缩放它们会带来更好的拟合。

<小时/>

就您而言,crcDp 都是非常小的数字,可以按比例放大。您可以使用比例因子,在一定范围内,拟合看起来相当稳健。完整示例:

import numpy as np
import matplotlib.pyplot as plt
import scipy.optimize as opt

#Generate data
crc=np.array([-1.4e-14, 7.3e-14, 1.9e-13, 3.9e-13, 6.e-13, 8.0e-13, 9.2e-13, 9.9e-13, 
              1.e-12, 1.e-12, 1.e-12, 1.0e-12, 1.1e-12, 1.1e-12, 1.1e-12, 1.0e-12, 1.1e-12])
time=np.array([0., 368., 648., 960., 1520.,1864., 2248., 2655., 3031., 
                3384., 3688., 4048., 4680., 5343., 6055.,  6928.,  8120.])

# add scale factors to the data as well as the fitting parameter
scale_factor_1 = 1e12 # 1./np.mean(crc) also works if you don't want to set the scale factor manually
scale_factor_2 = 1./2e11

#Define the function for the fit
def testcurve(x, Dp, dt):
    k = -Dp*(x+dt)*2e11 * scale_factor_2
    curve = 1e-12 * (1+2*(-np.exp(k) + np.exp(4*k) - np.exp(9*k) + np.exp(16*k))) * scale_factor_1
    curve[0]= 0
    return curve

#Set fit bounds 
dtmax=time[2]
param_bounds = ((-np.inf, -dtmax),(np.inf, dtmax))

#Perform fit
(par, par_cov) = opt.curve_fit(testcurve, time, crc*scale_factor_1, p0 = (5e-15/scale_factor_2, 0), bounds = param_bounds)

#Print and plot output
print(par[0]*scale_factor_2, par[1])

plt.plot(time, crc*scale_factor_1, 'o')
plt.plot(time, testcurve(time, par[0], par[1]), 'r-')
plt.show()

拟合结果:[6.273102923176595e-15, -21.12202697564494],它提供了合理的拟合,并且非常接近没有任何边界的结果:[6.27312512e-15, - 2.11307470e+01]

关于python - 使用边界时 scipy.optimize.curvefit 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43557489/

相关文章:

python - scipy.spatial.KDTree 和 scipy.spatial.cKDTree 之间的区别

python - 如何识别数据框列中的连续重复值?

python - 使用另一个文件中的函数通过 xlsx 写入 Excel

python - 在scipy中使用pdist计算杰卡德距离

python - 在不知道输出数组大小的情况下像matlab一样在python中连接数组

python - 因互相关而滞后?

io - 使用 scipy 打印 SuperLU 计算的 L 和 U 矩阵

python - 如何修改类方法?

python - Python 中无法捕获的异常

python - numpy 中是否有等效的 Matlab 点函数?