N-Gram Features in scikit-learn
Add phrases to your vectorizer.
N-Gram Features in scikit-learn is a free NLP Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the NLP Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Let the Library Do It
You could hand-roll n-grams, but scikit-learn builds them for you. The trick is one small argument on your vectorizer.
The ngram_range Knob
Every text vectorizer accepts ngram_range, a tuple of min and max sizes. It tells the vectorizer which n-grams to count.
from sklearn.feature_extraction.text import CountVectorizer
vec = CountVectorizer(ngram_range=(1, 2))Unigrams Are the Default
Leave it alone and ngram_range is (1, 1), pure single words. That is the plain bag-of-words you already know.
Adding Bigrams
Set ngram_range to (1, 2) and the vectorizer keeps single words and every adjacent pair, all in one feature space.
vec = CountVectorizer(ngram_range=(1, 2))
X = vec.fit_transform(["this is not good"])Bigrams Only
Want pairs alone? Use (2, 2). Now single words vanish and only bigrams survive as features.
vec = CountVectorizer(ngram_range=(2, 2))Inspect the Vocabulary
After fitting, peek at the learned features with get_feature_names_out. You will see both words and joined phrases listed.
print(vec.get_feature_names_out())
# ['is not', 'not good', 'this is']Phrases Joined by Space
scikit-learn names each bigram by joining its words with a single space, like not good. That string becomes one column.
Same Knob, TF-IDF
The same ngram_range works on TfidfVectorizer too. You get n-gram phrases that are also weighted by how distinctive they are.
from sklearn.feature_extraction.text import TfidfVectorizer
vec = TfidfVectorizer(ngram_range=(1, 2))Now Negation Survives
With bigrams on, not good lands in its own column. Your classifier can finally learn that this pair signals a negative review.
Watch the Feature Count
Adding bigrams can multiply your columns dramatically. The matrix stays sparse, but the vocabulary grows fast.
print(X.shape) # many more columns than unigrams alonePair It With min_df
To tame the blow-up, combine ngram_range with min_df. Dropping rare phrases keeps only the n-grams that repeat usefully.
vec = CountVectorizer(ngram_range=(1, 2), min_df=2)Quick Check
Which ngram_range gives you single words plus adjacent pairs in one vectorizer?
Recap: N-Grams in scikit-learn
One ngram_range tuple turns any vectorizer into an n-gram machine. Inspect features, then trim with min_df. Next you will choose the right range. 🎯
Frequently asked questions
Is the “N-Gram Features in scikit-learn” lesson free?
Yes — the full text of “N-Gram Features in scikit-learn” is free to read here on the web, and the NLP Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the NLP Academy course, upgrade to CoddyKit PRO.
What will I learn in “N-Gram Features in scikit-learn”?
Add phrases to your vectorizer. You practise NLP Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start NLP Academy?
No prior experience is required. NLP Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “N-Gram Features in scikit-learn” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this NLP Academy lesson?
Yes. Every NLP Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Why Single Words Lose Meaning
- Bigrams and Trigrams Explained
- N-Gram Features in scikit-learn
- Choosing the Right N-Gram Range