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},
2: {"upper": 10},
3: {"lower": 0.1, "upper": 0.5},
},
)
y_pred = model.predict(X_test)
print(model.coef_)
See more in the documentation
Licence
MIT