Skip to main content

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 constrainedlr

Example Usage

from constrainedlr import ConstrainedLinearRegression
from sklearn.metrics import mean_squared_error

model = ConstrainedLinearRegression()
model.fit(X_train, y_train, features_sign_constraints={6: 1, 7: -1})  # 6th and 7th feature (s3 and s4)

y_pred = model.predict(X_test)
print(mean_squared_error(y_test, y_pred))
print(model.coef_)

See full example in the notebook

Licence

MIT

Project details


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.0.9.tar.gz (5.0 kB view hashes)

Uploaded Source

Built Distribution

constrainedlr-0.0.9-py3-none-any.whl (4.5 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page