https://github.com/tsitsimis/constrainedlr
Drop-in replacement of sklearn's Linear Regression with coefficients constraints
https://github.com/tsitsimis/constrainedlr
constraints elasticnet-regression lasso-regression linear-regression machine-learning ml python-package quadratic-programming ridge-regression sklearn-compatible
Last synced: 5 months ago
JSON representation
Drop-in replacement of sklearn's Linear Regression with coefficients constraints
- Host: GitHub
- URL: https://github.com/tsitsimis/constrainedlr
- Owner: tsitsimis
- License: mit
- Created: 2023-05-13T17:54:49.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2024-05-05T12:54:58.000Z (about 2 years ago)
- Last Synced: 2026-01-06T15:10:13.500Z (5 months ago)
- Topics: constraints, elasticnet-regression, lasso-regression, linear-regression, machine-learning, ml, python-package, quadratic-programming, ridge-regression, sklearn-compatible
- Language: Python
- Homepage: https://tsitsimis.github.io/constrainedlr/
- Size: 637 KB
- Stars: 7
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Constrained Linear Regression
constrainedlr is a drop-in replacement for `scikit-learn`'s `linear_model.LinearRegression` with the extended capability to apply constraints on the model's coefficients, such as signs and lower/upper bounds.
## Installation
```bash
pip install constrainedlr
```
## Example Usage
### Coefficients sign constraints
```python
from constrainedlr import ConstrainedLinearRegression
model = ConstrainedLinearRegression()
model.fit(
X_train,
y_train,
coefficients_sign_constraints={0: "positive", 2: "negative"},
intercept_sign_constraint="positive",
)
y_pred = model.predict(X_test)
print(model.coef_, model.intercept_)
```
### Coefficients range constraints
```python
from constrainedlr import ConstrainedLinearRegression
model = ConstrainedLinearRegression()
model.fit(
X_train,
y_train,
coefficients_range_constraints={
0: {"lower": 2}, # 1st coefficient must be 2 or higher
2: {"upper": 10}, # 3rd coefficient must be smaller than 10
3: {"lower": 0.1, "upper": 0.5}, # 4th coefficient must be between 0.1 and 0.5
},
)
y_pred = model.predict(X_test)
print(model.coef_)
```
See more in the [documentation](https://tsitsimis.github.io/constrainedlr/)
### Licence
MIT