Scipy 教程-SciPy ODR
ODR是正交距离回归(Orthogonal Distance Regression)的缩写形式。它用于回归研究中。基本的线性回归用于通过在图中绘制最佳拟合线来估计两个变量y和x之间的关系。然后,问题出现了,为什么需要正交距离回归(ODR)?有时,在自变量(x)中发生测量误差,而不是在因变量(y)中。
正交距离回归(ODR)是一种用于计算垂直于线而不是垂直的误差的方法。
正交距离回归提供了ODRPACK来执行带有非线性函数的ODR。它基本上是一个FORTRAN-77库。它可以进行显式或隐式的ODR拟合。它也可以用于解决普通最小二乘问题(OLS)。
👉点击领取:最全Python资料合集
对单变量回归的scipy.odr实现
import numpy as npimport matplotlib.pyplot as pltfrom 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 = zreturn 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
热门推荐