XenonStack Glossary: AI, Data and Cloud Terms Explained

Feature Hashing: The Hashing Trick Explained

Written by Dr. Jagreet Kaur | 15 Jul 2024

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.

What is feature hashing?

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.

How does the hashing trick work?

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.

What are the benefits and limits of feature hashing?

AspectWhat the scikit-learn guide says
Speed and memoryIncreased speed and reduced memory use, because no vocabulary is built or stored
StateThe hasher is stateless, so there is nothing to fit or pickle
StreamingThe feature space is fixed, so data can be vectorised in mini-batches and memory stays bounded by the batch size
InspectabilityLost: there is no inverse_transform, and the original feature names cannot be recovered from the one-way hash
CollisionsReduced by a signed hash and a larger n_features, but not removed

How does feature hashing differ from a vocabulary-based vectoriser?

A vocabulary-based vectoriser such as CountVectorizer learns which features exist and stores them. Feature hashing skips that step.

QuestionVocabulary-based vectoriserFeature hashing
How is a column chosen?Looked up in a stored vocabularyComputed by a hash function and a modulo
Memory as data growsGrows with the number of distinct featuresFixed by the chosen vector length
Needs a first pass over the data?Yes, to build the vocabularyNo, so it suits online and out-of-core use
Can two features share a column?NoYes, a collision

How does feature hashing relate to AI agents?

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.

What should you do first?

  1. Pick the hash function and vector length, and write both down with the model version.
  2. Choose a power of two for n_features and test a smaller and a larger value against your accuracy metric.
  3. Keep the signed hash on for small tables, as the scikit-learn guide advises.
  4. Log the raw feature names for a sample of records, because the hash cannot be reversed.

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.

Frequently asked questions

What is feature hashing in simple terms?

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.

What is the hashing trick?

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.

What is a hash collision in feature hashing?

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.

Can you get the original features back from a hashed vector?

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.