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.
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:
Here:
• is the predicted output.
• are input features.
• are the coefficients (coef_).
• 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 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:
Where: • is the vector of coefficients. • is the matrix of input features. • 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 increases by 1.
• The intercept is 3, which represents the baseline value of 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
- What exactly is Keras's CategoricalCrossEntropy doing?
- What exactly is n_iter hyperparameter in randomizedSearch?
- What FFT descriptors should be used as feature to implement classification or clustering algorithm?
- What happens if loss function is multiplied by a constant?
- What happens when using higher version tf serving to serve a model from lower version tensorflow?
- What happens when we apply .fit method to a kNN model in Scikit-learn if kNN has no training phase?
- What happens when we call cpu.data.numpy on a PyTorch tensor?
- What has to be inside tf.distribute.Strategy.scope?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo 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 courseTrack 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.