0Pricing
NLP Academy · Lesson

Choosing the Right N-Gram Range

Balance signal against feature blow-up.

Choosing the Right N-Gram Range is a free NLP Academy lesson on CoddyKit — lesson 4 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.

A Real Trade-Off

Picking an n-gram range is a balancing act. More context helps the model, but it can flood you with features and noise.

The Cost of Going Big

Each step up in n can multiply your vocabulary. This feature blow-up means slower training and a hungrier memory footprint.

Rare Grams Add Noise

Long n-grams often appear once and never again. These rare features rarely generalize and can nudge the model toward overfitting.

Start Small and Simple

A great first move is (1, 2): unigrams plus bigrams. It catches negation and short phrases without exploding the feature count.

vec = CountVectorizer(ngram_range=(1, 2))

When Trigrams Earn Their Keep

Reach for (1, 3) only when fixed three-word phrases matter, like a product name. Otherwise the extra columns rarely pay off.

Prune With min_df

Set min_df so a gram must appear in several documents to count. This quietly removes the one-off phrases that only add noise.

vec = CountVectorizer(ngram_range=(1, 3), min_df=3)

Cap It With max_features

You can also hand the vectorizer a budget. max_features keeps only the top terms by frequency, holding the matrix to a fixed width.

vec = CountVectorizer(ngram_range=(1, 2), max_features=10000)

Let Data Decide

Do not guess forever. Try a few ranges and compare validation scores. The data will tell you where the gains flatten out.

Tune It Automatically

Drop ngram_range into a grid search and let scikit-learn test ranges for you, picking the option with the best cross-validated score.

params = {"vec__ngram_range": [(1, 1), (1, 2), (1, 3)]}

Watch for Overfitting

If training accuracy soars but validation lags, your range may be too wide. Shrinking n often generalizes better on new text.

A Sensible Default

For most text tasks, ngram_range (1, 2) with a small min_df is a strong, fast baseline. Reach higher only when evidence demands it.

Quick Check

Your trigram model overfits and trains slowly. Which single change most directly fights the feature blow-up?

Recap: Choosing Your Range

Start at (1, 2), prune with min_df, cap with max_features, and let validation pick the winner. You can now model context with n-grams. 🚀

Frequently asked questions

Is the “Choosing the Right N-Gram Range” lesson free?

Yes — the full text of “Choosing the Right N-Gram Range” 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 “Choosing the Right N-Gram Range”?

Balance signal against feature blow-up. 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 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Choosing the Right N-Gram Range” 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

  1. Why Single Words Lose Meaning
  2. Bigrams and Trigrams Explained
  3. N-Gram Features in scikit-learn
  4. Choosing the Right N-Gram Range
← Back to NLP Academy