tf-idf-Dokumentvektoren erstellen

Feature Engineering für NLP in Python

Rounak Banik

Data Scientist

n-Gramm-Modellierung

  • Gewicht der Dimension hängt von der Häufigkeit des zugehörigen Wortes ab.
    • Im Dokument steht das Wort human fünfmal.
    • Die Dimension zu human hat das Gewicht 5.
Feature Engineering für NLP in Python

Motivation

  • Manche Wörter kommen in allen Dokumenten sehr häufig vor
  • Korpus mit Dokumenten über das Universum
    • Ein Dokument enthält jupiter und universe je 20-mal.
    • jupiter kommt in den anderen Dokumenten selten vor, universe ist häufig.
    • Gib jupiter wegen Exklusivität mehr Gewicht.
Feature Engineering für NLP in Python

Anwendungen

  • Stopwörter automatisch erkennen
  • Suche
  • Empfehlungssysteme
  • Teilweise bessere Performance in Vorhersagemodellen
Feature Engineering für NLP in Python

Termfrequenz–inverse Dokumenthäufigkeit

  • Proportional zur Termfrequenz
  • Umgekehrt proportional zur Zahl der Dokumente, in denen er vorkommt
Feature Engineering für NLP in Python

Mathematische Formel

$$\red{w_{i,j}} = tf_{i,j} \cdot \log\left(\frac{N}{df_{i}}\right) $$

$$\red{w_{i,j}} \rightarrow \text{Gewicht des Terms } i \text{ in Dokument } j$$

Feature Engineering für NLP in Python

Mathematische Formel

$$w_{i,j} = \red{tf_{i,j}} \cdot \log\left(\frac{N}{df_{i}}\right) $$

$$w_{i,j} \rightarrow \text{Gewicht des Terms } i \text{ in Dokument } j$$

$$\red{tf_{i,j}} \rightarrow \text{Termfrequenz von Term } i \text{ in Dokument } j $$

Feature Engineering für NLP in Python

Mathematische Formel

$$w_{i,j} = tf_{i,j} \cdot \red{\log\left(\frac{N}{df_{i}}\right)} $$

$$w_{i,j} \rightarrow \text{Gewicht des Terms } i \text{ in Dokument } j$$

$$tf_{i,j} \rightarrow \text{Termfrequenz von Term } i \text{ in Dokument } j $$

$$\red{N} \rightarrow \text{Anzahl der Dokumente im Korpus } $$

$$\red{df_{i}} \rightarrow \text{Anzahl der Dokumente mit Term } i$$

Feature Engineering für NLP in Python

Mathematische Formel

$$w_{i,j} = tf_{i,j} \cdot \log\left(\frac{N}{df_{i}}\right) $$

$$w_{i,j} \rightarrow \text{Gewicht des Terms } i \text{ in Dokument } j$$

$$tf_{i,j} \rightarrow term \; frequency \; of \; term \; i \; in \; document \; j $$

$$N \rightarrow number \; of \; documents \; in \; the \; corpus $$

$$df_{i} \rightarrow number \; of \; documents \; cotaining \; term \; i$$

Example:

$\red{w_{library, document}} = 5 \cdot log(\frac{20}{8}) \approx 2 $

Feature Engineering für NLP in Python

tf-idf mit scikit-learn

# Import TfidfVectorizer
from sklearn.feature_extraction.text import TfidfVectorizer

# Create TfidfVectorizer object vectorizer = TfidfVectorizer()
# Generate matrix of word vectors tfidf_matrix = vectorizer.fit_transform(corpus) print(tfidf_matrix.toarray())
[[0.         0.         0.         0.         0.25434658 0.33443519
  0.33443519 0.         0.25434658 0.         0.25434658 0.
  0.76303975]
 [0.         0.46735098 0.         0.46735098 0.         0.
  0.         0.46735098 0.         0.46735098 0.35543247 0.
  0.        ]
...
Feature Engineering für NLP in Python

Lass uns üben!

Feature Engineering für NLP in Python

Preparing Video For Download...