Bag-of-n-grams represents a document with counts of contiguous token sequences: pairs are bigrams, triplets are trigrams, and larger values follow the same pattern. Unlike single-token counts, these features preserve local word combinations while still discarding the document’s broader word order. section 3.0
Choose the sequence length through the ngram_range hyperparameter. Combining 1-grams and 2-grams creates a vector containing individual tokens alongside phrases, often capturing a more useful match than either representation alone. section 3.4
Capturing “algorithmic bias”
With CountVectorizer(ngram_range=(1, 2)), a small corpus becomes a 16-by-616 sparse matrix. Inspecting the feature named “algorithmic bias” shows nonzero counts in documents containing that phrase, while cosine similarity can rank a matching document for the query “What is algorithmic bias?” section 3.4
>>> ngram_vectorizer = CountVectorizer(ngram_range=(1, 2))
>>> ngram_vectors = ngram_vectorizer.fit_transform(corpus)
>>> ngram_vectors
<16x616 sparse matrix of type '<class 'numpy.int64'>'
with 772 stored elements in Compressed Sparse Row format>
Inspect the runnable CountVectorizer configuration and the resulting sparse n-gram matrix. section 3.4
The trade-off is dimensionality: unique n-grams outnumber unique tokens, and large corpora can produce exponentially more features. Use the approach when phrase-level evidence matters, while recognizing its curse-of-dimensionality limits. section 3.4
