Machine learning with football · Chapter 6 · Categorical data

Feature crosses

Combine two categories into one, so the model can learn what each pair does together.

Estimated time: 15 minutes · page 4 of 5

Two categories as one

Cut the table into thirds: top, middle and bottom. Now every match has two categories, the home side's third and the away side's third. A model with one-hot features for each learns a home effect and an away effect and simply adds them up. That assumes a top home side gains the same against every visitor, whoever they are.

A feature cross combines the two into one feature with 3 × 3 = 9 values, one per pair: top against top, top against middle, and so on, each with its own weight. It does for categories what chapter 5's x² did for numbers. A linear model can now learn effects that depend on the combination.

The board shows all nine cells for Premier League. Each has the real home wins, what the additive model predicts, and what the crossed model predicts. Look for cells where the two disagree. That's where a combination does something the parts don't explain.

Check yourself

What does the feature cross let the model learn that the separate features cannot?
You cross home club (20 values) with away club (20 values). How many features?
Why does the crossed model look better on the board than in the second Python script?

The catch

A cross multiplies the number of values, so it gets sparse quickly. Thirds against thirds is 9 cells with a dozen matches each. Club against club would be 400 cells, and most pairs have met once at most. A cell with 0 home wins in 8 matches makes the model nearly certain, and one surprise then costs a lot of log loss.

On the board both models are scored on the matches they trained on, where more weights always look at least as good. Push λ up and the crossed model's predictions move toward the additive ones. The second Python script checks which one wins on matches neither model has seen.

Neural networks can find useful combinations on their own, at a higher computing cost. Chapter 8 shows how.

Terms on this page: Feature cross

Leagues