Skip to content

Commit

Permalink
Update README with examples
Browse files Browse the repository at this point in the history
  • Loading branch information
tsitsimis committed Jun 25, 2023
1 parent 1841ae7 commit 24960f6
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 15 deletions.
49 changes: 35 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,55 @@
<img src="https://img.shields.io/pypi/pyversions/constrainedlr.svg?color=%2334D058" alt="Supported Python versions">
</a>

constrainedlr 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.
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.

### 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
## Installation
```bash
pip install constrainedlr
```

### Example Usage
## Example Usage

### Coefficients sign constraints
```python
from constrainedlr import ConstrainedLinearRegression
from sklearn.metrics import mean_squared_error

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

model.fit(
X_train,
y_train,
coefficients_sign_constraints={0: "positive", 2: "negative"},
intercept_sign_constraint="positive",
)

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

print(model.coef_, model.intercept_)
```

### Coefficients range constraints
```python
from constrainedlr import ConstrainedLinearRegression

model = ConstrainedLinearRegression()

model.fit(
X_train,
y_train,
coefficients_sign_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 full example in the [notebook](./notebooks/demo.ipynb)
See more in the [documentation](https://tsitsimis.github.io/constrainedlr/)


### Licence
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = 'constrainedlr'
version = '0.1.3'
version = '0.1.4'
description = 'Constrained Linear Regression with sklearn-compatible API'
readme = 'README.md'
authors = [
Expand Down

0 comments on commit 24960f6

Please sign in to comment.