HashingVectorizer<T>

Models & Types · AiDotNet.Preprocessing.TextVectorizers

Converts text documents to a fixed-size hash-based feature matrix.

Inheritance: TextVectorizerBase<T> → HashingVectorizer<T>

For Beginners

HashingVectorizer is like CountVectorizer but:

  • Doesn’t need to see all data first (no vocabulary to learn)
  • Uses fixed memory regardless of vocabulary size
  • Slight accuracy loss from hash collisions
  • Great for very large or streaming text data

How It Works

HashingVectorizer uses the hashing trick to map tokens to a fixed number of features. Unlike CountVectorizer and TfidfVectorizer, it doesn’t require storing a vocabulary, making it memory-efficient for very large datasets or streaming applications.

The trade-off is that hash collisions can occur (different tokens map to same feature), and you cannot retrieve original tokens from the hash indices.

Constructors

Constructor Summary
HashingVectorizer(int nFeatures, ValueTuple<int, int>? nGramRange, bool lowercase, bool binary, bool alternateSign, HashingNorm norm, Func<string, IEnumerable<string>> tokenizer, HashSet<string> stopWords, ITokenizer advancedTokenizer) Creates a new instance of HashingVectorizer.

Properties

Property Summary
bool IsFitted { get; }
int FeatureCount { get; }
int NFeatures { get; } Gets the number of output features (hash buckets).

Methods

Method Summary
Matrix<T> FitTransform(IEnumerable<string> documents)
Matrix<T> Transform(IEnumerable<string> documents) Transforms documents to hash-based feature vectors.
string[] GetFeatureNamesOut()
void Fit(IEnumerable<string> documents) Fits the vectorizer (no-op for HashingVectorizer - fitting is not required).