Skip to main content
 首页 » 编程设计

python之对具有 x 和 y 误差的数据拟合幂律函数

2025年05月04日10sharpest

我有一组数据,我想拟合一个幂律函数

y=a*x**b 

使用 python 库。另一个问题是我在 x 和 y 方向上都有错误,我不知道哪个库可以让我适应这两个错误的功能。数据为here .我也尝试使用 gnuplot 来进行拟合,但看起来不太有前途,而且我无法使用错误信息。

有什么建议吗?

请您参考如下方法:

实际上,scipy 有一个 Orthogonal distance regression包裹。

这是他们的线性拟合示例,您所要做的就是将 f 更改为幂律:

from scipy.odr import Model, Data, ODR 
 
def f(p, x): 
    '''Linear function y = m*x + b''' 
    # B is a vector of the parameters. 
    # x is an array of the current x values. 
    # x is in the same format as the x passed to Data or RealData. 
    # 
    # Return an array in the same format as y passed to Data or RealData. 
    return p[0] * x ** p[1] 
 
linear = Model(f) 
#sx, sy are arrays os error estimates 
mydata = Data(x, y, wd=1./power(sx,2), we=1./power(sy,2)) 
#beta0 are the inital parameter estimates 
myodr = ODR(mydata, linear, beta0=[10, -1.0]) 
myoutput = myodr.run() 
myoutput.pprint()