liveBook · Manning

One-hot encoding

As explained in Build AI Drug Discovery Pipelines

One-hot encoding represents each categorical token as a sparse vector with one active coordinate and all other coordinates inactive.

One-hot encoding turns a categorical value into a vector with one active coordinate and zeros everywhere else. Each coordinate represents a category, so the encoding distinguishes categories without assigning them an artificial order or similarity. section 10.2.3 section 11.2.2

Encoding atom symbols

For an illustrative vocabulary [C, N, O], carbon is [1, 0, 0] and oxygen is [0, 0, 1]. A molecular graph can use a larger vocabulary, including an Unknown category for rare elements, to represent each atom’s symbol. section 11.2.2

An atom feature vector can combine several one-hot encodings: element, degree, attached hydrogen count, and implicit valence. Degree is the number of directly bonded non-hydrogen atoms. Adding an aromaticity flag produces a 78-dimensional vector with multiple active entries overall, even though each categorical group is one-hot. The graph conversion then normalizes each atom’s feature vector by its sum. section 11.2.2

Listing 11.2 Generate 78-dimensional Feature Vector for a Single Atom
def atom_features(atom):
    features = []
    features += one_of_k_encoding_unk(
        atom.GetSymbol(), ATOM_SYMBOLS
    )
    features += one_of_k_encoding_unk(
        atom.GetDegree(), [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    )
    features += one_of_k_encoding_unk(
        atom.GetTotalNumHs(), [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    )
    features += one_of_k_encoding_unk(
        atom.GetImplicitValence(), [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    )
    features += [int(atom.GetIsAromatic())]
 
    return np.array(features, dtype=np.float32)

See how the four categorical encodings and aromaticity flag are assembled for an atom. section 11.2.2

Protein features use the same idea for amino acid identity. Each residue’s one-hot encoding is concatenated with 12 biochemical features, including normalized continuous properties. These combined residue vectors are not normalized to sum to one. section 11.3.2

One-hot vectors do not express chemical similarity: every pair of distinct categories is equally distant. They can also become large and sparse as the vocabulary grows. A learned embedding instead maps token IDs to vectors whose dimension is a tunable choice; it can learn relationships among tokens. The efficiency advantage depends on the chosen embedding dimension and vocabulary size. section 10.2.3

Figure 10.3 Three-step process of converting molecular SMILES strings into dense vector representations for neural network processing. (1) Character-level tokenization breaks the input sequence into individual tokens, including special tokens like <sos> (start of sequence) and <eos> (end of sequence). (2) Vocabulary mapping converts each token to its corresponding integer ID using a learnable lookup table. (3) The embedding matrix (30 tokens × 128 dimensions) transforms these integer IDs into dense, learnable vector representations. The bottom flow diagram shows how this embedding layer fits into the overall autoencoder pipeline, taking a molecular input and converting the tokenized text to numerical vectors before passing to the encoder for compression. This approach enables the model to learn semantic relationships between chemically similar tokens during training, providing a more efficient and meaningful representation than sparse one-hot encoding.

Follow the alternative path from molecular SMILES tokens through integer IDs to learned embeddings in an autoencoder. section 10.2.3

How Build AI Drug Discovery Pipelines covers it

How deep it goes
Explains categorical vectors, their limitations, and feature assembly, while relying on a helper function for the encoding implementation.
Examples
Uses atom symbols, bonding degree, hydrogen counts, implicit valence, and amino acid identities as categorical features.
What you can do after
Evaluate/understand when categorical one-hot features are useful and when their sparsity and lack of similarity motivate learned embeddings.
How it connects
Connects one-hot encoding to embeddings, molecular graphs, graph neural networks, and biochemical feature normalization.

Also explained in

Compare how 10 books explain one-hot encoding →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build AI Drug Discovery Pipelines. The section links point to the parts of the book it draws on.