Hashing Vectorizer
To turn documents into count vectors the usual way, you first have to build a dictionary: read the whole corpus, collect every word, and give each one a column number. A hashing vectorizer throws the dictionary away and computes the column number from the word itself.
How the trick works
Each token goes through a hash function and the result is reduced modulo a fixed number of columns — a table of about a quarter of a million is a common default. The word "trainers" lands in the same slot every time, without anyone recording where that slot is. Three things follow immediately:
- There is no vocabulary to fit, so the transform is stateless and a document can be vectorised the moment it arrives.
- There is no out-of-vocabulary case — a word never seen before simply hashes somewhere.
- Memory is fixed by the number of columns you chose, not by how much text you will ever process.
What you give up
Two unrelated words can land in the same column and have their counts added together. With a large table those collisions are rare enough to leave classifier accuracy almost untouched, and a signed variant lets colliding contributions cancel rather than accumulate. The real loss is interpretability: the mapping runs one way only, so you cannot ask which word a feature stands for. IDF weighting needs a separate pass too, since document frequencies are not known in advance.
Why it matters
This is the representation for streaming and out-of-core text — a spam filter learning from a live feed of messages, where the vocabulary never stops growing. HashingVectorizer in scikit-learn pairs with an online classifier for exactly that, trading a sliver of accuracy for a pipeline that never has to see the corpus twice.
A hashing vectorizer converts text into a fixed-width feature vector by hashing each token to a column index instead of looking it up in a learned vocabulary. Nothing is fitted, so documents can be vectorised in a single streaming pass, with bounded memory and no out-of-vocabulary case. The cost is collisions, which merge the counts of unrelated words, and a one-way mapping that makes individual features impossible to interpret.
Think of a cloakroom with a fixed number of pegs and no list of who left what. Instead of writing anything down, the attendant works out the peg from the name itself, following the same rule every time.
A hashing vectorizer does that with words. Software needs words turned into numbers, and the usual method keeps a long register of every word it has ever met. This one calculates the slot straight from the word, so it can handle text as it arrives and never trips over a word it has not seen before. Now and then two words share a slot, and their tallies get mixed up together — that is the price of skipping the register.