Concepts / Interpreting Model Coefficients

Interpreting Model Coefficients

If you want to know what the model learned about a word, the coefficient for that word's feature is the place to look.

  • Programming

Why Coefficients Need a Picture

Once a text classifier has been trained, the natural question is which parts of the input it relied on. In a logistic regression model trained on tf-idf features, each feature has a coefficient, and those coefficients are what this article is about. A list of hundreds or thousands of numbers is hard to read, so the practice shown in the source is to draw them as a bar chart. The chart makes the relative sizes of the coefficients visible at a glance.

The goal here is to get you comfortable with that chart. You will see what the bars represent, learn the small piece of logic that picks out the largest and smallest values, and practice the steps that keep the feature names attached to their numbers. Selection logic is where beginners most often make mistakes, so this article slows down there. Treat every number you read from a chart as something you should be able to trace back to a value the fitted model stored.

What a Coefficient Measures

Definition

A logistic regression coefficient is a learned number for each feature. The source states that coefficients determine each feature's importance in the model. In this setting, each bar in the chart corresponds to one tf-idf feature, so each bar is a view of one learned number rather than of the raw text.

The features here are tf-idf features. They are extracted from the text using the tf-idf technique and then passed as input to the logistic regression model. The model learns one coefficient per feature from that input. This means the chart is a picture of the trained model, not of the raw text. If you want to know what the model learned about a word, the coefficient for that word's feature is the place to look.

Two readings of a coefficient matter. Its size indicates how strongly that feature is weighted in the model, and its sign indicates which way the feature pushes the prediction. The source describes the spread of markers in its plot as showing which combinations pushed the prediction up or down, which is the same sign-based reading applied to a different model.

Tracing the Hour and Day Labels

The source follows one concrete case. An earlier step combined day-of-week and time-of-day inputs into interaction features, which produced a linear model that performed about as well as a random forest on the same problem. Because there is one coefficient for each day and time combination, the model is easy to inspect. The first step toward reading those coefficients is building human-readable names for the original inputs. 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?

The hour list is created with a range that starts at zero, stops before twenty-four, and steps by three. How many hour labels will it contain, and which values are they?

Reveal answer
Answer:

It contains 8 labels: 0, 3, 6, 9, 12, 15, 18, and 21.

A range that starts at 0, stops before 24, and steps by 3 yields the values 0 through 21 in steps of 3. Counting them gives 8. Combined with the 7 day abbreviations, the base names form a set of 15 entries. The interaction features pair each day with each hour, so one coefficient per combination gives 8 times 7, or 56, candidate columns before any filtering.

Notice the trade-off the source points out. The transformed model has many more columns than the original inputs, because each new column is a product of existing features. Interpretation shifts from reading a few effects to reading a grid of combined effects. A plot of the coefficients is what makes that grid manageable.

Keeping Names and Coefficients in Step

The program that builds the plot 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. Two lists must stay in step through this process: the feature names and the coefficients. Each position in one list is supposed to describe the same position in the other.

Both lists must be filtered with the same test because the model stores coefficients in one fixed order, and the names are only useful when they sit at the same positions. The filtering idiom works like this: 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 uses made-up numbers, not the program from the source, to show the mechanism in isolation.

python
Output (expected)
['b' 'd']
[ 1.5 -2. ]
Notes on the code
  • mask is an array of True and False, one per position
  • names[mask] and coefs[mask] use the same mask, so they keep the same positions

Notice that 'b' and 'd' stay paired with 1.5 and negative 2.0 because both arrays were indexed with the same mask. If you had filtered only one of them, the pairing would be lost and the output would still look perfectly valid. A mismatch does not usually raise an error. It silently attaches the wrong label to a number, which is one of the most common ways a coefficient analysis goes wrong.

a0.0b1.5c0.0d-2.0
When the same mask removes the zero coefficients, which names still point to their own coefficients, and which positions are dropped from both lists?

Sorting Before Slicing

The chart of largest and smallest coefficients depends on a two-step selection. Picking the largest and smallest values requires sorting by the coefficient before slicing. Slicing an unsorted list selects by position, not by value, so a slice taken first returns whatever happens to sit at the ends of the original order. The safe order is to sort the pairs by value and then slice the first and last N.

The source uses an invented example to practice this selection, because the chart itself cannot be re-run here. The feature names and values are made up for illustration and are not output from a real model. The code pairs each name with its value, sorts the pairs by value, and slices off the two lowest and two highest. The middle of the list is simply left out.

python
Output (expected)
[('fri', -2.0), ('sat', -0.3)]
[('mon', 0.4), ('tue', 1.5)]
Notes on the code
  • sorted orders the pairs by the coefficient, the second element of each pair
  • ranked[:2] takes the two lowest values, ranked[-2:] takes the two highest
  • sun, with 0.1, sits in the middle and is not drawn
