2.1.10 · D2Data Preprocessing & Feature Engineering

Visual walkthrough — Feature creation and interaction terms

2,622 words12 min readBack to topic

Everything here builds on the parent note Feature creation and interaction terms. If a symbol shows up, we define it first.


Step 1 — One feature, one straight line

WHAT. We start with the simplest possible predictor. We have one input number, call it (say ad spend in dollars), and one output we want to guess, call it (say sales). We propose:

  • ("beta-zero") is the intercept — where the line sits when . It is the sales you'd make spending nothing on ads.
  • ("beta-one") is the slope — how many extra dollars of sales you get for each extra dollar of ad spend. Slope means "rise over run": rise in divided by run in .

WHY. Before adding anything fancy, we need to see what "linear" even buys us. A line has exactly one steepness, and that steepness is the same everywhere.

PICTURE. In the figure the blue line rises at a fixed rate. Pick any two points one unit apart along — the jump in (the red rise) is identical no matter where you stand. That "same jump everywhere" is the whole personality of a line.


Step 2 — Two features: the flat sheet

WHAT. Add a second input . To keep it concrete, let be is_weekend, a number that is either (weekday) or (weekend). Our model grows one term:

  • is how much sales shift just for it being the weekend, before any ads.

WHY. Two inputs means now lives above a plane — we plot on one floor axis, on the other, and as height. A model with only is a flat tilted sheet floating over that floor.

PICTURE. Look at the two blue lines in the figure — one is the weekday slice (), one the weekend slice (). They are parallel. The weekend line is lifted by , but it climbs at the same slope . That parallelness is the flatness of the sheet: sliding along only raises or lowers the line, never tilts it.


Step 3 — The question a flat sheet cannot answer

WHAT. Ask a sharper question: does each ad dollar do more on weekends? In symbols, we want the slope with respect to to depend on . But on the flat sheet the slope in the direction is:

The symbol ("partial derivative") just means: hold fixed and measure how fast climbs as grows — the slope of the -slice. We use a derivative here specifically because "how the effect of one feature changes" is a slope-of-a-slope question, and derivatives are the tool that reads off slopes.

WHY. The result has no in it. Mathematically, that is a proof the flat sheet can never let weekends change the ad slope. We are stuck.

PICTURE. The figure overlays the two parallel slices again and marks their slopes with a protractor angle. Both angles are equal. There is no room in to make them differ.


Step 4 — Introduce the product term

WHAT. Create a brand-new feature by multiplying the two inputs: . Give it its own weight and add it in:

  • is literally ad-spend times the 0/1 weekend flag.
  • is the weight the model learns for that product.

WHY multiplication and not, say, addition? Because a product is the smallest thing that is zero when either input is zero and grows with both together. On weekdays , so and the term switches off. On weekends , so and the term switches on. A sum could never toggle like that; a product is the natural "both-at-once" gate.

PICTURE. The figure shows the value of across the floor. It stays a flat zero along the entire weekday edge, then ramps up along the weekend edge — a triangular ramp pinned to the side.


Step 5 — Watch the slope become a variable

WHAT. Recompute the ad-spend slope, now with the interaction present. Group the terms:

WHY. The quantity in front of is now — it contains . That is exactly the thing Step 3 said was impossible on the flat sheet. Take the partial derivative again to make it airtight:

  • On a weekday (): slope .
  • On a weekend (): slope .

The two slices no longer have to be parallel — is the difference in their slopes.

PICTURE. The two blue lines now fan out from a shared starting point: same intercept behaviour, different steepness. The orange gap between them widens as grows — that widening is .


Step 6 — When is continuous, not just 0/1

WHAT. So far has only been a switch ( or ). But the exact same algebra works when is a continuous number — say is now temperature instead of a weekend flag. The model is unchanged:

WHY. With a binary there were exactly two slopes: (off) and (on) — two lines. With a continuous the slope slides smoothly as moves: every temperature gets its own ad-slope. So instead of two lines fanning apart, you get a whole continuous fan of lines — one per value of — sweeping steeper and steeper as grows.

PICTURE. The figure draws several slices for . Notice they are no longer two lines but a graded fan: each step up in tilts the line a little more by exactly . The switch has become a dimmer with a continuous dial.


Step 7 — The sheet is now a twisted surface

