I do not understand what's wrong with this data fitting:
我不明白这个数据拟合有什么问题:
from scipy.optimize import curve_fit
def sin_fit(x, *p):
a,b,c,d= p
return a + b*np.sin(c*x+ d)
# p0 is the initial guess for the fitting coefficients
p0 = [0.1, 1., 1., 0.1]
coeff, var_matrix = curve_fit(sin_fit, t, data, p0=p0)
I guess either there is something obvious wrong, which I am missing at the moment, or I cannot use the curve fit from scipy for this problem. Any ideas?
我想或者有一些明显错误的东西,我现在缺少了,或者我不能使用scipy的曲线拟合来解决这个问题。有任何想法吗?
2 个解决方案
#1
4
It seems like this is a problem with your initial guess, in particular the frequency you chose is too far off. At your current initial guess p0
, curve_fit
will not converge fast enough on a good answer, so you need to choose a better p0
. Here's an example of what I mean:
看起来这是你初步猜测的一个问题,特别是你选择的频率太远了。根据您当前的初始猜测p0,curve_fit将无法快速收敛到一个好的答案,因此您需要选择更好的p0。这是我的意思的一个例子:
t = np.linspace(0,50,1000)
data = 0.275 * (np.random.rand(len(t)) * 0.2 + 1.) * np.sin(2. * np.pi / 15. * t - 7.5)
p0 = [0.2, 0.5, 1.5 * np.pi / 14, 0.]
coeff, var_matrix = curve_fit(sin_fit, t, data, p0=p0)
plt.plot(t, data, 'bo')
plt.plot(t, sin_fit(t, *p0), 'g-')
plt.plot(t, sin_fit(t, *coeff), 'r-')
Below you can see that with a closer initial guess (green curve) curve_fit
will perform the fit better (red curve):
下面你可以看到,通过更接近的初始猜测(绿色曲线),curve_fit将更好地执行拟合(红色曲线):
#2
3
Everything works and the algorithm found a local minimum, as it is supposed to do. Your initial guess of the frequency is just far of from reality.
一切正常,算法找到了一个局部最小值,就像它应该做的那样。您对频率的初步猜测与实际情况相去甚远。
Good fitting does not only rely on the right model, but also on good starting values. Expecially for periodic curves, higher harmonics can form a local minimum.
良好的拟合不仅依赖于正确的模型,而且还依赖于良好的起始值。特别是对于周期性曲线,高次谐波可以形成局部最小值。
Thats why fitting is never a blackbox like data in -> result out mechanism. User interference is almost always needed. If that is not favorable, and you have such kind of data, rather use fourier analysis to get the information aimed for.
这就是为什么拟合永远不会像数据中的黑盒子那样 - >结果输出机制。几乎总是需要用户干扰。如果这不是有利的,并且您有这样的数据,而是使用傅里叶分析来获取目标信息。
#1
4
It seems like this is a problem with your initial guess, in particular the frequency you chose is too far off. At your current initial guess p0
, curve_fit
will not converge fast enough on a good answer, so you need to choose a better p0
. Here's an example of what I mean:
看起来这是你初步猜测的一个问题,特别是你选择的频率太远了。根据您当前的初始猜测p0,curve_fit将无法快速收敛到一个好的答案,因此您需要选择更好的p0。这是我的意思的一个例子:
t = np.linspace(0,50,1000)
data = 0.275 * (np.random.rand(len(t)) * 0.2 + 1.) * np.sin(2. * np.pi / 15. * t - 7.5)
p0 = [0.2, 0.5, 1.5 * np.pi / 14, 0.]
coeff, var_matrix = curve_fit(sin_fit, t, data, p0=p0)
plt.plot(t, data, 'bo')
plt.plot(t, sin_fit(t, *p0), 'g-')
plt.plot(t, sin_fit(t, *coeff), 'r-')
Below you can see that with a closer initial guess (green curve) curve_fit
will perform the fit better (red curve):
下面你可以看到,通过更接近的初始猜测(绿色曲线),curve_fit将更好地执行拟合(红色曲线):
#2
3
Everything works and the algorithm found a local minimum, as it is supposed to do. Your initial guess of the frequency is just far of from reality.
一切正常,算法找到了一个局部最小值,就像它应该做的那样。您对频率的初步猜测与实际情况相去甚远。
Good fitting does not only rely on the right model, but also on good starting values. Expecially for periodic curves, higher harmonics can form a local minimum.
良好的拟合不仅依赖于正确的模型,而且还依赖于良好的起始值。特别是对于周期性曲线,高次谐波可以形成局部最小值。
Thats why fitting is never a blackbox like data in -> result out mechanism. User interference is almost always needed. If that is not favorable, and you have such kind of data, rather use fourier analysis to get the information aimed for.
这就是为什么拟合永远不会像数据中的黑盒子那样 - >结果输出机制。几乎总是需要用户干扰。如果这不是有利的,并且您有这样的数据,而是使用傅里叶分析来获取目标信息。