【问题标题】:fit a power law function to the data with both x and y errors将幂律函数拟合到具有 x 和 y 误差的数据
【发布时间】:2015-01-07 05:07:06
【问题描述】:

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

y=a*x**b

使用 python 库。另一个问题是xy 方向都有错误,我不知道哪个库可以让我适应这两个错误的函数。数据为here。我还尝试使用 gnuplot 进行拟合,但看起来不太有希望,而且我无法使用错误信息。

有什么建议吗?

【问题讨论】:

    标签: python numpy scipy scikit-learn


    【解决方案1】:

    实际上,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()
    

    【讨论】:

    • 它只适用于线性回归还是也适用于非线性拟合?
    • 也应该适用于非线性模型。试一试。如果可行,请接受,以便其他人找到答案。
    • 您为什么不实际更改您的答案以适应幂律?它应该很简单
    • @eickenberg,是的,昨天很着急。完成。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-07
    • 2017-05-08
    • 2014-12-10
    • 1970-01-01
    相关资源
    最近更新 更多