0Pricing
NLP Academy · Leçon

TF-IDF avec scikit-learn

Vectoriser un corpus en quelques lignes

TF-IDF 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.

No Need to Hand-Code It

You understand the math, so now let scikit-learn do the heavy lifting. Its TfidfVectorizer turns raw documents into a weighted matrix in a few lines.

Import the Vectorizer

Everything lives in the feature_extraction.text module. Import TfidfVectorizer and you are ready to vectorize any list of text strings.

from sklearn.feature_extraction.text import TfidfVectorizer

Your Corpus Is a List

A corpus is just a Python list of strings, one per document. Each entry is the full text you want scored and compared.

corpus = [
    "the cat sat on the mat",
    "the dog chased the cat",
]

Fit and Transform

Call fit_transform to learn the vocabulary and compute TF-IDF in one step. It returns a sparse matrix of weighted features.

vec = TfidfVectorizer()
X = vec.fit_transform(corpus)
print(X.shape)

What fit Learned

The fit step builds the vocabulary and the IDF values from your corpus. After this the vectorizer knows every term and how rare it is.

Inspect the Vocabulary

You can list the learned feature names to see the columns. get_feature_names_out shows each word in vocabulary order. 🔎

print(vec.get_feature_names_out())

The Output Is Sparse

Most documents use only a few words, so the matrix is mostly zeros. scikit-learn stores it as a memory-saving sparse matrix by default.

Peek at Real Numbers

Convert a row to a dense array to actually read the weights. The fillers near zero and topic words stand out clearly.

print(X.toarray()[0].round(3))

Tune With Parameters

Handy options let you drop rare or common terms instantly. Set min_df and stop_words to clean the vocabulary as you vectorize.

vec = TfidfVectorizer(stop_words="english", min_df=2)

Reuse on New Text

Fit once on training data, then call transform on fresh documents. New text is mapped into the exact same vocabulary and IDF scale.

new_docs = ["a new cat appeared"]
X_new = vec.transform(new_docs)

Ready for a Model

This weighted matrix plugs straight into any scikit-learn classifier. TF-IDF features are a strong, fast baseline for real text tasks.

Quick Check

Which method learns the vocabulary and computes the TF-IDF matrix together?

Recap

You imported TfidfVectorizer, fit it on a corpus, inspected the sparse output, and learned to reuse it on new text. The math is now a one-liner. ✅

Questions Fréquemment Posées

La leçon « TF-IDF avec scikit-learn » est-elle gratuite ?

Oui — le texte complet de « TF-IDF 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 « TF-IDF avec scikit-learn » ?

Vectoriser un corpus en quelques lignes 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 « TF-IDF 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. Le problème des comptages bruts
  2. Fréquence des termes et fréquence inverse des documents
  3. TF-IDF avec scikit-learn
  4. Trouver les mots les plus importants
← Retour à NLP Academy