TF‑IDF-documentvectoren bouwen

Feature Engineering voor NLP in Python

Rounak Banik

Data Scientist

n-grammodellering

  • Gewicht van dimensie hangt af van de frequentie van het woord voor die dimensie.
    • Document bevat het woord human vijf keer.
    • Dimensie voor human heeft gewicht 5.
Feature Engineering voor NLP in Python

Motivatie

  • Sommige woorden komen overal vaak voor
  • Corpus met documenten over het heelal
    • Eén document heeft jupiter en universe elk 20 keer.
    • jupiter komt zelden in andere documenten voor. universe is algemeen.
    • Geef jupiter meer gewicht vanwege exclusiviteit.
Feature Engineering voor NLP in Python

Toepassingen

  • Stopwoorden automatisch detecteren
  • Zoeken
  • Aanbevelingssystemen
  • Soms betere prestaties in predictieve modellen
Feature Engineering voor NLP in Python

Term frequency–inverse document frequency

  • Evenredig met termfrequentie
  • Omgekeerd met het aantal documenten waarin hij voorkomt
Feature Engineering voor NLP in Python

Wiskundige formule

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

$$\red{w_{i,j}} \rightarrow \text{gewicht van term } i \text{ in document } j$$

Feature Engineering voor NLP in Python

Wiskundige formule

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

$$w_{i,j} \rightarrow \text{gewicht van term } i \text{ in document } j$$

$$\red{tf_{i,j}} \rightarrow \text{termfrequentie van term } i \text{ in document } j $$

Feature Engineering voor NLP in Python

Wiskundige formule

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

$$w_{i,j} \rightarrow \text{gewicht van term } i \text{ in document } j$$

$$tf_{i,j} \rightarrow \text{termfrequentie van term } i \text{ in document } j $$

$$\red{N} \rightarrow \text{aantal documenten in het corpus } $$

$$\red{df_{i}} \rightarrow \text{aantal documenten met term } i$$

Feature Engineering voor NLP in Python

Wiskundige formule

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

$$w_{i,j} \rightarrow \text{gewicht van term } i \text{ in document } j$$

$$tf_{i,j} \rightarrow term\;frequentie\;van\;term\;i\;in\;document\;j $$

$$N \rightarrow aantal\;documenten\;in\;het\;corpus $$

$$df_{i} \rightarrow aantal\;documenten\;met\;term\;i$$

Voorbeeld:

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

Feature Engineering voor NLP in Python

tf-idf met 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 voor NLP in Python

Laten we oefenen!

Feature Engineering voor NLP in Python

Preparing Video For Download...