【发布时间】:2020-12-11 13:09:33
【问题描述】:
Here is my data in excel 我想将这些数据拟合成正弦曲线
这是我的代码,
#Fitting function
def func(x, offset, A, freq, phi):
return offset + A * np.sin(freq * x + phi)
#Experimental x and y data points
# test_df is the input excel df
x_data = test_df['x_data']
y_data = test_df['y_data']
#Plot input data points
plt.plot(x_data, y_data, 'bo', label='experimental-data')
# Initial guess for the parameters
initial_guess = [.38, 2.3, .76, 2.77]
#Perform the curve-fit
popt, pcov = curve_fit(func, x_data, y_data, initial_guess)
print(popt)
#x values for the fitted function
x_fit = np.arange(0.0, 31, 0.01)
#Plot the fitted function
plt.plot(x_fit, func(x_fit, *popt), 'r')
plt.show()
【问题讨论】:
-
通过减少异常值的影响来尝试更稳健的拟合。 scipy-cookbook.readthedocs.io/items/robust_regression.html
-
这看起来很有希望,我会试试的。谢谢! @mikuszefski
标签: python matplotlib scikit-learn curve-fitting