特征哈希

term_id: feature_hashing

Category: basic_concepts

Definition

特征哈希,也称为哈希技巧(hashing trick),允许机器学习模型处理大型稀疏特征空间,而无需维护特征与索引之间的显式映射。通过应用哈希函数,模型可以直接将任意特征映射到固定的向量维度中,从而节省内存并简化特征工程流程。

Summary

一种利用哈希函数将高维稀疏特征映射到固定大小向量的技术。

Key Concepts

  • 哈希函数
  • 稀疏向量
  • 降维
  • 内存效率

Use Cases

  • 具有大型词汇表的文本分类
  • 拥有海量物品集的推荐系统
  • 实时流数据处理

Code Example

1
2
3
4
5
6
7
8
from sklearn.feature_extraction import FeatureHasher
import numpy as np

# Example: Hashing text features
hasher = FeatureHasher(n_features=10, input_type='string')
docs = ['hello world', 'hello python', 'world python']
hashed = hasher.transform(docs)
print(hashed.toarray())