如何使曲线拟合更好,同时集中在一些更好的精度

2024-04-20 06:13:42 发布

您现在位置:Python中文网/ 问答频道 /正文

在过去的两天里,我一直在研究一个特定的数据拟合(图片的橙色线1)。你知道吗

问题是,我希望它在更大的θ[0.1,1]上是准确的。事实上,我想从同一点开始(因此对于θ=1我们也得到了ψ=1),用这个形式:

ψ_f=a1*(1-x)**a2 +a3*(1-x)**a4+1

但它是超级坏,因为它得到了更大的inf。你知道吗

对于我使用的图像1scipy.optimize.curve_fit 对于一个简单的形式

ψ_f = a1 *x**a2

其他任何形式都很糟糕。你知道吗

你知道怎么做吗?:(

编辑:

数据是this file格式,使用以下加载代码:

ww=np.load('Hot3.npy')
s=ww[3]
z=np.array([ww[0],ww[1],ww[2])

xdata,ydata等于

xdata = s/max(s)
ydata = z[2]/min(z[2])

Orange is datafit


Tags: 数据图像a2a1np图片scipya3
1条回答
网友
1楼 · 发布于 2024-04-20 06:13:42

下面是一些示例代码,似乎给出了一个更好的拟合。请注意,我没有采取任何日志,也没有在日志规模绘图。 plot

import numpy, scipy, matplotlib
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
import warnings


ww=numpy.load('/home/zunzun/temp/hot3.npy')
xData = ww[3]/max(ww[3])
yData = ww[2]/min(ww[2])


def func(x, a, b, c): # Combined Power And Exponential equation from zunzun.com
    power = numpy.power(x, b)
    exponent = numpy.exp(c * x)
    return a * power * exponent 


# numpy defaults are all 1.0, try these instead
initialParameters = numpy.array([1.0,-1.0,-1.0])

# ignore intermediate overflow warning during curve_fit() routine
warnings.filterwarnings("ignore")

# curve fit the test data
fittedParameters, pcov = curve_fit(func, xData, yData, initialParameters)

modelPredictions = func(xData, *fittedParameters) 

absError = modelPredictions - yData

SE = numpy.square(absError) # squared errors
MSE = numpy.mean(SE) # mean squared errors
RMSE = numpy.sqrt(MSE) # Root Mean Squared Error, RMSE
Rsquared = 1.0 - (numpy.var(absError) / numpy.var(yData))

print('Parameters:', fittedParameters)
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,  'o')

    # create data for the fitted equation plot
    xModel = numpy.linspace(min(xData), max(xData))
    yModel = func(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)

相关问题 更多 >