【问题标题】:why scipy.optimize.curce fit function is not fitting the data points correctly and why giving large values of pfit?为什么 scipy.optimize.curce 拟合函数不能正确拟合数据点,为什么要给出较大的 pfit 值?
【发布时间】:2020-01-28 22:55:54
【问题描述】:

在下面的代码中,为什么拟合函数会给出较大的 pfit 值,以及为什么它不能正确拟合数据点。我的拟合功能有什么问题吗?

L = np.array([12,24,36,48])
Ec_L =np.array([-2.21173697, -2.01880398, -1.96508108, -2.0691906 ])

def ff(L,a,v,Ec):
    return (a*L**(-1.0/v))+Ec

x_data = 1.0/L
y_data = Ec_L

plt.scatter(x_data, y_data, marker='.', color='orange')

pfit,pcov = optimize.curve_fit(ff,x_data,y_data)
print("pfit: ",pfit)  #pfit:  [ 563.99154975 4377.13071157 -566.48046716]
print(pcov)

plt.plot(x_data, ff(L,*pfit), marker='.', color='red')

【问题讨论】:

    标签: python matplotlib scipy curve-fitting least-squares


    【解决方案1】:

    您在测试中使用L,但在您的拟合中使用1/L;我不知道你的意图,但如果你改为使用

    plt.plot(x_data, ff(1/L,*pfit), marker='.', color='red')
    

    合身看起来不那么不合身:

    【讨论】:

    • 是的,感谢您指出我的错误。你能解释一下为什么我需要选择 1/L 吗?我用 L 定义了我的拟合函数,为什么在 plt.plot (x_data,1/L) 中?
    • @huda_:我刚刚使用了1/L,因为这就是你定义x_data的方式;如果我只是说plt.plot(x_data, ff(x_data,*pfit), ...) 会更明显。
    【解决方案2】:

    您的数据似乎非常适合二次方程,并且似乎位于抛物线上。这是使用您的数据和二阶多项式方程的图形多项式拟合器,可以在代码顶部更改多项式阶数。

    import numpy, matplotlib
    import matplotlib.pyplot as plt
    
    
    L = [12,24,36,48]
    Ec_L = [-2.21173697, -2.01880398, -1.96508108, -2.0691906 ]
    
    # rename to match previous example code
    xData = numpy.array(L, dtype=float)
    yData = numpy.array(Ec_L, dtype=float)
    
    
    polynomialOrder = 2 # example quadratic equation
    
    
    # curve fit the test data
    fittedParameters = numpy.polyfit(xData, yData, polynomialOrder)
    print('Fitted Parameters:', fittedParameters)
    
    # predict a single value
    print('Single value prediction:', numpy.polyval(fittedParameters, 3.0))
    
    # Use polyval to find model predictions
    modelPredictions = numpy.polyval(fittedParameters, xData)
    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('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 = numpy.linspace(min(xData), max(xData))
        yModel = numpy.polyval(fittedParameters, xModel)
    
        # now the model as a line plot
        axes.plot(xModel, yModel)
    
        axes.set_title('numpy polyfit() quadratic example') # add a title
        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)
    

    【讨论】:

    • 二次方程显示最适合我的数据点。但是在我的拟合函数中,我有一个值 1/v,我需要通过拟合函数找出它,那么我将如何使用二次函数呢?此外,在我的拟合函数中,Ec 是截距,那么二次方程中的对应值是多少?
    • 如果方程中有“1/v”项的要求,则抛物线不适合,因为它不满足该要求。
    • 最后一个问题。在我的拟合函数中,Ec 是应变线的截距。在二次拟合中,Ec 是多少?如果我从二次方中找到 Ec 值(因为它最适合)然后我在我的拟合函数中插入该 Ec 值(具有 1/v)然后我只使用 1/v 作为拟合参数,那么是否正确?但是,二次方中的 Ec 是什么?
    • 我对问题上下文了解不多,无法回答,只能说二次方程参数之一是x = 0时的值,对于直线方程也是如此。
    猜你喜欢
    • 2018-12-09
    • 2012-05-26
    • 1970-01-01
    • 1970-01-01
    • 2016-07-20
    • 2022-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多