【问题标题】:scipy curve_fit is not changing parameters from initial guessscipy curve_fit 不会改变初始猜测的参数
【发布时间】:2018-12-22 05:33:46
【问题描述】:

我正在尝试将高斯拟合到一些非常清晰的数据中,出于某种原因,scipy.optimize.curve_fit 并没有从最初的猜测中明显改变参数,我不知道为什么。这是一个代码sn-p:

def gauss(x, A, mu, sigma):
    return A/(sigma*np.sqrt(2*np.pi))*np.exp(-((x-mu)**2/(2*sigma**2)))
p0 = [grad[ir_max]/5, plot_r[ir_max], 0.05]
print(p0)
fit, pcov = curve_fit(gauss, plot_r[ir_max-dr:ir_max+dr], grad[ir_max- 
dr:ir_max+dr], p0=p0)
print(fit)
print(plot_r[ir_max-dr:ir_max+dr])
print(grad[ir_max-dr:ir_max+dr])

plot(plot_r[ir_max-dr:ir_max+dr], grad[ir_max-dr:ir_max+dr], 'bo')
plot_r2 = np.linspace(plot_r[ir_max-dr], plot_r[ir_max+dr], 100)
plot(plot_r2, gauss(plot_r2, *fit), 'r--')

索引将采用较大数据集的子集,这是峰值周围相当受限样本的输出:

[7.7160651775860245, 1.641777, 0.05]
[7.71606518 1.64177704 0.05      ]
[1.5620524 1.5779973 1.5939423 1.6098871 1.6258321 1.641777  1.657722
 1.673667  1.6896119 1.7055569]
[ 7.21488949 15.13438187 25.0198808  33.35524257 37.91767649 38.58032589
 35.52668657 28.27396106 18.12926291  9.1928141 ]

还有剧情:

编辑: scipy.omtimize.leastsq 也有类似的问题,但使用 scipy.optimize.minimize 进行一些手动最小二乘最小化似乎可以正常工作:

def func(p):
    A, mu, sigma = p
    resid = gauss(plot_r[ir_max-dr:ir_max+dr], A, mu, sigma)
    return grad[ir_max-dr:ir_max+dr] - resid
from scipy.optimize import leastsq
fit2 = leastsq(func, p0, full_output=True)
print(fit2[0])
print(fit2)

def func2(p):
    A, mu, sigma = p
    resid = gauss(plot_r[ir_max-dr:ir_max+dr], A, mu, sigma)
    return np.sum( np.power(grad[ir_max-dr:ir_max+dr] - resid,2) )
from scipy.optimize import minimize
fit3 = minimize(func2, p0, method='Nelder-Mead')
print(fit3.x)



plot(plot_r[ir_max-dr:ir_max+dr], grad[ir_max-dr:ir_max+dr], 'bo')
plot_r2 = np.linspace(plot_r[ir_max-dr], plot_r[ir_max+dr], 100)
plot(plot_r2, gauss(plot_r2, *fit), 'r--')
plot(plot_r2, gauss(plot_r2, *fit2[0]), 'm--')
plot(plot_r2, gauss(plot_r2, *fit3.x), 'c--')

print 语句的结果是:

[7.71606518 1.64177704 0.05      ]
(array([7.71606518, 1.64177704, 0.05      ]), None, {'fvec': array([-10.05377583, -12.15582504, -13.93739459, -16.87939946,
       -20.59538122, -22.98496647, -22.98637114, -21.96068096,
       -20.82792474, -18.09731079]), 'nfev': 21, 'fjac': array([[ 9.57864291e+03,  0.00000000e+00,  0.00000000e+00,
         5.34522484e-01, -0.00000000e+00, -0.00000000e+00,
        -0.00000000e+00,  5.34522484e-01, -0.00000000e+00,
         2.67261242e-01],
       [ 0.00000000e+00, -0.00000000e+00,  0.00000000e+00,
         0.00000000e+00,  0.00000000e+00,  0.00000000e+00,
         0.00000000e+00,  0.00000000e+00,  0.00000000e+00,
         0.00000000e+00],
       [ 0.00000000e+00,  0.00000000e+00, -0.00000000e+00,
         0.00000000e+00,  0.00000000e+00,  0.00000000e+00,
         0.00000000e+00,  0.00000000e+00,  0.00000000e+00,
         0.00000000e+00]]), 'ipvt': array([3, 2, 1], dtype=int32), 'qtf': array([ 34.78215243,   6.75567556, -13.93739459])}, 'The relative error between two consecutive iterates is at most 0.000000', 2)
[4.21310821 1.6364875  0.04207018]

和情节:

编辑 2/错误: 为任何发现这一点的人记录,显然这(我认为)是 scipy 中的一个错误。我发现这两个数组是np.float32 和np.float64,如果我这样做,显然 scipy 无法正确处理:

xdata = np.copy(plot_r[ir_max-dr:ir_max+dr])
ydata = np.copy(grad[ir_max-dr:ir_max+dr])
fit, pcov = curve_fit(gauss, xdata, ydata, p0=p0, method='lm')

配合失败,但是

xdata = xdata.astype(dtype=np.float32)
ydata = ydata.astype(dtype=np.float32)
fit, pcov = curve_fit(gauss, xdata, ydata, p0=p0, method='lm')

工作正常...

【问题讨论】:

  • 既然您成功调试了问题,我认为您最好自行回答并附上 github 问题的链接 :-)

标签: python scipy


【解决方案1】:

答案:为任何发现此问题的人记录,显然这(我认为)是 scipy 中的一个错误。我发现这两个数组是 np.float32 和 np.float64 ,如果我这样做,显然 scipy 无法正确处理:

xdata = np.copy(plot_r[ir_max-dr:ir_max+dr])
ydata = np.copy(grad[ir_max-dr:ir_max+dr])
fit, pcov = curve_fit(gauss, xdata, ydata, p0=p0, method='lm')

配合失败,但是

xdata = xdata.astype(dtype=np.float32)
ydata = ydata.astype(dtype=np.float32)
fit, pcov = curve_fit(gauss, xdata, ydata, p0=p0, method='lm')

有效,本质上问题是最小二乘最小化无法处理不同的精度输入。用 scipy 打开 bug:12

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-07-12
    • 2016-01-18
    • 2013-07-30
    • 1970-01-01
    • 2018-03-12
    • 2019-07-25
    • 1970-01-01
    • 2020-11-26
    相关资源
    最近更新 更多