0Pricing
NLP Academy · Lezione

Feature N-gram in scikit-learn

Aggiungere frasi al proprio vettorizzatore

Feature N-gram in scikit-learn è una lezione NLP Academy gratuita su CoddyKit. Questa è la lezione 3 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.

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 alone

Pair 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. 🎯

Domande Frequenti

La lezione «Feature N-gram in scikit-learn» è gratuita?

Sì — il testo completo di «Feature N-gram in scikit-learn» è 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 «Feature N-gram in scikit-learn»?

Aggiungere frasi al proprio vettorizzatore 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 3 di 4.

Quanto tempo richiede la lezione «Feature N-gram in scikit-learn»?

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

  1. Perché le singole parole perdono significato
  2. Bigrams e trigrams spiegati
  3. Feature N-gram in scikit-learn
  4. Scegliere l'intervallo N-gram corretto
← Torna a NLP Academy