Constrained Linear Regression with sklearn-compatible API
Project description
Constrained Linear Regression
constrained-lr is a drop-in replacement for scikit-learn
's linear_model.LinearRegression
with the additional flexibility to define more complex (but linear) constraints on the model's coefficients.
Use-cases
SHAP
The Kernel SHAP algorithm includes the training of a constrainted linear regression model where the sum of its coefficients is equal to the model's prediction
Marketing Mix Modeling
In Marketing Mix Modeling (MMM), the attribution of sales to various marketing channels can be informed by business sense or prior knowledge, by enforcing the contribution of channel variables to be positive or negative.
Installation
pip install constrained-lr
Example Usage
from constrained_lr import ConstrainedLinearRegression
from sklearn.metrics import mean_squared_error
model = ConstrainedLinearRegression()
model.fit(X_train, y_train, features_sign_constraints={"s3": 1, "s4": -1})
y_pred = model.predict(X_test)
print(mean_squared_error(y_test, y_pred))
print(dict(zip(X.columns, model.coef_)))
See full example in the notebook
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
Built Distribution
Hashes for constrainedlr-0.0.8-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 74bd5b6107a097f52511e6c77da0cd85da9a6e938d9771c8150f300e9ea3e4ea |
|
MD5 | 961c83ce25dcaaf71c968736636d790a |
|
BLAKE2b-256 | 39202d7d817058d1cbf434ecf7ffd39c3e04f34699b19da52a1a1e4cb42d16c1 |