Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 65

Interpreting Regression Coefficients

Recommended resources

Hello! Welcome back to our series on regression.

In our last lesson, you built a complete workflow to train and evaluate regression models. You learned how to split data, train a model with .fit(), make predictions with .predict(), and measure performance using metrics like R-squared and RMSE. We can now answer the question: how well does our model predict?

Today, we move from "how well" to "what." We will focus on interpreting the model's coefficients to understand the specific relationships it has learned between the features and the target. This is a crucial skill for translating a model's output into actionable insights. By the end of this lesson, you will be able to explain what your regression model has learned about the data.

The Linear Regression Equation and its Coefficients

Let's quickly revisit the equation for multiple linear regression:

When you train a model in scikit-learn, it solves for the optimal values of these parameters:

  • is the intercept, which you can access via model.intercept_.
  • are the coefficients for each feature , which you can access as an array via model.coef_.

The following video provides a great refresher on how these mathematical terms map directly to the attributes of a trained scikit-learn model.

Machine Learning Tutorial Python - 3: Linear Regression Multiple Variables

Watch this segment from the 'Linear Regression Multiple Variables' video by codebasics. It clearly connects the mathematical equation to the Python code and shows where to find the learned coefficients and intercept after training.

Watch from 02:04 to 08:17. Pay attention to how the presenter defines the equation and then, after fitting the model, prints out the reg.coef_ and reg.intercept_ values, identifying them as the 'm' values and 'b' in the equation.

Now that we know where to find these coefficients, let's learn how to interpret what they mean.


Interpreting Coefficients of Numerical Features

The interpretation of a coefficient for a numerical feature is the most straightforward.

A coefficient represents the expected change in the target variable for a one-unit increase in the corresponding feature, assuming all other features are held constant.

This "holding all other features constant" part is crucial. We are trying to isolate the impact of a single feature.

Interpreting Multiple Regression Coefficients
This image provides a perfect example. For the feature X2 (attic insulation), the coefficient is -14.831. This means that for every one-inch increase in attic insulation, the monthly heating cost is expected to decrease by $14.83, assuming the outside temperature and furnace age do not change.

To see this in action with code, we'll use an excellent article from Machine Learning Mastery that uses the Ames Housing dataset to predict SalePrice.

Interpreting Coefficients in Linear Regression Models

This first section of the article, 'Interpreting Coefficients in Linear Regression Models', demonstrates how to find and interpret the coefficient for a single numerical feature, 'GrLivArea' (living area).

Read the section 'Interpreting Coefficients in Linear Models with a Single Numerical Feature'. Focus on the final interpretation: what does the coefficient value of 110.52 mean in the context of house prices and square footage?

As the article explains, a coefficient of 110.52 for GrLivArea means that, on average, for each additional square foot of living area, the house's sale price is predicted to increase by $110.52, all else being equal.

The book "Interpretable Machine Learning" provides a useful template for this interpretation.

6 Linear Regression – Interpretable Machine Learning

For a structured way to think about interpretation, let's consult the 'Interpretable Machine Learning' e-book by Christoph Molnar.

Read the subsection 'Interpretation'. Pay special attention to the text template provided for interpreting numerical features.


Interpreting Coefficients of Categorical Features

Interpreting categorical features is a bit more nuanced because they are typically transformed using one-hot encoding first (as you learned in Module 5).

When you one-hot encode a feature, you create new binary (0/1) columns for each category. To avoid multicollinearity, one category is usually dropped and becomes the reference category.

  • The model's intercept () now represents the average prediction for this reference category (when all numerical features are 0).
  • The coefficient for another category represents the difference in the predicted target between that category and the reference category.

Let's return to the Machine Learning Mastery article, which has a fantastic explanation of this process.

Interpreting Coefficients in Linear Regression Models

This next section of the article walks through interpreting coefficients for the 'Neighborhood' feature. It shows how to select a reference category ('MeadowV') and how to interpret the other neighborhood coefficients relative to it.

