Machine learning with football · Chapter 6 · Categorical data

Vocabulary and one-hot encoding

A vocabulary, a vector with a single 1, and a slot for clubs nobody has seen. The standard way to encode a category.

Estimated time: 20 minutes · page 2 of 5

A vocabulary, then one-hot

Start with a vocabulary, the list of every value the feature can take. For the home club that's the 20 clubs of Premier League, sorted A to Z, and each gets an index. The board shows it.

The index alone is a trap. Feed in Arsenal as 1 and Wolves as 20 and the model reads Wolves as twenty times as much of something, an order and a size the clubs don't have. Instead, each club becomes a one-hot vector as long as the vocabulary, with a 1 in its own slot and 0 everywhere else. The model then learns one weight per club, which you can read as that club's strength at home.

The vector's length is its dimension. Nearly all of it is zeros, which makes it a sparse feature, so programs store just the position of the 1 instead of the full list. When several slots can be 1 at once, such as the positions a player can play, it's multi-hot.

Clubs nobody has seen

A model trained on this season's clubs will meet a promoted side next season. Give the vocabulary one extra slot, out of vocabulary (OOV), for any value it doesn't know. Rare values can go there on purpose too, like clubs that played only once. Press A club from another league on the board and watch where the 1 lands.

Check yourself

True or false: a model can train directly on the strings "Arsenal FC" and "Chelsea FC".
Arsenal gets index 1 and Wolves index 20, and you feed those numbers in as they are. What goes wrong?
With 20 clubs plus an out-of-vocabulary slot, how long is each club's one-hot vector?

Leagues