scipy.optimize.curve_fit 与数据不正确匹配

gra*_*ger 3 python optimization scipy

我正在尝试用高斯曲线拟合我的数据。这是我的代码:

\n\n
import numpy as np\nfrom scipy import optimize\n\n# The independent variable where the data is measured\nx_coord = np.array([-0.1216    , -0.11692308, -0.11224615, -0.10756923, -0.10289231,\n       -0.09821538, -0.09353846, -0.08886154, -0.08418462, -0.07950769,\n       -0.07483077, -0.07015385, -0.06547692, -0.0608    , -0.05612308,\n       -0.05144615, -0.04676923, -0.04209231, -0.03741538, -0.03273846,\n       -0.02806154, -0.02338462, -0.01870769, -0.01403077, -0.00935385,\n       -0.00467692,  0.        ,  0.00467692,  0.00935385,  0.01403077,\n        0.01870769,  0.02338462,  0.02806154,  0.03273846,  0.03741538,\n        0.04209231,  0.04676923,  0.05144615,  0.05612308,  0.0608    ,\n        0.06547692,  0.07015385,  0.07483077,  0.07950769,  0.08418462,\n        0.08886154,  0.09353846,  0.09821538,  0.10289231,  0.10756923,\n        0.11224615,  0.11692308])\n\n# The dependent data \xe2\x80\x94 nominally f(x_coord)\ny = np.array([-0.0221931 , -0.02323915, -0.02414913, -0.0255389 , -0.02652465,\n       -0.02888672, -0.03075954, -0.03355392, -0.03543005, -0.03839526,\n       -0.040933  , -0.0456585 , -0.04849097, -0.05038776, -0.0466699 ,\n       -0.04202133, -0.034239  , -0.02667525, -0.01404582, -0.00122683,\n        0.01703862,  0.03992694,  0.06704549,  0.11362071,  0.28149172,\n        0.6649422 ,  1.        ,  0.6649422 ,  0.28149172,  0.11362071,\n        0.06704549,  0.03992694,  0.01703862, -0.00122683, -0.01404582,\n       -0.02667525, -0.034239  , -0.04202133, -0.0466699 , -0.05038776,\n       -0.04849097, -0.0456585 , -0.040933  , -0.03839526, -0.03543005,\n       -0.03355392, -0.03075954, -0.02888672, -0.02652465, -0.0255389 ,\n       -0.02414913, -0.02323915])\n\n# define a gaussian function to fit the data\ndef gaussian(x, a, b, c):\n    val = a * np.exp(-(x - b)**2 / c**2)\n    return val\n\n# fit the data    \npopt, pcov = optimize.curve_fit(gaussian, x_coord, y, sigma = np.array([0.01] * len(x_coord)))\n\n# plot the data and the fitting curve\nplt.plot(x_coord, y, \'b-\', x_coord, gaussian(x_coord, popt[0], popt[1], popt[2]), \'r:\')\n
Run Code Online (Sandbox Code Playgroud)\n\n

该图显示拟合曲线完全错误:\n蓝线:数据; 红色虚线:拟合曲线

\n\n

我应该怎样做才能获得良好的拟合曲线?

\n

cel*_*cel 5

这实际上是一个非常好的问题,它说明找到right(局部)最优值可能非常困难。

通过p0参数,您可以给优化例程一个提示,大约在您期望的最佳位置。

如果您从最初的猜测开始[1,0,0.1]

# fit the data
sigma = np.array([0.01] * len(x_coord))
popt, pcov = optimize.curve_fit(gaussian, x_coord, y, sigma=sigma, p0=[1,0,0.1])
Run Code Online (Sandbox Code Playgroud)

你得到以下结果:

情节1


一些注意事项:您被迫curve_fit拟合没有常数项的钟形曲线。这让事情变得有些尴尬。

如果允许 offset d,您将得到:

# define a gaussian function to fit the data
def gaussian(x, a, b, c, d):
    val = a* np.exp(-(x - b)**2 / c**2) + d
    return val
Run Code Online (Sandbox Code Playgroud)

并得到如下结果:

# fit the data    
popt, pcov = optimize.curve_fit(gaussian, x_coord, y)

# plot the data and the fitting curve
plt.plot(x_coord, y, 'b-', x_coord, gaussian(x_coord, *popt), 'r:')
Run Code Online (Sandbox Code Playgroud)

阴谋

这看起来更像是一个合理的选择。虽然看起来高斯与数据的拟合不太好。


非常尖的形状表明拉普拉斯算子可能更适合:

# define a laplacian function to fit the data
def laplacian(x, a, b, c, d):
    val = a* np.exp(-np.abs(x - b) / c) + d
    return val

# fit the data    
popt, pcov = optimize.curve_fit(laplacian, x_coord, y, p0=[1,0,0.01,-0.1])

# plot the data and the fitting curve
plt.plot(x_coord, y, 'b-', x_coord, laplacian(x_coord, *popt), 'r:')
Run Code Online (Sandbox Code Playgroud)

这是结果:

在此输入图像描述