使用scipy、python和numpy进行非线性e^(-x)回归

8 投票
1 回答
4859 浏览
提问于 2025-04-16 08:49

下面的代码让我得到了一条平坦的线,而不是一个像e^(-x)那样的漂亮曲线来适应我的数据。有没有人能告诉我怎么修改下面的代码,让它能更好地拟合我的数据呢?

import numpy as np  
import matplotlib.pyplot as plt
import scipy.optimize

def _eNegX_(p,x):
    x0,y0,c,k=p  
    y = (c * np.exp(-k*(x-x0))) + y0
    return y

def _eNegX_residuals(p,x,y):
    return y - _eNegX_(p,x)

def Get_eNegX_Coefficients(x,y):
    print 'x is:  ',x  
    print 'y is:  ',y 

    # Calculate p_guess for the vectors x,y.  Note that p_guess is the
    # starting estimate for the minimization.
    p_guess=(np.median(x),np.min(y),np.max(y),.01)

    # Calls the leastsq() function, which calls the residuals function with an initial 
    # guess for the parameters and with the x and y vectors.  Note that the residuals
    # function also calls the _eNegX_ function.  This will return the parameters p that
    # minimize the least squares error of the _eNegX_ function with respect to the original
    # x and y coordinate vectors that are sent to it.
    p, cov, infodict, mesg, ier = scipy.optimize.leastsq(  
        _eNegX_residuals,p_guess,args=(x,y),full_output=1,warning=True)

    # Define the optimal values for each element of p that were returned by the leastsq() function. 
    x0,y0,c,k=p  
    print('''Reference data:\  
    x0 = {x0}
    y0 = {y0}
    c = {c}
    k = {k}
    '''.format(x0=x0,y0=y0,c=c,k=k))  

    print 'x.min() is:  ',x.min()
    print 'x.max() is:  ',x.max()
    # Create a numpy array of x-values
    numPoints = np.floor((x.max()-x.min())*100)
    xp = np.linspace(x.min(), x.max(), numPoints)
    print 'numPoints is:  ',numPoints
    print 'xp is:  ',xp
    print 'p is:  ',p
    pxp=_eNegX_(p,xp)
    print 'pxp is:  ',pxp

    # Plot the results  
    plt.plot(x, y, '>', xp, pxp, 'g-')
    plt.xlabel('BPM%Rest') 
    plt.ylabel('LVET/BPM',rotation='vertical')
    plt.xlim(0,3)
    plt.ylim(0,4)
    plt.grid(True) 
    plt.show()

    return p

# Declare raw data for use in creating regression equation 
x = np.array([1,1.425,1.736,2.178,2.518],dtype='float')  
y = np.array([3.489,2.256,1.640,1.043,0.853],dtype='float')  

p=Get_eNegX_Coefficients(x,y)

1 个回答

11

看起来你遇到的问题是出在你最开始的猜测上;像(1, 1, 1, 1)这样的初始值就能很好地工作:看起来不错的图
你有

p_guess=(np.median(x),np.min(y),np.max(y),.01)

对于这个函数

def _eNegX_(p,x):
    x0,y0,c,k=p  
    y = (c * np.exp(-k*(x-x0))) + y0
    return y

所以这是 test_data_maxe^( -.01(x - test_data_median)) + test_data_min

我对选择好的起始参数不是很了解,但我可以说一些事情。leastsq在这里找到的是一个局部最小值——选择这些值的关键是找到正确的“山”去攀登,而不是试图减少最小化算法需要做的工作。你的初始猜测看起来像这样(绿色): (1.736, 0.85299999999999998, 3.4889999999999999, 0.01) 替代文本

这导致了你得到的平坦线(蓝色): (-59.20295956, 1.8562 , 1.03477144, 0.69483784)

调整线的高度比增加k值带来的效果更大。如果你知道自己是在拟合这种数据,使用一个更大的k值。如果你不确定,我想你可以通过抽样你的数据来找到一个合适的k值,或者从前半部分和后半部分的平均斜率反推,但我不知道该怎么做。

补充:你也可以尝试多个初始猜测,运行几次最小化,然后选择残差最小的那条线。

撰写回答