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

2024-05-12

下面的代码为我提供了一条最佳拟合线的平坦线,而不是沿着 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)

It looks like it's a problem with your initial guesses; something like (1, 1, 1, 1) works fine:graph that looks good
You have

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

I don't know much about the art of choosing good starting parameters, but I can say a few things. leastsq is finding a local minimum here - the key in choosing these values is to find the right mountain to climb, not to try to cut down on the work that the minimization algorithm has to do. Your initial guess looks like this (green): (1.736, 0.85299999999999998, 3.4889999999999999, 0.01) alt text

这会导致你的平线(蓝色):(-59.20295956, 1.8562 , 1.03477144, 0.69483784)

调整线的高度比增加 k 值获得的收益更大。如果您知道自己适合此类数据,请使用更大的 k。如果您不知道,我想您可以尝试通过对数据进行采样来找到合适的 k 值,或者从前半部分和后半部分的平均值之间的斜率返回,但我不知道该怎么做关于那个。

编辑:您也可以从几次猜测开始,运行几次最小化,然后选择残差最低的线。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 scipy、python、numpy 进行非线性 e^(-x) 回归 的相关文章

  • QSortFilterProxyModel + QAbstractItemModel modelIndex.internalPointer() 导致崩溃

    我在 PyQt 4 8 Python 2 7 中实现了自己的 QAbstractItemModel class FriendListModel QtCore QAbstractItemModel def init self groups c
  • 使用 Flask SQLAlchemy 进行表(模型)继承

    我遵循了这个建议question https stackoverflow com questions 1337095 sqlalchemy inheritance但我仍然收到此错误 sqlalchemy exc NoForeignKeysE
  • 同情因子简单关系

    我在 sympy 中有一个简单的因式分解问题 无法解决 我在 sympy 处理相当复杂的积分方面取得了巨大成功 但我对一些简单的事情感到困惑 如何得到 phi 2 2 phi phi 0 phi 0 2 8 因式分解 phi phi 0 2
  • 运行源代码中包含 Unicode 字符的 Python 2.7 代码

    我想运行一个在源代码中包含 unicode utf 8 字符的 Python 源文件 我知道这可以通过添加评论来完成 coding utf 8 在一开始的时候 但是 我希望不使用这种方法来做到这一点 我能想到的一种方法是以转义形式编写 un
  • 01 无效令牌[重复]

    这个问题在这里已经有答案了 嘿 学习 python3有一段时间了 遇到字典和dictionary name get 方法并尝试获取随机键值 问题 data data get key 1 它有效并且返回 1 但如果我使用data get ke
  • 在 Python 中比较日期 - 如何处理时区修饰符

    我正在做Python日期比较 假设我有一个这样的约会 Fri Aug 17 12 34 00 2012 0000 我按以下方式解析它 dt datetime strptime Fri Aug 17 12 34 00 2012 0000 a
  • Python 中的二进制相移键控

    我目前正在编写一些代码 以使用音频转换通过激光传输消息 文件 和其他数据 我当前的代码使用 python 中 binascii 模块中的 hexlify 函数将数据转换为二进制 然后为 1 发出一个音调 为 0 发出不同的音调 这在理论上是
  • Python3模拟用另一个函数替换函数

    如何使用 python 中的另一个函数来模拟一个函数 该函数也将提供一个模拟对象 我有类似以下操作的代码 def foo arg1 arg2 r bar arg1 does interesting things 我想替换的实现bar函数 让
  • python 语言环境奇怪的错误。这究竟是怎么回事?

    所以今天我升级到了 bazaar 2 0 2 我开始收到这条消息 顺便说一句 我在雪豹上 bzr warning unknown locale UTF 8 Could not determine what text encoding to
  • Docker:通过 Gunicorn 运行 Flask 应用程序 - Worker 超时?表现不佳?

    我正在尝试创建一个用Python Flask编写的新应用程序 由gunicorn运行 然后进行dockerized 我遇到的问题是 docker 容器内的性能非常差 不一致 我最终得到了响应 但我不明白为什么性能会下降 有时我会在日志中看到
  • Python/Flask:应用程序在关闭后正在运行

    我正在开发一个简单的 Flask Web 应用程序 我使用 Eclipse Pydev 当我开发该应用程序时 由于代码更改 我必须经常重新启动该应用程序 这就是问题所在 当我运行该应用程序时 我可以在本地主机上看到该框架 这很好 但是当我想
  • 如何在Python中正确声明ctype结构+联合?

    我正在制作一个二进制数据解析器 虽然我可以依靠 C 但我想看看是否可以使用 Python 来完成该任务 我对如何实现这一点有一些了解 我当前的实现如下所示 from ctypes import class sHeader Structure
  • 散景中的时间序列流

    我想在散景中绘制实时时间序列 我只想在每次更新时绘制新的数据点 我怎样才能做到这一点 散景网站上有一个动画情节的示例 但它每次都需要重新绘制整个图片 另外 我正在寻找一个简单的示例 我可以在其中逐点绘制时间序列的实时绘图 散景效果0 11
  • 将 ASCII 字符转换为“”unicode 表示法的脚本

    我正在对 Linux 区域设置文件进行一些更改 usr share i18n locales like pt BR 并且需要格式化字符串 例如 d m Y H M 必须以 Unicode 指定 其中每个 在本例中为 ASCII 字符表示为
  • 如何在 Tkinter 的 Button 小部件中创建多个标签?

    我想知道如何在 Tkinter 中创建具有多个标签的按钮小部件 如下图所示 带有子标签的按钮 https i stack imgur com jOZRw jpg正如您所看到的 在某些按钮中有一个子标签 例如按钮 X 有另一个小标签 A 我试
  • if/else 在 while 循环内正确缩进[关闭]

    Closed 这个问题是无法重现或由拼写错误引起 help closed questions 目前不接受答案 我开始学习 Python 编程大约几周了 我遇到了一些麻烦 下面的代码是一个小程序 用于检查列表中是否有偶数 如果找到第一个偶数
  • tweepy 流到 sqlite 数据库 - 语法错误[重复]

    这个问题在这里已经有答案了 可能的重复 tweepy 流到 sqlite 数据库 语法无效 https stackoverflow com questions 9434205 tweepy stream to sqlite database
  • 通过子类化 `io.TextIOWrapper` 来子类化文件 - 但它的构造函数有什么签名?

    我正在尝试子类化io TextIOWrapper下列的这个帖子 https stackoverflow com a 23796737 974555 虽然我的目标不同 以此开始 注意 动机 https stackoverflow com a
  • 带有整数的 np.sqrt 和 where 条件返回错误结果

    当我将 numpy sqrt 方法应用于带有 a 的整数数组时 我得到了奇怪的结果where健康 状况 见下文 对于整数 a np array 1 4 9 np sqrt a where a gt 5 Out 3 array 0 0 5 3
  • Python 中的 C 指针算术

    我正在尝试将一个简单的 C 程序转换为 Python 但由于我对 C 和 Python 都一无所知 这对我来说很困难 我被 C 指针困住了 有一个函数采用 unsigned long int 指针并将其值添加到 while 循环中的某些变量

随机推荐