Feature hashing is a technique that turns features into a vector by applying a hash function to each feature and using the hash value as its index directly, without looking it up in a stored table. It is also known as the hashing trick, and it is widely used to keep machine learning inputs fast and memory efficient.
This page is for data scientists, ML engineers and technology leaders. It covers how feature hashing works, its trade-offs, how it differs from a vocabulary approach and what to settle before you use it.
The scikit-learn feature extraction guide describes feature hashing, also called the hashing trick, as a technique behind a "high-speed, low-memory vectorizer". Instead of building a hash table of the features seen in training, a hash function is applied to each feature to find its column index in the sample matrix directly.
The idea was studied in the paper Feature Hashing for Large Scale Multitask Learning by Weinberger, Dasgupta, Attenberg, Langford and Smola. Its abstract reports empirical evidence that hashing works well for dimensionality reduction.
Each raw feature, such as a word, a category or a field name and value, is passed through a hash function. A modulo turns the hash value into a column index in a vector of fixed length. The value at that index is then added to, so repeated features in one sample are summed.
Two details from the scikit-learn guide matter in practice. First, because a simple modulo is used, a power of two is advised for the vector length (the n_features parameter), otherwise features are not mapped evenly to columns. Second, collisions between unrelated features are possible, so a signed hash is used: the sign of the hash value sets the sign of the stored value, which means collisions are likely to cancel out rather than accumulate error.
| Aspect | What the scikit-learn guide says |
|---|---|
| Speed and memory | Increased speed and reduced memory use, because no vocabulary is built or stored |
| State | The hasher is stateless, so there is nothing to fit or pickle |
| Streaming | The feature space is fixed, so data can be vectorised in mini-batches and memory stays bounded by the batch size |
| Inspectability | Lost: there is no inverse_transform, and the original feature names cannot be recovered from the one-way hash |
| Collisions | Reduced by a signed hash and a larger n_features, but not removed |
A vocabulary-based vectoriser such as CountVectorizer learns which features exist and stores them. Feature hashing skips that step.
| Question | Vocabulary-based vectoriser | Feature hashing |
|---|---|---|
| How is a column chosen? | Looked up in a stored vocabulary | Computed by a hash function and a modulo |
| Memory as data grows | Grows with the number of distinct features | Fixed by the chosen vector length |
| Needs a first pass over the data? | Yes, to build the vocabulary | No, so it suits online and out-of-core use |
| Can two features share a column? | No | Yes, a collision |
Agents that read streams of text, events or records need inputs of a stable size. Hashing gives that without a vocabulary that must be shared and kept in step across services. The cost is traceability: once a feature is hashed you cannot read its name back, so the feature definitions, the hash function and the vector length must be recorded beside the model. For AI agent and machine learning work, see ElixirClaw, the AI agents computing platform.
To map one decision your agents make, and the data and policy it depends on, book a Context Workshop. See what happens in the workshop.
It is a way to turn features into a numeric vector by applying a hash function to each feature and using the result as its position, so no list of known features has to be stored.
The hashing trick is another name for feature hashing. The scikit-learn guide uses both terms for the method that computes a feature column index directly with a hash function.
A collision happens when two different features map to the same column. A signed hash makes collisions likely to cancel out rather than add up, and a larger vector length makes them less frequent.
No. The hash function is one way, so scikit-learn offers no inverse_transform for its hasher. Keep a record of the raw features if you need to explain a result.