Linear Regression with positive coefficients in Python

Viewed 9979

I'm trying to find a way to fit a linear regression model with positive coefficients.

The only way I found is sklearn's Lasso model, which has a positive=True argument, but doesn't recommend using with alpha=0 (means no other constraints on the weights).

Do you know of another model/method/way to do it?

3 Answers

Many functions can keep linear regression model with positive coefficients.

  1. scipy.optimize.nnls can solve above problem.
  2. scikit-learn LinearRegression can set the parameter positive=True to solve this. And, the sklearn also uses the scipy.optimize.nnls. Interestingly, you can learn how to write multiple targets outputs in source code.
  3. Additionally, if you want to solve linear least squares with bounds on the variables. You can see lsq_linear .

As of version 0.24, scikit-learn LinearRegression includes a similar argument positive, which does exactly that; from the docs:

positive : bool, default=False

When set to True, forces the coefficients to be positive. This option is only supported for dense arrays.

New in version 0.24.

Related