Interpreting Linear Regression Coefficients
A coefficient is the expected change in the target for a one-unit change in one input column.
A Trained Model You Can Read Line by Line
A trained linear regression model is more than a prediction machine. It stores one number per input column, and those numbers are called coefficients. Reading them tells you what the model has learned about each input. The practical payoff is that you can often explain a model's behavior by looking at a short list of numbers instead of treating the model as a black box.
This article follows one concrete case. An earlier step combined day-of-week and time-of-day inputs into interaction features. That produced a linear model that performed about as well as a random forest on the same problem. The interaction features made the model easy to inspect, because there is one coefficient for each day and time combination. This article is about reading those coefficients correctly, and about the small code steps that carry them from the model into a plot.
Keeping Names and Coefficients in Step
A coefficient list is only meaningful if you know which input each number belongs to. The first step is therefore to build a list of human-readable names for the original inputs. In this case there are two groups: a list of hour labels generated in steps of three hours across the day, and a list of seven day abbreviations. The two lists are joined into one list of base feature names.
What do you think happens?
How many hour labels does a range from 0 up to but not including 24, stepping by 3, produce?
Reveal answer
8
The labels are 0, 3, 6, 9, 12, 15, 18, and 21. The stop value 24 is excluded, so the last label is 21. Combined with the seven day abbreviations, the base feature list therefore has 15 entries before any interactions are built.
The program that follows does three things in sequence. It asks the transformer for the names of all the interaction features it generated. It then applies a boolean test to the learned coefficients to keep only the features whose coefficient is not zero. Finally, it extracts those nonzero coefficients as a separate list. As you follow the execution, keep track of two lists that must stay in step: the feature names and the coefficients. Each position in one list describes the same position in the other.
The filtering relies on a NumPy idiom. Comparing an array to a value produces an array of True and False, and indexing with that array keeps only the True positions. The example below is not the program from the source. It uses made-up numbers to show the same mechanism in isolation, so you can see what the mask does before applying it to real coefficients.
['b' 'd']
[ 1.5 -2. ]- coefs != 0 compares every coefficient to zero and returns an array of True and False values
- names[mask] keeps only the names at True positions
- coefs[mask] keeps the coefficients at the same True positions, so the pairing survives
Walk through the mask example one line at a time to see why the pairing holds.- 1The names array holds four labels and the coefs array holds four values, in the same order.
- 2The mask is computed from the coefficients alone: positions with 0.0 become False, and the other positions become True.
- 3Indexing names with the mask keeps 'b' and 'd', because those are the only positions where the coefficient is nonzero.
- 4Indexing coefs with the same mask keeps 1.5 and -2.0 at the matching positions, so 'b' stays with 1.5 and 'd' stays with -2.0.
Before you run code like this on a real model, predict its size. If every hour label is paired with every day, the number of interaction columns is the product of the two group sizes. In the setup described here, that is 8 hour labels multiplied by 7 day abbreviations, giving 56 columns. The nonzero filter then removes any of those whose learned coefficient is exactly zero, so the final list can be shorter than 56. Working out the expected count first gives you a check: if the output length does not match your prediction, something upstream changed.
What a Linear Coefficient Says About the Target
A coefficient is the expected change in the target for a one-unit change in one input column. The phrase 'expected change' matters: the coefficient describes the average effect the model learned, not a guaranteed result for a single case. The phrase 'one input column' matters too: the rule is stated for one input at a time, with the other inputs held fixed. The coefficient's sign gives the direction of the change, and its size gives how much the target moves per unit of that input.
Reading one coefficient in a study-score model
A model predicts a test score from hours studied, with a learned coefficient of 3.0 for hours studied. Interpret that coefficient, assuming every other input stays the same.
- 1Identify the target and the input
The target is the test score and the input is hours studied.
- 2Apply the one-unit rule
Increasing hours studied by exactly one, with other inputs fixed, changes the predicted score by the coefficient.
- 3Read the sign
The coefficient is positive, so the predicted score moves up.
- 4Read the size
The predicted score rises by about 3.0 points for each additional hour studied, according to the learned rule.
A coefficient of 3.0 means the expected score increases by 3.0 for each one-hour increase in study time, with other inputs held constant.
A coefficient is always read relative to one input and one target, under the condition that the other inputs stay fixed. Changing the input by more or less than one unit does not give a new coefficient; you scale the one-unit effect instead.
Following the Plot From Code to Axes
Once the names and coefficients are aligned, the plotting step draws one marker for each nonzero coefficient. The figure is set wide and short so many labels fit along one line. The tick labels are rotated vertically so long names do not overlap, and the feature names are placed at the tick positions. The vertical spread of the markers shows which combinations pushed the prediction up or down.
A plot is only as trustworthy as its labels, and this one can mislead. In the plotting code, the coefficient values are passed as the first and only data argument, so they are drawn on the vertical axis. The feature names are placed on the horizontal axis through the tick settings. Yet the horizontal axis is labeled as the feature magnitude, and the vertical axis is labeled as the feature. Read literally, the labels describe the opposite of what each axis shows.
To locate the problem, trace the three things that determine what a reader sees: which array is passed as the data, which values are used for the tick positions and tick labels, and which text is given to each axis-label call. A swap shows up when the label text for an axis does not match the variable plotted on it. The fix is to correct the axis-label calls so the vertical axis names the coefficient and the horizontal axis names the feature, then check that the markers and their labels still line up.
Logistic Coefficients for Words
A trained logistic regression model is not only a prediction tool. It also stores a number, called a coefficient, for every feature it learned from. In a text classifier, the features are the words in the documents, so each word has its own coefficient. Those coefficients record what the model picked up from the training data. Each coefficient belongs to one feature, which in this setting is one word.
This raises a practical question. A text model trained on movie reviews in the source had 27,271 features left after infrequent words were removed. That is far too many coefficients to read one by one, so the approach is to look only at the largest coefficients and see which words they belong to. The rest of this section explains how to read those coefficients once you have picked them out.
Read the sign first. Each coefficient is attached to one word and expresses how the model treats that word when it decides a label. Negative coefficients belong to words the model considers indicative of negative reviews, while positive coefficients belong to words that indicate positive reviews. The sign therefore tells you which side of the decision the word pushes toward. The source's movie-review examples fit this intuition: on the negative side, worst, waste, disappointment, and laughable are indicators of bad reviews, while excellent, wonderful, enjoyable, and refreshing indicate positive reviews.
A word's coefficient describes its association with a class, not a judgment about the word in isolation. The same word can point toward different classes depending on what else the model has learned, so always read the coefficient together with its sign and its neighbors.
Why Single Words Can Mislead
Some words look unclear when read alone. The source names bit, job, and today as examples. Their weight may come from phrases such as good job or best today, where the surrounding words carry the meaning. A coefficient for a lone word is therefore a summary of how that word behaved in the training data, and it may depend heavily on the phrases it appeared in.
The source's sorted example makes this concrete. The word bit has a negative coefficient, but its magnitude is very small, so under the reading rules it is a weak signal and reads as slightly negative rather than as a strong indicator. A careful reader would still check the word's neighbors in the data before drawing a conclusion, because bit may only carry meaning inside a phrase.
Common Mistakes When Reading Coefficients
Common mistake Filtering the feature names but not the coefficients, or the reverse
What it looks likeApplying the nonzero mask to the names list and then plotting the full, unfiltered coefficient array.
Why it failsThe two lists are only meaningful at matching positions. Filtering one list shifts the pairing, and the output still looks valid.
Do this insteadCompute the mask once from the coefficients and apply that same mask to both the names and the coefficients.
Common mistake Trusting a coefficient plot without reading its axis labels
What it looks likeReading the horizontal axis as the coefficient value when the code plots coefficients on the vertical axis.
Why it failsThe labels can describe the opposite of what is drawn, so any conclusion built on them is reversed.
Do this insteadCheck which array each axis plots and confirm that its label names that quantity before interpreting the markers.
Common mistake Reading a coefficient's size without its sign
What it looks likeCalling a large negative coefficient a strong positive word because it has a large magnitude.
Why it failsMagnitude gives strength and sign gives direction. Ignoring the sign reverses the meaning for a logistic word coefficient.
Do this insteadRead the sign to find the class the word points toward, then use the magnitude to judge how strongly it does so.
Common mistake Treating a single word's coefficient as the meaning of the phrase
What it looks likeConcluding that job is always a positive review word because it appears in good job.
Why it failsWords such as bit, job, and today may get their weight from phrases, so their lone coefficient can mislead.
Do this insteadCheck the phrases the word appears in before deciding what its coefficient says about the text.
Common mistake Describing a one-unit coefficient as a guaranteed change for every case
Reading a Sorted Coefficient List
The table below is a small illustration of the reading process. The coefficient values are invented for this lesson so the pattern is easy to see. They are not the source model's actual numbers. The words are sorted from the most negative coefficient to the most positive, and each word is labeled with the review type its sign indicates.
| Word | Illustrative coefficient | Sign reading | Strength reading |
|---|---|---|---|
| worst | -2.10 | negative review | strong |
| waste | -1.85 | negative review | strong |
| bit | -0.04 | negative review | very weak |
| enjoyable | 1.40 | positive review | moderate |
| excellent | 1.75 | positive review | strong |
Invented coefficients for illustration only, sorted from most negative to most positive.
Using the illustrative table, which word is the strongest indicator of a negative review, and why is bit a weak signal even though it is negative?
Hints
- Look at magnitudes first to find the strongest signal, then check the sign to find its direction.
- bit has a negative sign, but its magnitude is very small compared with the other values.
- Think about what a reader should check in the data before drawing a conclusion from a weak word.
A colleague's coefficient plot shows the feature names along the vertical axis and the coefficient values along the horizontal axis, but the axis labels say the opposite. Describe the first two checks you would make before trusting any conclusion from the plot.
Hints
- Identify which array is passed as the data and which values set the tick positions.
- Compare each axis label with the quantity actually plotted on that axis.
Key Takeaways
- A linear regression coefficient is the expected change in the target for a one-unit change in one input, with the other inputs held fixed.
- Names and coefficients must be filtered with the same boolean mask, because a mismatch silently attaches the wrong label to a number.
- In a logistic regression text classifier, each word has its own coefficient; the sign gives the class the word points toward and the magnitude gives its strength.
- Coefficient plots can mislead when axis labels do not match the plotted quantity, so check the labels and the plotting calls before drawing conclusions.
- Single words can be ambiguous, so check the phrases a word appears in before interpreting its coefficient.