[英]scipy curve_fit is not changing parameters from initial guess
我正在尝试将高斯拟合到一些非常清晰的数据中,并且由于某种原因scipy.optimize.curve_fit
并没有从最初的猜测中明显改变参数,我不知道为什么。 这是一个代码片段:
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 / BUG:为任何发现此问题的人记录,显然这(我认为)是 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 中的一个错误。 我发现这两个数组是 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')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.