Python技术迷

Scipy 教程-SciPy ODR

整理:python架构师

ODR是正交距离回归(Orthogonal Distance Regression)的缩写形式。它用于回归研究中。基本的线性回归用于通过在图中绘制最佳拟合线来估计两个变量y和x之间的关系。然后,问题出现了,为什么需要正交距离回归(ODR)?有时,在自变量(x)中发生测量误差,而不是在因变量(y)中。

标准线性回归集中于根据X值预测Y值,因此有用的做法是计算Y值中的误差(如下图中的黑色虚线所示)。然而,更好的做法是考虑X和Y中的误差(如下图中的红色虚线所示)。

Image

正交距离回归(ODR)是一种用于计算垂直于线而不是垂直的误差的方法。

正交距离回归提供了ODRPACK来执行带有非线性函数的ODR。它基本上是一个FORTRAN-77库。它可以进行显式或隐式的ODR拟合。它也可以用于解决普通最小二乘问题(OLS)。

专属福利 

👉点击领取:最全Python资料合集

对单变量回归的scipy.odr实现

单变量回归可以定义为确定一个自变量和一个因变量之间的关系。考虑以下示例:
import numpy as np  import matplotlib.pyplot as plt  from scipy.odr import *  import random  # Initiate some data, and generate the random number using random.random().  a = np.array([0, 1, 2, 3, 4, 5, 6, 7])  b = np.array([i**2 + random.random() for i in x])  # Define a quadratic function ( in this case) to fit the data with.  def linear_func(z, a):     m, p = z     return m*a + p  # Creating a model for fitting.  linear_model_fit = Model(linear_func)  # Creating a RealData object using our initiated data from above.  data = RealData(x, y)  # Fixed up ODR with the model and data.  odr = ODR(data, linear_model_fit, beta0=[0., 1.])  # Here we run the regression using the run().  out = odr.run()  # Use the in-built pprint method to give us results.  out.pprint()
输出:
Beta: [ 7.62787497 -8.53630181]Beta Std Error: [0.89306061 3.69444539]Beta Covariance: [[ 1.52116591 -5.32408057] [-5.32408057 26.0323407 ]]Residual Variance: 0.5243065494144553Inverse Condition #: 0.18510252155770376Reason(s) for Halting:  Sum of squares convergence
Image
 1
Image
热门推荐
Image