Read the section 'Interpreting Coefficients in Linear Models with a Single Categorical Feature'. Focus on how the Y-intercept relates to the reference category's average price, and what a specific neighborhood's 'Average Coefficient' (e.g., BrkSide's 29235) actually means.

From the article, the model's intercept is about $96,827, which is the average price in the reference neighborhood, "MeadowV". The coefficient for "BrkSide" is $29,235. This means a house in "BrkSide" is predicted to cost, on average, $29,235 more than a similar house in "MeadowV".

Test your understanding!

You are building a model to predict the salary of data scientists. Your features include Years of Experience (numerical) and Education Level (categorical: 'Bachelors', 'Masters', 'PhD'). You use 'Bachelors' as the reference category for one-hot encoding.

After training, your model has the following attributes:

  • intercept_: 60,000
  • coef_ for Years of Experience: 8,000
  • coef_ for Education Level_Masters: 15,000
  • coef_ for Education Level_PhD: 35,000

How would you interpret the coefficient for Education Level_Masters?

Show answer

The coefficient for Education Level_Masters is 15,000. This means that, holding all other features (like years of experience) constant, a data scientist with a Master's degree is predicted to earn $15,000 more on average than a data scientist with a Bachelor's degree.


Important Considerations and Caveats

While coefficient interpretation is powerful, there are two critical points to remember.

1. Correlation and Context Matter

A feature's coefficient can change depending on what other features are in the model. This happens because features are often correlated. For example, the value of an extra square foot of living area might be different in an expensive neighborhood compared to a cheaper one.

The Machine Learning Mastery article demonstrates this beautifully.

Interpreting Coefficients in Linear Regression Models

Read this final section to see how the coefficient for 'GrLivArea' changes when the 'Neighborhood' feature is also included in the model.

Read the section 'Discussion on Combining Numerical and Categorical Features'. Notice that the 'GrLivArea' coefficient dropped from 110.52 to 78.93. The text explains why this happens—the model is now adjusting for the fact that some of the price variation is explained by neighborhood.

This shows that a coefficient's value is not an absolute truth but is always interpreted in the context of the other features in the model.

2. Coefficient Magnitude is Not Feature Importance (Without Scaling)

It is tempting to look at the coefficients and assume that the feature with the largest coefficient is the most "important." This is often incorrect.

The scale of the feature affects the scale of its coefficient. Imagine a feature distance_in_km with a coefficient of 10. If you change the feature to distance_in_meters, its values will be 1000 times larger, and its new coefficient will become 1000 times smaller (0.01), even though the feature's underlying importance is the same.

To make coefficients more comparable for gauging importance, you should scale your numerical features (e.g., using StandardScaler, as we've done before) before training the model. After scaling, features are on a similar scale, and their coefficient magnitudes give a better (though still not perfect) sense of their relative impact.


Conclusion

Great job today! You've moved beyond just evaluating a model's score to understanding what the model has actually learned. This is the key to extracting real-world value and insights from your machine learning work.

Key Takeaways:

  • A numerical feature's coefficient is the change in the target for a one-unit increase in that feature, holding others constant.
  • A categorical feature's coefficient is the difference in the target compared to the reference category, holding others constant.
  • The intercept is the baseline prediction when all numerical features are zero and all categorical features are at their reference category.
  • The interpretation of any single coefficient is always conditional on the other features included in the model.
  • You cannot compare raw coefficient magnitudes to judge feature importance unless the features have been scaled.

Preview of the Next Lesson:

In the last lesson, we calculated R² and MSE to get a single number representing model performance. In the next lesson, Assess regression model fit using R-squared, MSE, and residual analysis, we will dive deeper into model assessment. We'll focus particularly on residual analysis—analyzing the errors of our model. This is a powerful diagnostic tool that can tell us if the assumptions of linear regression are being met and help us identify where our model is going wrong.