Hashing Trick

Last Updated: July 29, 2026 | By Mihail Sebastian | AI Dictionary

A technique that maps features into a fixed-length vector with a hash function, trading occasional collisions for bounded memory and no stored vocabulary.

What is the Hashing Trick?

The hashing trick maps categorical or text features into a fixed-length vector by running each value through a hash function and using the result as its column index. Memory stays bounded no matter how many distinct values appear, at the cost of occasional collisions where two features share an index.

Unlike one-hot encoding, it needs no dictionary of known categories, so it handles vocabularies too large to store and values never seen in training.

How the Hashing Trick Works

Pick a vector size, say 2^18 (262,144) slots. For each feature – a word, a ZIP code, a user ID – compute its hash modulo 262,144 and increment that slot. Encoding is a single function call: no lookup table to build, store, or update.

Collisions are the price. Two unrelated features that hash to the same slot become indistinguishable to the model. The vector size sets the trade-off: more slots mean fewer collisions but more memory; fewer slots mean the reverse. In practice, models tolerate a modest collision rate with little accuracy loss, which is what makes the trick viable.

Example of the Hashing Trick

A spam filter classifies emails by the words they contain. The vocabulary is effectively unbounded: new words, misspellings, and obfuscations like “v1agra” arrive daily, so a fixed dictionary is stale on arrival.

With the hashing trick, every word in an incoming email is hashed straight into a 262,144-slot vector, unknown words included. The filter never maintains a vocabulary, and a spammer’s novel misspelling still lands in a slot the model can learn from.

Related AI terms: One-Hot Encoding · Categorical Encoding · Feature Engineering · Dimensionality Reduction

Did you like the Hashing Trick gist?

Learn about 250+ need-to-know artificial intelligence terms in the AI Dictionary.

Mihail Sebastian — Writes about AI governance, regulation, and the technology behind them. Placeholder bio — replace with a real credential line. About

Read the Governor's Letter

Stay ahead with Governor's Letter, the newsletter delivering expert insights, AI updates, and curated knowledge directly to your inbox.

By subscribing to the Governor's Letter, you consent to receive emails from AI Guv.
We respect your privacy - read our Privacy Policy to learn how we protect your information.

Browse All AI Terms A–Z

Every term in the dictionary, in alphabetical order. Jump to a letter or scroll the full list.

A

B

C

D

E

F

G

H

I

J

K

L

M

N

O

P

Q

R

S

T

U

V

W

X

Y

Z