0Pricing
NLP Academy · Lezione

TF-IDF con scikit-learn

Vettorizzare un corpus in poche righe

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

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

Domande Frequenti

La lezione «TF-IDF con scikit-learn» è gratuita?

Sì — il testo completo di «TF-IDF con 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 «TF-IDF con scikit-learn»?

Vettorizzare un corpus in poche righe 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 «TF-IDF con 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. Il problema dei conteggi grezzi
  2. Frequenza dei termini e frequenza inversa dei documenti
  3. TF-IDF con scikit-learn
  4. Trovare le parole più importanti
← Torna a NLP Academy