Inferensys

Glossary

Feature Hashing

A dimensionality reduction technique that maps high-cardinality categorical features to a fixed-size vector using a hash function, trading potential collisions for a bounded memory footprint without maintaining a vocabulary.
Data engineer managing feature store on laptop, feature definitions visible, casual data engineering session.
DIMENSIONALITY REDUCTION

What is Feature Hashing?

A technique for mapping high-cardinality categorical variables to a fixed-size vector space without maintaining a vocabulary lookup table.

Feature hashing, also known as the hashing trick, is a dimensionality reduction technique that maps arbitrary categorical features to a fixed-size vector using a hash function. It eliminates the need for a vocabulary dictionary by deterministically assigning each feature value to a column index, trading potential hash collisions for a bounded, predictable memory footprint during both training and inference.

In streaming or online learning environments where new categories appear continuously, feature hashing prevents the memory blowup associated with one-hot encoding. The MurmurHash or xxHash functions are commonly used for their speed and uniform distribution properties. While collisions introduce a small amount of noise, signed hash functions can mitigate this by subtracting counts for negative assignments, preserving the expected dot product between vectors.

DIMENSIONALITY REDUCTION

Key Characteristics of Feature Hashing

Feature hashing, also known as the hashing trick, is a fast and space-efficient method for vectorizing high-cardinality categorical features without maintaining a vocabulary. It maps an arbitrary number of features to a fixed-size vector using a hash function, trading potential collisions for a bounded memory footprint.

01

Vocabulary-Free Vectorization

Unlike one-hot encoding, feature hashing does not require a pre-built vocabulary or dictionary mapping string values to indices. The hash function computes the index directly from the raw feature value. This eliminates the memory overhead of storing a vocabulary and the engineering complexity of handling out-of-vocabulary (OOV) inputs during inference. Any new categorical value encountered in production is immediately mapped to a bucket without model updates.

02

Dimensionality Control via Modulo

The output dimensionality is fixed and user-defined by specifying the number of hash buckets (e.g., 2^18). The hash function maps an input to an integer, and the modulo operation (hash(x) % D) constrains it to the pre-allocated vector size. This guarantees a strict, predictable memory budget regardless of the cardinality of the input feature, making it ideal for streaming and online learning environments where feature cardinality is unbounded.

03

Collision Handling

Multiple distinct input features can map to the same hash bucket, creating a collision. This is the primary trade-off of feature hashing. The signed hash trick mitigates this by using a second hash function to determine the sign (+1 or -1) of the feature value, allowing collisions to partially cancel out in linear models. While collisions introduce noise, the impact is often negligible in high-dimensional settings with sparse features.

04

Stateless and Parallelizable

The transformation is stateless—each feature is hashed independently without requiring global aggregation or coordination. This makes feature hashing embarrassingly parallelizable across distributed computing frameworks like Apache Spark. There is no need to synchronize a shared vocabulary across worker nodes, significantly simplifying distributed feature engineering pipelines.

05

Common Hash Functions

The MurmurHash3 algorithm is the most widely used non-cryptographic hash function for feature hashing due to its excellent distribution properties and speed. In practice, many implementations use a 32-bit or 64-bit variant. The signed hash trick often employs a second hash function, such as the Jenkins hash, to determine the sign bit, ensuring that collisions are not purely additive.

06

Applications in Text and Categorical Data

Feature hashing is a cornerstone of the Vowpal Wabbit online learning system and is widely used in:

  • Text classification: Hashing n-gram features to avoid storing a massive term dictionary.
  • Ad click prediction: Handling high-cardinality categorical features like ad IDs and user agents.
  • Email spam filtering: Efficiently representing tokenized email content in a fixed-size feature space.
FEATURE HASHING EXPLAINED

Frequently Asked Questions

Clear, technical answers to the most common questions about the hashing trick for high-cardinality categorical data in machine learning pipelines.

Feature hashing, also known as the hashing trick, is a dimensionality reduction technique that maps an arbitrary number of categorical features or tokens into a fixed-size vector of dimension D using a deterministic hash function. The mechanism works by applying a hash function h(x) → {1, 2, ..., D} to each feature name-value pair (e.g., city=London), computing the modulo of the hash with the target dimension, and incrementing the corresponding index in the output vector. A signed hash function is often employed, where a second hash determines the sign (+1 or -1) to mitigate collision bias through cancellation. This eliminates the need to maintain a vocabulary mapping in memory, making it ideal for streaming and online learning scenarios where the full feature space is unknown a priori. The trade-off is the introduction of hash collisions, where distinct features map to the same index, introducing a controlled amount of noise proportional to the inverse of the output dimension.

Prasad Kumkar

About the author

Prasad Kumkar

CEO & MD, Inference Systems

Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.

His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.