Scipy优化曲线拟合不适合简单拟合
我试图将曲线适合并减去该数据集以删除长期变化,例如:
blue曲线是原始的(添加了偏移以查看差异),然后橙色是橙色拟合的结果然后减去。但是,由于某种原因,我的合身性无法正常工作,并且输出看起来像这样:
,我不确定为什么它失败了...代码使用(我生成了上面的该图) )但是由于某种原因,它似乎已经停止了,我认为我没有任何改变。
Here is the code I am using:
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from scipy.optimize import curve_fit
df = pd.read_csv('stack1.csv')
def harmonics(day, A, B, C, D):
return A * np.sin( (2 * np.pi * day)/365.25 ) + B * np.cos( (2 * np.pi * day)/365.25 ) + C * np.sin( (4 * np.pi * day)/365.25 ) + D * np.cos( (4 *np.pi * day)/365.25 )
popt, pcov = curve_fit(harmonics, df['X'], df['Y'])
Y_fit = list(np.asarray(df['Y']) - harmonics(np.asarray(df['X']), popt[0],popt[1],popt[2],popt[3]))
plt.plot(df['X'],df['Y']+0.7)
plt.plot(df['X'],Y_fit)
plt.show()
and here is the data I am trying to fit: 数据
我不确定为什么它不起作用,我尝试了所有可以找到的东西(包括更改初始猜测和界限),但没有运气。有人可以看看,让我知道如何解决这个问题吗?谢谢!
I am trying to fit and subtract a curve to this dataset to remove long term variations, like this:
where the blue curve is the original (offset added to see the difference), then the orange is the result of fitting then subtracting. However, for some reason my fit is not working and the output has been looking like this:
and I'm not sure why it's failing... The code used to work (I generated that figure above) but for some reason it seems to have stopped and I don't think I changed anything.
Here is the code I am using:
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from scipy.optimize import curve_fit
df = pd.read_csv('stack1.csv')
def harmonics(day, A, B, C, D):
return A * np.sin( (2 * np.pi * day)/365.25 ) + B * np.cos( (2 * np.pi * day)/365.25 ) + C * np.sin( (4 * np.pi * day)/365.25 ) + D * np.cos( (4 *np.pi * day)/365.25 )
popt, pcov = curve_fit(harmonics, df['X'], df['Y'])
Y_fit = list(np.asarray(df['Y']) - harmonics(np.asarray(df['X']), popt[0],popt[1],popt[2],popt[3]))
plt.plot(df['X'],df['Y']+0.7)
plt.plot(df['X'],Y_fit)
plt.show()
and here is the data I am trying to fit: data
I'm not sure why it isn't working, and I have tried everything I could find (including changing the initial guess and the bounds) but have had no luck. Can someone please take a look and let me know how to fix this? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论