M2-066M2: Classical Machine LearningFeature EngineeringHard
⚡Flashcard Drill➔Mastery:
What is the Hashing Trick (Feature Hashing)? Pros and Cons
📐 Mathematical Definition
⚡ One-Sentence Core Answer
It projects arbitrary strings or categories into a fixed-size index space using hash functions, bounding memory and handling streaming vocabularies at the cost of irreversibility and collisions.
📌 Key Takeaways
- •Memory bounded: fixed feature dimension regardless of input vocabulary growth
- •Streaming friendly: processes out-of-vocabulary terms without updating dictionaries
- •Unbiased hash kernel: sign hashing ensures expected dot-product unbiasedness
📐 Mathematical Derivations
The Hashing Trick maps tokens to indices via . To make the inner product an unbiased estimator of the original dot product, a second independent hash function assigns signs: . Unbiasedness Proof: For two vectors , . When , ; when , and . Hence , with variance decreasing inversely with bucket count : .
🏭 Production Trade-offs
Engineering trade-offs: ① Extreme Scalability: Essential in large-scale click-through rate (CTR) prediction (e.g., Vowpal Wabbit, search advertising) with hundreds of millions of sparse tokens. ② Collision Overhead: As the number of unique features approaches , hash collisions inject noise, degrading model capacity. ③ Model Interpretability: Irreversible—weights associated with bucket cannot be mapped back to specific words or categorical IDs without separate auxiliary tracking.
⚠️ Common Interview Pitfalls
- ✕Setting the hash bucket size too small, causing severe collision noise and model underfitting
- ✕Omitting the sign hashing function , which leads to accumulated positive correlation bias under collisions
🎯 Interviewer Follow-up Inquiries
- ?How does the sign hash function guarantee that collisions do not introduce systematic inner-product bias?
- ?How do you inspect feature importance when using the feature hashing trick in production?
⚡
Test muscle memory on this topic via 3-Layer Flashcards
Active Recall: Mentally formulate Prompt ➔ Math Derivation ➔ Benchmark Implementation in 3 minutes.
📚
Associated Knowledge Base Guides & Mindmaps
Explore the comprehensive technical article, exam cards, and global architecture tree.
中文速览与面试要点
什么是特征哈希(hashing trick)?它的优缺点
用哈希函数把高基数特征映射到固定维度,省内存、支持流式;代价是哈希冲突。
核心要点
- 哈希技巧的做法:不维护'类别→索引'的字典,而是用哈希函数直接把类别名映射到 [0, m) 的索引(h(x)=hash(x) mod m)。 四个优点 :① 内存与存储 ——无需保存词表(高基数场景下词表可能占数百 MB),且特征维度固定为 m(可控);② 支持流式与在线学习 ——新类别自然映射到某维,无需重建词表(无'未知类别'问题);③ 训练-服务一致 ——只要哈希函数与 m 一致,离线与在线的映射自动相同,避免了词表版本不一致的 bug;④ 计算高效 ——哈希是 O(1) 且可并行。 缺点 : 哈希冲突 ——不同类别映射到同一维,导致特征值相加(语义混淆);冲突率约 1−e^−n/m(n 为不同类别数),故 m 应远大于 n(如 10–100 倍)。
- 实践要点:① 符号哈希(signed hashing) ——给每个类别额外哈希出一个 ±1 符号,冲突时值相减而非相加。这使冲突的期望贡献为 0(无偏),缓解了冲突的破坏性(类似随机投影的 Johnson-Lindenstrauss 性质);sklearn 的 HashingVectorizer(alternate_sign=True) 即此。② m 的选择 ——m 越大冲突越少但内存与计算越大;实践中取 2 的幂(便于位运算)且为 n 的 10–100 倍。③ 哈希 vs 词表 ——若能维护词表(离线批处理、类别集合固定),显式词表更好(无冲突、可解释、可查每个类别的权重);哈希适合 流式、极高基数、类别动态变化 的场景(如 URL、用户 ID、搜索 query)。④ 哈希 vs 嵌入 ——哈希是'无学习的固定映射',嵌入是'学习到的稠密表示';嵌入表达力更强(能捕捉类别相似性)但需要数据训练且需维护词表;实践中常见组合:先哈希降到可控维度,再学嵌入(如推荐系统对超高频 ID)。⑤ 可解释性损失 ——哈希后无法反查某维对应哪些类别,调试困难。
- 无需维护词表,适合在线学习
- 冲突可通过加大 m 与符号哈希缓解
常见陷阱
- m 太小导致严重哈希冲突
- 不用符号哈希(冲突时值相加引入偏差)