WHAT. Put both inputs back into 3-D at once. Instead of a flat plane, now sits on a surface that we call bilinear.

WHY. This twisted surface is the geometric meaning of "interaction." The corner over (high , high ) is lifted extra high by , pulling one corner up and creating a gentle saddle-like twist. That corner lift is the synergy we wanted to model.

PICTURE. The mesh in the figure is a straight line along the front edge and along the side edge, but the far corner warps upward. Trace any single edge: it is dead straight (Step 1's promise survives along one direction), yet the whole sheet is no longer flat.


Step 8 — Edge, degenerate, and practical cases (never leave the reader stranded)

WHAT & WHY & PICTURE, case by case — the figure shows four in one panel:

  1. . The product term vanishes; slices go parallel again; we collapse back to the flat sheet of Step 2. Interaction is a strict generalization — the flat model is the special case .
  2. everywhere (feature never "on"). Then , the whole column is zeros, and is unlearnable — the fit has no information to set it. This is why a constant/degenerate feature makes interactions useless.
  3. Interaction without main effects. Drop and , keep only . Now you can no longer describe an ad effect that exists on its own — the surface is pinned flat along both axes. (This is Mistake 2 from the parent note, shown geometrically.)
  4. negative. The far corner dips down instead of up — antagonism, not synergy. Ads could pay less on weekends. Same machinery, opposite twist.

The one-picture summary

How to read the figure below. Two panels share the same axes: the horizontal axis is (ad spend) and the vertical axis is (sales). In each panel, the blue line is the weekday slice () and the orange line is the weekend slice ().

  • Left panel — the flat model. Blue and orange are parallel: the ad-slope is the constant no matter the day. Weekend is only lifted by .
  • Right panel — after adding . The orange line fans away from the blue one: its slope is now , so the gap between them grows with . That growing gap is the interaction — the constant slope has become a variable slope.

The single takeaway: add the product and the constant slope becomes the variable slope — parallel lines become fanned lines, a flat sheet becomes a twisted one.

Recall Feynman retelling — say it to a 12-year-old

Imagine a ramp that turns dollars of advertising into dollars of sales. On a normal day the ramp has a certain steepness — every ad dollar gives the same reward. Now you notice weekends feel different: the same dollar seems to work harder. A plain model can only lift the whole ramp up on weekends; it's stuck at the same steepness because its steepness is a fixed number with no "weekend" inside it. So we invent one new number: ad-spend multiplied by the weekend flag. On weekdays that number is zero (the weekend flag is 0), so nothing changes. On weekends it becomes just the ad-spend again, and it adds extra steepness to the ramp — an amount we call . Now the ramp can be gentle on weekdays and steep on weekends. In pictures the two ramps stop being parallel and fan apart, and the flat sheet over the whole map develops a twist at the busy corner. If the second feature is a smooth number like temperature instead of a yes/no flag, you don't get two ramps — you get a whole sliding fan of ramps, one steepness per temperature. If is zero we're back to boring parallel ramps; if it's negative the weekend ramp is shallower. One warning from the real world: when both inputs are big, the product looks a lot like the inputs themselves, which confuses the fit, so we usually center the features (shift them to sit around zero) before multiplying. That single multiply-two-things-together trick is the entire idea of an interaction term.

Recall Quick self-check

What is the effective slope of once the interaction is added? ::: — it now depends on . Why a product and not a sum for interactions? ::: A product is zero when either input is zero and grows with both together, so it acts like a switch dimmer; a sum can only shift, never toggle a slope. What does mean geometrically? ::: The twist disappears; the slices go parallel; you recover the plain flat-sheet linear model. What changes when is continuous instead of 0/1? ::: You get a smooth fan of lines — one slope per value of — instead of just two lines. Why do we usually center features before making an interaction? ::: To reduce multicollinearity between the product and its parent features, stabilising and clarifying the fitted weights.

See also: Linear Regression (the flat sheet), Overfitting and Underfitting (why not add every product), Regularization (L1, L2) and Feature Selection Methods (taming too many interactions), Feature Scaling and Normalization (centering before interacting), Exploratory Data Analysis (spotting where a twist is needed), Decision Trees and Random Forests (models that find interactions automatically), Cross-Validation (checking a new interaction actually helps).