fri-2.0sat-0.3sun0.1mon0.4tue1.5
After sorting by value, which features fall in the first two positions, the middle, and the last two positions?

What the Bar Chart Shows and Leaves Open

The source produces the chart with a single call to a plotting helper. The call does not compute anything on its own. It receives the fitted coefficients, the feature names, and a count parameter, and then draws them. Selecting which coefficients appear is handled inside the helper, which is why the count parameter matters for what you see. The excerpt relies on two names defined earlier in the original workflow, grid and feature_names, and does not show how they were created, so treat them as given inputs.

The source describes the chart as showing the 25 largest and 25 smallest coefficients, but the call that produces it passes n_top_features=40. The excerpt does not explain how that parameter divides between the largest and smallest groups, or whether it counts the total number of bars. Do not assume a 25 and 25 split when you read the chart.

The word smallest is also ambiguous. The source does not say whether it means the lowest signed value, as in the example above, or the value closest to zero in size. The two readings can produce very different bars. Confirm which one the helper uses before comparing the two ends of the chart. Features with values in the middle of the ranking are not drawn at all, so the picture highlights the extremes rather than every feature.

bar length per drawnfeaturesign of each drawncoefficientfeatures ranked inthe middleexact 25 and 25 splitdefinition ofsmallest
Which properties of the drawn bars can be read directly, and which questions the chart cannot answer without checking the helper?

Common Ways Coefficient Reading Goes Wrong

  • Common mistake

    Slicing the coefficient list before sorting it

    What it looks like

    Taking the first two and last two items of the original order and reporting them as the largest and smallest coefficients.

    Why it fails

    A slice selects by position, not by value, so the ends of the original list have nothing to do with the ends of the ranking.

    Do this instead

    Sort the name and coefficient pairs by value first, then slice the first and last N.

  • Common mistake

    Filtering only one of the two parallel lists

    What it looks like

    Removing zero coefficients from the coefficient list but not from the feature name list.

    Why it fails

    The two lists must stay in step. The output still looks valid, so the wrong label attaches to a number silently.

    Do this instead

    Build one boolean mask from the coefficients and apply it to both the names and the coefficients.

  • Common mistake

    Assuming the chart shows an even split of 25 largest and 25 smallest

    Why it fails

    The source describes 25 and 25, but the call passes n_top_features=40, and the excerpt does not explain how that parameter divides the bars.

    Do this instead

    Read the count parameter and the helper's definition before assuming how many bars come from each end.

  • Common mistake

    Reading smallest as the value closest to zero

    Why it fails

    The source does not settle whether smallest means the lowest signed value or the smallest magnitude. The two readings produce different bars.

    Do this instead

    Confirm which definition the helper uses before comparing the two ends of the chart.

  • Common mistake

    Trusting axis labels without checking them against the plotting code

Where the Axis Labels Can Mislead

A plot is only as trustworthy as its labels. When you read the plotting code closely, the axis labels in the source are attached in an unexpected way. 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.

The line responsible is the one in the plotting code that assigns the axis labels. When you find it, compare the label text against the arguments in the data call. If the label says the horizontal axis is magnitude while the coefficients are drawn vertically, the chart is misleading even though every bar is correct. Check the axis labels before drawing any conclusion from a coefficient plot.

Predict Before You Trace

MEDIUM

Using the made-up list of pairs from earlier, [('mon', 0.4), ('fri', -2.0), ('tue', 1.5), ('sun', 0.1), ('sat', -0.3)], predict the output of two selections before reading the answer. First, sort the pairs by value and take the first two and last two. Second, slice the original unsorted list the same way. Then explain which features the two selections share and why the order of operations matters.

Hints
  • Sort by the second element of each pair, from lowest to highest.
  • The middle item after sorting is the one left out of both ends.
  • Compare which items land in the first and last positions of the original list.

Key Takeaways

  • A logistic regression coefficient is a learned number for each tf-idf feature, and the source states that coefficients determine each feature's importance in the model. Its size and sign are the two things to read.
  • A coefficient in linear regression is the expected change in the target for a one-unit change in one input column, which is what makes a short list of coefficients explainable.
  • Selecting the largest and smallest values requires sorting by value before slicing, because slicing an unsorted list selects by position.
  • Feature names and coefficients must be filtered with the same boolean mask so that every name keeps pointing to its own number.
  • A coefficient plot can mislead through its labels, and the 25 and 25 split and the meaning of smallest should be confirmed in the helper before drawing conclusions.

Related concepts

More concepts starting with I