Constrained Linear Regression with sklearn-compatible API
Project description
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
pip install constrainedlr
Example Usage
Coefficients sign constraints
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
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
Licence
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
constrainedlr-0.1.5.tar.gz
(9.0 kB
view hashes)
Built Distribution
Close
Hashes for constrainedlr-0.1.5-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c0ff6e118dca5d3ae8f57ff1205a723dfbbddd18086787f53f9899ef345e537e |
|
MD5 | 1bed9f53cbbee5830115dde5cf84d2a9 |
|
BLAKE2b-256 | e999c33b9ac51d6f983f21410a6b416ac80112410e3c4ab666e89d5605ec0d46 |