跳转到主要内容

scikit-learn风格的约束线性回归

项目描述

约束线性回归

PyPI version

这是scikit-learn风格约束线性回归的Python实现。当前版本支持每个斜率系数的上限和下限。

此项目是在这个问题之后开发的 https://stackoverflow.com/questions/50410037

安装: pip install constrained-linear-regression

如果您希望所有系数均为非负数,可以使用此模型

from constrained_linear_regression import ConstrainedLinearRegression
from sklearn.datasets import load_boston
from sklearn.linear_model import LinearRegression
X, y = load_boston(return_X_y=True)
model = ConstrainedLinearRegression(nonnegative=True)
model.fit(X, y)
print(model.intercept_)
print(model.coef_)

输出将如下所示

-36.99292986145538
[0.         0.05286515 0.         4.12512386 0.         8.04017956
 0.         0.         0.         0.         0.         0.02273805
 0.        ]

您还可以为任何选定的系数施加任意界限

model = ConstrainedLinearRegression()
min_coef = np.repeat(-np.inf, X.shape[1])
min_coef[0] = 0
min_coef[4] = -1
max_coef = np.repeat(4, X.shape[1])
max_coef[3] = 2
model.fit(X, y, max_coef=max_coef, min_coef=min_coef)
print(model.intercept_)
print(model.coef_)

输出将如下所示

24.060175576410515
[ 0.          0.04504673 -0.0354073   2.         -1.          4.
 -0.01343263 -1.17231216  0.2183103  -0.01375266 -0.7747823   0.01122374
 -0.56678676]

如果您想应用相应的惩罚,可以设置系数 lassoridge。但是,由于优化算法的差异,对于 lasso,输出可能不会与 sklearn.linear_model.Lasso 的结果完全相同。

项目详情


下载文件

下载适用于您平台的应用程序。如果您不确定选择哪个,请了解更多关于 安装包 的信息。

源分布

constrained_linear_regression-0.0.5.tar.gz (6.5 kB 查看哈希值)

上传时间: 源代码

构建版本

constrained_linear_regression-0.0.5-py3-none-any.whl (7.8 kB 查看哈希值)

上传时间: Python 3

由以下支持