sklearn
LinearRegression
coef_
machine learning
data interpretation

What exactly is coef_ from sklearn LinearRegression? and how to interpret a formula from it

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

Understanding coef_ in sklearn's LinearRegression

When using sklearn to perform linear regression, one of the fundamental outputs of the model is the coef_ attribute. This article will explore what coef_ represents, how it's calculated, and how to interpret it within the context of a linear regression model.

What is coef_?

In the LinearRegression class provided by sklearn, coef_ is an attribute that stores the coefficients of the linear model. These coefficients are the values that linearly combine with the independent variables (features) to predict the dependent variable (target). In the context of linear regression, these coefficients represent the slope parameters for each feature in the dataset.

The linear regression model can be expressed as:

y=w_1⋅x_1+w_2⋅x_2+…+w_n⋅x_n+by = w\_1 \cdot x\_1 + w\_2 \cdot x\_2 + \ldots + w\_n \cdot x\_n + b

Here: • yy is the predicted output. • x1,x2,…,xnx_1, x_2, \ldots, x_n are input features. • w1,w2,…,wnw_1, w_2, \ldots, w_n are the coefficients (coef_). • bb is the intercept term, stored in intercept_.

The coef_ attribute does not include the intercept. It holds the slope coefficients, which indicate the change in the predicted variable yy for a one-unit change in the respective feature, keeping all other features constant.

Calculating coef_

The coefficients in a linear regression model are determined using methods such as Ordinary Least Squares (OLS), which minimize the sum of squared residuals (the differences between observed and predicted values).

The formula to estimate the coefficients in matrix notation is:

w=(XTX)−1XTy\mathbf{w} = (\mathbf{X}^T \mathbf{X})^{-1} \mathbf{X}^T \mathbf{y}

Where: • w\mathbf{w} is the vector of coefficients. • X\mathbf{X} is the matrix of input features. • y\mathbf{y} is the vector of outputs.

The sklearn library handles this computation internally when you call the fit method on your LinearRegression object.

Interpreting coef_

The coef_ values guide understanding the relationship between each independent variable and the dependent variable:

• Magnitude: A larger magnitude of a coefficient indicates that the associated feature has a more significant impact on the predicted variable. Small magnitudes suggest lesser influence.

• Sign: The sign of the coefficient (positive or negative) indicates the direction of the relationship: • Positive: As the feature increases, the predicted variable increases. • Negative: As the feature increases, the predicted variable decreases.

Example

Let's see an example for a clearer understanding:

• Here, both features have a coefficient of 1, indicating that each feature contributes equally to the prediction, and for every unit increase in either feature, the predicted yy increases by 1. • The intercept is 3, which represents the baseline value of yy when all predictors are zero. • Regularization: In models like Ridge or Lasso, regularization terms are added to the loss function, which can shrink the coefficients (coef_) to reduce model complexity and prevent overfitting. • Feature Scaling: It is often recommended to scale features before applying linear regression, especially when using regularization, to ensure that all features contribute equally to the distance calculations. • Multiple Regression: In cases with multiple features, careful attention should be paid to the size and sign of each coefficient to fully understand the model's behavior.


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
Basis Lab
No course covers this one yet

Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.

Build my 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