← All posts
Machine Learning · Linear Model Explainability · Part 4 of 9

When One Feature Changes Another Feature’s Effect

Our original model adds 5 dollars per visit for everyone. What if an extra visit contributes more for Premium customers? We can express that difference directly, then explain it without asking readers to interpret a mysterious product coefficient.

Let the visit rate depend on the plan

Use this separate illustrative equation for the chapter:

$$\hat y=20+5v+10p+2vp.$$

The new term is $2vp$: two times visits times the Premium indicator. We have added it to the original equation to study its behavior. These are stipulated coefficients, not a new fit to the four original observed amounts.

For Basic, $p=0$, so both terms involving $p$ disappear. For Premium, $p=1$, so the new term adds another 2 per visit:

Basic: 20 + 5 × visits.
Premium: 30 + 7 × visits.

That is the interaction in plain language: one more visit adds 5 to a Basic prediction and 7 to a Premium prediction. The relationship between visits and the prediction depends on the plan.

All predictions use the interaction equation above, in dollars.
VisitsBasic predictionPremium predictionPlan difference
0203010
1253712
2304414
3355116
4405818

Read the interaction in either direction

Read down the Basic column: every extra visit adds 5. Read down the Premium column: every extra visit adds 7. Now read across a row. At one visit, Premium adds 12; at four visits, it adds 18.

These are two descriptions of the same interaction. The extra-visit effect depends on plan, and the plan effect depends on visits.

$$\begin{aligned}\text{One extra visit}&:\;5+2p\\\text{Basic to Premium}&:\;10+2v.\end{aligned}$$
Basic predictions rise by 5 per visit and Premium predictions rise by 7, making the gap widen from 10 to 20 across zero to five visits.
Parallel lines would have a constant plan gap. Here the Premium line is steeper, so the gap increases with visits. Select the figure to view it at full size.

Rebuild Maya’s prediction

Maya has four visits and Premium. Apply the four terms individually:

Maya’s prediction under this chapter’s interaction model.
TermCalculationAmount ($)
Starting amount2020
Visits5 × 420
Premium10 × 110
Interaction2 × 4 × 18
Total20 + 20 + 10 + 858

The 8-dollar term belongs to the combination of visits and Premium. Listing it separately is a faithful decomposition of the equation. Assigning it entirely to visits or entirely to plan would be an additional choice. SHAP offers a way to share joint contributions, but we do not need that machinery to explain the equation itself.

Read the main coefficients at their reference values

The coefficient 5 is the visits slope when Premium is 0: the Basic plan. The coefficient 10 is the Premium difference when visits is 0. They are often called main-effect coefficients. Once the product term exists, neither is the complete effect for every customer.

Zero visits may be a less useful comparison than two visits, the average of our training rows. Center visits by defining $u=v-2$, so $v=u+2$. Substitute this into the equation and collect terms:

$$\begin{aligned}\hat y&=20+5(u+2)+10p\\&\quad+2(u+2)p\\&=30+5u+14p+2up.\end{aligned}$$

The plan coefficient is now 14. It describes the Premium difference at two visits, because that is where $u=0$. Maya has $u=2$, so her prediction is 30 + 10 + 14 + 4 = 58. Centering changes the reference point and the coefficient meanings, while preserving every prediction.

Explore both plans

Change visits with Basic selected, then repeat with Premium. Finally, hold visits fixed and switch plans. The explanation below the equation reports both differences.

Make the product from the original inputs

def predict_interaction(visits, premium):
    return 20 + 5*visits + 10*premium + 2*visits*premium

for visits in [1, 4]:
    basic = predict_interaction(visits, 0)
    premium = predict_interaction(visits, 1)
    print(visits, basic, premium, premium - basic)
# 1 25 37 12
# 4 40 58 18

When fitting such a model, its input columns would be visits, Premium, and their product. When predicting, construct that product again from the supplied values. Changing a plan while leaving the product frozen would no longer evaluate the stated model.

Write the explanation around a comparison

For Maya, a useful sentence is: “With four visits, the model predicts 58 dollars for Premium and 40 for Basic, a difference of 18.” That sentence identifies the model, the customer’s visit count, both outcomes, and their difference.

Now that we can explain the equation’s behavior, the next question is whether its inputs themselves are easy to understand. The next chapter starts one step earlier, with the measurements from which features are built.

Sources and worked examples

This chapter develops ideas from the lessons below in A Data Odyssey’s Explainable AI playlist. The customer data, calculations, figures, and interactive examples here are original teaching examples. The creator’s companion notebooks use different datasets.