# 在Python中线性拟合，在x和y坐标中具有不确定性[关闭] -- python 领域 和 curve-fitting 领域 和 linear-equation 领域 相关 的问题

## Linear fitting in python with uncertainty in both x and y coordinates [closed]

9 ### 问题

closed 1年前

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

https：//github.com/josephmeiring/kmpfit http：//www.astro.rug.nl/software/kapteyn/kmpfittorial。 html＃

Hi I would like to ask my fellow python users how they perform their linear fitting.

I have been searching for the last two weeks on methods/libraries to perform this task and I would like to share my experience:

If you want to perform a linear fitting based on the least-squares method you have many options. For example you can find classes in both numpy and scipy. Myself I have opted by the one presented by linfit (which follows the design of the linfit function in IDL):

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

This method assumes you are introducing the sigmas in your y-axis coordinates to fit your data.

However, if you have quantified the uncertainty in both the x and y axes there aren't so many options. (There is not IDL "Fitexy" equivalent in the main python scientific libraries). So far I have found only the "kmpfit" library to perform this task. Fortunately, it has a very complete website describing all its functionality:

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

If anyone knows additional approaches I would love to know them as well.

In any case I hope this helps.

## 回答列表

21  ` ` 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, 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()  ` ` Orthogonal distance regression in Scipy allows you to do non-linear fitting using errors in both `x` and `y`.

Shown below is a simple example based on the example given on the scipy page. It attempts to fit a quadratic function to some randomised data.

``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, 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() `` 7 ` ` \$time = date("Y-m-d", \$timestamp/1000); echo \$time; // output: 2015-03-31 0 ` ` You can use eigenvector of covariance matrix associated with the largest eigenvalue to perform linear fitting.

``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() `` ## 相关问题

0  使用Pandas将对象（数值）转换为DateTime  ( Convert object numeric value to datetime using pandas )

0  使用命令buildozer android部署运行，但每次我收到以下错误  ( Using command buildozer android deploy run but every time i am getting the foll )

0  使用多处理嵌套并行进程  ( Nesting parallel processes using multiprocessing )

0  KIVY - 如何将滚动视图拖放到另一个布局的项目？  ( Kivy how to drag and drop items from scrollview to another layout )

1  根据条件平等查找DataFrame中的匹配对或链条  ( Find matching pairs or chains of persons in dataframe based on equality of cond )

5  Python中的群集计算快速JSON序列化（和与泡块的比较）？  ( Fast json serialization and comparison with pickle for cluster computing in py )

0  Boketh Plot隐藏HTML网站并仅在Jupyter中显示输出？  ( Boketh plot hide html site and show output only in jupyter )

1  为什么PIP显示包未安装到当前公共区域环境的网站包装？  ( Why does pip show packages which were not installed to the site packages of the )

0  Python - 使用pyinstaller创建的exe文件在提供错误时继续崩溃  ( Python created exe file using pyinstaller keeps on crashing with error provide )

1  如何用两组重新表达URL？  ( How to regex url with two groups )

0  int函数及其参数  ( The int function and its parameters )

0  在Django管理员内联的Autofill FookeKey  ( Autofill foreignkey in django admin inline )

10  在熊猫列表中切割多个列，按名称列表  ( Slicing multiple ranges of columns in pandas by list of names )

1  为Mac创建Python应用程序  ( Create python app for mac )