Concepts / tf-idf Features

tf-idf Features

For tf-idf features, each feature is a numeric weight attached to a term.

  • Programming

Turning Coefficients Into a Picture

Imagine a text classifier built with logistic regression on tf-idf features. Training is finished, and the model has learned one coefficient for each feature. The coefficients are just numbers. A long list of them is hard to interpret by eye, so the usual next step is to plot them and look at which features carry the most weight.

The plot is the lens for everything in this article. Before you can read it correctly, you need to understand two things: what a single coefficient means, and how the plot decides which coefficients to show. We will build up both, one step at a time, using a small example you can trace by hand.

Pairing Coefficients With Feature Names

Two inputs must line up before any chart can be read. The first is the coefficient array, a list of numbers produced by the trained model. The second is the feature name list, a separate list of the terms the model was given. The plot works only if position i in the coefficient array corresponds to position i in the feature name list. If that correspondence breaks, every bar gets labeled with the wrong term, and the chart looks confident while being wrong.

The source describes the chart as pairing each coefficient with its feature name and then displaying the largest and smallest values. Before reading the real chart, practice the core operation with a tiny stand-in. The four features and coefficients below were invented for this article, and the code uses only built-in Python.

great1.2great1.2boring-0.8plot0.1plot0.1boring-0.8waste-1.5waste-1.5rank 1rank 2rank 3rank 4
Which coefficient belongs to which feature name, and how does sorting change the order of the bars? The input order is on the left; the order after sorting from largest to smallest is on the right. Changed positions are marked.
python
Output (expected)
[('great', 1.2), ('boring', -0.8), ('plot', 0.1), ('waste', -1.5)]
great 1.2
plot 0.1
boring -0.8
waste -1.5
Notes on the code
  • zip pairs position i of the feature list with position i of the coefficient list
  • sorted with key=lambda p: p[1] orders the pairs by the coefficient, not by the feature name
  • reverse=True puts the largest coefficient first

Notice what happened. The pairs were built first, so each name stayed attached to its own number through the sort. Sorting moved the pairs around, but it never separated a name from its coefficient. This is why the pairing must happen before any sorting or selection. If you sorted the two lists separately, the names would no longer match the numbers.

What One Coefficient Measures

Definition

A logistic regression coefficient measures the change in log odds for a one-unit change in its predictor.

The phrase one-unit change carries most of the meaning. A coefficient is a rate of change per unit of its feature, so it only means something relative to what one unit of that feature is. For tf-idf features, each feature is a numeric weight attached to a term. That is why the coefficients are the model's learned weights on those term weights. A coefficient of 1.2 on the feature great says that, holding the other features fixed, increasing that term's tf-idf weight by one unit shifts the model's log odds by 1.2 in the positive direction. A negative coefficient shifts the log odds the other way.

Reading Both Ends of the Chart

The source chart displays the 25 largest and 25 smallest coefficients of the trained model. The length of each bar shows the size of that coefficient. The largest coefficients are the features with the strongest positive weight in the model's ranking, and the smallest coefficients are the other extreme. Reading it means looking at both ends of the range. Looking only at the top of the list would hide half of the story, because the most negative bars are just as informative as the most positive ones.

The chart is laid out as a horizontal arrangement of feature bars in two halves: one half for the largest coefficients and one for the smallest. Each bar is labeled by its feature name. Use the labels to answer the question the chart is built for: which features does the model weight most heavily, and in which direction? The next example uses the same two-ended selection on the small invented data set.

python
Output (expected)
largest: [('great', 1.2), ('plot', 0.1)]
smallest: [('boring', -0.8), ('waste', -1.5)]
Notes on the code
  • ranked[:2] takes the two largest coefficients from the front of the sorted list
  • ranked[-2:] takes the two smallest coefficients from the back of the sorted list
  • Both ends are selected, mirroring the source chart's two-sided display
great1.2boring-0.8plot0.1waste-1.5
Which features push the prediction most strongly in each direction? The positive-coefficient features form one column and the negative-coefficient features form the other.

Read the comparison carefully. The largest column holds the strongest positive weights, and the smallest column holds the most negative ones. Size alone is not the whole picture: waste has the largest magnitude in this example, even though it sits in the smallest column. The direction tells you which class the feature pushes toward, and the length tells you how strongly.

When the Bar Count Disagrees With the Setting

A coefficient plot can diverge from expectations in a specific way. The source states that the chart shows 25 largest and 25 smallest coefficients, which would be 50 bars in total. The call that produces it passes n_top_features=40. The source does not explain how those two numbers relate. So do not assume the chart shows exactly the count in the caption, and do not assume it shows exactly the number in the call.

Caption: 25 + 2550 bars describedCall:n_top_features=40parameter settingFunctiondocumentationhow the parameter isappliedBars actually drawncount the chartcomparecheckconfirm
If the code requests a number of top features, how many bars actually appear? Each stage is a place where the expected count and the displayed count can disagree.
  • Common mistake

    Reading only the top bars of the chart

    Why it fails

    The source states that both ends carry information, so the smallest coefficients are part of the story.

    Do this instead

    Read both halves, and note the direction of the strongest negative weights as well as the strongest positive ones.

  • Common mistake

    Sorting the coefficient list and the feature name list separately

    What it looks like

    features sorted alphabetically while coefs stays in model order

    Why it fails

    Position i must correspond across the two lists, so separate sorting breaks the pairing and labels each bar with the wrong term.

    Do this instead

    Pair the two lists first with zip, then sort the pairs as a unit.

  • Common mistake

    Assuming the bar count equals the number in the call or the number in the caption

    Why it fails

    The source text and the call do not state the same number, and the source does not explain their relation.

    Do this instead

    Count the displayed bars and check the function's documentation for how the parameter is applied.

  • Common mistake

    Reading a coefficient as the change in probability

    Why it fails

    The source defines a coefficient as a change in log odds per one-unit change in the predictor.

    Do this instead

    Describe the effect in terms of log odds, and recognize that the chart does not report a probability for any single prediction.

Predict the Selected Bars

What do you think happens?

If you keep only the two largest coefficients from the four-feature example, which feature names appear?

  • great and boring

  • great and plot

  • waste and boring

  • plot and waste

Reveal answer
Answer:

great and plot

Sorting the pairs from largest to smallest gives great 1.2, plot 0.1, boring -0.8, waste -1.5. Taking the first two pairs selects great and plot, and the names stay attached to their own coefficients throughout.

EASY

Using the same four pairs, decide which two features make up the smallest half of the chart, and state which direction each one pushes the log odds.

Hints
  • Take the last two pairs of the sorted list, not the first two
  • A negative coefficient shifts the log odds in the negative direction

Key Takeaways

  • A logistic regression coefficient measures the change in log odds for a one-unit change in its predictor, so it describes a rate rather than a single prediction.
  • The coefficient array and the feature name list must line up position by position, so pair them before sorting or selecting anything.
  • The source chart shows the 25 largest and 25 smallest coefficients, and both ends carry information that should be read together.
  • Count the displayed bars and check the function's documentation, because the source's caption (50 bars) and the call (n_top_features=40) do not state the same number.

Related concepts

More concepts starting with T