Skip to main content

constrained-linear-regression

PyPI version

This is a Python implementation of constrained linear regression in scikit-learn style. The current version supports upper and lower bound for each slope coefficient.

It was developed after this question https://stackoverflow.com/questions/50410037

Installation: pip install constrained-linear-regression

You can use this model, for example, if you want all coefficients to be non-negative:

from constrained_linear_regression import ConstrainedLinearRegression
from sklearn.datasets import load_boston
from sklearn.linear_model import LinearRegression
X, y = load_boston(return_X_y=True)
model = ConstrainedLinearRegression(nonnegative=True)
model.fit(X, y)
print(model.intercept_)
print(model.coef_)

The output will be like

-36.99292986145538
[0.         0.05286515 0.         4.12512386 0.         8.04017956
 0.         0.         0.         0.         0.         0.02273805
 0.        ]

You can also impose arbitrary bounds for any coefficients you choose

model = ConstrainedLinearRegression()
min_coef = np.repeat(-np.inf, X.shape[1])
min_coef[0] = 0
min_coef[4] = -1
max_coef = np.repeat(4, X.shape[1])
max_coef[3] = 2
model.fit(X, y, max_coef=max_coef, min_coef=min_coef)
print(model.intercept_)
print(model.coef_)

The output will be

24.060175576410515
[ 0.          0.04504673 -0.0354073   2.         -1.          4.
 -0.01343263 -1.17231216  0.2183103  -0.01375266 -0.7747823   0.01122374
 -0.56678676]

You can also set coefficients lasso and ridge if you want to apply the corresponding penalties. For lasso, however, the output might not be exactly equal to the result of sklearn.linear_model.Lasso due to the difference in the optimization algorithm.

Metadata

Release files for constrained-linear-regression 0.0.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for constrained-linear-regression 0.0.5
File Size Uploaded
constrained_linear_regression-0.0.5.tar.gz 6.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for constrained-linear-regression 0.0.5
File Interpreter ABI Platform
constrained_linear_regression-0.0.5-py3-none-any.whl Python 3 none any Details

Total release size: 14.4 kB

Release files / constrained_linear_regression-0.0.5.tar.gz

Download URL constrained_linear_regression-0.0.5.tar.gz
Size 6.5 kB
Tags Source
SHA-256 checksum
How to use checksums
f7a8721d4cbe5b5a5a450c01c728bb25ae9b7efde39a6ff2ec4b36c5af432e85
BLAKE2b-256 checksum
How to use checksums
f60b12aa3fdadf40e1c9f1bc3a6c53ceab193c7ba01c51a7b995d2aca7fd8a78
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.2 importlib_metadata/7.1.0 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.6

Release files / constrained_linear_regression-0.0.5-py3-none-any.whl

Download URL constrained_linear_regression-0.0.5-py3-none-any.whl
Size 7.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e628b340a920beb50294f6c6f50ff993e602b259f3c7eeb9b15889f23c10bfaa
BLAKE2b-256 checksum
How to use checksums
5d829d7f62455227d7631e267ceead8d99ea08a1216684446c1a2051a14def1a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.2 importlib_metadata/7.1.0 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.6

Release history Release notifications | RSS feed

This release

0.0.5 This release

2 release files

0.0.4

2 release files

0.0.3

1 release file

0.0.2

1 release file

0.0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page