python - 当我翻转 x 和 y 输入时,scipy 的正交距离回归(ODR)不收敛
问题描述
如果我翻转 x 输入和 y 输入(通过同一行,我的意思是我采用 ODR 估计的系数,设置y=0
并求解x
(或x=0
并在翻转时求解y
):y = mx + b
x = my + b
-当我翻转 x 和 y 输入时 x 的值不同)。
我发现如果我玩,maxit
我sstol
可以接近收敛,但这些值不一定适合每个x
/y
对。
我错过了一步,还是误解了这一点?从下面的两个图中可以看出,当我翻转 x 和 y 输入时,我得到不同的线(您可以通过每个相交的点数直观地看出它们是不同的)。
import scipy.odr as odr # orthogonal distance regression
import scipy
import seaborn as sns
from scipy.stats import linregress
import matplotlib.pyplot as plt
import pandas as pd
def linear_fit(coefs, x):
'''Linear function y = m*x + b'''
m,b = coefs
return m*x + b
def perform_odr(x, y):
OLS_m,OLS_b = linregress(x, y)[:2] # estimate coefficients from OLS
linmod = scipy.odr.Model(linear_fit)
mydata = scipy.odr.Data(x, y)
myodr = scipy.odr.ODR(mydata, linmod, beta0=[OLS_m, OLS_b]) #, maxit=75, sstol=0.000000001)
return myodr.run()
def plot_odr(x, y, ax=None):
"""Plot Orthogonal Distance Regression line of best fit."""
if ax is None:
fig, ax = plt.subplots()
fitted = perform_odr(x, y)
fitted.pprint()
print('y = %sx + %s' % tuple(round(coef, 5) for coef in fitted.beta))
# extend line to boundaries of figure by adding points corresponding to xmin/xmax
xlims = plt.xlim()
ylims = plt.ylim()
xnew = pd.Series(xlims[0]).append(x)
xnew = xnew.append(pd.Series(xlims[1]))
# plot
plt.plot(xnew, linear_fit(fitted.beta, xnew), 'k-', linewidth=1.5, label='ODR regression', zorder=1)
plt.xlim(xlims)
plt.ylim(ylims)
return ax, fitted
x = [25.88, 33.77, 26.55, 36.17, 36.57, 32.43, 38.65, 37.2, 33.57, 32.06, 25.48, 33.12, 26.24, 38.0, 37.25, 23.4, 34.28, 30.8, 34.22, 36.76, 30.46, 33.61, 35.32, 24.99, 39.23, 29.22, 32.54, 29.28, 31.31, 33.46, 28.06, 39.59, 28.98, 28.12, 27.21, 36.54, 25.35]
y = [0.05959377810359001, 0.06366157531738281, 0.03445526957511902, 0.021249201148748398, 0.013540455140173435, 0.033232174813747406, 0.02101573720574379, 0.021147532388567924, 0.014944841153919697, 0.02261320687830448, 0.04238538816571236, 0.02475816197693348, 0.04893920198082924, 0.01641816273331642, 0.02175772748887539, 0.029113013297319412, 0.019221415743231773, 0.019603392109274864, 0.01673588529229164, 0.020033005625009537, 0.02505655400454998, 0.03193040192127228, 0.02318655699491501, 0.032157305628061295, 0.017262479290366173, 0.02122090384364128, 0.04239807650446892, 0.028340162709355354, 0.02409830316901207, 0.029440563172101974, 0.026672156527638435, 0.010255633853375912, 0.02117013931274414, 0.027912333607673645, 0.020978346467018127, 0.015505819581449032, 0.02792999893426895]
fig, ax = plt.subplots()
plt.scatter(x,y)
ax,fit = plot_odr(x, y, ax)
fig, ax = plt.subplots()
plt.scatter(y,x)
ax,fit = plot_odr(y, x, ax)
解决方案
推荐阅读
- python - 在 Selenium、python 中使用 XPath 时出错
- android - 在 RecyclerView 中显示 MediaStream 对象后,是否有正确的方法来处理/释放它?
- c# - SQLHelper.DB 未将参数传递给存储过程
- git - Git - “丢失”提交
- npm - Azure DevOps 构建突然失败,问题看起来是自动更新的 NPM 版本
- java - 使用多个类时构造函数未定义
- css - 使用 flex 在 React 中的全高
- python-3.x - selenium.common.exceptions.ElementNotInteractableException:消息:尝试使用 Selenium 和 Python 登录时出现元素不可交互错误
- kubernetes - 在 AKS 中添加自定义 DNS
- php - 用 prev 值填充空数组点