python中的倒标准最小二乘回归ISR

问题描述

嗨,我只是想执行逆最小二乘回归

Y -> m *x + c

python 中是否有可用的包或函数

解决方法

我假设 inverted least squares regression 是指最小化最佳拟合线与数据之间的水平距离而不是垂直距离的回归。如果是这种情况,您可以简单地在 x 上运行 y 的回归,然后设置 m* = 1/ma*=-a/m。举个例子:

import matplotlib.pyplot as plt
import pandas as pd 
from sklearn.linear_model import LinearRegression

# DATA
x = np.linspace(0,1,100).reshape((-1,1))
y = 0.5*x + np.random.normal(0,0.1,size=x.shape)

# INVERTED LINEAR REGRESSION
model = LinearRegression(fit_intercept=True)
model.fit(y,x)
m = 1/model.coef_[0][0]
a = -model.intercept_/model.coef_[0][0]

# PLOT
plt.scatter(x,y,color='r',s=12,alpha=0.5)
plt.plot(x,m*x + a)
plt.xlabel('x')
plt.ylabel('y')

enter image description here

,

您可以尝试 statsmodels 它有一些回归算法,如果它没有您要找的算​​法,请检查其他算法的实现并用它来实现您的

链接:https://www.statsmodels.org/stable/api.html