打印曲线拟合功能



我一直在努力寻找一种方法来获取下面曲线拟合函数的确定参数以进行打印。该图与我的数据正确匹配,但我无法弄清楚如何获得它产生的方程。任何帮助将不胜感激!

import matplotlib.pyplot as plt
import numpy as np
from scipy.optimize import curve_fit
x_data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31]
y_data = [.99, 1, .98, .93, .85, .77, .67, .56, .46, .36, .27, .19, .12, .07, .03, .01, 0, .01, .05, .09, .16, .24, .33, .44, .55, .65, .76, .85, .93, .98, 1]
x_val = np.array(x_data)
y_val = np.array(y_data)
def fitFunc(x, a, b, c, d):
return a * np.sin((2* np.pi / b) * x - c) + d
print(a, b, c, d)
plt.plot(x_val, y_val, marker='.', markersize=0, linewidth='0.5', color='green')
popt, pcov = curve_fit(fitFunc, x_val, y_val)
plt.plot(x_val, fitFunc(x_val, *popt), color='orange', linestyle='--')

下面是使用数据的图形示例,请注意公式。此示例使用根据数据的散点图手动估计的初始参数估计值,默认情况下默认curve_fit估计值均为 1.0,在这种情况下效果不佳。

import numpy as np
import scipy, matplotlib
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
xData = np.array([1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0, 17.0, 18.0, 19.0, 20.0, 21.0, 22.0, 23.0, 24.0, 25.0, 26.0, 27.0, 28.0, 29.0, 30.0, 31.0])
yData = np.array([.99, 1.0, 0.98, 0.93, 0.85, 0.77, 0.67, 0.56, 0.46, 0.36, 0.27, 0.19, 0.12, 0.07, 0.03, 0.01, 0, 0.01, 0.05, 0.09, 0.16, 0.24, 0.33, 0.44, 0.55, 0.65, 0.76, 0.85, 0.93, 0.98, 1.0])

def fitFunc(x, amplitude, center, width, offset):
return amplitude * np.sin(np.pi * (x - center) / width) + offset
# these are the curve_fit default parameter estimates, and
# do not work well for this data and equation - manually estimate below
#initialParameters = np.array([1.0, 1.0, 1.0, 1.0])
# eyeball the scatterplot for some better, simple, initial parameter estimates
initialParameters = np.array([0.5, 1.0, 16.0, 0.5])
# curve fit the test data using initial parameters
fittedParameters, pcov = curve_fit(fitFunc, xData, yData, initialParameters)
print(fittedParameters)
modelPredictions = fitFunc(xData, *fittedParameters) 
absError = modelPredictions - yData
SE = np.square(absError) # squared errors
MSE = np.mean(SE) # mean squared errors
RMSE = np.sqrt(MSE) # Root Mean Squared Error, RMSE
Rsquared = 1.0 - (np.var(absError) / np.var(yData))
print('RMSE:', RMSE)
print('R-squared:', Rsquared)
print()

##########################################################
# graphics output section
def ModelAndScatterPlot(graphWidth, graphHeight):
f = plt.figure(figsize=(graphWidth/100.0, graphHeight/100.0), dpi=100)
axes = f.add_subplot(111)
# first the raw data as a scatter plot
axes.plot(xData, yData,  'D')
# create data for the fitted equation plot
xModel = np.linspace(min(xData), max(xData))
yModel = fitFunc(xModel, *fittedParameters)
# now the model as a line plot
axes.plot(xModel, yModel)
axes.set_xlabel('X Data') # X axis data label
axes.set_ylabel('Y Data') # Y axis data label
plt.show()
plt.close('all') # clean up after using pyplot
graphWidth = 800
graphHeight = 600
ModelAndScatterPlot(graphWidth, graphHeight)

最新更新