【问题标题】:Linear fitting in python with uncertainty in both x and y coordinates [closed]python中的线性拟合,x和y坐标都有不确定性[关闭]
【发布时间】:2014-03-26 18:32:26
【问题描述】:

您好,我想问一下我的 Python 用户,他们是如何进行线性拟合的。

过去两周我一直在寻找执行此任务的方法/库,我想分享一下我的经验:

如果您想基于最小二乘法执行线性拟合,您有很多选择。例如,您可以在 numpy 和 scipy 中找到类。我自己选择了 linfit 提供的那个(遵循 IDL 中 linfit 函数的设计):

http://nbviewer.ipython.org/github/djpine/linfit/blob/master/linfit.ipynb

此方法假设您在 y 轴坐标中引入 sigma 以适合您的数据。

但是,如果您已经量化了 x 轴和 y 轴的不确定性,则没有那么多选择。 (主要的 Python 科学库中没有 IDL“Fitexy”等价物)。到目前为止,我只找到了“kmpfit”库来执行这项任务。幸运的是,它有一个非常完整的网站来描述其所有功能:

https://github.com/josephmeiring/kmpfit http://www.astro.rug.nl/software/kapteyn/kmpfittutorial.html#

如果有人知道其他方法,我也很想知道。

无论如何,我希望这会有所帮助。

【问题讨论】:

    标签: python curve-fitting linear-equation


    【解决方案1】:

    Scipy 中的Orthogonal distance regression 允许您使用xy 中的错误进行非线性拟合。

    下面显示的是基于 scipy 页面上给出的示例的简单示例。它试图将二次函数拟合到一些随机数据。

    import numpy as np
    import matplotlib.pyplot as plt
    from scipy.odr import *
    
    import random
    
    # Initiate some data, giving some randomness using random.random().
    x = np.array([0, 1, 2, 3, 4, 5])
    y = np.array([i**2 + random.random() for i in x])
    
    x_err = np.array([random.random() for i in x])
    y_err = np.array([random.random() for i in x])
    
    # Define a function (quadratic in our case) to fit the data with.
    def quad_func(p, x):
         m, c = p
         return m*x**2 + c
    
    # Create a model for fitting.
    quad_model = Model(quad_func)
    
    # Create a RealData object using our initiated data from above.
    data = RealData(x, y, sx=x_err, sy=y_err)
    
    # Set up ODR with the model and data.
    odr = ODR(data, quad_model, beta0=[0., 1.])
    
    # Run the regression.
    out = odr.run()
    
    # Use the in-built pprint method to give us results.
    out.pprint()
    '''Beta: [ 1.01781493  0.48498006]
    Beta Std Error: [ 0.00390799  0.03660941]
    Beta Covariance: [[ 0.00241322 -0.01420883]
     [-0.01420883  0.21177597]]
    Residual Variance: 0.00632861634898189
    Inverse Condition #: 0.4195196193536024
    Reason(s) for Halting:
      Sum of squares convergence'''
    
    x_fit = np.linspace(x[0], x[-1], 1000)
    y_fit = quad_func(out.beta, x_fit)
    
    plt.errorbar(x, y, xerr=x_err, yerr=y_err, linestyle='None', marker='x')
    plt.plot(x_fit, y_fit)
    
    plt.show()
    

    【讨论】:

    • 谢谢 这很有用 我不知道这个 ODR 方法。
    • 这有一个问题是只支持相对 sigmas,因为我有 tested so far。所以你不合时宜的错误并没有真正涵盖所有内容。
    【解决方案2】:

    您可以使用与最大特征值关联的协方差矩阵的特征向量进行线性拟合。

    import numpy as np
    import matplotlib.pyplot as plt
    
    x = np.arange(6, dtype=float)
    y = 3*x + 2
    x += np.random.randn(6)/10
    y += np.random.randn(6)/10
    
    xm = x.mean()
    ym = y.mean()
    
    C = np.cov([x-xm,y-ym])
    evals,evecs = np.linalg.eig(C)
    
    a = evecs[1,evals.argmax()]/evecs[0,evals.argmax()]
    b = ym-a*xm
    
    xx=np.linspace(0,5,100)
    yy=a*xx+b
    
    plt.plot(x,y,'ro',xx,yy)
    plt.show()
    

    【讨论】:

    • 谢谢,这很有趣
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-06
    • 1970-01-01
    • 1970-01-01
    • 2019-11-06
    • 2022-11-13
    • 1970-01-01
    相关资源
    最近更新 更多