python - 打印曲线拟合功能

标签 python numpy printing curve-fitting trigonometry

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

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)

关于python - 打印曲线拟合功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52712821/

相关文章:

java - 如何从java打印excel文件?

python - 涉及列表和索引的基本 Python 练习出现问题

python - 如何在Python中重复石头、剪刀、布的游戏

python - 自动格式化 Python 注释

python - 自定义 runserver 命令在静态文件上获取 404

python - 如何非对称地填充数组(例如,仅从一侧)?

python - numpy.concatenate 如何在列表上工作

python - 如何将带有依赖项的 python 脚本打包成 zip/tar?

javascript - 在 birt 中缩放到 "Fit All Columns on One Page"

css - `page-break-inside: avoid;` 的跨浏览器支持