TL;DR
A linear model adds up the effect of each input on its own, so it cannot by itself say that two inputs matter only when they occur together. A cross-product transformation fixes this by creating one extra input for each pair of inputs. That new input is 1 only when both original inputs are on, and 0 otherwise. Feeding these crossed inputs to a model such as Logistic Regression lets it learn a separate weight for the combination, which is how large recommendation and advertising systems have handled pairs like "this user" and "this app".
Builds on: Logistic Regression, One-Hot Encoding, Interaction Effect · Leads to: Wide & Deep learning, Embedding Layer
This lesson is about machine learning methods. It is not advice for any particular decision.
Where it's used
App recommendations. In the 2016 Wide & Deep paper, a team at Google built a recommender for the Google Play store. The "wide" part of the model is a linear model over many sparse yes-or-no features, and it uses cross-product transformations to capture pairs. The paper's example is a feature that is 1 only when the user has already installed one app and the app being shown is another. A linear model over single features cannot represent that pairing, but the crossed feature can carry a weight of its own. The paper pairs this with a neural "deep" part that generalises to pairs it has never seen.
Click prediction in advertising. Ad systems predict whether a user will click an ad from features such as the user's country, the device, the publisher site and the ad's category. Many useful signals are pairs: a particular category of ad may do well on phones but poorly on desktops. Engineers at large ad companies have described training logistic models on huge numbers of crossed categorical features, since a linear model is cheap enough to train and serve over very large data.
How it works
Take two yes-or-no inputs. means the impression happened in the morning, and means the user is on a phone. Both are indicators, and each is written as a one-hot input. The cross of and is a third input, . For yes-or-no inputs, the product is the same as "both are on".
Use a logistic model with bias , weight on , weight on , and weight on the cross . The score is , and the predicted probability is , where is the sigmoid, which turns any score into a value between 0 and 1.
- Start with no inputs on. and , so the cross is . The score is . The probability is . Check: only the bias is counting.
- Turn on the morning input only. , , . The score rises by 0.4 to , and . Check: one input moved the score by its own weight, and nothing else changed.
- Turn on the phone input only. , , . The score falls by 0.2 to , and . Check: this weight is negative, so the phone lowers the score here.
- Turn on both, without the cross. Add the two single effects: , and . Check: this is what a model with no cross would predict for both inputs on.
- Turn on both, with the cross. Now , so the extra is added: , and . Check: the jump from step 4 is exactly the cross weight, and it appears only in this case.
- Read the result. The cross raised the probability from about 0.31 to about 0.67 for the pair, but left the three other cases alone. That is the whole idea: one extra input whose only job is to switch on when the pair is present.
- p = sigmoid(z)
flowchart LR A["Morning M (0 or 1)"] --> C["Score z = b + wM M + wP P + v C"] B["Phone P (0 or 1)"] --> C A --> D["Cross C = M times P"] B --> D D --> C C --> E["Sigmoid gives probability"]
The math
Given two inputs and taking values in finite sets, a cross-product transformation creates one new input for every pair of values . The new input is when and , and otherwise. For yes-or-no inputs this reduces to .
Read the definition in words. If can be one of 3 values and one of 4, there are crossed inputs, and exactly one of them is 1 for any example. This is the same one-hot idea as in One-Hot Encoding, applied to pairs instead of single values.
Step 1 in symbols: the cross of two indicators
For , the cross is . Its truth table is:
Step 2 in symbols: the score with a cross
The score is additive over the inputs, with the cross as one more input:
Here is the bias, and are the single-input weights, and is the weight on the cross. With the score for the four cases is , , , and . The first three match a model with no cross. Only the last one carries , which is why the cross behaves like an interaction.
Step 3 in symbols: learning the cross weight
Training is a gradient descent step on the log-loss, as for any logistic model. For one example with label and predicted probability , the gradient with respect to is . Since is 0 for most examples, the cross weight is learned only from examples where both inputs are on, which is why sparse crosses need a lot of data.
Step 4 in symbols: the logit view
Write the Log-Odds for the four cases with . The effect of turning on when is , and the effect of turning on when is . The difference between these two effects is . This is the interaction term in log-odds form.
Worked examples
Use the model from the walkthrough: , , , .
- Case : , so . Then .
- Case : , so . Then .
- Case : , so . Then .
- Case : , so . Then .
- Interpretation: without the cross, case 4 would have and probability . The cross adds to the score only when both inputs are on, and it moves that case from 0.31 to 0.67.
Suppose the observed click rates are , , and , where the first digit is and the second is . We want a logistic model with a cross that reproduces these rates.
- Convert each rate to log-odds. .
- .
- .
- The bias is the log-odds of the all-off case: .
- The morning weight is the change from to : .
- The phone weight is the change from to : . The phone alone does not change the click rate.
- The cross weight is .
- Check the fourth cell: , and . This matches .
- Interpretation: the table cannot be reproduced by single-input weights alone. The cross of is the part that says the two inputs together push clicks up more than either does alone.
An ad system has 20 device types and 500 ad categories. Crossing them gives possible cross inputs, and each impression switches on only one of them. Take one impression with these active inputs: a bias of , device "phone" with weight , country "US" with weight , and the cross of phone with the category "games" with weight .
- Add the active weights: .
- Apply the sigmoid: .
- Interpretation: a 23 percent predicted click probability. The cross of phone and games contributes , which is the largest feature weight in the sum, so the model is saying that games on phones are a strong pairing.
- Now the same impression with the category "news" instead. The cross is not active, and no other inputs change.
- Try it: with the category "news", what is the score and the probability ?
Answer: , and . The probability falls from about 0.23 to about 0.11, and the whole drop comes from the cross weight of no longer applying.
Common mistakes and tradeoffs
It is tempting to think that crossing features always improves a model. In fact, a cross adds a weight for every pair that appears in the data, and most pairs are rare or never seen. A rare pair gets a weight estimated from very few examples, so it can be noisy. This is why large systems often keep crosses only for pairs with enough data, or regularise them with an L2 penalty.
It is tempting to think that the cross replaces the single-input features. In fact, the single inputs must stay in the model. The cross measures only the extra effect of the pair, on top of each input's own effect. Without the single inputs, the model cannot represent the common case where one input alone matters.
It is tempting to think that a cross of two numbers means something like adding them. In fact, for yes-or-no inputs the cross is an AND, and for real-valued inputs it is a product. A product of two numbers is a different thing from either input, so its scale and sign need care.
When to use it. Use crosses when the inputs are categorical, the model is linear, the number of examples is large, and you have a clear reason to think certain pairs matter together. Crosses are a cheap and interpretable way to capture interactions.
When not to use it. Avoid crosses when data is small, when many inputs might interact in complex ways, or when most pairs are never observed. A linear model with a huge number of crosses can overfit and become slow to store.
Alternatives. An embedding inside a neural network can learn pair effects without listing every pair by hand, and it can generalise to pairs it never saw. Factorisation-style models, which learn a short vector for each input and combine vectors to score pairs, need fewer weights than a full cross table.
| Approach | Learns pair effects | Needs data for each pair | Interpretable |
|---|---|---|---|
| Single inputs only | No | No | Yes |
| Explicit cross | Yes, one weight per pair | Yes | Yes |
| Neural network with embeddings | Yes, through shared structure | Less | Less |
History
Before modern machine learning, statisticians captured interactions in regression by adding product terms, which is the same idea as the cross applied to numeric inputs. The approach was standard in applied statistics for decades.
For very large categorical data, the problem was the number of possible pairs. Models in the early 2010s for click prediction used logistic regression over one-hot inputs, and engineers at large advertising companies described explicitly crossing categorical features to capture pairs that single features missed. The approach was practical because the linear model could be trained and served cheaply on very large data.
The 2016 Wide & Deep paper combined this with a neural network. Its linear "wide" part kept explicit cross-product features, while its "deep" part learned from embeddings. The combination showed that memorising known pairs and generalising to new ones could be done in one model, and the idea spread through recommendation and ranking systems.
Recap
- A linear model adds each input's effect, so it cannot capture an effect that exists only when two inputs are both on.
- A cross-product input is 1 only when a specific pair of values occurs, and 0 otherwise.
- Adding a cross to a logistic model gives the pair its own weight, which is an interaction in log-odds, as in Interaction Effect.
- The cross needs data for each pair, and the single inputs must stay in the model.
- Read next: Wide & Deep learning for the combined wide and deep model, and Embedding Layer for the neural alternative.