0Pricing
NLP Academy · Leçon

Caractéristiques en N-grammes avec scikit-learn

Ajouter des expressions à votre vectoriseur

Caractéristiques en N-grammes avec scikit-learn est une leçon NLP Academy gratuite sur CoddyKit. Ceci est la leçon 3 sur 4. Tu peux lire la leçon complète ci-dessous gratuitement — puis la pratiquer en direct dans le navigateur avec un éditeur de code intégré et un tuteur IA 24/7. Elle fait partie du parcours d'apprentissage NLP Academy, et ta progression se synchronise sur le web et l'application CoddyKit. Le cours NLP Academy comprend 4 leçons au total.

Certaines parties de cette leçon n'ont pas encore été traduites et s'affichent en anglais.

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

Questions Fréquemment Posées

La leçon « Caractéristiques en N-grammes avec scikit-learn » est-elle gratuite ?

Oui — le texte complet de « Caractéristiques en N-grammes avec scikit-learn » est gratuit à lire ici sur le web. Pour la pratiquer de manière interactive (un éditeur de code intégré et un tuteur IA 24/7) et déverrouiller le reste du cours NLP Academy, passe à CoddyKit PRO. Le cours NLP Academy comprend 4 leçons au total.

Qu'est-ce que j'apprendrai dans « Caractéristiques en N-grammes avec scikit-learn » ?

Ajouter des expressions à votre vectoriseur Tu pratiques NLP Academy avec du code pratique que tu exécutes directement dans le navigateur, et un tuteur IA 24/7 répond à tes questions au fur et à mesure que tu avances dans la leçon.

Dois-je avoir de l'expérience pour commencer NLP Academy ?

Aucune expérience préalable n'est requise. NLP Academy sur CoddyKit est structuré pour les débutants jusqu'aux apprenants avancés, donc tu peux commencer ici ou depuis le début et avancer à ton rythme. Ceci est la leçon 3 sur 4.

Combien de temps prend la leçon « Caractéristiques en N-grammes avec scikit-learn » ?

La plupart des leçons CoddyKit prennent environ 5–10 minutes. Chacune est courte et interactive, tu progresses régulièrement et tu repiques exactement où tu t'es arrêté sur le web et l'app.

Peux-tu écrire et exécuter du code dans cette leçon NLP Academy ?

Oui. Chaque leçon NLP Academy inclut un éditeur de code intégré, tu écris et exécutes du vrai code directement dans ton navigateur et tu reçois des retours IA instantanés — aucune configuration locale requise.

Toutes les leçons de ce cours

  1. Pourquoi les mots isolés perdent leur sens
  2. Bigrams et trigrams expliqués
  3. Caractéristiques en N-grammes avec scikit-learn
  4. Choisir la bonne plage de N-grammes
← Retour à NLP Academy