N-gram Language Features: Preserve Local Word Order
Learn what n-grams capture, how to set their range, and how to balance context and feature growth.
Published Updated
An n-gram is a contiguous sequence of N tokens: unigram (1), bigram (2), trigram (3). Bag-of-Words ignores position; n-grams retain local order, not the whole sentence. CountVectorizer defaults to (1,1); (1,2) adds bigrams. Its documented is this example distinguishes a question from a sentence with the same word set. Wider ranges create more, often rarer features and can increase sparsity and cost. Compare ranges on the same validation split, fit vocabulary only on training data, and keep tokenizer, range, and vocabulary consistent at inference.