liveBook · Manning

Categorical encoding

As explained in Deep Learning with Python, Third Edition

A representation scheme where one position is 1 and all others are 0 to encode a discrete category or symbol.

Categorical encoding represents each discrete category with a vector containing one 1 at the category index and 0s elsewhere. It turns labels or token IDs into numeric features that neural-network layers can ingest. section 4.2.2 section 14.5.1

Encoding classification labels

For labels numbered from 0 through 45, create an array shaped (number_of_labels, 46), then set results[i, label] = 1.0 for each example. NumPy can implement this directly, while Keras provides to_categorical(train_labels). section 4.2.2

Use one-hot encoding with these practical distinctions:

  • Encode token ID sequences by giving every token a vocabulary-sized vector with one active position. section 14.5.1
  • Expect sparse, binary, high-dimensional features: a 30,000-word vocabulary produces vectors with 30,000 positions. section 14.5.2
  • Treat the representation as an independence assumption: distinct categories receive orthogonal vectors with no learned similarity. section 14.5.2
  • Match the label representation to the loss: categorical_crossentropy expects categorical labels, whereas integer labels use sparse_categorical_crossentropy. section 4.2.6

How Deep Learning with Python, Third Edition covers it

How deep it goes
Defines the representation, implements it with NumPy and Keras, and demonstrates its dimensional cost before deferring to word embeddings.
Examples
Uses 46-class label vectors, 30,000-word token vectors, NumPy and Keras implementations, and a bidirectional LSTM sequence model.
What it assumes
Assumes discrete labels or integer token IDs while teaching the vector representation and its model-input consequences.
What you can do after
Build one-hot labels or token inputs with NumPy or Keras and select a compatible categorical loss.
How it connects
Connects categorical labels with classification loss functions, tokenization with recurrent models, and word representations with word embeddings.

Also explained in

Compare how 13 books explain categorical encoding →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Deep Learning with Python, Third Edition. The section links point to the parts of the book it draws on.