python - 在没有初始猜测的情况下拟合指数衰减

标签 python numpy scipy

有谁知道一个 scipy/numpy 模块,它可以让数据适应指数衰减?

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

编辑:

curve_fit 有效,但它可能会在没有初始猜测参数的情况下失败,有时需要这样做。我正在使用的代码是

#!/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”,它会惨败。

最佳答案

你有两个选择:

  1. 线性化系统,并在数据日志中加入一行。
  2. 使用非线性求解器(例如 scipy.optimize.curve_fit

第一个选项是迄今为止最快和最强大的。但是,它要求您先验地知道 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 偏移值才能使用线性解决方案。非线性解决方案不需要这种先验知识。

关于python - 在没有初始猜测的情况下拟合指数衰减,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3938042/

相关文章:

python - scipy.signal welch 与 matlab pwelch 等价

Python 日志记录 - 如何继承根记录器级别和处理程序

python - PyQt 从用户获取日期

python - 尝试将 python 与 photos.capture_image() 一起用于 Kairos 注册 API

numpy 函数给出不正确的结果 - 手动和 Excel 检查

python - 包.模块导入

python - 按字符串向 pandas 数据框添加颜色

python - 二维 numpy 数组组上最快的应用函数

python - python中返回多维列表元素的函数

python - Python 中的复杂集成