【发布时间】:2020-04-05 03:56:23
【问题描述】:
我正在尝试使用scipy.optimize.curve_fit 拟合一些数据。我有read the documentation 和this StackOverflow post,但似乎都没有回答我的问题。
我有some data,它很简单,2D 数据看起来近似于三角函数。我想用一个通用的三角函数来拟合它
使用scipy。
我的做法如下:
from __future__ import division
import numpy as np
from scipy.optimize import curve_fit
#Load the data
data = np.loadtxt('example_data.txt')
t = data[:,0]
y = data[:,1]
#define the function to fit
def func_cos(t,A,omega,dphi,C):
# A is the amplitude, omega the frequency, dphi and C the horizontal/vertical shifts
return A*np.cos(omega*t + dphi) + C
#do a scipy fit
popt, pcov = curve_fit(func_cos, t,y)
#Plot fit data and original data
fig = plt.figure(figsize=(14,10))
ax1 = plt.subplot2grid((1,1), (0,0))
ax1.plot(t,y)
ax1.plot(t,func_cos(t,*popt))
这个输出:
蓝色是数据,橙色是拟合。显然我做错了什么。有什么指点吗?
【问题讨论】:
-
请上传
'example_data.txt'的样本数据,否则难以重现。 -
可以通过
some_data超链接访问
标签: python scipy curve-fitting scipy-optimize