TF-IDF mit scikit-learn
Ein Korpus mit wenigen Zeilen vektorisieren
TF-IDF mit scikit-learn ist eine kostenlose NLP Academy-Lektion auf CoddyKit. Dies ist Lektion 3 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des NLP Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der NLP Academy-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
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 TfidfVectorizerYour 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. ✅
Häufig gestellte Fragen
Ist die Lektion „TF-IDF mit scikit-learn“ kostenlos?
Ja — der vollständige Text von „TF-IDF mit scikit-learn“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des NLP Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der NLP Academy-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „TF-IDF mit scikit-learn“?
Ein Korpus mit wenigen Zeilen vektorisieren Du übst NLP Academy mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.
Brauche ich Erfahrung, um NLP Academy zu starten?
Keine Vorkenntnisse erforderlich. NLP Academy auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 3 von 4.
Wie lange dauert die Lektion „TF-IDF mit scikit-learn“?
Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.
Kann ich in dieser NLP Academy-Lektion Code schreiben und ausführen?
Ja. Jede NLP Academy-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.
Alle Lektionen in diesem Kurs
- Das Problem mit Rohzählungen
- Termhäufigkeit und inverse Dokumenthäufigkeit
- TF-IDF mit scikit-learn
- Die wichtigsten Wörter finden