The Hashing Trick

easy
Checkpoint challenge
Solve this to show you know the material and jump straight to Unit 5. Everything before it counts as cleared.

With millions of possible tokens or IDs, storing a vocabulary is expensive. The hashing trick maps each feature straight to one of n_features columns with a hash, with no vocabulary needed. A second hash picks a sign (+1 or −1), so collisions cancel out on average instead of piling up.

Use h = int(hashlib.md5(token.encode()).hexdigest(), 16):

  • column = h % n_features
  • sign = +1 if (h >> 64) % 2 == 0 else -1

Implement hash_vectorize(docs, n_features=2**10): tokenise each doc by lowercasing and splitting on whitespace, and add sign to X[i, column] for every token occurrence. Return a float array. Then implement collision_rate(tokens, n_features): the fraction of distinct tokens that share a column with at least one other distinct token.

#features#nlp#hashing2 visible + 1 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
The Hashing Trick · TechGradify