Skip to main content
 首页 » 编程设计

python之如何强制 scipy.optimize.curve_fit 修复第一点

2024年09月07日7daizhj

我使用此代码通过使用 scipy.optimize.curve_fit 拟合指数来平滑数据:

def smooth_data_v1(x_arr,y_arr): 
    def func(x, a, b, c): 
        return a*np.exp(-b*x)+c 
 
    #Scale data 
    y = y_orig / 10000.0 
    x = 500.0 * x_orig 
 
    popt, pcov = curve_fit(func, x, y, p0=(1, 0.01, 1)) 
 
    y_smooth = func(x, *popt) # Calcaulate smoothed values for same points 
 
    #Undo scaling 
    y_final = y_smooth * 10000.0 
 
    return y_final 

但是我希望估计的指数曲线通过第一个点。

坏情况:

好案例:

我尝试使用第一个点 x0,y0 删除最后一个参数:

def smooth_data_v2(x_orig,y_orig): 
    x0 = x_orig[0] 
    y0 = y_orig[0] 
 
    def func(x, a, b): 
        return a*np.exp(-b*x)+y0-a*np.exp(-b*x0) 
 
    #Scale data 
    y = y_orig / 10000.0 
    x = 500.0 * x_orig 
 
    popt, pcov = curve_fit(func, x, y, p0=(1, 0.01)) 
 
    y_smooth = func(x, *popt) # Calcaulate smoothed values for same points 
 
    #Undo scaling 
    y_final = y_smooth * 10000.0 
 
    return y_final 

无论如何出了问题,我得到:

a 参数真的很大 popt [4.45028144e+05 2.74698863e+01]

有什么想法吗?

更新:

数据示例

x_orig [  0.   1.   2.   3.   4.   5.   6.   7.   8.   9.  10.  11.  12.  13.  14.] 
y_orig [ 445057.  447635.  450213.  425089.  391746.  350725.  285433.  269027. 
  243835.  230587.  216757.  202927.  189097.  175267.  161437.] 

请您参考如下方法:

Scipy curve_fit允许传递参数 sigma,它被设计为加权拟合的标准偏差。但是这个数组可以填充任意数据:

from scipy.optimize import curve_fit 
 
def smooth_data_v1(x_arr,y_arr): 
    def func(x, a, b, c): 
        return a*np.exp(-b*x)+c 
 
    #create the weighting array 
    y_weight = np.empty(len(y_arr)) 
    #high pseudo-sd values, meaning less weighting in the fit 
    y_weight.fill(10) 
    #low values for point 0 and the last points, meaning more weighting during the fit procedure  
    y_weight[0] = y_weight[-5:-1] = 0.1 
 
    popt, pcov = curve_fit(func, x_arr, y_arr, p0=(y_arr[0], 1, 1), sigma = y_weight, absolute_sigma = True) 
    print("a, b, c:", *popt) 
    y_smooth = func(x_arr, *popt) 
 
    return y_smooth 
 
 
x_orig = np.asarray([  0,   1,   2,   3,   4,   5,   6,   7,   8,   9,  10,  11,  12,  13,  14]) 
y_orig = np.asarray([ 445057,  447635,  450213,  425089,  391746,  350725,  285433,  269027, 
  243835,  230587,  216757,  202927,  189097,  175267,  161437]) 
 
print(smooth_data_v1(x_orig, y_orig)) 

如您所见,现在第一个点和最后一个点都接近原始值,但这种“限制值”有时会以其他数据点为代价。
您可能还注意到,我删除了您的重新缩放部分。恕我直言,在曲线拟合程序之前不应该这样做。通常最好使用原始数据。此外,您的数据并不能很好地用指数函数表示,因此 b 值很小。