python中的线性拟合在x和y坐标中均具有不确定性 [英] Linear fitting in python with uncertainty in both x and y coordinates
问题描述
您好,我想问一下我的python用户,他们如何进行线性拟合。
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:
如果要基于最小二乘法执行线性拟合,则有很多选择。例如,您可以在numpy和scipy中找到类。我自己选择了linfit提出的方案(遵循IDL中linfit函数的设计):
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
此方法假设您在y轴坐标中引入了西格玛以适合您的数据。
This method assumes you are introducing the sigmas in your y-axis coordinates to fit your data.
但是,如果您已量化了x和y轴上的不确定性,那么选择就不多了。 (在主要的python科学库中没有等效的IDL Fitexy)。到目前为止,我仅找到 kmpfit库来执行此任务。幸运的是,它有一个非常完整的网站,描述了其所有功能:
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.
推荐答案
Scipy中的
正交距离回归允许您进行非线性使用 x
和 y
中的错误进行拟合。
Orthogonal distance regression in Scipy allows you to do non-linear fitting using errors in both x
and y
.
下面显示的是基于scipy页面上给出的示例的简单示例。
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[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()
这篇关于python中的线性拟合在x和y坐标中均具有不确定性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!