无初始猜测的指数衰减拟合

2024-03-28 20:49:11 发布

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

有没有人知道一个scipy/numpy模块,它允许对数据进行指数衰减?

Google search返回了一些博客文章,例如-http://exnumerus.blogspot.com/2010/04/how-to-fit-exponential-decay-example-in.html,但是这个解决方案需要预先指定y偏移量,这并不总是可能的

编辑:

曲线拟合是可行的,但如果没有对参数的初始猜测,它可能会失败得很惨,有时这是必需的。我使用的代码是

#!/usr/bin/env python
import numpy as np
import scipy as sp
import pylab as pl
from scipy.optimize.minpack import curve_fit

x = np.array([  50.,  110.,  170.,  230.,  290.,  350.,  410.,  470.,  
530.,  590.])
y = np.array([ 3173.,  2391.,  1726.,  1388.,  1057.,   786.,   598.,   
443.,   339.,   263.])

smoothx = np.linspace(x[0], x[-1], 20)

guess_a, guess_b, guess_c = 4000, -0.005, 100
guess = [guess_a, guess_b, guess_c]

exp_decay = lambda x, A, t, y0: A * np.exp(x * t) + y0

params, cov = curve_fit(exp_decay, x, y, p0=guess)

A, t, y0 = params

print "A = %s\nt = %s\ny0 = %s\n" % (A, t, y0)

pl.clf()
best_fit = lambda x: A * np.exp(t * x) + y0

pl.plot(x, y, 'b.')
pl.plot(smoothx, best_fit(smoothx), 'r-')
pl.show()

但如果我们去掉“p0=guess”,它就失败了。


Tags: lambdaimportnumpyasnpscipyparamsarray
3条回答

无初始猜测的指数拟合过程-非迭代过程:

enter image description here

这来自论文(第16-17页):https://fr.scribd.com/doc/14674814/Regressions-et-equations-integrales

如有必要,这可用于初始化非线性回归演算,以便选择特定的优化标准。

示例:

乔金顿的例子很有趣。不幸的是,没有显示数据,只有图表。因此,下面的数据(x,y)来自于对图形的图形扫描,因此数值可能并不完全是Joe Kington使用的数值。然而,考虑到点的广泛分散性,“拟合”曲线的各自方程彼此非常接近。

enter image description here

上图是金顿图的副本。

下图显示了使用上述程序获得的结果。

你有两个选择:

  1. 将系统线性化,并将一条直线拟合到数据日志中。
  2. 使用非线性解算器(例如^{}

到目前为止,第一个选择是最快、最强大的。但是,它要求你知道y偏移的先验,否则不可能线性化方程。(也就是说,y = A * exp(K * t)可以通过拟合y = log(A * exp(K * t)) = K * t + log(A)来线性化,但是y = A*exp(K*t) + C只能通过拟合y - C = K*t + log(A)来线性化,并且由于y是您的自变量,因此必须事先知道C这是一个线性系统。

如果使用非线性方法,则a)不能保证收敛并得到解,b)速度会慢得多,c)对参数的不确定性给出的估计要差得多,d)通常精度要低得多。然而,与线性反演相比,非线性方法有一个巨大的优势:它可以求解非线性方程组。在您的情况下,这意味着您不必事先知道C

举个例子,我们用线性和非线性的方法来求解y=A*exp(K*t)和一些噪声数据:

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


def main():
    # Actual parameters
    A0, K0, C0 = 2.5, -4.0, 2.0

    # Generate some data based on these
    tmin, tmax = 0, 0.5
    num = 20
    t = np.linspace(tmin, tmax, num)
    y = model_func(t, A0, K0, C0)

    # Add noise
    noisy_y = y + 0.5 * (np.random.random(num) - 0.5)

    fig = plt.figure()
    ax1 = fig.add_subplot(2,1,1)
    ax2 = fig.add_subplot(2,1,2)

    # Non-linear Fit
    A, K, C = fit_exp_nonlinear(t, noisy_y)
    fit_y = model_func(t, A, K, C)
    plot(ax1, t, y, noisy_y, fit_y, (A0, K0, C0), (A, K, C0))
    ax1.set_title('Non-linear Fit')

    # Linear Fit (Note that we have to provide the y-offset ("C") value!!
    A, K = fit_exp_linear(t, y, C0)
    fit_y = model_func(t, A, K, C0)
    plot(ax2, t, y, noisy_y, fit_y, (A0, K0, C0), (A, K, 0))
    ax2.set_title('Linear Fit')

    plt.show()

def model_func(t, A, K, C):
    return A * np.exp(K * t) + C

def fit_exp_linear(t, y, C=0):
    y = y - C
    y = np.log(y)
    K, A_log = np.polyfit(t, y, 1)
    A = np.exp(A_log)
    return A, K

def fit_exp_nonlinear(t, y):
    opt_parms, parm_cov = sp.optimize.curve_fit(model_func, t, y, maxfev=1000)
    A, K, C = opt_parms
    return A, K, C

def plot(ax, t, y, noisy_y, fit_y, orig_parms, fit_parms):
    A0, K0, C0 = orig_parms
    A, K, C = fit_parms

    ax.plot(t, y, 'k--', 
      label='Actual Function:\n $y = %0.2f e^{%0.2f t} + %0.2f$' % (A0, K0, C0))
    ax.plot(t, fit_y, 'b-',
      label='Fitted Function:\n $y = %0.2f e^{%0.2f t} + %0.2f$' % (A, K, C))
    ax.plot(t, noisy_y, 'ro')
    ax.legend(bbox_to_anchor=(1.05, 1.1), fancybox=True, shadow=True)

if __name__ == '__main__':
    main()

Fitting exp

注意,线性解提供的结果更接近实际值。但是,我们必须提供y偏移值才能使用线性解。非线性解不需要先验知识。

我将使用scipy.optimize.curve_fit函数。它的doc字符串甚至有一个拟合指数衰减的例子,我将在这里复制:

>>> import numpy as np
>>> from scipy.optimize import curve_fit
>>> def func(x, a, b, c):
...     return a*np.exp(-b*x) + c

>>> x = np.linspace(0,4,50)
>>> y = func(x, 2.5, 1.3, 0.5)
>>> yn = y + 0.2*np.random.normal(size=len(x))

>>> popt, pcov = curve_fit(func, x, yn)

由于随机噪声的加入,拟合参数会有所变化,但是我得到了2.47990495,1.40709306,0.53753635作为a,b,和c,所以噪声在那里不是很糟糕。如果我适合y而不是yn,我会得到精确的a、b和c值。

相关问题 更多 >