Scegliere l'intervallo N-gram corretto
Bilanciare il segnale con l'esplosione delle feature
Scegliere l'intervallo N-gram corretto è una lezione NLP Academy gratuita su CoddyKit. Questa è la lezione 4 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento NLP Academy, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso NLP Academy include 4 lezioni in totale.
Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.
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. 🚀
Domande Frequenti
La lezione «Scegliere l'intervallo N-gram corretto» è gratuita?
Sì — il testo completo di «Scegliere l'intervallo N-gram corretto» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso NLP Academy, passa a CoddyKit PRO. Il corso NLP Academy include 4 lezioni in totale.
Cosa imparerò in «Scegliere l'intervallo N-gram corretto»?
Bilanciare il segnale con l'esplosione delle feature Eserciti NLP Academy con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.
Ho bisogno di esperienza per iniziare NLP Academy?
Non è richiesta alcuna esperienza precedente. NLP Academy su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 4 di 4.
Quanto tempo richiede la lezione «Scegliere l'intervallo N-gram corretto»?
La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.
Posso scrivere ed eseguire codice in questa lezione NLP Academy?
Sì. Ogni lezione NLP Academy include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.
Tutte le lezioni di questo corso
- Perché le singole parole perdono significato
- Bigrams e trigrams spiegati
- Feature N-gram in scikit-learn
- Scegliere l'intervallo N-gram corretto