Skip to main content

Constrained linear regression in scikit-learn style

Project description

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.

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

constrained_linear_regression-0.0.3.tar.gz (3.8 kB view details)

Uploaded Source

File details

Details for the file constrained_linear_regression-0.0.3.tar.gz.

File metadata

  • Download URL: constrained_linear_regression-0.0.3.tar.gz
  • Upload date:
  • Size: 3.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/3.10.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.9.6

File hashes

Hashes for constrained_linear_regression-0.0.3.tar.gz
Algorithm Hash digest
SHA256 b7cda2e5f98e5945385043b298caf1fefbbee631d4bad588e3cccfe3af8770f6
MD5 f139e700ca04990d2e93f769e8746753
BLAKE2b-256 81e26f4356ae8753ac084c5a4b57b1656307f9093e2a1a03e626abd4c23b3160

See more details on using hashes here.

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