python
linear regression
positive coefficients
machine learning
data science

Linear Regression with positive coefficients in Python

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

If your domain says every feature should contribute positively, unconstrained ordinary least squares may be the wrong model because it can learn negative coefficients. In Python, the simplest solution is to use a regression estimator that enforces nonnegative coefficients during fitting.

The important nuance is that "positive coefficients" usually refers to the feature weights, not necessarily the intercept. A fitted model may still have a negative intercept while all learned feature coefficients remain nonnegative.

Use LinearRegression(positive=True) in scikit-learn

Current scikit-learn supports nonnegative coefficients directly for dense input data:

python
1import numpy as np
2from sklearn.linear_model import LinearRegression
3
4X = np.array([
5    [1.0, 2.0],
6    [2.0, 1.0],
7    [3.0, 4.0],
8    [4.0, 3.0],
9])
10
11y = np.array([5.0, 4.5, 9.0, 8.5])
12
13model = LinearRegression(positive=True)
14model.fit(X, y)
15
16print("Coefficients:", model.coef_)
17print("Intercept:", model.intercept_)

This tells the estimator to solve a constrained optimization problem in which the feature coefficients cannot go below zero.

Why positivity changes the fit

Ordinary least squares minimizes squared error without any sign restrictions. Once you add a positivity constraint, the model can no longer choose the unconstrained best fit if that fit needs a negative coefficient.

That means:

  • the fit may be slightly worse than unconstrained OLS
  • the coefficients satisfy the domain rule
  • the model becomes easier to interpret when negative effects are impossible by design

This is common in settings where features represent additive quantities such as counts, dosages, or exposure levels.

Add regularization when needed

Sometimes you want nonnegative coefficients and regularization at the same time. In that case, use an estimator such as Lasso with positive=True.

python
1from sklearn.linear_model import Lasso
2
3model = Lasso(alpha=0.1, positive=True)
4model.fit(X, y)
5
6print(model.coef_)

This is useful when you want sparsity or more stable behavior under correlated features while still enforcing the sign constraint.

Regularization changes the objective, so do not expect the same coefficients you would get from plain constrained least squares.

Build the full pipeline normally

A positivity constraint does not replace normal model hygiene. You should still split data properly and optionally scale features.

python
1from sklearn.model_selection import train_test_split
2from sklearn.pipeline import make_pipeline
3from sklearn.preprocessing import StandardScaler
4from sklearn.linear_model import LinearRegression
5
6X_train, X_test, y_train, y_test = train_test_split(
7    X, y, test_size=0.25, random_state=42
8)
9
10model = make_pipeline(
11    StandardScaler(),
12    LinearRegression(positive=True)
13)
14
15model.fit(X_train, y_train)
16print(model.score(X_test, y_test))

Scaling is not mandatory for every linear regression workflow, but it often makes broader pipelines easier to compare and maintain.

Make sure the constraint is justified

A positivity constraint is strong prior knowledge. It should come from the problem, not from a desire to make the coefficients "look nicer."

If a feature can reasonably have a negative relationship with the target, forcing it positive distorts the model. Use this constraint only when negative weights are physically, economically, or logically implausible.

Also note that positive coefficients do not guarantee positive predictions. The intercept and the feature values still determine the final output.

Common Pitfalls

The most common mistake is assuming scikit-learn cannot do this directly. Modern LinearRegression does support positive=True for appropriate inputs.

Another issue is expecting the intercept to be constrained positive too. The positivity option applies to coefficients, not automatically to the intercept.

Developers also sometimes force positivity for interpretability even when the domain does not justify it. That can degrade model quality without a principled benefit.

Finally, check estimator and data-format support in your real workflow. A positivity flag on one model does not mean every estimator or input representation behaves the same way.

Summary

  • Use LinearRegression(positive=True) in scikit-learn when you want nonnegative linear coefficients.
  • The positivity constraint applies to feature weights, not automatically to the intercept.
  • Constrained regression may fit slightly worse than unconstrained OLS, but it respects domain rules.
  • Use models such as Lasso with positive=True when you also want regularization.
  • Apply the constraint only when nonnegative effects are truly justified by the problem.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.