sklearn.linear\u model.LinearRegression如何在多重共线性数据集中找到解决方案

2024-09-29 23:17:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试使用扩展的波士顿数据集训练线性回归模型:

from mglearn.datasets import load_extended_boston
X, y = load_extended_boston()

from sklearn.linear_model import LinearRegression
linearReg = LinearRegression()
linearReg.fit(X, y)

对于X中的每个特征,LinearRegression给出了相应的系数

但是,X^TX不是可逆的。因此,当我在R中拟合这个线性模型时,程序将自动删除一个特性以找到解决方案

sklearn.linear\u model.LinearRegression是否使用了一些不同的算法


Tags: 数据from模型importextendedmodelload线性
1条回答
网友
1楼 · 发布于 2024-09-29 23:17:30

您可以尝试Ridge regression,这是LinearRegression加上L2正则化。L2正则化意味着它不仅最小化到目标的平方距离,而且还试图保持系数较小。这使得它对于共线数据集更加稳定

从文档中:

Ridge regression addresses some of the problems of Ordinary Least Squares by imposing a penalty on the size of the coefficients. The ridge coefficients minimize a penalized residual sum of squares:

The complexity parameter controls the amount of shrinkage: the larger the value of , the greater the amount of shrinkage and thus the coefficients become more robust to collinearity.

https://scikit-learn.org/stable/modules/linear_model.html#ridge-regressionhttps://scikit-learn.org/stable/modules/generated/sklearn.linear_model.Ridge.html

我不同意你的看法

Antoine Dubuis: LinearRegression is using least square and therefore requires having an invertible X.T @ X.

也许它不是那样的意思,但从技术上讲,你必须有一个可逆的协方差矩阵来做linear regression/least squares。例如,您可以使用QR分解来完成。我猜他的意思是,这对于^{中的实现是必要的。但我认为做一些正规化是明智的。这样想:如果你的问题是你的问题有很多解决方案(这就是共线的意思),你想怎么解决?你想选择任何一种解决方案,还是想提出一个标准,说明哪种解决方案是最好的,例如,最常见的

相关问题 更多 >

    热门问题